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