1 package org.opentrafficsim.road.network.lane.object;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.opentrafficsim.core.geometry.OTSLine3D;
5 import org.opentrafficsim.core.network.LongitudinalDirectionality;
6 import org.opentrafficsim.core.network.Network;
7 import org.opentrafficsim.core.network.NetworkException;
8 import org.opentrafficsim.core.object.StaticObject;
9 import org.opentrafficsim.road.network.lane.CrossSectionElement;
10 import org.opentrafficsim.road.network.lane.Lane;
11 import org.opentrafficsim.road.network.lane.object.sensor.SingleSensor;
12
13 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
14 import nl.tudelft.simulation.language.Throw;
15 import nl.tudelft.simulation.language.d3.DirectedPoint;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public abstract class AbstractLaneBasedObject extends StaticObject implements LaneBasedObject
31 {
32
33 private static final long serialVersionUID = 20160909L;
34
35
36 private final Lane lane;
37
38
39 private final LongitudinalDirectionality direction;
40
41
42 private final Length longitudinalPosition;
43
44
45 private final DirectedPoint location;
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public AbstractLaneBasedObject(final String id, final Lane lane, final LongitudinalDirectionality direction,
60 final Length longitudinalPosition, final OTSLine3D geometry, final Length height) throws NetworkException
61 {
62 super(id, geometry, height);
63
64 Throw.whenNull(lane, "lane is null");
65 Throw.whenNull(direction, "Longitudinal direction is null");
66 Throw.whenNull(longitudinalPosition, "longitudinal position is null");
67 Throw.when(longitudinalPosition.si < 0.0 || longitudinalPosition.si > lane.getCenterLine().getLengthSI(),
68 NetworkException.class, "Position of the object on the lane is out of bounds");
69
70 this.lane = lane;
71 this.direction = direction;
72 this.longitudinalPosition = longitudinalPosition;
73 DirectedPoint p = lane.getCenterLine().getLocationExtended(this.longitudinalPosition);
74 this.location = new DirectedPoint(p.x, p.y, p.z + 0.01, p.getRotX(), p.getRotY(), p.getRotZ());
75
76
77 if (!(this instanceof SingleSensor))
78 {
79 this.lane.addLaneBasedObject(this);
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94 public AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
95 final OTSLine3D geometry, final Length height) throws NetworkException
96 {
97 this(id, lane, LongitudinalDirectionality.DIR_BOTH, longitudinalPosition, geometry, height);
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public AbstractLaneBasedObject(final String id, final Lane lane, final LongitudinalDirectionality direction,
111 final Length longitudinalPosition, final OTSLine3D geometry) throws NetworkException
112 {
113 this(id, lane, direction, longitudinalPosition, geometry, Length.ZERO);
114 }
115
116
117
118
119
120
121
122
123
124
125 public AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
126 final OTSLine3D geometry) throws NetworkException
127 {
128 this(id, lane, longitudinalPosition, geometry, Length.ZERO);
129 }
130
131
132 @Override
133 public final String getFullId()
134 {
135 return getLane().getFullId() + "." + super.getId();
136 }
137
138
139 @Override
140 public final Lane getLane()
141 {
142 return this.lane;
143 }
144
145
146 @Override
147 public final LongitudinalDirectionality getDirection()
148 {
149 return this.direction;
150 }
151
152
153 @Override
154 public final Length getLongitudinalPosition()
155 {
156 return this.longitudinalPosition;
157 }
158
159
160 @Override
161 @SuppressWarnings("checkstyle:designforextension")
162 public DirectedPoint getLocation()
163 {
164 return this.location;
165 }
166
167
168 @Override
169 public final StaticObject clone(final Network newNetwork, final SimulatorInterface.TimeDoubleUnit newSimulator, final boolean animation)
170 throws NetworkException
171 {
172 throw new NetworkException("LaneBasedObjects should be cloned with the clone(lane, simulator, animation) method");
173 }
174
175
176 @Override
177 @SuppressWarnings("checkstyle:designforextension")
178 public String toString()
179 {
180 return "LaneBasedObject[" + getId() + "]";
181 }
182
183
184
185
186
187
188
189
190
191 @SuppressWarnings("checkstyle:designforextension")
192 public abstract AbstractLaneBasedObject clone(CrossSectionElement newCSE, SimulatorInterface.TimeDoubleUnit newSimulator,
193 boolean animation) throws NetworkException;
194
195 }