View Javadoc
1   package org.opentrafficsim.core.object;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.draw.line.PolyLine2d;
5   import org.djutils.draw.line.Polygon2d;
6   import org.djutils.draw.point.OrientedPoint2d;
7   import org.djutils.event.LocalEventProducer;
8   import org.djutils.exceptions.Throw;
9   import org.opentrafficsim.base.geometry.BoundingPolygon;
10  import org.opentrafficsim.base.geometry.OtsBounds2d;
11  import org.opentrafficsim.core.animation.Drawable;
12  import org.opentrafficsim.core.network.NetworkException;
13  
14  /**
15   * A static object with a height that a GTU might have to avoid, or which can cause occlusion for perception. All objects are
16   * potential event producers, which allows them to signal that their state has changed.
17   * <p>
18   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * </p>
21   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
23   */
24  public class StaticObject extends LocalEventProducer implements LocatedObject, Drawable
25  {
26      /** */
27      private static final long serialVersionUID = 20160400L;
28  
29      /** the id. */
30      private final String id;
31  
32      /** The top-level 2D outline of the object. */
33      private final PolyLine2d geometry;
34  
35      /** Location. */
36      private final OrientedPoint2d location;
37  
38      /** Bounds. */
39      private final OtsBounds2d bounds;
40  
41      /** The height of the object. */
42      private final Length height;
43  
44      /**
45       * @param id String; the id
46       * @param location OrientedPoint2d; location.
47       * @param geometry PolyLine2d; the top-level 2D outline of the object
48       * @param height Length; the height of the object
49       */
50      protected StaticObject(final String id, final OrientedPoint2d location, final PolyLine2d geometry, final Length height)
51      {
52          Throw.whenNull(id, "object id cannot be null");
53          Throw.whenNull(geometry, "geometry cannot be null");
54          Throw.whenNull(height, "height cannot be null");
55  
56          this.id = id;
57          this.geometry = geometry;
58          this.location = location;
59  
60          this.bounds = BoundingPolygon.geometryToBounds(location, geometry);
61          this.height = height;
62      }
63  
64      /**
65       * Initialize the object after it has been fully created.
66       * @throws NetworkException e.g. on error registering the object in the network
67       */
68      @SuppressWarnings("checkstyle:designforextension")
69      protected void init() throws NetworkException
70      {
71          // notify the potential animation of the existence of a StaticObject
72          // These next events are fired by the Network when the object is registered in the Network.
73          // fireTimedEvent(Network.OBJECT_ADD_EVENT, this.id);
74          // fireTimedEvent(Network.ANIMATION_OBJECT_ADD_EVENT, this);
75      }
76  
77      /**
78       * Make a static object and carry out the initialization after it has been fully created.
79       * @param id String; the id
80       * @param geometry Polygon2d; the top-level 2D outline of the object
81       * @param height Length; the height of the object
82       * @return the static object
83       * @throws NetworkException e.g. on error registering the object in the network
84       */
85      public static StaticObject create(final String id, final Polygon2d geometry, final Length height) throws NetworkException
86      {
87          OrientedPoint2d point = new OrientedPoint2d(geometry.getBounds().midPoint(), 0.0);
88          StaticObject staticObject = new StaticObject(id, point, geometry, height);
89          staticObject.init();
90          return staticObject;
91      }
92  
93      /**
94       * Make a static object with zero height and carry out the initialization after it has been fully created.
95       * @param id String; the id
96       * @param geometry Polygon2d; the top-level 2D outline of the object
97       * @return the static object
98       * @throws NetworkException e.g. on error registering the object in the network
99       */
100     public static StaticObject create(final String id, final Polygon2d geometry) throws NetworkException
101     {
102         return create(id, geometry, Length.ZERO);
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public PolyLine2d getGeometry()
108     {
109         return this.geometry;
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public final Length getHeight()
115     {
116         return this.height;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public final String getId()
122     {
123         return this.id;
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     @SuppressWarnings("checkstyle:designforextension")
129     public String getFullId()
130     {
131         return this.id;
132     }
133 
134     /** {@inheritDoc} */
135     @Override
136     @SuppressWarnings("checkstyle:designforextension")
137     public OrientedPoint2d getLocation()
138     {
139         return this.location;
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     @SuppressWarnings("checkstyle:designforextension")
145     public OtsBounds2d getBounds()
146     {
147         return this.bounds;
148     }
149 
150     /** {@inheritDoc} */
151     @Override
152     @SuppressWarnings("checkstyle:designforextension")
153     public String toString()
154     {
155         return "StaticObject [geometry=" + getGeometry() + ", height=" + this.height + "]";
156     }
157 
158 }