DirectEgoPerception.java

  1. package org.opentrafficsim.core.gtu.perception;

  2. import org.djunits.value.vdouble.scalar.Acceleration;
  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.djunits.value.vdouble.scalar.Speed;
  5. import org.opentrafficsim.base.TimeStampedObject;
  6. import org.opentrafficsim.core.gtu.Gtu;
  7. import org.opentrafficsim.core.gtu.GtuException;

  8. /**
  9.  * <p>
  10.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  12.  * </p>
  13.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  14.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  15.  * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
  16.  * @param <G> GTU type
  17.  * @param <P> perception type
  18.  */

  19. public class DirectEgoPerception<G extends Gtu, P extends Perception<G>> extends AbstractPerceptionCategory<G, P>
  20.         implements EgoPerception<G, P>
  21. {

  22.     /** */
  23.     private static final long serialVersionUID = 20160811L;

  24.     /** Speed. */
  25.     private TimeStampedObject<Speed> speed;

  26.     /** Speed. */
  27.     private TimeStampedObject<Acceleration> acceleration;

  28.     /** Length. */
  29.     private TimeStampedObject<Length> length;

  30.     /** Width. */
  31.     private TimeStampedObject<Length> width;

  32.     /**
  33.      * @param perception P; perception
  34.      */
  35.     public DirectEgoPerception(final P perception)
  36.     {
  37.         super(perception);
  38.     }

  39.     /** {@inheritDoc} */
  40.     @Override
  41.     public final void updateSpeed() throws GtuException
  42.     {
  43.         synchronized (getGtu())
  44.         {
  45.             this.speed = new TimeStampedObject<Speed>(getGtu().getSpeed(), getTimestamp());
  46.         }
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public final void updateAcceleration() throws GtuException
  51.     {
  52.         synchronized (getGtu())
  53.         {
  54.             this.acceleration = new TimeStampedObject<Acceleration>(getGtu().getAcceleration(), getTimestamp());
  55.         }
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public final void updateLength() throws GtuException
  60.     {
  61.         synchronized (getGtu())
  62.         {
  63.             this.length = new TimeStampedObject<Length>(getGtu().getLength(), getTimestamp());
  64.         }
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public final void updateWidth() throws GtuException
  69.     {
  70.         synchronized (getGtu())
  71.         {
  72.             this.width = new TimeStampedObject<Length>(getGtu().getWidth(), getTimestamp());
  73.         }
  74.     }

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public final Acceleration getAcceleration()
  78.     {
  79.         return this.acceleration.getObject();
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public final Speed getSpeed()
  84.     {
  85.         return this.speed.getObject();
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public final Length getLength()
  90.     {
  91.         return this.length.getObject();
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public final Length getWidth()
  96.     {
  97.         return this.width.getObject();
  98.     }

  99.     /**
  100.      * Return the time stamped speed.
  101.      * @return time stamped speed
  102.      */
  103.     public final TimeStampedObject<Speed> getTimeStampedSpeed()
  104.     {
  105.         return this.speed;
  106.     }

  107.     /**
  108.      * Return the time stamped acceleration.
  109.      * @return time stamped acceleration
  110.      */
  111.     public final TimeStampedObject<Acceleration> getTimeStampedAcceleration()
  112.     {
  113.         return this.acceleration;
  114.     }

  115.     /**
  116.      * Return the time stamped length.
  117.      * @return time stamped length
  118.      */
  119.     public final TimeStampedObject<Length> getTimeStampedLength()
  120.     {
  121.         return this.length;
  122.     }

  123.     /**
  124.      * Return the time stamped width.
  125.      * @return time stamped width
  126.      */
  127.     public final TimeStampedObject<Length> getTimeStampedWidth()
  128.     {
  129.         return this.width;
  130.     }

  131.     /** {@inheritDoc} */
  132.     @Override
  133.     public final String toString()
  134.     {
  135.         return "DirectEgoPerception [speed=" + this.speed + ", acceleration=" + this.acceleration + ", length=" + this.length
  136.                 + ", width=" + this.width + "]";
  137.     }

  138.     @Override
  139.     public int hashCode()
  140.     {
  141.         final int prime = 31;
  142.         int result = 1;
  143.         result = prime * result + ((this.acceleration == null) ? 0 : this.acceleration.hashCode());
  144.         result = prime * result + ((this.length == null) ? 0 : this.length.hashCode());
  145.         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
  146.         result = prime * result + ((this.width == null) ? 0 : this.width.hashCode());
  147.         return result;
  148.     }

  149.     @Override
  150.     public boolean equals(final Object obj)
  151.     {
  152.         if (this == obj)
  153.             return true;
  154.         if (obj == null)
  155.             return false;
  156.         if (getClass() != obj.getClass())
  157.             return false;
  158.         DirectEgoPerception other = (DirectEgoPerception) obj;
  159.         if (this.acceleration == null)
  160.         {
  161.             if (other.acceleration != null)
  162.                 return false;
  163.         }
  164.         else if (!this.acceleration.equals(other.acceleration))
  165.             return false;
  166.         if (this.length == null)
  167.         {
  168.             if (other.length != null)
  169.                 return false;
  170.         }
  171.         else if (!this.length.equals(other.length))
  172.             return false;
  173.         if (this.speed == null)
  174.         {
  175.             if (other.speed != null)
  176.                 return false;
  177.         }
  178.         else if (!this.speed.equals(other.speed))
  179.             return false;
  180.         if (this.width == null)
  181.         {
  182.             if (other.width != null)
  183.                 return false;
  184.         }
  185.         else if (!this.width.equals(other.width))
  186.             return false;
  187.         return true;
  188.     }

  189. }