View Javadoc
1   package org.opentrafficsim.core.object;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djutils.event.EventProducer;
7   import org.djutils.exceptions.Throw;
8   import org.opentrafficsim.base.Identifiable;
9   import org.opentrafficsim.core.animation.Drawable;
10  import org.opentrafficsim.core.geometry.Bounds;
11  import org.opentrafficsim.core.geometry.DirectedPoint;
12  import org.opentrafficsim.core.geometry.OTSLine3D;
13  import org.opentrafficsim.core.network.Network;
14  import org.opentrafficsim.core.network.NetworkException;
15  
16  /**
17   * A static object with a height that a GTU might have to avoid, or which can cause occlusion for perception. All objects are
18   * potential event producers, which allows them to signal that their state has changed.
19   * <p>
20   * Copyright (c) 2013-2022 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 Nov 26, 2015 <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   */
28  public class StaticObject extends EventProducer implements ObjectInterface, Serializable, Identifiable, Drawable
29  {
30      /** */
31      private static final long serialVersionUID = 20160400L;
32  
33      /** the id. */
34      private final String id;
35  
36      /** The top-level 2D outline of the object. */
37      private final OTSLine3D geometry;
38  
39      /** The height of the object. */
40      private final Length height;
41  
42      /**
43       * @param id String; the id
44       * @param geometry OTSLine3D; the top-level 2D outline of the object
45       * @param height Length; the height of the object
46       */
47      protected StaticObject(final String id, final OTSLine3D geometry, final Length height)
48      {
49          Throw.whenNull(id, "object id cannot be null");
50          Throw.whenNull(geometry, "geometry cannot be null");
51          Throw.whenNull(height, "height cannot be null");
52  
53          this.id = id;
54          this.geometry = geometry;
55          this.height = height;
56      }
57  
58      /**
59       * Initialize the object after it has been fully created.
60       * @throws NetworkException e.g. on error registering the object in the network
61       */
62      @SuppressWarnings("checkstyle:designforextension")
63      protected void init() throws NetworkException
64      {
65          // notify the potential animation of the existence of a StaticObject
66          // These next events are fired by the OTSNetwork when the object is registered in the Network.
67          // fireTimedEvent(Network.OBJECT_ADD_EVENT, this.id);
68          // fireTimedEvent(Network.ANIMATION_OBJECT_ADD_EVENT, this);
69      }
70  
71      /**
72       * Make a static object and carry out the initialization after it has been fully created.
73       * @param id String; the id
74       * @param geometry OTSLine3D; the top-level 2D outline of the object
75       * @param height Length; the height of the object
76       * @return the static object
77       * @throws NetworkException e.g. on error registering the object in the network
78       */
79      public static StaticObject create(final String id, final OTSLine3D geometry, final Length height) throws NetworkException
80      {
81          StaticObject staticObject = new StaticObject(id, geometry, height);
82          staticObject.init();
83          return staticObject;
84      }
85  
86      /**
87       * Make a static object with zero height and carry out the initialization after it has been fully created.
88       * @param id String; the id
89       * @param geometry OTSLine3D; the top-level 2D outline of the object
90       * @return the static object
91       * @throws NetworkException e.g. on error registering the object in the network
92       */
93      public static StaticObject create(final String id, final OTSLine3D geometry) throws NetworkException
94      {
95          return create(id, geometry, Length.ZERO);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public final OTSLine3D getGeometry()
101     {
102         return this.geometry;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public final Length getHeight()
108     {
109         return this.height;
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public final String getId()
115     {
116         return this.id;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     @SuppressWarnings("checkstyle:designforextension")
122     public String getFullId()
123     {
124         return this.id;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     @SuppressWarnings("checkstyle:designforextension")
130     public DirectedPoint getLocation()
131     {
132         return this.geometry.getLocation();
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     @SuppressWarnings("checkstyle:designforextension")
138     public Bounds getBounds()
139     {
140         return this.geometry.getBounds();
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     @SuppressWarnings("checkstyle:designforextension")
146     public String toString()
147     {
148         return "StaticObject3D [geometry=" + getGeometry() + ", height=" + this.height + "]";
149     }
150 
151     /**
152      * Clone the StaticObject for e.g., copying a network.
153      * @param newNetwork Network; the new network to which the clone belongs
154      * @param animation boolean; whether to (re)create animation or not
155      * @return a clone of this object
156      * @throws NetworkException in case the cloning fails
157      */
158     @SuppressWarnings("checkstyle:designforextension")
159     public StaticObject clone(final Network newNetwork, final boolean animation) throws NetworkException
160     {
161         // FIXME: why does this method have any arguments?
162         return new StaticObject(this.id, this.geometry, this.height);
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public Serializable getSourceId()
168     {
169         return this.id;
170     }
171 
172 }