View Javadoc
1   package org.opentrafficsim.core.object;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.draw.bounds.Bounds2d;
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.OtsLocatable;
10  import org.opentrafficsim.base.geometry.OtsShape;
11  import org.opentrafficsim.base.geometry.PolygonShape;
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://github.com/peter-knoppers">Peter Knoppers</a>
23   */
24  public class StaticObject extends LocalEventProducer implements LocatedObject
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 Polygon2d contour;
34  
35      /** Location. */
36      private final OrientedPoint2d location;
37  
38      /** Bounds. */
39      private final Bounds2d bounds;
40  
41      /** Shape. */
42      private final OtsShape shape;
43  
44      /** The height of the object. */
45      private final Length height;
46  
47      /**
48       * @param id the id
49       * @param location location.
50       * @param contour the top-level 2D outline of the object
51       * @param height the height of the object
52       */
53      protected StaticObject(final String id, final OrientedPoint2d location, final Polygon2d contour, final Length height)
54      {
55          Throw.whenNull(id, "object id cannot be null");
56          Throw.whenNull(contour, "geometry cannot be null");
57          Throw.whenNull(height, "height cannot be null");
58  
59          this.id = id;
60          this.contour = contour;
61          this.location = location;
62  
63          Polygon2d relativeContour = OtsLocatable.relativeContour(this);
64          this.shape = new PolygonShape(relativeContour);
65          this.bounds = relativeContour.getBounds();
66          this.height = height;
67      }
68  
69      /**
70       * Initialize the object after it has been fully created.
71       * @throws NetworkException e.g. on error registering the object in the network
72       */
73      @SuppressWarnings("checkstyle:designforextension")
74      protected void init() throws NetworkException
75      {
76          // notify the potential animation of the existence of a StaticObject
77          // These next events are fired by the Network when the object is registered in the Network.
78          // fireTimedEvent(Network.OBJECT_ADD_EVENT, this.id);
79          // fireTimedEvent(Network.ANIMATION_OBJECT_ADD_EVENT, this);
80      }
81  
82      /**
83       * Make a static object and carry out the initialization after it has been fully created.
84       * @param id the id
85       * @param geometry the top-level 2D outline of the object
86       * @param height the height 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 Polygon2d geometry, final Length height) throws NetworkException
91      {
92          OrientedPoint2d point = new OrientedPoint2d(geometry.getBounds().midPoint(), 0.0);
93          StaticObject staticObject = new StaticObject(id, point, geometry, height);
94          staticObject.init();
95          return staticObject;
96      }
97  
98      /**
99       * Make a static object with zero height and carry out the initialization after it has been fully created.
100      * @param id the id
101      * @param geometry the top-level 2D outline of the object
102      * @return the static object
103      * @throws NetworkException e.g. on error registering the object in the network
104      */
105     public static StaticObject create(final String id, final Polygon2d geometry) throws NetworkException
106     {
107         return create(id, geometry, Length.ZERO);
108     }
109 
110     @Override
111     public Polygon2d getContour()
112     {
113         return this.contour;
114     }
115 
116     @Override
117     public OtsShape getShape()
118     {
119         return this.shape;
120     }
121 
122     @Override
123     public final Length getHeight()
124     {
125         return this.height;
126     }
127 
128     @Override
129     public final String getId()
130     {
131         return this.id;
132     }
133 
134     @Override
135     @SuppressWarnings("checkstyle:designforextension")
136     public String getFullId()
137     {
138         return this.id;
139     }
140 
141     @Override
142     @SuppressWarnings("checkstyle:designforextension")
143     public OrientedPoint2d getLocation()
144     {
145         return this.location;
146     }
147 
148     @Override
149     @SuppressWarnings("checkstyle:designforextension")
150     public Bounds2d getBounds()
151     {
152         return this.bounds;
153     }
154 
155     @Override
156     @SuppressWarnings("checkstyle:designforextension")
157     public String toString()
158     {
159         return "StaticObject [contour=" + getContour() + ", height=" + this.height + "]";
160     }
161 
162 }