1 package org.opentrafficsim.road.network.lane.conflict;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.opentrafficsim.core.geometry.OTSGeometryException;
8 import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
9 import org.opentrafficsim.road.network.lane.Lane;
10
11 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12 import nl.tudelft.simulation.language.Throw;
13 import nl.tudelft.simulation.language.d3.DirectedPoint;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class DefaultConflictRule implements ConflictRule
29 {
30
31
32 private Map<String, ConflictPriority> map = null;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @Override
72 public ConflictPriority determinePriority(final Conflict conflict)
73 {
74 if (this.map == null)
75 {
76 ConflictPriority[] conflictPriorities = getConflictRules(conflict.getLane(), conflict.getLongitudinalPosition(),
77 conflict.getOtherConflict().getLane(), conflict.getOtherConflict().getLongitudinalPosition(),
78 conflict.getConflictType());
79 this.map = new HashMap<>();
80 this.map.put(conflict.getId(), conflictPriorities[0]);
81 this.map.put(conflict.getOtherConflict().getId(), conflictPriorities[1]);
82 }
83 ConflictPriority out = this.map.get(conflict.getId());
84 Throw.when(out == null, IllegalArgumentException.class,
85 "Conflict %s is not related to a conflict that was used before in the same conflict rule.", conflict);
86 return out;
87 }
88
89
90
91
92
93
94
95
96
97
98 private static ConflictPriority[] getConflictRules(final Lane lane1, final Length longitudinalPosition1, final Lane lane2,
99 final Length longitudinalPosition2, final ConflictType conflictType)
100 {
101 Throw.when(conflictType.equals(ConflictType.SPLIT), UnsupportedOperationException.class,
102 "DefaultConflictRule is not for use on a split conflict. Use SplitConflictRule instead.");
103 ConflictPriority[] conflictRules = new ConflictPriority[2];
104 Priority priority1 = lane1.getParentLink().getPriority();
105 Priority priority2 = lane2.getParentLink().getPriority();
106 if (priority1.isAllStop() && priority2.isAllStop())
107 {
108 conflictRules[0] = ConflictPriority.ALL_STOP;
109 conflictRules[1] = ConflictPriority.ALL_STOP;
110 }
111 else if (priority1.equals(priority2) || (priority1.isYield() && priority2.isStop())
112 || (priority2.isYield() && priority1.isStop()))
113 {
114 Throw.when(priority1.isTurnOnRed() || priority1.isBusStop(), IllegalArgumentException.class,
115 "Both priorities are either 'turn on red' or 'bus stop', which is not allowed. "
116 + "Use BusStopConflictRule for bus stops.");
117
118 DirectedPoint p1;
119 DirectedPoint p2;
120 try
121 {
122 p1 = lane1.getCenterLine().getLocation(longitudinalPosition1);
123 p2 = lane2.getCenterLine().getLocation(longitudinalPosition2);
124 }
125 catch (OTSGeometryException exception)
126 {
127 throw new RuntimeException("Conflict position is not on its lane.", exception);
128 }
129 double diff = p2.getRotZ() - p1.getRotZ();
130 while (diff > Math.PI)
131 {
132 diff -= 2 * Math.PI;
133 }
134 while (diff < -Math.PI)
135 {
136 diff += 2 * Math.PI;
137 }
138 if (diff > 0.0)
139 {
140
141 conflictRules[0] = priority1.isStop() ? ConflictPriority.STOP : ConflictPriority.YIELD;
142 conflictRules[1] = ConflictPriority.PRIORITY;
143 }
144 else
145 {
146
147 conflictRules[0] = ConflictPriority.PRIORITY;
148 conflictRules[1] = priority2.isStop() ? ConflictPriority.STOP : ConflictPriority.YIELD;
149 }
150 }
151 else if ((priority1.isPriority() || priority1.isNone())
152 && (priority2.isNone() || priority2.isYield() || priority2.isStop()))
153 {
154 conflictRules[0] = ConflictPriority.PRIORITY;
155 conflictRules[1] = priority2.isStop() ? ConflictPriority.STOP : ConflictPriority.YIELD;
156 }
157 else if ((priority2.isPriority() || priority2.isNone())
158 && (priority1.isNone() || priority1.isYield() || priority1.isStop()))
159 {
160 conflictRules[0] = priority1.isStop() ? ConflictPriority.STOP : ConflictPriority.YIELD;
161 conflictRules[1] = ConflictPriority.PRIORITY;
162 }
163 else if (priority1.isTurnOnRed() && (priority2.isYield() || priority2.isStop()))
164 {
165 conflictRules[0] = ConflictPriority.TURN_ON_RED;
166 conflictRules[1] = ConflictPriority.PRIORITY;
167 }
168 else if (priority2.isTurnOnRed() && (priority1.isYield() || priority1.isStop()))
169 {
170 conflictRules[0] = ConflictPriority.PRIORITY;
171 conflictRules[1] = ConflictPriority.TURN_ON_RED;
172 }
173 else
174 {
175 throw new IllegalArgumentException(
176 "Could not sort out conflict priority from link priorities " + priority1 + " and " + priority2);
177 }
178 return conflictRules;
179 }
180
181
182 @Override
183 public final ConflictRule clone(final SimulatorInterface.TimeDoubleUnit newSimulator)
184 {
185 return new DefaultConflictRule();
186 }
187
188
189 @Override
190 public final String toString()
191 {
192 return "DefaultConflictRule";
193 }
194
195 }