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.DirectedPoint;
6 import org.opentrafficsim.core.geometry.OtsLine3d;
7 import org.opentrafficsim.core.network.NetworkException;
8 import org.opentrafficsim.core.object.StaticObject;
9 import org.opentrafficsim.road.network.lane.Lane;
10 import org.opentrafficsim.road.network.lane.object.detector.LaneDetector;
11
12 /**
13 * An abstract implementation of the LaneBasedObject interface with the required fields being initialized and getters for those
14 * fields. All StaticObjects are EventProducers, allowing them to provide state changes to subscribers.<br>
15 * <br>
16 * Note that extending classes must use a create(...) factory method that calls init() after fully constructing the object to
17 * avoid "half constructed" objects to be registered in the network.
18 * <p>
19 * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21 * </p>
22 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
23 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
24 * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
25 */
26 public abstract class AbstractLaneBasedObject extends StaticObject implements LaneBasedObject
27 {
28 /** */
29 private static final long serialVersionUID = 20160909L;
30
31 /** The lane for which this is a sensor. */
32 private final Lane lane;
33
34 /** The position (between 0.0 and the length of the Lane) of the sensor on the design line of the lane. */
35 private final Length longitudinalPosition;
36
37 /** The DirectedPoint that indicates the location on the lane. */
38 private final DirectedPoint location;
39
40 /**
41 * Construct a new AbstractLanebasedObject with the required fields.
42 * @param id String; the id of the new object
43 * @param lane Lane; The lane on which the new object resides. If the new object is a Sensor; it is automatically registered
44 * on the lane
45 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
46 * line of the lane
47 * @param geometry OtsLine3d; the geometry of the object, which provides its location and bounds as well
48 * @param height Length; the height of the object, in case it is a 3D object
49 * @throws NetworkException when the position on the lane is out of bounds
50 */
51 protected AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
52 final OtsLine3d geometry, final Length height) throws NetworkException
53 {
54 super(id, geometry, height);
55
56 Throw.whenNull(lane, "lane is null");
57 Throw.whenNull(longitudinalPosition, "longitudinal position is null");
58 Throw.when(longitudinalPosition.si < 0.0 || longitudinalPosition.si > lane.getCenterLine().getLengthSI(),
59 NetworkException.class, "Position of the object on the lane is out of bounds");
60
61 this.lane = lane;
62 this.longitudinalPosition = longitudinalPosition;
63 DirectedPoint p = lane.getCenterLine().getLocationExtended(this.longitudinalPosition);
64 this.location = new DirectedPoint(p.x, p.y, p.z + 0.01, p.getRotX(), p.getRotY(), p.getRotZ());
65 }
66
67 /**
68 * Construct a new LaneBasedObject with the required fields.
69 * @param id String; the id of the new AbstractLaneBasedObject
70 * @param lane Lane; The lane for which this is a sensor
71 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
72 * line of the lane
73 * @param geometry OtsLine3d; the geometry of the object, which provides its location and bounds as well
74 * @throws NetworkException when the position on the lane is out of bounds
75 */
76 protected AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
77 final OtsLine3d geometry) throws NetworkException
78 {
79 this(id, lane, longitudinalPosition, geometry, Length.ZERO);
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 protected void init() throws NetworkException
85 {
86 super.init();
87
88 // OTS-218: detectors register themselves.
89 if (!(this instanceof LaneDetector))
90 {
91 this.lane.addLaneBasedObject(this); // implements OTS-218
92 }
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public final String getFullId()
98 {
99 return getLane().getFullId() + "." + super.getId();
100 }
101
102 /** {@inheritDoc} */
103 @Override
104 public final Lane getLane()
105 {
106 return this.lane;
107 }
108
109 /** {@inheritDoc} */
110 @Override
111 public final Length getLongitudinalPosition()
112 {
113 return this.longitudinalPosition;
114 }
115
116 /** {@inheritDoc} */
117 @Override
118 @SuppressWarnings("checkstyle:designforextension")
119 public DirectedPoint getLocation()
120 {
121 return this.location;
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 @SuppressWarnings("checkstyle:designforextension")
127 public String toString()
128 {
129 return "LaneBasedObject[" + getId() + "]";
130 }
131
132 }