View Javadoc
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.DirectedPoint2d;
6   import org.djutils.draw.point.Point2d;
7   import org.djutils.exceptions.Throw;
8   import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
9   import org.opentrafficsim.core.object.LocatedObject;
10  import org.opentrafficsim.road.network.lane.Lane;
11  
12  /**
13   * Objects that can be encountered on a Lane like conflict areas, GTUs, traffic lights, stop lines, etc.
14   * <p>
15   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * </p>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public interface LaneBasedObject extends LocatedObject
23  {
24  
25      /**
26       * Returns the lane.
27       * @return lane
28       */
29      Lane getLane();
30  
31      /**
32       * Returns the longitudinal position.
33       * @return position (between 0.0 and the length of the lane)
34       */
35      Length getLongitudinalPosition();
36  
37      /**
38       * Returns the length of the object. The default value is zero.
39       * @return length of the object
40       */
41      default Length getLength()
42      {
43          return Length.ZERO;
44      }
45  
46      @Override
47      DirectedPoint2d getLocation();
48  
49      /**
50       * Returns the line that represent the location of this object on the lane.
51       * @return the line that represent the location of this object on the lane
52       */
53      default PolyLine2d getLine()
54      {
55          return makeLine(getLane(), getLongitudinalPosition());
56      }
57  
58      /**
59       * Returns the simulator.
60       * @return simulator
61       */
62      default OtsSimulatorInterface getSimulator()
63      {
64          return getLane().getLink().getSimulator();
65      }
66  
67      /**
68       * Make a geometry perpendicular to the center line of the lane with a length 90% of the width of the lane.
69       * @param lane the lane for which to make a perpendicular geometry
70       * @param longitudinalPosition the position on the lane
71       * @return lateral line at position
72       */
73      static PolyLine2d makeLine(final Lane lane, final Length longitudinalPosition)
74      {
75          return makeLine(lane, longitudinalPosition, 0.9);
76      }
77  
78      /**
79       * Make a geometry perpendicular to the center line of the lane with a length of given fraction of the width of the lane.
80       * @param lane the lane for which to make a perpendicular geometry
81       * @param longitudinalPosition the position on the lane
82       * @param relativeWidth lane width to use
83       * @return lateral line at position
84       */
85      static PolyLine2d makeLine(final Lane lane, final Length longitudinalPosition, final double relativeWidth)
86      {
87          Throw.whenNull(lane, "lane is null");
88          Throw.whenNull(longitudinalPosition, "position is null");
89          Throw.whenNull(relativeWidth, "relatve width is null");
90          double w50 = lane.getWidth(longitudinalPosition).si * 0.5 * relativeWidth;
91          DirectedPoint2d c = lane.getCenterLine().getLocationExtended(longitudinalPosition);
92          double a = c.getDirZ();
93          Point2d p1 = new Point2d(c.x + w50 * Math.cos(a + Math.PI / 2), c.y - w50 * Math.sin(a + Math.PI / 2));
94          Point2d p2 = new Point2d(c.x - w50 * Math.cos(a + Math.PI / 2), c.y + w50 * Math.sin(a + Math.PI / 2));
95          return new PolyLine2d(p1, p2);
96      }
97  
98  }