AbstractHeadwayGtu.java

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

  2. import java.util.ArrayList;
  3. import java.util.EnumSet;
  4. import java.util.List;

  5. import org.djunits.value.vdouble.scalar.Acceleration;
  6. import org.djunits.value.vdouble.scalar.Length;
  7. import org.djunits.value.vdouble.scalar.Speed;
  8. import org.djunits.value.vdouble.scalar.Time;
  9. import org.djutils.exceptions.Throw;
  10. import org.opentrafficsim.core.gtu.GtuException;
  11. import org.opentrafficsim.core.gtu.GtuType;
  12. import org.opentrafficsim.core.network.NetworkException;
  13. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  14. import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
  15. import org.opentrafficsim.road.network.speed.SpeedLimitTypes;

  16. /**
  17.  * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
  18.  * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
  19.  * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
  20.  * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
  21.  * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
  22.  * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
  23.  * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
  24.  * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
  25.  * 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
  26.  * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
  27.  * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
  28.  * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
  29.  * positions.
  30.  * <p>
  31.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  32.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  33.  * </p>
  34.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  35.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  36.  */
  37. public abstract class AbstractHeadwayGtu extends AbstractHeadwayCopy implements HeadwayGtu
  38. {
  39.     /** */
  40.     private static final long serialVersionUID = 20160410L;

  41.     /** The perceived GTU Type, or null if unknown. */
  42.     private final GtuType gtuType;

  43.     /** Whether the GTU is facing the same direction. */
  44.     private final boolean facingSameDirection;

  45.     /** The observable characteristics of the GTU. */
  46.     private final EnumSet<GtuStatus> gtuStatus = EnumSet.noneOf(GtuStatus.class);

  47.     /** Perceived desired speed. */
  48.     private final Speed desiredSpeed;

  49.     /** Perceived width. */
  50.     private final Length width;

  51.     /**
  52.      * Construct a new Headway information object, for a moving GTU ahead of us or behind us.
  53.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  54.      * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
  55.      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
  56.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  57.      * @param length the (perceived) length of the other object; can not be null.
  58.      * @param width the (perceived) width of the other object; can not be null.
  59.      * @param speed the (perceived) speed of the other object; can be null if unknown.
  60.      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
  61.      * @param desiredSpeed Speed; desired speed
  62.      * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
  63.      * @throws GtuException when id is null, objectType is null, or parameters are inconsistent
  64.      */
  65.     @SuppressWarnings("checkstyle:parameternumber")
  66.     public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length distance, final boolean facingSameDirection,
  67.             final Length length, final Length width, final Speed speed, final Acceleration acceleration,
  68.             final Speed desiredSpeed, final GtuStatus... gtuStatus) throws GtuException
  69.     {
  70.         super(ObjectType.GTU, id, distance, length, speed, acceleration);
  71.         Throw.whenNull(width, "Width may not be null.");
  72.         this.width = width;
  73.         this.facingSameDirection = facingSameDirection;
  74.         this.gtuType = gtuType;
  75.         this.desiredSpeed = desiredSpeed;
  76.         for (GtuStatus status : gtuStatus)
  77.         {
  78.             this.gtuStatus.add(status);
  79.         }
  80.     }

  81.     /**
  82.      * Construct a new Headway information object, for a non-moving GTU ahead of us or behind us.
  83.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  84.      * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
  85.      * @param distance Length; the distance to the other Gtu; if this constructor is used, distance cannot be null.
  86.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  87.      * @param length the (perceived) length of the other object; can not be null.
  88.      * @param width the (perceived) width of the other object; can not be null.
  89.      * @param desiredSpeed Speed; desired speed
  90.      * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
  91.      * @throws GtuException when id is null, or parameters are inconsistent
  92.      */
  93.     public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length distance, final boolean facingSameDirection,
  94.             final Length length, final Length width, final Speed desiredSpeed, final GtuStatus... gtuStatus) throws GtuException
  95.     {
  96.         super(ObjectType.GTU, id, distance, length);
  97.         Throw.whenNull(width, "Width may not be null.");
  98.         this.width = width;
  99.         this.facingSameDirection = facingSameDirection;
  100.         this.gtuType = gtuType;
  101.         this.desiredSpeed = desiredSpeed;
  102.         for (GtuStatus status : gtuStatus)
  103.         {
  104.             this.gtuStatus.add(status);
  105.         }
  106.     }

  107.     /**
  108.      * Construct a new Headway information object, for a moving GTU parallel with us.
  109.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  110.      * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
  111.      * @param overlapFront the front-front distance to the other Gtu; if this constructor is used, this value cannot be null.
  112.      * @param overlap the 'center' overlap with the other Gtu; if this constructor is used, this value cannot be null.
  113.      * @param overlapRear the rear-rear distance to the other Gtu; if this constructor is used, this value cannot be null.
  114.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  115.      * @param length the (perceived) length of the other object; can not be null.
  116.      * @param width the (perceived) width of the other object; can not be null.
  117.      * @param speed the (perceived) speed of the other Gtu; can be null if unknown.
  118.      * @param acceleration the (perceived) acceleration of the other Gtu; can be null if unknown.
  119.      * @param desiredSpeed Speed; desired speed
  120.      * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
  121.      * @throws GtuException when id is null, or parameters are inconsistent
  122.      */
  123.     @SuppressWarnings("checkstyle:parameternumber")
  124.     public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length overlapFront, final Length overlap,
  125.             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
  126.             final Speed speed, final Acceleration acceleration, final Speed desiredSpeed, final GtuStatus... gtuStatus)
  127.             throws GtuException
  128.     {
  129.         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
  130.         Throw.whenNull(width, "Width may not be null.");
  131.         this.width = width;
  132.         this.facingSameDirection = facingSameDirection;
  133.         this.gtuType = gtuType;
  134.         this.desiredSpeed = desiredSpeed;
  135.         for (GtuStatus status : gtuStatus)
  136.         {
  137.             this.gtuStatus.add(status);
  138.         }
  139.     }

  140.     /**
  141.      * Construct a new Headway information object, for a non-moving GTU parallel with us.
  142.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  143.      * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
  144.      * @param overlapFront the front-front distance to the other Gtu; if this constructor is used, this value cannot be null.
  145.      * @param overlap the 'center' overlap with the other Gtu; if this constructor is used, this value cannot be null.
  146.      * @param overlapRear the rear-rear distance to the other Gtu; if this constructor is used, this value cannot be null.
  147.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  148.      * @param length the (perceived) length of the other object; can not be null.
  149.      * @param width the (perceived) width of the other object; can not be null.
  150.      * @param desiredSpeed Speed; desired speed
  151.      * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
  152.      * @throws GtuException when id is null, or parameters are inconsistent
  153.      */
  154.     @SuppressWarnings("checkstyle:parameternumber")
  155.     public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length overlapFront, final Length overlap,
  156.             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
  157.             final Speed desiredSpeed, final GtuStatus... gtuStatus) throws GtuException
  158.     {
  159.         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
  160.         Throw.whenNull(width, "Width may not be null.");
  161.         this.width = width;
  162.         this.facingSameDirection = facingSameDirection;
  163.         this.gtuType = gtuType;
  164.         this.desiredSpeed = desiredSpeed;
  165.         for (GtuStatus status : gtuStatus)
  166.         {
  167.             this.gtuStatus.add(status);
  168.         }
  169.     }

  170.     /**
  171.      * @return gtuType
  172.      */
  173.     @Override
  174.     public final GtuType getGtuType()
  175.     {
  176.         return this.gtuType;
  177.     }

  178.     /** {@inheritDoc} */
  179.     @Override
  180.     public final Speed getDesiredSpeed()
  181.     {
  182.         return this.desiredSpeed;
  183.     }

  184.     /**
  185.      * @return facingSameDirection
  186.      */
  187.     @Override
  188.     public final boolean isFacingSameDirection()
  189.     {
  190.         return this.facingSameDirection;
  191.     }

  192.     /** @return were the braking lights on? */
  193.     @Override
  194.     public final boolean isBrakingLightsOn()
  195.     {
  196.         return this.gtuStatus.contains(GtuStatus.BRAKING_LIGHTS);
  197.     }

  198.     /** @return was the left turn indicator on? */
  199.     @Override
  200.     public final boolean isLeftTurnIndicatorOn()
  201.     {
  202.         return this.gtuStatus.contains(GtuStatus.LEFT_TURNINDICATOR);
  203.     }

  204.     /** @return was the right turn indicator on? */
  205.     @Override
  206.     public final boolean isRightTurnIndicatorOn()
  207.     {
  208.         return this.gtuStatus.contains(GtuStatus.RIGHT_TURNINDICATOR);
  209.     }

  210.     /** @return were the emergency lights on? */
  211.     @Override
  212.     public final boolean isEmergencyLightsOn()
  213.     {
  214.         return this.gtuStatus.contains(GtuStatus.EMERGENCY_LIGHTS);
  215.     }

  216.     /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
  217.     @Override
  218.     public final boolean isHonking()
  219.     {
  220.         return this.gtuStatus.contains(GtuStatus.HONK);
  221.     }

  222.     /**
  223.      * For subclasses that create a copy of themselves.
  224.      * @return set of gtu status
  225.      */
  226.     protected final GtuStatus[] getGtuStatus()
  227.     {
  228.         return this.gtuStatus.toArray(new GtuStatus[this.gtuStatus.size()]);
  229.     }

  230.     /**
  231.      * Collects GTU statuses from a gtu.
  232.      * @param gtu LaneBasedGtu; gtu
  233.      * @param when Time; time
  234.      * @return GTU statuses
  235.      */
  236.     public static final GtuStatus[] getGtuStatuses(final LaneBasedGtu gtu, final Time when)
  237.     {
  238.         List<GtuStatus> statuses = new ArrayList<>();
  239.         if (gtu.isBrakingLightsOn(when))
  240.         {
  241.             statuses.add(GtuStatus.BRAKING_LIGHTS);
  242.         }
  243.         if (gtu.getTurnIndicatorStatus(when).isHazard())
  244.         {
  245.             statuses.add(GtuStatus.EMERGENCY_LIGHTS);
  246.         }
  247.         else if (gtu.getTurnIndicatorStatus(when).isLeft())
  248.         {
  249.             statuses.add(GtuStatus.LEFT_TURNINDICATOR);
  250.         }
  251.         else if (gtu.getTurnIndicatorStatus(when).isRight())
  252.         {
  253.             statuses.add(GtuStatus.RIGHT_TURNINDICATOR);
  254.         }
  255.         return statuses.toArray(new GtuStatus[statuses.size()]);
  256.     }

  257.     /**
  258.      * Creates speed limit info for given GTU.
  259.      * @param gtu LaneBasedGtu; gtu to the the speed limit info for
  260.      * @return speed limit info for given GTU
  261.      */
  262.     public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGtu gtu)
  263.     {
  264.         SpeedLimitInfo sli = new SpeedLimitInfo();
  265.         sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
  266.         try
  267.         {
  268.             sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().lane().getSpeedLimit(gtu.getType()));
  269.         }
  270.         catch (NetworkException | GtuException exception)
  271.         {
  272.             throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
  273.         }
  274.         return sli;
  275.     }

  276.     /** {@inheritDoc} */
  277.     @Override
  278.     public Length getWidth()
  279.     {
  280.         return this.width;
  281.     }

  282.     /** {@inheritDoc} */
  283.     @Override
  284.     @SuppressWarnings("checkstyle:designforextension")
  285.     public String toString()
  286.     {
  287.         return "AbstractHeadwayGtu [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
  288.                 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
  289.                 + "]";
  290.     }

  291. }