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.network.Network;
7 import org.opentrafficsim.core.network.NetworkException;
8 import org.opentrafficsim.core.object.StaticObject;
9 import org.opentrafficsim.road.network.lane.CrossSectionElement;
10 import org.opentrafficsim.road.network.lane.Lane;
11
12 import nl.tudelft.simulation.language.Throw;
13
14 /**
15 * An abstract implementation of the LaneBasedObject interface with the required fields being initialized and getters for those
16 * fields. All StaticObjects are EventProducers, allowing them to provide state changes to subscribers.
17 * <p>
18 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20 * </p>
21 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
22 * initial version Sep 10, 2016 <br>
23 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
26 */
27 public abstract class AbstractLaneBasedObject extends StaticObject implements LaneBasedObject
28 {
29 /** */
30 private static final long serialVersionUID = 20160909L;
31
32 /** The lane for which this is a sensor. */
33 private final Lane lane;
34
35 /** The position (between 0.0 and the length of the Lane) of the sensor on the design line of the lane. */
36 private final Length longitudinalPosition;
37
38 /**
39 * Construct a new LanebasedObject with the required fields.
40 * @param lane Lane; The lane for which this is a sensor
41 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
42 * line of the lane
43 * @param geometry the geometry of the object, which provides its location and bounds as well
44 * @param height the height of the object, in case it is a 3D object
45 * @throws NetworkException when the position on the lane is out of bounds
46 */
47 public AbstractLaneBasedObject(final Lane lane, final Length longitudinalPosition, final OTSLine3D geometry,
48 final Length height) throws NetworkException
49 {
50 super(geometry, height);
51
52 Throw.whenNull(lane, "lane is null");
53 Throw.whenNull(longitudinalPosition, "longitudinal position is null");
54 Throw.whenNull(geometry, "geometry is null");
55 Throw.when(longitudinalPosition.si < 0.0 || longitudinalPosition.si > lane.getCenterLine().getLengthSI(),
56 NetworkException.class, "Position of the object on the lane is out of bounds");
57
58 this.lane = lane;
59 this.longitudinalPosition = longitudinalPosition;
60 }
61
62 /**
63 * Construct a new LanebasedObject with the required fields.
64 * @param lane Lane; The lane for which this is a sensor
65 * @param longitudinalPosition Length; The position (between 0.0 and the length of the Lane) of the sensor on the design
66 * line of the lane
67 * @param geometry the geometry of the object, which provides its location and bounds as well
68 * @throws NetworkException when the position on the lane is out of bounds
69 */
70 public AbstractLaneBasedObject(final Lane lane, final Length longitudinalPosition, final OTSLine3D geometry)
71 throws NetworkException
72 {
73 this(lane, longitudinalPosition, geometry, Length.ZERO);
74 }
75
76 /** {@inheritDoc} */
77 @Override
78 public final Lane getLane()
79 {
80 return this.lane;
81 }
82
83 /** {@inheritDoc} */
84 @Override
85 public final Length getLongitudinalPosition()
86 {
87 return this.longitudinalPosition;
88 }
89
90 /** {@inheritDoc} */
91 @Override
92 public final StaticObject clone(final Network newNetwork, final OTSSimulatorInterface newSimulator, final boolean animation)
93 throws NetworkException
94 {
95 throw new NetworkException("LaneBasedObjects should be cloned with the clone(lane, simulator, animation) method");
96 }
97
98 /**
99 * Clone the LAneBasedObject for e.g., copying a network.
100 * @param newCSE the new cross section element to which the clone belongs
101 * @param newSimulator the new simulator for this network
102 * @param animation whether to (re)create animation or not
103 * @return a clone of this object
104 * @throws NetworkException in case the cloning fails
105 */
106 @SuppressWarnings("checkstyle:designforextension")
107 public abstract AbstractLaneBasedObject clone(final CrossSectionElement newCSE, final OTSSimulatorInterface newSimulator,
108 final boolean animation) throws NetworkException;
109
110 }