1 package org.opentrafficsim.road.network.lane.object;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.djutils.draw.line.PolyLine2d;
5 import org.djutils.draw.point.OrientedPoint2d;
6 import org.djutils.draw.point.Point2d;
7 import org.djutils.exceptions.Throw;
8 import org.opentrafficsim.core.object.LocatedObject;
9 import org.opentrafficsim.road.network.lane.Lane;
10
11 /**
12 * Objects that can be encountered on a Lane like conflict areas, GTUs, traffic lights, stop lines, etc.
13 * <p>
14 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16 * </p>
17 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
18 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
19 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
20 */
21 public interface LaneBasedObject extends LocatedObject
22 {
23
24 /** @return The lane for which this is a sensor. */
25 Lane getLane();
26
27 /** @return the position (between 0.0 and the length of the Lane) of the sensor on the design line of the lane. */
28 Length getLongitudinalPosition();
29
30 /** {@inheritDoc} */
31 @Override
32 OrientedPoint2d getLocation();
33
34 /**
35 * Make a geometry perpendicular to the center line of the lane with a length 90% of the width of the lane.
36 * @param lane Lane; the lane for which to make a perpendicular geometry
37 * @param longitudinalPosition Length; the position on the lane
38 * @return an OtsLine2d that describes the line
39 */
40 static PolyLine2d makeGeometry(final Lane lane, final Length longitudinalPosition)
41 {
42 return makeGeometry(lane, longitudinalPosition, 0.9);
43 }
44
45 /**
46 * Make a geometry perpendicular to the center line of the lane with a length of given fraction of the width of the lane.
47 * @param lane Lane; the lane for which to make a perpendicular geometry
48 * @param longitudinalPosition Length; the position on the lane
49 * @param relativeWidth double; lane width to use
50 * @return an OtsLine2d that describes the line
51 */
52 static PolyLine2d makeGeometry(final Lane lane, final Length longitudinalPosition, final double relativeWidth)
53 {
54 Throw.whenNull(lane, "lane is null");
55 Throw.whenNull(longitudinalPosition, "position is null");
56 Throw.whenNull(relativeWidth, "relatve width is null");
57 double w50 = lane.getWidth(longitudinalPosition).si * 0.5 * relativeWidth;
58 OrientedPoint2d c = lane.getCenterLine().getLocationExtended(longitudinalPosition);
59 double a = c.getDirZ();
60 Point2d p1 = new Point2d(c.x + w50 * Math.cos(a + Math.PI / 2), c.y - w50 * Math.sin(a + Math.PI / 2));
61 Point2d p2 = new Point2d(c.x - w50 * Math.cos(a + Math.PI / 2), c.y + w50 * Math.sin(a + Math.PI / 2));
62 return new PolyLine2d(p1, p2);
63 }
64
65 }