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 * An abstract implementation of the LaneBasedObject interface with the required fields being initialized and getters for those
19 * fields. All StaticObjects are EventProducers, allowing them to provide state changes to subscribers.<br>
20 * <br>
21 * Note that extending classes must use a create(...) factory method that calls init() after fully constructing the object to
22 * avoid "half constructed" objects to be registered in the network.
23 * <p>
24 * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26 * </p>
27 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
28 * initial version Sep 10, 2016 <br>
29 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
31 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
32 */
33 public abstract class AbstractLaneBasedObject extends StaticObject implements LaneBasedObject
34 {
35 /** */
36 private static final long serialVersionUID = 20160909L;
37
38 /** The lane for which this is a sensor. */
39 private final Lane lane;
40
41 /** The direction in which this is valid. */
42 private final LongitudinalDirectionality direction;
43
44 /** The position (between 0.0 and the length of the Lane) of the sensor on the design line of the lane. */
45 private final Length longitudinalPosition;
46
47 /** The DirectedPoint that indicates the location on the lane. */
48 private final DirectedPoint location;
49
50 /**
51 * Construct a new AbstractLanebasedObject with the required fields.
52 * @param id String; the id of the new object
53 * @param lane Lane; The lane on which the new object resides. If the new object is a Sensor; it is automatically registered
54 * on the lane
55 * @param direction LongitudinalDirectionality; the directionality in which this is valid.
56 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
57 * line of the lane
58 * @param geometry OTSLine3D; the geometry of the object, which provides its location and bounds as well
59 * @param height Length; the height of the object, in case it is a 3D object
60 * @throws NetworkException when the position on the lane is out of bounds
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 * Construct a new AbstractLanebasedObject with the required fields.
82 * @param id String; the id of the new object
83 * @param lane Lane; The lane on which the new object resides. If the new object is a Sensor; it is automatically registered
84 * on the lane
85 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
86 * line of the lane
87 * @param geometry OTSLine3D; the geometry of the object, which provides its location and bounds as well
88 * @param height Length; the height of the object, in case it is a 3D object
89 * @throws NetworkException when the position on the lane is out of bounds
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 * Construct a new LaneBasedObject with the required fields.
99 * @param id String; the id of the new AbstractLaneBasedObject
100 * @param lane Lane; The lane for which this is a sensor
101 * @param direction LongitudinalDirectionality; the directionality in which this is valid.
102 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
103 * line of the lane
104 * @param geometry OTSLine3D; the geometry of the object, which provides its location and bounds as well
105 * @throws NetworkException when the position on the lane is out of bounds
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 * Construct a new LaneBasedObject with the required fields.
115 * @param id String; the id of the new AbstractLaneBasedObject
116 * @param lane Lane; The lane for which this is a sensor
117 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
118 * line of the lane
119 * @param geometry OTSLine3D; the geometry of the object, which provides its location and bounds as well
120 * @throws NetworkException when the position on the lane is out of bounds
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 /** {@inheritDoc} */
129 @Override
130 protected void init() throws NetworkException
131 {
132 super.init();
133
134 // OTS-218: sensors register themselves.
135 if (!(this instanceof SingleSensor))
136 {
137 this.lane.addLaneBasedObject(this); // implements OTS-218
138 }
139 }
140
141 /** {@inheritDoc} */
142 @Override
143 public final String getFullId()
144 {
145 return getLane().getFullId() + "." + super.getId();
146 }
147
148 /** {@inheritDoc} */
149 @Override
150 public final Lane getLane()
151 {
152 return this.lane;
153 }
154
155 /** {@inheritDoc} */
156 @Override
157 public final LongitudinalDirectionality getDirection()
158 {
159 return this.direction;
160 }
161
162 /** {@inheritDoc} */
163 @Override
164 public final Length getLongitudinalPosition()
165 {
166 return this.longitudinalPosition;
167 }
168
169 /** {@inheritDoc} */
170 @Override
171 @SuppressWarnings("checkstyle:designforextension")
172 public DirectedPoint getLocation()
173 {
174 return this.location;
175 }
176
177 /** {@inheritDoc} */
178 @Override
179 public final StaticObject clone(final Network newNetwork, final SimulatorInterface.TimeDoubleUnit newSimulator,
180 final boolean animation) throws NetworkException
181 {
182 throw new NetworkException("LaneBasedObjects should be cloned with the clone(lane, simulator, animation) method");
183 }
184
185 /** {@inheritDoc} */
186 @Override
187 @SuppressWarnings("checkstyle:designforextension")
188 public String toString()
189 {
190 return "LaneBasedObject[" + getId() + "]";
191 }
192
193 /**
194 * Clone the LaneBasedObject for e.g., copying a network.
195 * @param newCSE CrossSectionElement; the new cross section element to which the clone belongs
196 * @param newSimulator SimulatorInterface.TimeDoubleUnit; the new simulator for this network
197 * @return AbstractLaneBasedObject; a clone of this object
198 * @throws NetworkException in case the cloning fails
199 */
200 @SuppressWarnings("checkstyle:designforextension")
201 public abstract AbstractLaneBasedObject clone(CrossSectionElement newCSE, SimulatorInterface.TimeDoubleUnit newSimulator)
202 throws NetworkException;
203
204 }