View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.djutils.event.TimedEventType;
8   import org.djutils.exceptions.Throw;
9   import org.djutils.exceptions.Try;
10  import org.djutils.metadata.MetaData;
11  import org.djutils.metadata.ObjectDescriptor;
12  import org.opentrafficsim.core.geometry.OTSLine3D;
13  import org.opentrafficsim.core.geometry.OTSPoint3D;
14  import org.opentrafficsim.core.network.LinkType;
15  import org.opentrafficsim.core.network.NetworkException;
16  import org.opentrafficsim.core.network.OTSLink;
17  import org.opentrafficsim.core.network.OTSNetwork;
18  import org.opentrafficsim.road.network.OTSRoadNetwork;
19  import org.opentrafficsim.road.network.RoadNetwork;
20  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
21  
22  import nl.tudelft.simulation.language.d3.DirectedPoint;
23  
24  /**
25   * A CrossSectionLink is a link with lanes where GTUs can possibly switch between lanes.
26   * <p>
27   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * <p>
30   * $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
31   * initial version Aug 19, 2014 <br>
32   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
35   */
36  public class CrossSectionLink extends OTSLink implements Serializable
37  {
38      /** */
39      private static final long serialVersionUID = 20141015L;
40  
41      /** List of cross-section elements. */
42      private final List<CrossSectionElement> crossSectionElementList = new ArrayList<>();
43  
44      /** List of lanes. */
45      private final List<Lane> lanes = new ArrayList<>();
46  
47      /** The policy to generally keep left, keep right, or keep lane. */
48      private final LaneKeepingPolicy laneKeepingPolicy;
49  
50      /** Priority. */
51      // TODO per GTUDirectionality / LongitudinalDirectionality?
52      private Priority priority = Priority.NONE;
53  
54      /** Fraction in range 0...1 to divide origin or destination flow over connectors. */
55      private Double demandWeight = null;
56  
57      /** Line over which GTUs enter or leave the link at the start node. */
58      private OTSLine3D startLine;
59  
60      /** Line over which GTUs enter or leave the link at the end node. */
61      private OTSLine3D endLine;
62  
63      /**
64       * The (regular, not timed) event type for pub/sub indicating the addition of a Lane to a CrossSectionLink. <br>
65       * Payload: Object[] { String networkId, String linkId, String LaneId, int laneNumber } <br>
66       * TODO work in a different way with lane numbers to align to standard lane numbering.
67       */
68      public static final TimedEventType LANE_ADD_EVENT = new TimedEventType("LINK.LANE.ADD",
69              new MetaData("Lane data", "Lane data",
70                      new ObjectDescriptor[] { new ObjectDescriptor("Network id", "Network id", String.class),
71                              new ObjectDescriptor("Link id", "Link id", String.class),
72                              new ObjectDescriptor("Lane id", "Lane id", String.class),
73                              new ObjectDescriptor("Lane number", "Lane number", Integer.class) }));
74  
75      /**
76       * The (regular, not timed) event type for pub/sub indicating the removal of a Lane from a CrossSectionLink. <br>
77       * Payload: Object[] { String networkId, String linkId, String LaneId } <br>
78       * TODO allow for the removal of a Lane; currently this is not possible.
79       */
80      public static final TimedEventType LANE_REMOVE_EVENT = new TimedEventType("LINK.LANE.REMOVE",
81              new MetaData("Lane data", "Lane data",
82                      new ObjectDescriptor[] { new ObjectDescriptor("Network id", "Network id", String.class),
83                              new ObjectDescriptor("Link id", "Link id", String.class),
84                              new ObjectDescriptor("Lane id", "Lane id", String.class),
85                              new ObjectDescriptor("Lane number", "Lane number", Integer.class) }));
86  
87      /**
88       * Construction of a cross section link.
89       * @param network OTSRoadNetwork; the network
90       * @param id String; the link id.
91       * @param startNode OTSRoadNode; the start node (directional).
92       * @param endNode OTSRoadNode; the end node (directional).
93       * @param linkType LinkType; the link type
94       * @param designLine OTSLine3D; the design line of the Link
95       * @param laneKeepingPolicy LaneKeepingPolicy; the policy to generally keep left, keep right, or keep lane
96       * @throws NetworkException if link already exists in the network, if name of the link is not unique, or if the start node
97       *             or the end node of the link are not registered in the network.
98       */
99      @SuppressWarnings("checkstyle:parameternumber")
100     public CrossSectionLink(final OTSRoadNetwork network, final String id, final OTSRoadNode startNode,
101             final OTSRoadNode endNode, final LinkType linkType, final OTSLine3D designLine,
102             final LaneKeepingPolicy laneKeepingPolicy) throws NetworkException
103     {
104         super(network, id, startNode, endNode, linkType, designLine);
105         this.laneKeepingPolicy = laneKeepingPolicy;
106     }
107 
108     /**
109      * Clone a CrossSectionLink for a new network.
110      * @param newNetwork Network; the new network to which the clone belongs
111      * @param link CrossSectionLink; the link to clone from
112      * @throws NetworkException if link already exists in the network, if name of the link is not unique, or if the start node
113      *             or the end node of the link are not registered in the network.
114      */
115     protected CrossCrossSectionLinkfinal OTSRoadNetwork newNetwork, final CrossSectionLink link) throws NetworkException
116     {
117         super(newNetwork, link);
118         this.laneKeepingPolicy = link.laneKeepingPolicy;
119         for (CrossSectionElement cse : link.crossSectionElementList)
120         {
121             cse.clone(this, newNetwork.getSimulator());
122             // the CrossSectionElement will add itself to the Link (OTS-237)
123         }
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public OTSRoadNetwork getNetwork()
129     {
130         return (OTSRoadNetwork) super.getNetwork();
131     }
132 
133     /**
134      * Add a cross section element at the end of the list. <br>
135      * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction.
136      * @param cse CrossSectionElement; the cross section element to add.
137      */
138     protected final void addCrossSectionElement(final CrossSectionElement cse)
139     {
140         this.crossSectionElementList.add(cse);
141         if (cse instanceof Lane)
142         {
143             this.lanes.add((Lane) cse);
144             fireTimedEvent(LANE_ADD_EVENT, new Object[] { getNetwork().getId(), getId(), cse.getId(), this.lanes.indexOf(cse) },
145                     getSimulator().getSimulatorTime());
146         }
147     }
148 
149     /**
150      * Retrieve a safe copy of the cross section element list.
151      * @return List&lt;CrossSectionElement&gt;; the cross section element list.
152      */
153     public final List<CrossSectionElement> getCrossSectionElementList()
154     {
155         return this.crossSectionElementList == null ? new ArrayList<>() : new ArrayList<>(this.crossSectionElementList);
156     }
157 
158     /**
159      * Retrieve the lane keeping policy.
160      * @return LaneKeepingPolicy; the lane keeping policy on this CrossSectionLink
161      */
162     public final LaneKeepingPolicy getLaneKeepingPolicy()
163     {
164         return this.laneKeepingPolicy;
165     }
166 
167     /**
168      * Find a cross section element with a specified id.
169      * @param id String; the id to search for
170      * @return CrossSectionElement; the cross section element with the given id, or null if not found
171      */
172     public final CrossSectionElement getCrossSectionElement(final String id)
173     {
174         for (CrossSectionElement cse : this.crossSectionElementList)
175         {
176             if (cse.getId().equals(id))
177             {
178                 return cse;
179             }
180         }
181         return null;
182     }
183 
184     /**
185      * Return a safe copy of the list of lanes of this CrossSectionLink.
186      * @return List&lt;Lane&gt;; the list of lanes.
187      */
188     public final List<Lane> getLanes()
189     {
190         return this.lanes == null ? new ArrayList<>() : new ArrayList<>(this.lanes);
191     }
192 
193     /**
194      * @return priority.
195      */
196     public final Priority getPriority()
197     {
198         return this.priority;
199     }
200 
201     /**
202      * @param priority Priority; set priority.
203      */
204     public final void setPriority(final Priority priority)
205     {
206         this.priority = priority;
207     }
208 
209     /**
210      * Sets the demand weight. This is only applicable to links of type CONNECTOR.
211      * @param demandWeight double; demand weight, which is any positive value
212      */
213     public final void setDemandWeight(final double demandWeight)
214     {
215         Throw.when(demandWeight < 0.0, IllegalArgumentException.class, "Demand weight should be positive.");
216         Throw.when(!getLinkType().isConnector(), IllegalArgumentException.class,
217                 "Demand weight can only be set on connectors.");
218         this.demandWeight = demandWeight;
219     }
220 
221     /**
222      * Clears the demand weight. This is only applicable to links of type CONNECTOR.
223      */
224     public final void clearDemandWeight()
225     {
226         this.demandWeight = null;
227     }
228 
229     /**
230      * Returns the demand weight. This is only applicable to links of type CONNECTOR.
231      * @return Double; demand weight, any positive value, or {@code null}
232      */
233     public final Double getDemandWeight()
234     {
235         return this.demandWeight;
236     }
237 
238     /**
239      * Returns the line over which GTUs enter and leave the link at the start node.
240      * @return OTSLine3D; line over which GTUs enter and leave the link at the start node
241      */
242     public OTSLine3D getStartLine()
243     {
244         if (this.startLine == null)
245         {
246             double left = Double.NaN;
247             double right = Double.NaN;
248             for (Lane lane : this.lanes)
249             {
250                 double half = lane.getBeginWidth().si * .5;
251                 if (!Double.isNaN(left))
252                 {
253                     left = Math.max(left, lane.getDesignLineOffsetAtBegin().si + half);
254                     right = Math.min(right, lane.getDesignLineOffsetAtBegin().si - half);
255                 }
256                 else
257                 {
258                     left = lane.getDesignLineOffsetAtBegin().si + half;
259                     right = lane.getDesignLineOffsetAtBegin().si - half;
260                 }
261             }
262             OTSPoint3D start = getStartNode().getPoint();
263             double heading = getStartNode().getHeading() + .5 * Math.PI;
264             double cosHeading = Math.cos(heading);
265             double sinHeading = Math.sin(heading);
266             OTSPoint3D leftPoint = new OTSPoint3D(start.x + cosHeading * left, start.y + sinHeading * left);
267             OTSPoint3D rightPoint = new OTSPoint3D(start.x - cosHeading * right, start.y - sinHeading * right);
268             this.startLine = Try.assign(() -> new OTSLine3D(leftPoint, rightPoint), "Invalid startline on CrossSectionLink.");
269         }
270         return this.startLine;
271     }
272 
273     /**
274      * Returns the line over which GTUs enter and leave the link at the end node.
275      * @return OTSLine3D; line over which GTUs enter and leave the link at the end node
276      */
277     public OTSLine3D getEndLine()
278     {
279         if (this.endLine == null)
280         {
281             double left = Double.NaN;
282             double right = Double.NaN;
283             for (Lane lane : this.lanes)
284             {
285                 double half = lane.getEndWidth().si * .5;
286                 if (!Double.isNaN(left))
287                 {
288                     left = Math.max(left, lane.getDesignLineOffsetAtEnd().si + half);
289                     right = Math.min(right, lane.getDesignLineOffsetAtEnd().si - half);
290                 }
291                 else
292                 {
293                     left = lane.getDesignLineOffsetAtEnd().si + half;
294                     right = lane.getDesignLineOffsetAtEnd().si - half;
295                 }
296             }
297             OTSPoint3D start = getEndNode().getPoint();
298             DirectedPoint p = Try.assign(() -> getEndNode().getLocation(), "Unexpected remote exception.");
299             double heading = p.getRotZ() + .5 * Math.PI;
300             double cosHeading = Math.cos(heading);
301             double sinHeading = Math.sin(heading);
302             OTSPoint3D leftPoint = new OTSPoint3D(start.x + cosHeading * left, start.y + sinHeading * left);
303             OTSPoint3D rightPoint = new OTSPoint3D(start.x + cosHeading * right, start.y + sinHeading * right);
304             this.endLine = Try.assign(() -> new OTSLine3D(leftPoint, rightPoint), "Invalid endline on CrossSectionLink.");
305         }
306         return this.endLine;
307     }
308 
309     /** {@inheritDoc} */
310     @Override
311     public final String toString()
312     {
313         return "CrossSectionLink [name=" + this.getId() + ", nodes=" + getStartNode().getId() + "-" + getEndNode().getId()
314                 + ", crossSectionElementList=" + this.crossSectionElementList + ", lanes=" + this.lanes + ", laneKeepingPolicy="
315                 + this.laneKeepingPolicy + "]";
316     }
317 
318     /** {@inheritDoc} */
319     @Override
320     @SuppressWarnings("checkstyle:designforextension")
321     public CrossSectionLink clone(final OTSNetwork newNetwork) throws NetworkException
322     {
323         Throw.when(!(newNetwork instanceof RoadNetwork), NetworkException.class,
324                 "CrossSectionLink.clone. newNetwork not of the type Roadnetwork");
325         return new CrossSectionLink((OTSRoadNetwork) newNetwork, this);
326     }
327 
328     /**
329      * Priority of a link.
330      * <p>
331      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
332      * <br>
333      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
334      * <p>
335      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 12 dec. 2016 <br>
336      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
337      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
338      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
339      */
340     public enum Priority
341     {
342         /** Traffic has priority. */
343         PRIORITY,
344 
345         /** No priority. */
346         NONE,
347 
348         /** Turn on red. */
349         TURN_ON_RED,
350 
351         /** Yield. */
352         YIELD,
353 
354         /** Need to stop. */
355         STOP,
356 
357         /** Priority according to all-stop rules. */
358         ALL_STOP,
359 
360         /** Priority at bus stop, i.e. bus has right of way if it wants to leave the bus stop. */
361         BUS_STOP;
362 
363         /**
364          * Returns whether this is priority.
365          * @return whether this is priority
366          */
367         public boolean isPriority()
368         {
369             return this.equals(PRIORITY);
370         }
371 
372         /**
373          * Returns whether this is none.
374          * @return whether this is none
375          */
376         public boolean isNone()
377         {
378             return this.equals(NONE);
379         }
380 
381         /**
382          * Returns whether this is turn on red.
383          * @return whether this is turn on red
384          */
385         public boolean isTurnOnRed()
386         {
387             return this.equals(TURN_ON_RED);
388         }
389 
390         /**
391          * Returns whether this is yield.
392          * @return whether this is yield
393          */
394         public boolean isYield()
395         {
396             return this.equals(YIELD);
397         }
398 
399         /**
400          * Returns whether this is stop.
401          * @return whether this is stop
402          */
403         public boolean isStop()
404         {
405             return this.equals(STOP);
406         }
407 
408         /**
409          * Returns whether this is all-stop.
410          * @return whether this is all-stop
411          */
412         public boolean isAllStop()
413         {
414             return this.equals(ALL_STOP);
415         }
416 
417         /**
418          * Returns whether this is bus stop.
419          * @return whether this is bus stop
420          */
421         public boolean isBusStop()
422         {
423             return this.equals(BUS_STOP);
424         }
425 
426     }
427 
428 }