View Javadoc
1   package org.opentrafficsim.animation.lane;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.draw.point.OrientedPoint2d;
5   import org.opentrafficsim.base.geometry.BoundingBox;
6   import org.opentrafficsim.base.geometry.OtsBounds2d;
7   import org.opentrafficsim.base.geometry.OtsLocatable;
8   import org.opentrafficsim.core.geometry.OtsGeometryException;
9   import org.opentrafficsim.core.gtu.Gtu;
10  import org.opentrafficsim.road.gtu.lane.perception.LaneStructureRecord;
11  import org.opentrafficsim.road.gtu.lane.perception.RollingLaneStructure;
12  
13  /**
14   * LaneStructureLocatable.java.
15   * <p>
16   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * </p>
19   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
20   */
21  public class LaneStructureLocatable implements OtsLocatable
22  {
23      /** RollingLaneStructure. */
24      private final RollingLaneStructure rollingLaneStructure;
25  
26      /** GTU. */
27      private final Gtu gtu;
28  
29      /**
30       * @param rollingLaneStructure RollingLaneStructure; the rolling lane structure
31       * @param gtu Gtu; the gtu
32       */
33      public LaneStructureLocatable(final RollingLaneStructure rollingLaneStructure, final Gtu gtu)
34      {
35          this.rollingLaneStructure = rollingLaneStructure;
36          this.gtu = gtu;
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      public OrientedPoint2d getLocation()
42      {
43          LaneStructureRecord rt = this.rollingLaneStructure.getRootRecord();
44          if (rt == null)
45          {
46              return this.gtu.getLocation();
47          }
48          Length position = rt.getStartDistance().neg();
49          position = position.lt0() ? Length.ZERO : position;
50          try
51          {
52              return rt.getLane().getCenterLine().getLocation(position);
53          }
54          catch (OtsGeometryException exception)
55          {
56              throw new RuntimeException("Unable to return location.", exception);
57          }
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public OtsBounds2d getBounds()
63      {
64          return new BoundingBox(1000000, 1000000);
65      }
66  
67      /**
68       * @return rollingLaneStructure
69       */
70      public final RollingLaneStructure getRollingLaneStructure()
71      {
72          return this.rollingLaneStructure;
73      }
74  
75      /**
76       * @return gtu
77       */
78      public final Gtu getGtu()
79      {
80          return this.gtu;
81      }
82  
83  }