View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.Set;
8   import java.util.UUID;
9   
10  import org.djunits.value.vdouble.scalar.Length;
11  import org.opentrafficsim.core.geometry.OTSGeometryException;
12  import org.opentrafficsim.core.gtu.GTUType;
13  import org.opentrafficsim.core.network.LateralDirectionality;
14  import org.opentrafficsim.core.network.NetworkException;
15  
16  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
17  
18  /**
19   * <p>
20   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * <p>
23   * $LastChangedDate: 2015-09-03 13:38:01 +0200 (Thu, 03 Sep 2015) $, @version $Revision: 1378 $, by $Author: averbraeck $,
24   * initial version Oct 25, 2014 <br>
25   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
27   */
28  public abstract class RoadMarkerAlong extends CrossSectionElement
29  {
30      /** */
31      private static final long serialVersionUID = 20141025L;
32  
33      /** Lateral permeability per GTU type and direction. */
34      private final Map<GTUType, Set<LateralDirectionality>> permeabilityMap = new HashMap<>();
35  
36      /**
37       * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction, with the direction from
38       * the StartNode towards the EndNode as the longitudinal direction.
39       * @param parentLink Cross Section Link to which the element belongs.
40       * @param startCenterPosition the lateral start position compared to the linear geometry of the Cross Section Link at the
41       *            start of the road marker.
42       * @param endCenterPosition the lateral end position compared to the linear geometry of the Cross Section Link at the end of
43       *            the road marker.
44       * @param beginWidth start width, positioned <i>symmetrically around</i> the lateral start position.
45       * @param endWidth end width, positioned <i>symmetrically around</i> the lateral end position.
46       * @throws OTSGeometryException when creation of the center line or contour geometry fails
47       * @throws NetworkException when id equal to null or not unique
48       */
49      public RoadMarkerAlong(final CrossSectionLink parentLink, final Length startCenterPosition, final Length endCenterPosition,
50              final Length beginWidth, final Length endWidth) throws OTSGeometryException, NetworkException
51      {
52          super(parentLink, UUID.randomUUID().toString(), startCenterPosition, endCenterPosition, beginWidth, endWidth);
53      }
54  
55      /**
56       * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction, with the direction from
57       * the StartNode towards the EndNode as the longitudinal direction.
58       * @param parentLink Cross Section Link to which the element belongs.
59       * @param lateralCenterPosition the lateral start position compared to the linear geometry of the Cross Section Link.
60       * @param width start width, positioned <i>symmetrically around</i> the lateral start position.
61       * @throws OTSGeometryException when creation of the center line or contour geometry fails
62       * @throws NetworkException when id equal to null or not unique
63       */
64      public RoadMarkerAlong(final CrossSectionLink parentLink, final Length lateralCenterPosition, final Length width)
65              throws OTSGeometryException, NetworkException
66      {
67          super(parentLink, UUID.randomUUID().toString(), lateralCenterPosition, width);
68      }
69  
70      /**
71       * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction, with the direction from
72       * the StartNode towards the EndNode as the longitudinal direction.
73       * @param parentLink Cross Section Link to which the element belongs.
74       * @param crossSectionSlices The offsets and widths at positions along the line, relative to the design line of the parent
75       *            link. If there is just one with and offset, there should just be one element in the list with Length = 0. If
76       *            there are more slices, the last one should be at the length of the design line. If not, a NetworkException is
77       *            thrown.
78       * @throws OTSGeometryException when creation of the center line or contour geometry fails
79       * @throws NetworkException when id equal to null or not unique
80       */
81      public RoadMarkerAlong(final CrossSectionLink parentLink, final List<CrossSectionSlice> crossSectionSlices)
82              throws OTSGeometryException, NetworkException
83      {
84          super(parentLink, UUID.randomUUID().toString(), crossSectionSlices);
85      }
86  
87      /**
88       * Clone a RoadMarkerAlong for a new network.
89       * @param newCrossSectionLink the new link to which the clone belongs
90       * @param newSimulator the new simulator for this network
91       * @param animation whether to (re)create animation or not
92       * @param cse the element to clone from
93       * @throws NetworkException if link already exists in the network, if name of the link is not unique, or if the start node
94       *             or the end node of the link are not registered in the network.
95       */
96      protected RoadMarkerAlong(final CrossSectionLink newCrossSectionLink, final SimulatorInterface.TimeDoubleUnit newSimulator,
97              final boolean animation, final RoadMarkerAlong cse) throws NetworkException
98      {
99          super(newCrossSectionLink, newSimulator, animation, cse);
100         this.permeabilityMap.putAll(cse.permeabilityMap);
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     protected final double getZ()
106     {
107         return 0.0001;
108     }
109 
110     /**
111      * Add lateral permeability for a GTU type in the direction of the design line of the overarching CrossSectionLink.
112      * Therefore, the lateral directionality of one-sided permeability has to be switched for left lanes. This is done because
113      * the CrossSectionLink has no idea in which direction vehicles will be moving. On a 1+1 lane road with overtaking
114      * possibilities, the longitudinal directionality of both lanes will be BOTH. Example:
115      * 
116      * <pre>
117      * Suppose the design line runs from left to right.
118      * 
119      * =========================
120      * 
121      * LANE 1L (BACKWARD)         GTUs are allowed to move to lane 2L 
122      *                            Permeability RIGHT is true, although vehicles will go to the LEFT...
123      * -------------------------  
124      * =========================
125      * 
126      * LANE 2L (BACKWARD)         GTUs are NOT allowed to move to lane 1L nor to lane 2R
127      *                            No permeability defined (empty set)
128      * =========================
129      * =========================
130      * 
131      * LANE 2R (FORWARD)          GTUs are NOT allowed to move to lane 1R nor to lane 2L
132      *                            No permeability defined (empty set)
133      * =========================
134      * -------------------------
135      * 
136      * LANE 1R (FORWARD)          GTUs are allowed to move to lane 2R
137      *                            Permeability LEFT is true
138      * =========================
139      * </pre>
140      * 
141      * <b>Note:</b> GTUType.ALL can be used to set permeability for all types of GTU at once.
142      * <p>
143      * @param gtuType GTU type to add permeability for.
144      * @param lateralDirection direction to add (LEFT or RIGHT) compared to the direction of the design line.
145      */
146     public final void addPermeability(final GTUType gtuType, final LateralDirectionality lateralDirection)
147     {
148         if (!this.permeabilityMap.containsKey(gtuType))
149         {
150             this.permeabilityMap.put(gtuType, new HashSet<LateralDirectionality>(2));
151         }
152         this.permeabilityMap.get(gtuType).add(lateralDirection);
153     }
154 
155     /**
156      * @param gtuType GTU type to look for.
157      * @param lateralDirection direction to look for (LEFT or RIGHT) compared to the direction of the design line.
158      * @return whether the road marker is permeable for the GTU type.
159      */
160     public final boolean isPermeable(final GTUType gtuType, final LateralDirectionality lateralDirection)
161     {
162         for (GTUType testGTUType = gtuType; null != testGTUType; testGTUType = testGTUType.getParent())
163         {
164             Set<LateralDirectionality> directions = this.permeabilityMap.get(testGTUType);
165             if (null != directions)
166             {
167                 return directions.contains(lateralDirection);
168             }
169         }
170         return false;
171     }
172 
173     /**
174      * @return permeabilityMap for internal use in (sub)classes.
175      */
176     protected final Map<GTUType, Set<LateralDirectionality>> getPermeabilityMap()
177     {
178         return this.permeabilityMap;
179     }
180 
181 }