View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Map;
6   
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.opentrafficsim.core.gtu.GTUDirectionality;
9   import org.opentrafficsim.core.gtu.GTUType;
10  import org.opentrafficsim.road.network.lane.Lane;
11  
12  /**
13   * A light-weight wrapper for LaneRecord search tools (PerceptionIterator). This is suitable for situations where parts of the
14   * network not in the LaneStructure need to be perceived, such as conflicting lanes at intersection conflicts. Searches can only
15   * be simple upstream or downstream searches, without lateral movement and without regard of a route. This class should not be
16   * used whenever the LaneStructure can be used, as this class builds up a new tree each time step.
17   * <p>
18   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 feb. 2018 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class LaneDirectionRecord implements LaneRecord<LaneDirectionRecord>
27  {
28  
29      /** Lane. */
30      private final Lane lane;
31  
32      /** Direction of travel. */
33      private final GTUDirectionality dir;
34  
35      /** Distance to start. */
36      private final Length startDistance;
37  
38      /** GTU type. */
39      private final GTUType gtuType;
40  
41      /** Stored next lanes. */
42      private List<LaneDirectionRecord> next;
43  
44      /** Stored prev lanes. */
45      private List<LaneDirectionRecord> prev;
46  
47      /**
48       * Constructor.
49       * @param lane Lane; lane
50       * @param dir GTUDirectionality; direction of travel
51       * @param startDistance Length; distance to start
52       * @param gtuType GTUType; GTU type
53       */
54      public LaneDirectionRecord(final Lane lane, final GTUDirectionality dir, final Length startDistance, final GTUType gtuType)
55      {
56          this.lane = lane;
57          this.dir = dir;
58          this.startDistance = startDistance;
59          this.gtuType = gtuType;
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public List<LaneDirectionRecord> getNext()
65      {
66          if (this.next == null)
67          {
68              Map<Lane, GTUDirectionality> map = this.lane.downstreamLanes(this.dir, this.gtuType);
69              this.next = new ArrayList<>();
70              Length distance = this.startDistance.plus(getLength());
71              for (Lane down : map.keySet())
72              {
73                  this.next.add(new LaneDirectionRecord(down, map.get(down), distance, this.gtuType));
74              }
75          }
76          return this.next;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public List<LaneDirectionRecord> getPrev()
82      {
83          if (this.prev == null)
84          {
85              Map<Lane, GTUDirectionality> map = this.lane.upstreamLanes(this.dir, this.gtuType);
86              this.prev = new ArrayList<>();
87              for (Lane up : map.keySet())
88              {
89                  this.prev.add(new LaneDirectionRecord(up, map.get(up), this.startDistance.minus(up.getLength()), this.gtuType));
90              }
91          }
92          return this.prev;
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public Length getStartDistance()
98      {
99          return this.startDistance;
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public Length getLength()
105     {
106         return this.lane.getLength();
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public GTUDirectionality getDirection()
112     {
113         return this.dir;
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public Lane getLane()
119     {
120         return this.lane;
121     }
122 
123 }