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.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      /**
31       * Returns the length of the object. The default value is zero.
32       * @return Length; length of the object.
33       */
34      default Length getLength()
35      {
36          return Length.ZERO;
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      OrientedPoint2d getLocation();
42  
43      /**
44       * Make a geometry perpendicular to the center line of the lane with a length 90% of the width of the lane.
45       * @param lane Lane; the lane for which to make a perpendicular geometry
46       * @param longitudinalPosition Length; the position on the lane
47       * @return an OtsLine2d that describes the line
48       */
49      static PolyLine2d makeGeometry(final Lane lane, final Length longitudinalPosition)
50      {
51          return makeGeometry(lane, longitudinalPosition, 0.9);
52      }
53  
54      /**
55       * Make a geometry perpendicular to the center line of the lane with a length of given fraction of the width of the lane.
56       * @param lane Lane; the lane for which to make a perpendicular geometry
57       * @param longitudinalPosition Length; the position on the lane
58       * @param relativeWidth double; lane width to use
59       * @return an OtsLine2d that describes the line
60       */
61      static PolyLine2d makeGeometry(final Lane lane, final Length longitudinalPosition, final double relativeWidth)
62      {
63          Throw.whenNull(lane, "lane is null");
64          Throw.whenNull(longitudinalPosition, "position is null");
65          Throw.whenNull(relativeWidth, "relatve width is null");
66          double w50 = lane.getWidth(longitudinalPosition).si * 0.5 * relativeWidth;
67          OrientedPoint2d c = lane.getCenterLine().getLocationExtended(longitudinalPosition);
68          double a = c.getDirZ();
69          Point2d p1 = new Point2d(c.x + w50 * Math.cos(a + Math.PI / 2), c.y - w50 * Math.sin(a + Math.PI / 2));
70          Point2d p2 = new Point2d(c.x - w50 * Math.cos(a + Math.PI / 2), c.y + w50 * Math.sin(a + Math.PI / 2));
71          return new PolyLine2d(p1, p2);
72      }
73  
74  }