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