StaticObject.java

  1. package org.opentrafficsim.core.object;

  2. import java.io.Serializable;

  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.djutils.event.LocalEventProducer;
  5. import org.djutils.exceptions.Throw;
  6. import org.opentrafficsim.base.Identifiable;
  7. import org.opentrafficsim.core.animation.Drawable;
  8. import org.opentrafficsim.core.geometry.Bounds;
  9. import org.opentrafficsim.core.geometry.DirectedPoint;
  10. import org.opentrafficsim.core.geometry.OtsLine3d;
  11. import org.opentrafficsim.core.network.NetworkException;

  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-2023 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://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  21.  */
  22. public class StaticObject extends LocalEventProducer implements LocatedObject, Serializable, Identifiable, Drawable
  23. {
  24.     /** */
  25.     private static final long serialVersionUID = 20160400L;

  26.     /** the id. */
  27.     private final String id;

  28.     /** The top-level 2D outline of the object. */
  29.     private final OtsLine3d geometry;

  30.     /** The height of the object. */
  31.     private final Length height;

  32.     /**
  33.      * @param id String; the id
  34.      * @param geometry OtsLine3d; the top-level 2D outline of the object
  35.      * @param height Length; the height of the object
  36.      */
  37.     protected StaticObject(final String id, final OtsLine3d geometry, final Length height)
  38.     {
  39.         Throw.whenNull(id, "object id cannot be null");
  40.         Throw.whenNull(geometry, "geometry cannot be null");
  41.         Throw.whenNull(height, "height cannot be null");

  42.         this.id = id;
  43.         this.geometry = geometry;
  44.         this.height = height;
  45.     }

  46.     /**
  47.      * Initialize the object after it has been fully created.
  48.      * @throws NetworkException e.g. on error registering the object in the network
  49.      */
  50.     @SuppressWarnings("checkstyle:designforextension")
  51.     protected void init() throws NetworkException
  52.     {
  53.         // notify the potential animation of the existence of a StaticObject
  54.         // These next events are fired by the Network when the object is registered in the Network.
  55.         // fireTimedEvent(Network.OBJECT_ADD_EVENT, this.id);
  56.         // fireTimedEvent(Network.ANIMATION_OBJECT_ADD_EVENT, this);
  57.     }

  58.     /**
  59.      * Make a static object and carry out the initialization after it has been fully created.
  60.      * @param id String; the id
  61.      * @param geometry OtsLine3d; the top-level 2D outline of the object
  62.      * @param height Length; the height of the object
  63.      * @return the static object
  64.      * @throws NetworkException e.g. on error registering the object in the network
  65.      */
  66.     public static StaticObject create(final String id, final OtsLine3d geometry, final Length height) throws NetworkException
  67.     {
  68.         StaticObject staticObject = new StaticObject(id, geometry, height);
  69.         staticObject.init();
  70.         return staticObject;
  71.     }

  72.     /**
  73.      * Make a static object with zero height and carry out the initialization after it has been fully created.
  74.      * @param id String; the id
  75.      * @param geometry OtsLine3d; the top-level 2D outline 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) throws NetworkException
  80.     {
  81.         return create(id, geometry, Length.ZERO);
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public final OtsLine3d getGeometry()
  86.     {
  87.         return this.geometry;
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public final Length getHeight()
  92.     {
  93.         return this.height;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public final String getId()
  98.     {
  99.         return this.id;
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     @SuppressWarnings("checkstyle:designforextension")
  104.     public String getFullId()
  105.     {
  106.         return this.id;
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     @SuppressWarnings("checkstyle:designforextension")
  111.     public DirectedPoint getLocation()
  112.     {
  113.         return this.geometry.getLocation();
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     @SuppressWarnings("checkstyle:designforextension")
  118.     public Bounds getBounds()
  119.     {
  120.         return this.geometry.getBounds();
  121.     }

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     @SuppressWarnings("checkstyle:designforextension")
  125.     public String toString()
  126.     {
  127.         return "StaticObject [geometry=" + getGeometry() + ", height=" + this.height + "]";
  128.     }

  129. }