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.LocalEventProducer;
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.NetworkException;
14  
15  /**
16   * A static object with a height that a GTU might have to avoid, or which can cause occlusion for perception. All objects are
17   * potential event producers, which allows them to signal that their state has changed.
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   */
25  public class StaticObject extends LocalEventProducer implements LocatedObject, Serializable, Identifiable, Drawable
26  {
27      /** */
28      private static final long serialVersionUID = 20160400L;
29  
30      /** the id. */
31      private final String id;
32  
33      /** The top-level 2D outline of the object. */
34      private final OtsLine3d geometry;
35  
36      /** The height of the object. */
37      private final Length height;
38  
39      /**
40       * @param id String; the id
41       * @param geometry OtsLine3d; the top-level 2D outline of the object
42       * @param height Length; the height of the object
43       */
44      protected StaticObject(final String id, final OtsLine3d geometry, final Length height)
45      {
46          Throw.whenNull(id, "object id cannot be null");
47          Throw.whenNull(geometry, "geometry cannot be null");
48          Throw.whenNull(height, "height cannot be null");
49  
50          this.id = id;
51          this.geometry = geometry;
52          this.height = height;
53      }
54  
55      /**
56       * Initialize the object after it has been fully created.
57       * @throws NetworkException e.g. on error registering the object in the network
58       */
59      @SuppressWarnings("checkstyle:designforextension")
60      protected void init() throws NetworkException
61      {
62          // notify the potential animation of the existence of a StaticObject
63          // These next events are fired by the Network when the object is registered in the Network.
64          // fireTimedEvent(Network.OBJECT_ADD_EVENT, this.id);
65          // fireTimedEvent(Network.ANIMATION_OBJECT_ADD_EVENT, this);
66      }
67  
68      /**
69       * Make a static object and carry out the initialization after it has been fully created.
70       * @param id String; the id
71       * @param geometry OtsLine3d; the top-level 2D outline of the object
72       * @param height Length; the height of the object
73       * @return the static object
74       * @throws NetworkException e.g. on error registering the object in the network
75       */
76      public static StaticObject create(final String id, final OtsLine3d geometry, final Length height) throws NetworkException
77      {
78          StaticObject staticObject = new StaticObject(id, geometry, height);
79          staticObject.init();
80          return staticObject;
81      }
82  
83      /**
84       * Make a static object with zero height and carry out the initialization after it has been fully created.
85       * @param id String; the id
86       * @param geometry OtsLine3d; the top-level 2D outline of the object
87       * @return the static object
88       * @throws NetworkException e.g. on error registering the object in the network
89       */
90      public static StaticObject create(final String id, final OtsLine3d geometry) throws NetworkException
91      {
92          return create(id, geometry, Length.ZERO);
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public final OtsLine3d getGeometry()
98      {
99          return this.geometry;
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public final Length getHeight()
105     {
106         return this.height;
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public final String getId()
112     {
113         return this.id;
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     @SuppressWarnings("checkstyle:designforextension")
119     public String getFullId()
120     {
121         return this.id;
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     @SuppressWarnings("checkstyle:designforextension")
127     public DirectedPoint getLocation()
128     {
129         return this.geometry.getLocation();
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     @SuppressWarnings("checkstyle:designforextension")
135     public Bounds getBounds()
136     {
137         return this.geometry.getBounds();
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     @SuppressWarnings("checkstyle:designforextension")
143     public String toString()
144     {
145         return "StaticObject [geometry=" + getGeometry() + ", height=" + this.height + "]";
146     }
147 
148 }