AbstractHeadway.java

  1. package org.opentrafficsim.road.gtu.lane.perception.headway;

  2. import org.djunits.value.vdouble.scalar.Length;
  3. import org.djutils.exceptions.Throw;
  4. import org.opentrafficsim.core.gtu.GtuException;

  5. /**
  6.  * Super class for non-delayed and non-erroneous perception. Sub classes should wrap the actual simulation object to obtain
  7.  * information. One exception to this is {@link AbstractHeadwayCopy} (and all it's sub classes), which contains such information
  8.  * directly, and is a super class for delayed and/or erroneous perception.
  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.  */
  17. public abstract class AbstractHeadway implements Headway
  18. {

  19.     /** */
  20.     private static final long serialVersionUID = 20170324L;

  21.     /** The (perceived) distance to the other object. When objects are parallel, the distance is null. */
  22.     private final Length distance;

  23.     /**
  24.      * The (perceived) front overlap to the other object. This value should be null if there is no overlap. In the figure below
  25.      * for two GTUs, it is distance c, positive for GTU1, negative for GTU2.
  26.      *
  27.      * <pre>
  28.      * ----------
  29.      * |  GTU 1 |          -----&gt;
  30.      * ----------
  31.      *      ---------------
  32.      *      |    GTU 2    |          -----&gt;
  33.      *      ---------------
  34.      * | a  | b |     c   |
  35.      * </pre>
  36.      */
  37.     private final Length overlapFront;

  38.     /**
  39.      * The (perceived) rear overlap to the other object. This value should be null if there is no overlap. In the figure below
  40.      * for two GTUs, it is distance a, positive for GTU1, negative for GTU2.
  41.      *
  42.      * <pre>
  43.      * ----------
  44.      * |  GTU 1 |          -----&gt;
  45.      * ----------
  46.      *      ---------------
  47.      *      |    GTU 2    |          -----&gt;
  48.      *      ---------------
  49.      * | a  | b |     c   |
  50.      * </pre>
  51.      */
  52.     private final Length overlapRear;

  53.     /**
  54.      * The (perceived) overlap with the other object. This value should be null if there is no overlap. In the figure below for
  55.      * two GTUs, it is distance b, positive for GTU1 and GTU2.
  56.      *
  57.      * <pre>
  58.      * ----------
  59.      * |  GTU 1 |          -----&gt;
  60.      * ----------
  61.      *      ---------------
  62.      *      |    GTU 2    |          -----&gt;
  63.      *      ---------------
  64.      * | a  | b |     c   |
  65.      * </pre>
  66.      */
  67.     private final Length overlap;

  68.     /**
  69.      * Construct a new Headway information object, for an object in front, behind, or in parallel with us. <br>
  70.      * @param distance Length; the distance to the other object
  71.      * @param overlapFront Length; the front-front distance to the other object
  72.      * @param overlap Length; the 'center' overlap with the other object
  73.      * @param overlapRear Length; the rear-rear distance to the other object
  74.      * @throws GtuException when id is null, or parameters are inconsistent
  75.      */
  76.     protected AbstractHeadway(final Length distance, final Length overlapFront, final Length overlap, final Length overlapRear)
  77.             throws GtuException
  78.     {
  79.         Throw.when(distance != null && (overlap != null || overlapFront != null || overlapRear != null), GtuException.class,
  80.                 "overlap parameter cannot be null for front / rear headway");
  81.         this.distance = distance;

  82.         Throw.when(distance == null && (overlap == null || overlapFront == null || overlapRear == null), GtuException.class,
  83.                 "overlap parameter cannot be null for parallel headway");
  84.         Throw.when(overlap != null && overlap.si < 0, GtuException.class, "overlap cannot be negative");
  85.         this.overlap = overlap;
  86.         this.overlapFront = overlapFront;
  87.         this.overlapRear = overlapRear;
  88.     }

  89.     /**
  90.      * Construct a new Headway information object, for an object ahead of us or behind us.
  91.      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
  92.      * @throws GtuException when id is null, or parameters are inconsistent
  93.      */
  94.     public AbstractHeadway(final Length distance) throws GtuException
  95.     {
  96.         this(distance, null, null, null);
  97.     }

  98.     /**
  99.      * Construct a new Headway information object, for an object parallel with us.
  100.      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
  101.      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
  102.      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
  103.      * @throws GtuException when id is null, or parameters are inconsistent
  104.      */
  105.     @SuppressWarnings("checkstyle:parameternumber")
  106.     public AbstractHeadway(final Length overlapFront, final Length overlap, final Length overlapRear) throws GtuException
  107.     {
  108.         this(null, overlapFront, overlap, overlapRear);
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public final Length getDistance()
  113.     {
  114.         return this.distance;
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     public final Length getOverlapFront()
  119.     {
  120.         return this.overlapFront;
  121.     }

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public final Length getOverlapRear()
  125.     {
  126.         return this.overlapRear;
  127.     }

  128.     /** {@inheritDoc} */
  129.     @Override
  130.     public final Length getOverlap()
  131.     {
  132.         return this.overlap;
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     public final boolean isAhead()
  137.     {
  138.         return this.distance != null && this.distance.si > 0.0;
  139.     }

  140.     /** {@inheritDoc} */
  141.     @Override
  142.     public final boolean isBehind()
  143.     {
  144.         return this.distance != null && this.distance.si < 0.0;
  145.     }

  146.     /** {@inheritDoc} */
  147.     @Override
  148.     public final boolean isParallel()
  149.     {
  150.         return this.overlap != null;
  151.     }

  152.     /** {@inheritDoc} */
  153.     @SuppressWarnings("checkstyle:designforextension")
  154.     @Override
  155.     public int hashCode()
  156.     {
  157.         final int prime = 31;
  158.         int result = 1;
  159.         result = prime * result + ((this.distance == null) ? 0 : this.distance.hashCode());
  160.         result = prime * result + ((this.overlap == null) ? 0 : this.overlap.hashCode());
  161.         result = prime * result + ((this.overlapFront == null) ? 0 : this.overlapFront.hashCode());
  162.         result = prime * result + ((this.overlapRear == null) ? 0 : this.overlapRear.hashCode());
  163.         return result;
  164.     }

  165.     /** {@inheritDoc} */
  166.     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
  167.     @Override
  168.     public boolean equals(final Object obj)
  169.     {
  170.         if (this == obj)
  171.             return true;
  172.         if (obj == null)
  173.             return false;
  174.         if (getClass() != obj.getClass())
  175.             return false;
  176.         AbstractHeadway other = (AbstractHeadway) obj;
  177.         if (this.distance == null)
  178.         {
  179.             if (other.distance != null)
  180.                 return false;
  181.         }
  182.         else if (!this.distance.equals(other.distance))
  183.             return false;
  184.         if (this.overlap == null)
  185.         {
  186.             if (other.overlap != null)
  187.                 return false;
  188.         }
  189.         else if (!this.overlap.equals(other.overlap))
  190.             return false;
  191.         if (this.overlapFront == null)
  192.         {
  193.             if (other.overlapFront != null)
  194.                 return false;
  195.         }
  196.         else if (!this.overlapFront.equals(other.overlapFront))
  197.             return false;
  198.         if (this.overlapRear == null)
  199.         {
  200.             if (other.overlapRear != null)
  201.                 return false;
  202.         }
  203.         else if (!this.overlapRear.equals(other.overlapRear))
  204.             return false;
  205.         return true;
  206.     }

  207.     /** {@inheritDoc} */
  208.     @SuppressWarnings("checkstyle:designforextension")
  209.     @Override
  210.     public String toString()
  211.     {
  212.         if (isParallel())
  213.         {
  214.             return String.format("Parallel to object %s of type %s with speed %s", getId(), getObjectType(), getSpeed());
  215.         }
  216.         return String.format("Headway %s to object %s of type %s with speed %s", getDistance(), getId(), getObjectType(),
  217.                 getSpeed());
  218.     }

  219. }