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