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