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