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-2019 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 CrossSectionLink; Cross Section Link to which the element belongs.
40 * @param startCenterPosition Length; the lateral start position compared to the linear geometry of the Cross Section Link
41 * at the start of the road marker.
42 * @param endCenterPosition Length; the lateral end position compared to the linear geometry of the Cross Section Link at
43 * the end of the road marker.
44 * @param beginWidth Length; start width, positioned <i>symmetrically around</i> the lateral start position.
45 * @param endWidth Length; 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 CrossSectionLink; Cross Section Link to which the element belongs.
59 * @param lateralCenterPosition Length; the lateral start position compared to the linear geometry of the Cross Section
60 * Link.
61 * @param width Length; start width, positioned <i>symmetrically around</i> the lateral start position.
62 * @throws OTSGeometryException when creation of the center line or contour geometry fails
63 * @throws NetworkException when id equal to null or not unique
64 */
65 public RoadMarkerAlong(final CrossSectionLink parentLink, final Length lateralCenterPosition, final Length width)
66 throws OTSGeometryException, NetworkException
67 {
68 super(parentLink, UUID.randomUUID().toString(), lateralCenterPosition, width);
69 }
70
71 /**
72 * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction, with the direction from
73 * the StartNode towards the EndNode as the longitudinal direction.
74 * @param parentLink CrossSectionLink; Cross Section Link to which the element belongs.
75 * @param crossSectionSlices List<CrossSectionSlice>; The offsets and widths at positions along the line, relative to
76 * the design line of the parent link. If there is just one with and offset, there should just be one element in
77 * the list with Length = 0. If there are more slices, the last one should be at the length of the design line.
78 * If not, a NetworkException is thrown.
79 * @throws OTSGeometryException when creation of the center line or contour geometry fails
80 * @throws NetworkException when id equal to null or not unique
81 */
82 public RoadMarkerAlong(final CrossSectionLink parentLink, final List<CrossSectionSlice> crossSectionSlices)
83 throws OTSGeometryException, NetworkException
84 {
85 super(parentLink, UUID.randomUUID().toString(), crossSectionSlices);
86 }
87
88 /**
89 * Clone a RoadMarkerAlong for a new network.
90 * @param newCrossSectionLink CrossSectionLink; the new link to which the clone belongs
91 * @param newSimulator SimulatorInterface.TimeDoubleUnit; the new simulator for this network
92 * @param cse RoadMarkerAlong; 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 RoadMarkerAlong cse) throws NetworkException
98 {
99 super(newCrossSectionLink, newSimulator, 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 GTUType; GTU type to add permeability for.
144 * @param lateralDirection LateralDirectionality; direction to add (LEFT or RIGHT) compared to the direction of the design
145 * line.
146 */
147 public final void addPermeability(final GTUType gtuType, final LateralDirectionality lateralDirection)
148 {
149 if (!this.permeabilityMap.containsKey(gtuType))
150 {
151 this.permeabilityMap.put(gtuType, new HashSet<LateralDirectionality>(2));
152 }
153 this.permeabilityMap.get(gtuType).add(lateralDirection);
154 }
155
156 /**
157 * @param gtuType GTUType; GTU type to look for.
158 * @param lateralDirection LateralDirectionality; direction to look for (LEFT or RIGHT) compared to the direction of the
159 * design line.
160 * @return whether the road marker is permeable for the GTU type.
161 */
162 public final boolean isPermeable(final GTUType gtuType, final LateralDirectionality lateralDirection)
163 {
164 for (GTUType testGTUType = gtuType; null != testGTUType; testGTUType = testGTUType.getParent())
165 {
166 Set<LateralDirectionality> directions = this.permeabilityMap.get(testGTUType);
167 if (null != directions)
168 {
169 return directions.contains(lateralDirection);
170 }
171 }
172 return false;
173 }
174
175 /**
176 * @return permeabilityMap for internal use in (sub)classes.
177 */
178 protected final Map<GTUType, Set<LateralDirectionality>> getPermeabilityMap()
179 {
180 return this.permeabilityMap;
181 }
182
183 }