AbstractHeadwayCopy.java

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

  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.djutils.exceptions.Throw;
  6. import org.opentrafficsim.core.gtu.GTUException;

  7. /**
  8.  * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
  9.  * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
  10.  * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
  11.  * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
  12.  * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
  13.  * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
  14.  * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
  15.  * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
  16.  * when projected from lane 1 to lane 2 is the same as the projection from lane 2 to lane 1. The same holds for shapes with
  17.  * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
  18.  * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
  19.  * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
  20.  * positions. See examples in
  21.  * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
  22.  * <p>
  23.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  24.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  25.  * <p>
  26.  * @version $Revision: 1368 $, $LastChangedDate: 2015-09-02 00:20:20 +0200 (Wed, 02 Sep 2015) $, by $Author: averbraeck $,
  27.  *          initial version 11 feb. 2015 <br>
  28.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  29.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  30.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  31.  */
  32. public abstract class AbstractHeadwayCopy extends AbstractHeadway
  33. {
  34.     /** */
  35.     private static final long serialVersionUID = 20160410L;

  36.     /** The id of the other object for comparison purposes, cannot be null. */
  37.     private final String id;

  38.     /** The (perceived) length of the other object. Can be null if unknown. */
  39.     private final Length length;

  40.     /** The (perceived) speed of the other object. v&gt;0 seen from driver chair. Can be null if unknown. */
  41.     private final Speed speed;

  42.     /** The (perceived) acceleration of the other object. Can be null if unknown. */
  43.     private final Acceleration acceleration;

  44.     /** The object type. */
  45.     private final ObjectType objectType;

  46.     /**
  47.      * Construct a new Headway information object, for an object in front, behind, or in parallel with us. <br>
  48.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  49.      * @param id String; the id of the object for comparison purposes, can not be null.
  50.      * @param distance Length; the distance to the other object
  51.      * @param length Length; the length of the other object, can be null if not applicable.
  52.      * @param overlapFront Length; the front-front distance to the other object
  53.      * @param overlap Length; the 'center' overlap with the other object
  54.      * @param overlapRear Length; the rear-rear distance to the other object
  55.      * @param speed the (perceived) speed of the other object; can be null if unknown.
  56.      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
  57.      * @throws GTUException when id is null, or parameters are inconsistent
  58.      */
  59.     @SuppressWarnings("checkstyle:parameternumber")
  60.     private AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length,
  61.             final Speed speed, final Acceleration acceleration, final Length overlapFront, final Length overlap,
  62.             final Length overlapRear) throws GTUException
  63.     {
  64.         super(distance, overlapFront, overlap, overlapRear);
  65.         Throw.when(id == null, GTUException.class, "Object id of a headway cannot be null");
  66.         this.id = id;

  67.         this.objectType = objectType;
  68.         this.length = length;
  69.         this.speed = speed;
  70.         this.acceleration = acceleration;
  71.     }

  72.     /**
  73.      * Construct a new Headway information object, for a moving object ahead of us or behind us.
  74.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  75.      * @param id String; the id of the object for comparison purposes, can not be null.
  76.      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
  77.      * @param speed the (perceived) speed of the other object; can be null if unknown.
  78.      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
  79.      * @throws GTUException when id is null, or parameters are inconsistent
  80.      */
  81.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Speed speed,
  82.             final Acceleration acceleration) throws GTUException
  83.     {
  84.         this(objectType, id, distance, null, speed, acceleration, null, null, null);
  85.     }

  86.     /**
  87.      * Construct a new Headway information object, for a non-moving object ahead of us or behind us.
  88.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  89.      * @param id String; the id of the object for comparison purposes, can not be null.
  90.      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
  91.      * @throws GTUException when id is null, or parameters are inconsistent
  92.      */
  93.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance) throws GTUException
  94.     {
  95.         this(objectType, id, distance, null, Speed.ZERO, Acceleration.ZERO, null, null, null);
  96.     }

  97.     /**
  98.      * Construct a new Headway information object, for a moving object parallel with us.
  99.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  100.      * @param id String; the id of the object for comparison purposes, can not be null.
  101.      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
  102.      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
  103.      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
  104.      * @param speed the (perceived) speed of the other object; can be null if unknown.
  105.      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
  106.      * @throws GTUException when id is null, or parameters are inconsistent
  107.      */
  108.     @SuppressWarnings("checkstyle:parameternumber")
  109.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
  110.             final Length overlapRear, final Speed speed, final Acceleration acceleration) throws GTUException
  111.     {
  112.         this(objectType, id, null, null, speed, acceleration, overlapFront, overlap, overlapRear);
  113.     }

  114.     /**
  115.      * Construct a new Headway information object, for a non-moving object parallel with us.
  116.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  117.      * @param id String; the id of the object for comparison purposes, can not be null.
  118.      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
  119.      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
  120.      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
  121.      * @throws GTUException when id is null, or parameters are inconsistent
  122.      */
  123.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
  124.             final Length overlapRear) throws GTUException
  125.     {
  126.         this(objectType, id, null, null, null, null, overlapFront, overlap, overlapRear);
  127.     }

  128.     /**
  129.      * Construct a new Headway information object, for a moving object ahead of us or behind us.
  130.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  131.      * @param id String; the id of the object for comparison purposes, can not be null.
  132.      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
  133.      * @param length the length of the other object; if this constructor is used, length cannot be null.
  134.      * @param speed the (perceived) speed of the other object; can be null if unknown.
  135.      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
  136.      * @throws GTUException when id is null, or parameters are inconsistent
  137.      */
  138.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length,
  139.             final Speed speed, final Acceleration acceleration) throws GTUException
  140.     {
  141.         this(objectType, id, distance, length, speed, acceleration, null, null, null);
  142.         Throw.whenNull(length, "Length may not be null.");
  143.     }

  144.     /**
  145.      * Construct a new Headway information object, for a non-moving object ahead of us or behind us.
  146.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  147.      * @param id String; the id of the object for comparison purposes, can not be null.
  148.      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
  149.      * @param length the length of the other object; if this constructor is used, length cannot be null.
  150.      * @throws GTUException when id is null, or parameters are inconsistent
  151.      */
  152.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length)
  153.             throws GTUException
  154.     {
  155.         this(objectType, id, distance, length, null, null, null, null, null);
  156.         Throw.whenNull(length, "Length may not be null.");
  157.     }

  158.     /**
  159.      * Construct a new Headway information object, for a moving object parallel with us.
  160.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  161.      * @param id String; the id of the object for comparison purposes, can not be null.
  162.      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
  163.      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
  164.      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
  165.      * @param length the length of the other object; if this constructor is used, length cannot be null.
  166.      * @param speed the (perceived) speed of the other object; can be null if unknown.
  167.      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
  168.      * @throws GTUException when id is null, or parameters are inconsistent
  169.      */
  170.     @SuppressWarnings("checkstyle:parameternumber")
  171.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
  172.             final Length overlapRear, final Length length, final Speed speed, final Acceleration acceleration)
  173.             throws GTUException
  174.     {
  175.         this(objectType, id, null, length, speed, acceleration, overlapFront, overlap, overlapRear);
  176.         Throw.whenNull(length, "Length may not be null.");
  177.     }

  178.     /**
  179.      * Construct a new Headway information object, for a non-moving object parallel with us.
  180.      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
  181.      * @param id String; the id of the object for comparison purposes, can not be null.
  182.      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
  183.      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
  184.      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
  185.      * @param length the length of the other object; if this constructor is used, length cannot be null.
  186.      * @throws GTUException when id is null, or parameters are inconsistent
  187.      */
  188.     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
  189.             final Length overlapRear, final Length length) throws GTUException
  190.     {
  191.         this(objectType, id, null, length, null, null, overlapFront, overlap, overlapRear);
  192.         Throw.whenNull(length, "Length may not be null.");
  193.     }

  194.     /** {@inheritDoc} */
  195.     @Override
  196.     public final String getId()
  197.     {
  198.         return this.id;
  199.     }

  200.     /** {@inheritDoc} */
  201.     @Override
  202.     public final Length getLength()
  203.     {
  204.         return this.length;
  205.     }

  206.     /** {@inheritDoc} */
  207.     @Override
  208.     public final Speed getSpeed()
  209.     {
  210.         return this.speed;
  211.     }

  212.     /** {@inheritDoc} */
  213.     @Override
  214.     public final ObjectType getObjectType()
  215.     {
  216.         return this.objectType;
  217.     }

  218.     /** {@inheritDoc} */
  219.     @Override
  220.     public final Acceleration getAcceleration()
  221.     {
  222.         return this.acceleration;
  223.     }

  224.     /** {@inheritDoc} */
  225.     @Override
  226.     public int hashCode()
  227.     {
  228.         final int prime = 31;
  229.         int result = super.hashCode();
  230.         result = prime * result + ((this.acceleration == null) ? 0 : this.acceleration.hashCode());
  231.         result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
  232.         result = prime * result + ((this.length == null) ? 0 : this.length.hashCode());
  233.         result = prime * result + ((this.objectType == null) ? 0 : this.objectType.hashCode());
  234.         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
  235.         return result;
  236.     }

  237.     /** {@inheritDoc} */
  238.     @Override
  239.     public boolean equals(final Object obj)
  240.     {
  241.         if (this == obj)
  242.         {
  243.             return true;
  244.         }
  245.         if (!super.equals(obj))
  246.         {
  247.             return false;
  248.         }
  249.         if (getClass() != obj.getClass())
  250.         {
  251.             return false;
  252.         }
  253.         AbstractHeadwayCopy other = (AbstractHeadwayCopy) obj;
  254.         if (this.acceleration == null)
  255.         {
  256.             if (other.acceleration != null)
  257.             {
  258.                 return false;
  259.             }
  260.         }
  261.         else if (!this.acceleration.equals(other.acceleration))
  262.         {
  263.             return false;
  264.         }
  265.         if (this.id == null)
  266.         {
  267.             if (other.id != null)
  268.             {
  269.                 return false;
  270.             }
  271.         }
  272.         else if (!this.id.equals(other.id))
  273.         {
  274.             return false;
  275.         }
  276.         if (this.length == null)
  277.         {
  278.             if (other.length != null)
  279.             {
  280.                 return false;
  281.             }
  282.         }
  283.         else if (!this.length.equals(other.length))
  284.         {
  285.             return false;
  286.         }
  287.         if (this.objectType != other.objectType)
  288.         {
  289.             return false;
  290.         }
  291.         if (this.speed == null)
  292.         {
  293.             if (other.speed != null)
  294.             {
  295.                 return false;
  296.             }
  297.         }
  298.         else if (!this.speed.equals(other.speed))
  299.         {
  300.             return false;
  301.         }
  302.         return true;
  303.     }

  304. }