1 package org.opentrafficsim.road.network.lane.object;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
5 import org.opentrafficsim.core.geometry.OTSLine3D;
6 import org.opentrafficsim.core.gtu.GTUDirectionality;
7 import org.opentrafficsim.core.network.LongitudinalDirectionality;
8 import org.opentrafficsim.core.network.Network;
9 import org.opentrafficsim.core.network.NetworkException;
10 import org.opentrafficsim.core.object.StaticObject;
11 import org.opentrafficsim.road.network.lane.CrossSectionElement;
12 import org.opentrafficsim.road.network.lane.Lane;
13 import org.opentrafficsim.road.network.lane.object.sensor.SingleSensor;
14
15 import nl.tudelft.simulation.language.Throw;
16 import nl.tudelft.simulation.language.d3.DirectedPoint;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public abstract class AbstractLaneBasedObject extends StaticObject implements LaneBasedObject
32 {
33
34 private static final long serialVersionUID = 20160909L;
35
36
37 private final Lane lane;
38
39
40 private final LongitudinalDirectionality direction;
41
42
43 private final Length longitudinalPosition;
44
45
46 private final DirectedPoint location;
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public AbstractLaneBasedObject(final String id, final Lane lane, final LongitudinalDirectionality direction,
61 final Length longitudinalPosition, final OTSLine3D geometry, final Length height) throws NetworkException
62 {
63 super(id, geometry, height);
64
65 Throw.whenNull(lane, "lane is null");
66 Throw.whenNull(direction, "Longitudinal direction is null");
67 Throw.whenNull(longitudinalPosition, "longitudinal position is null");
68 Throw.when(longitudinalPosition.si < 0.0 || longitudinalPosition.si > lane.getCenterLine().getLengthSI(),
69 NetworkException.class, "Position of the object on the lane is out of bounds");
70
71 this.lane = lane;
72 this.direction = direction;
73 this.longitudinalPosition = longitudinalPosition;
74 DirectedPoint p = lane.getCenterLine().getLocationExtended(this.longitudinalPosition);
75 this.location = new DirectedPoint(p.x, p.y, p.z + 0.01, p.getRotX(), p.getRotY(), p.getRotZ());
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 Lane getLane()
134 {
135 return this.lane;
136 }
137
138
139 public final LongitudinalDirectionality getDirection()
140 {
141 return this.direction;
142 }
143
144
145 @Override
146 public final Length getLongitudinalPosition()
147 {
148 return this.longitudinalPosition;
149 }
150
151
152 @Override
153 @SuppressWarnings("checkstyle:designforextension")
154 public DirectedPoint getLocation()
155 {
156 return this.location;
157 }
158
159
160 @Override
161 public final StaticObject clone(final Network newNetwork, final OTSSimulatorInterface newSimulator, final boolean animation)
162 throws NetworkException
163 {
164 throw new NetworkException("LaneBasedObjects should be cloned with the clone(lane, simulator, animation) method");
165 }
166
167
168
169
170
171
172
173
174
175 @SuppressWarnings("checkstyle:designforextension")
176 public abstract AbstractLaneBasedObject clone(final CrossSectionElement newCSE, final OTSSimulatorInterface newSimulator,
177 final boolean animation) throws NetworkException;
178
179 }