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.OrientedPoint2d;
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      /** @return The lane for which this is a sensor. */
26      Lane getLane();
27  
28      /** @return the position (between 0.0 and the length of the Lane) of the sensor on the design line of the lane. */
29      Length getLongitudinalPosition();
30  
31      /**
32       * Returns the length of the object. The default value is zero.
33       * @return length of the object.
34       */
35      default Length getLength()
36      {
37          return Length.ZERO;
38      }
39  
40      @Override
41      OrientedPoint2d getLocation();
42  
43      /**
44       * Returns the line that represent the location of this object on the lane.
45       * @return the line that represent the location of this object on the lane.
46       */
47      default PolyLine2d getLine()
48      {
49          return makeLine(getLane(), getLongitudinalPosition());
50      }
51      
52      /**
53       * Returns the simulator.
54       * @return simulator
55       */
56      default OtsSimulatorInterface getSimulator()
57      {
58          return getLane().getLink().getSimulator();
59      }
60  
61      /**
62       * Make a geometry perpendicular to the center line of the lane with a length 90% of the width of the lane.
63       * @param lane the lane for which to make a perpendicular geometry
64       * @param longitudinalPosition the position on the lane
65       * @return an polygon that describes the object
66       */
67      static PolyLine2d makeLine(final Lane lane, final Length longitudinalPosition)
68      {
69          return makeLine(lane, longitudinalPosition, 0.9);
70      }
71  
72      /**
73       * Make a geometry perpendicular to the center line of the lane with a length of given fraction of the width of the lane.
74       * @param lane the lane for which to make a perpendicular geometry
75       * @param longitudinalPosition the position on the lane
76       * @param relativeWidth lane width to use
77       * @return an OtsLine2d that describes the line
78       */
79      static PolyLine2d makeLine(final Lane lane, final Length longitudinalPosition, final double relativeWidth)
80      {
81          Throw.whenNull(lane, "lane is null");
82          Throw.whenNull(longitudinalPosition, "position is null");
83          Throw.whenNull(relativeWidth, "relatve width is null");
84          double w50 = lane.getWidth(longitudinalPosition).si * 0.5 * relativeWidth;
85          OrientedPoint2d c = lane.getCenterLine().getLocationExtended(longitudinalPosition);
86          double a = c.getDirZ();
87          Point2d p1 = new Point2d(c.x + w50 * Math.cos(a + Math.PI / 2), c.y - w50 * Math.sin(a + Math.PI / 2));
88          Point2d p2 = new Point2d(c.x - w50 * Math.cos(a + Math.PI / 2), c.y + w50 * Math.sin(a + Math.PI / 2));
89          return new PolyLine2d(p1, p2);
90      }
91  
92  }