1 package org.opentrafficsim.road.network.lane;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.opentrafficsim.core.geometry.OTSLine3D;
10 import org.opentrafficsim.core.gtu.GTUType;
11 import org.opentrafficsim.core.network.LinkType;
12 import org.opentrafficsim.core.network.LongitudinalDirectionality;
13 import org.opentrafficsim.core.network.OTSLink;
14 import org.opentrafficsim.core.network.OTSNode;
15 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
16
17 /**
18 * A CrossSectionLink is a link with lanes where GTUs can possibly switch between lanes.
19 * <p>
20 * Copyright (c) 2013-2016 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-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
24 * initial version Aug 19, 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 * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
28 */
29 public class CrossSectionLink extends OTSLink implements Serializable
30 {
31 /** */
32 private static final long serialVersionUID = 20141015L;
33
34 /** List of cross-section elements. */
35 private final List<CrossSectionElement> crossSectionElementList = new ArrayList<>();
36
37 /** List of lanes. */
38 private final List<Lane> lanes = new ArrayList<>();
39
40 /** The policy to generally keep left, keep right, or keep lane. */
41 private final LaneKeepingPolicy laneKeepingPolicy;
42
43 /**
44 * Construction of a cross section link.
45 * @param id the link id.
46 * @param startNode start node (directional).
47 * @param endNode end node (directional).
48 * @param linkType the linktype
49 * @param designLine the OTSLine3D design line of the Link
50 * @param directionalityMap the directions (FORWARD, BACKWARD, BOTH, NONE) that GTUtypes can traverse this link
51 * @param laneKeepingPolicy the policy to generally keep left, keep right, or keep lane
52 */
53 public CrossSectionLink(final String id, final OTSNode startNode, final OTSNode endNode, final LinkType linkType,
54 final OTSLine3D designLine, final Map<GTUType, LongitudinalDirectionality> directionalityMap,
55 final LaneKeepingPolicy laneKeepingPolicy)
56 {
57 super(id, startNode, endNode, linkType, designLine, directionalityMap);
58 this.laneKeepingPolicy = laneKeepingPolicy;
59 }
60
61 /**
62 * Construction of a link, with a general directionality for GTUType.ALL. Other directionalities can be added with the
63 * method addDirectionality(...) later.
64 * @param id the link id.
65 * @param startNode start node (directional).
66 * @param endNode end node (directional).
67 * @param linkType the linktype
68 * @param designLine the OTSLine3D design line of the Link
69 * @param directionality the default directionality for all GTUs
70 * @param laneKeepingPolicy the policy to generally keep left, keep right, or keep lane
71 */
72 public CrossSectionLink(final String id, final OTSNode startNode, final OTSNode endNode, final LinkType linkType,
73 final OTSLine3D designLine, final LongitudinalDirectionality directionality,
74 final LaneKeepingPolicy laneKeepingPolicy)
75 {
76 super(id, startNode, endNode, linkType, designLine, directionality);
77 this.laneKeepingPolicy = laneKeepingPolicy;
78 }
79
80 /**
81 * Construction of a link, on which no traffic is allowed after construction of the link. Directionality for GTUTypes can be
82 * added with the method addDirectionality(...) later.
83 * @param id the link id.
84 * @param startNode start node (directional).
85 * @param endNode end node (directional).
86 * @param linkType the linktype
87 * @param designLine the OTSLine3D design line of the Link
88 * @param laneKeepingPolicy the policy to generally keep left, keep right, or keep lane
89 */
90 public CrossSectionLink(final String id, final OTSNode startNode, final OTSNode endNode, final LinkType linkType,
91 final OTSLine3D designLine, final LaneKeepingPolicy laneKeepingPolicy)
92 {
93 this(id, startNode, endNode, linkType, designLine, new HashMap<GTUType, LongitudinalDirectionality>(),
94 laneKeepingPolicy);
95 }
96
97 /**
98 * Add a cross section element at the end of the list. <br>
99 * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction.
100 * @param cse the cross section element to add.
101 */
102 protected final void addCrossSectionElement(final CrossSectionElement cse)
103 {
104 this.crossSectionElementList.add(cse);
105 if (cse instanceof Lane)
106 {
107 this.lanes.add((Lane) cse);
108 }
109 }
110
111 /**
112 * Add a cross section element at specified index in the list.<br>
113 * <b>Note:</b> LEFT is seen as a positive lateral direction, RIGHT as a negative lateral direction.
114 * @param index the location to insert the element.
115 * @param cse the cross section element to add.
116 */
117 protected final void addCrossSectionElement(final CrossSectionElement cse, final int index)
118 {
119 this.crossSectionElementList.add(index, cse);
120 if (cse instanceof Lane)
121 {
122 this.lanes.add((Lane) cse);
123 }
124 }
125
126 /**
127 * @return crossSectionElementList.
128 */
129 public final List<CrossSectionElement> getCrossSectionElementList()
130 {
131 return this.crossSectionElementList;
132 }
133
134 /**
135 * @return laneKeepingPolicy
136 */
137 public final LaneKeepingPolicy getLaneKeepingPolicy()
138 {
139 return this.laneKeepingPolicy;
140 }
141
142 /**
143 * @param id the cse.id to search for
144 * @return the cross section element with the given id, or null if not found
145 */
146 public final CrossSectionElement getCrossSectionElement(final String id)
147 {
148 for (CrossSectionElement cse : this.crossSectionElementList)
149 {
150 if (cse.getId().equals(id))
151 {
152 return cse;
153 }
154 }
155 return null;
156 }
157
158 /**
159 * @return lanes
160 */
161 public final List<Lane> getLanes()
162 {
163 return this.lanes;
164 }
165
166 /** {@inheritDoc} */
167 @Override
168 public final String toString()
169 {
170 return "CrossSectionLink [crossSectionElementList=" + this.crossSectionElementList + ", lanes=" + this.lanes
171 + ", laneKeepingPolicy=" + this.laneKeepingPolicy + "]";
172 }
173 }