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