OvertakingConditions.java

  1. package org.opentrafficsim.road.network.lane.changing;

  2. import java.io.Serializable;
  3. import java.util.Collection;

  4. import org.djunits.value.vdouble.scalar.Speed;
  5. import org.opentrafficsim.core.gtu.GTUType;
  6. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  7. import org.opentrafficsim.road.network.lane.Lane;

  8. /**
  9.  * This class implements the overtaking conditions. Examples are:
  10.  * <ul>
  11.  * <li>Overtaking on the left is allowed for all GTU types (not likely when there are e.g., both cars and bicycles in the
  12.  * model).</li>
  13.  * <li>A GTU type CAR can overtake a GTU type TRACTOR on the left on this road, but no other types of GTUs.</li>
  14.  * <li>When the speed of the GTU you try to overtake is lower than 50 km/h, you can overtake on the left.</li>
  15.  * <li>When the speed of the GTU you try to overtake is 25 km/h less than the maximum speed of the lane on which you and the
  16.  * other GTU are driving, you can overtake on the left or right.</li>
  17.  * <li>only overtake vehicles that have a maximum speed of under 25 km/h.</li>
  18.  * <li>Overtaking on the left is allowed for all GTU types, but overtaking on the right is also allowed when traffic density is
  19.  * below a certain number</li>
  20.  * <li>Overtaking on the left is allowed for all GTU types, but overtaking on the right is also allowed when the distance to a
  21.  * traffic light is less than 200 m</li>
  22.  * <li>Overtaking on the left and the right is allowed for all GTUs. This can e.g. be used on an American highway where all GTUs
  23.  * that are allowed on the highway can indeed overtake on the right or the left.</li>
  24.  * </ul>
  25.  * <b>Note:</b> The class only checks whether it is <b>allowed</b> to overtake another GTU on this road, not whether it is
  26.  * possible or safe to do so. That has to be checked by the GTU itself based on e.g., gap acceptance.
  27.  * <p>
  28.  * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  29.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  30.  * <p>
  31.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  32.  * initial version Sep 13, 2015 <br>
  33.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  34.  */
  35. public interface OvertakingConditions
  36. {
  37.     /**
  38.      * Implementation of the overtaking conditions. E.g., is a car allowed on this road to overtake a tractor? If so, on which
  39.      * side(s)?
  40.      * @param lane the lane for which to evaluate the overtaking conditions
  41.      * @param gtu the GTU that might overtake another GTU
  42.      * @param predecessorGTU the GTU in front of the GTU that might want to overtake
  43.      * @return an overtaking direction: LEFT, RIGHT, BOTH or NONE
  44.      */
  45.     OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu, final LaneBasedGTU predecessorGTU);

  46.     /********************************************************************************************************************/
  47.     /********************** IMPLEMENTATION CLASSES OF MOST COMMON OVERTAKING CONDITIONS *****************************/
  48.     /********************************************************************************************************************/

  49.     /**
  50.      * Overtaking on the left allowed for all GTUs. Note: overtaking on the right not allowed, so vehicles will stall on a
  51.      * multilane road near a traffic light. Also, bicycles will overtake cars on the "wrong" side of the road in this simple
  52.      * condition!
  53.      * <p>
  54.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  55.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  56.      * </p>
  57.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  58.      * initial version Sep 13, 2015
  59.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  60.      */
  61.     public static class LeftOnly implements OvertakingConditions
  62.     {

  63.         /** {@inheritDoc} */
  64.         @Override
  65.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  66.             final LaneBasedGTU predecessorGTU)
  67.         {
  68.             return OvertakingDirection.LEFT;
  69.         }

  70.         /** {@inheritDoc} */
  71.         @Override
  72.         public final String toString()
  73.         {
  74.             return "LeftOnly []";
  75.         }
  76.     }

  77.     /**
  78.      * Overtaking on the right allowed for all GTUs. Note: overtaking on the left not allowed, so vehicles will stall on a
  79.      * multilane road near a traffic light. Also, bicycles will overtake cars on the "wrong" side of the road in this simple
  80.      * condition!
  81.      * <p>
  82.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  83.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  84.      * </p>
  85.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  86.      * initial version Sep 13, 2015
  87.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  88.      */
  89.     public static class RightOnly implements OvertakingConditions
  90.     {
  91.        
  92.         /** {@inheritDoc} */
  93.         @Override
  94.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  95.             final LaneBasedGTU predecessorGTU)
  96.         {
  97.             return OvertakingDirection.RIGHT;
  98.         }

  99.         /** {@inheritDoc} */
  100.         @Override
  101.         public final String toString()
  102.         {
  103.             return "RightOnly []";
  104.         }
  105.     }

  106.     /**
  107.      * No overtaking allowed. Note if there are multiple lanes, vehicles will stall near a traffic light.
  108.      * <p>
  109.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  110.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  111.      * </p>
  112.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  113.      * initial version Sep 13, 2015
  114.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  115.      */
  116.     public static class None implements OvertakingConditions
  117.     {
  118.        
  119.         /** {@inheritDoc} */
  120.         @Override
  121.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  122.             final LaneBasedGTU predecessorGTU)
  123.         {
  124.             return OvertakingDirection.NONE;
  125.         }

  126.         /** {@inheritDoc} */
  127.         @Override
  128.         public final String toString()
  129.         {
  130.             return "None []";
  131.         }
  132.     }

  133.     /**
  134.      * Overtaking on both sides allowed. This is, e.g., the situation for an American highway.
  135.      * <p>
  136.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  137.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  138.      * </p>
  139.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  140.      * initial version Sep 13, 2015
  141.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  142.      */
  143.     public static class LeftAndRight implements OvertakingConditions
  144.     {
  145.        
  146.         /** {@inheritDoc} */
  147.         @Override
  148.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  149.             final LaneBasedGTU predecessorGTU)
  150.         {
  151.             return OvertakingDirection.BOTH;
  152.         }

  153.         /** {@inheritDoc} */
  154.         @Override
  155.         public final String toString()
  156.         {
  157.             return "LeftAndRight []";
  158.         }
  159.     }

  160.     /**
  161.      * Overtaking on the left allowed for all GTUs, and overtaking on the right allowed under a given speed.
  162.      * <p>
  163.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  164.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  165.      * </p>
  166.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  167.      * initial version Sep 13, 2015
  168.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  169.      */
  170.     public static class LeftAlwaysRightSpeed implements OvertakingConditions, Serializable
  171.     {
  172.         /** */
  173.         private static final long serialVersionUID = 20150913L;
  174.        
  175.         /** The speed under which overtaking on the "wrong" side is allowed. */
  176.         private final Speed rightOvertakingSpeedMax;

  177.         /**
  178.          * @param rightOvertakingSpeedMax the speed under which overtaking on the "wrong" side is allowed
  179.          */
  180.         public LeftAlwaysRightSpeed(final Speed rightOvertakingSpeedMax)
  181.         {
  182.             this.rightOvertakingSpeedMax = rightOvertakingSpeedMax;
  183.         }

  184.         /** {@inheritDoc} */
  185.         @Override
  186.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  187.             final LaneBasedGTU predecessorGTU)
  188.         {
  189.             return gtu.getSpeed().lt(this.rightOvertakingSpeedMax) ? OvertakingDirection.BOTH
  190.                 : OvertakingDirection.LEFT;
  191.         }

  192.         /** {@inheritDoc} */
  193.         @Override
  194.         public final String toString()
  195.         {
  196.             return "LeftAlwaysRightSpeed [rightOvertakingSpeedMax=" + this.rightOvertakingSpeedMax + "]";
  197.         }
  198.     }

  199.     /**
  200.      * Overtaking on the right allowed for all GTUs, and overtaking on the left allowed under a given speed.
  201.      * <p>
  202.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  203.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  204.      * </p>
  205.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  206.      * initial version Sep 13, 2015
  207.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  208.      */
  209.     public static class RightAlwaysLeftSpeed implements OvertakingConditions, Serializable
  210.     {
  211.         /** */
  212.         private static final long serialVersionUID = 20150913L;
  213.        
  214.         /** The speed under which overtaking on the "wrong" side is allowed. */
  215.         private final Speed leftOvertakingSpeedMax;

  216.         /**
  217.          * @param leftOvertakingSpeedMax the speed under which overtaking on the "wrong" side is allowed
  218.          */
  219.         public RightAlwaysLeftSpeed(final Speed leftOvertakingSpeedMax)
  220.         {
  221.             this.leftOvertakingSpeedMax = leftOvertakingSpeedMax;
  222.         }

  223.         /** {@inheritDoc} */
  224.         @Override
  225.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  226.             final LaneBasedGTU predecessorGTU)
  227.         {
  228.             return gtu.getSpeed().lt(this.leftOvertakingSpeedMax) ? OvertakingDirection.BOTH
  229.                 : OvertakingDirection.RIGHT;
  230.         }

  231.         /** {@inheritDoc} */
  232.         @Override
  233.         public final String toString()
  234.         {
  235.             return "RightAlwaysLeftSpeed [leftOvertakingSpeedMax=" + this.leftOvertakingSpeedMax + "]";
  236.         }
  237.     }

  238.     /**
  239.      * Provide a collection of GTUs that can overtake another collection of GTUs on the left side, but not vice versa. Example:
  240.      * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE, SCOOTER}.
  241.      * In the latter case, cars, trucks and busses can overtake all other GTUs, but bicycles and scooters cannot overtake cars,
  242.      * trucks or busses. Another example is a lane where cars and motors can overtake all other road users, but trucks are not
  243.      * allowed to overtake. In that case, we would allow {CAR, MOTOR} to overtake {ALL} or {CAR, MOTOR} to overtake {CAR, MOTOR,
  244.      * TRUCK} in that lane.
  245.      * <p>
  246.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  247.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  248.      * </p>
  249.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  250.      * initial version Sep 13, 2015
  251.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  252.      */
  253.     public static class LeftSet implements OvertakingConditions, Serializable
  254.     {
  255.         /** */
  256.         private static final long serialVersionUID = 20150913L;
  257.        
  258.         /** A collection of GTUs that can overtake another collection of GTUs. */
  259.         private final Collection<GTUType> overtakingGTUs;

  260.         /** A collection of GTUs that can be overtaken by another collection of GTUs. */
  261.         private final Collection<GTUType> overtakenGTUs;

  262.         /**
  263.          * Provide a collection of GTUs that can overtake another collection of GTUs on the left, but not vice versa. Example:
  264.          * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE,
  265.          * SCOOTER}, or {CAR, TRUCK, BUS} can overtake {TRACTOR}.
  266.          * @param overtakingGTUs the GTUs that can overtake a set of other GTUs, e.g., CAR, TRUCK. If overtakingGTUs contains
  267.          *            GTUType.ALL, all GTUs can overtake.
  268.          * @param overtakenGTUs the GTUs that can be overtaken, e.g., BICYCLE, SCOOTER. If overtakenGTUs contains GTUType.ALL,
  269.          *            all GTUs can be overtaken.
  270.          */
  271.         public LeftSet(final Collection<GTUType> overtakingGTUs, final Collection<GTUType> overtakenGTUs)
  272.         {
  273.             this.overtakingGTUs = overtakingGTUs;
  274.             this.overtakenGTUs = overtakenGTUs;
  275.         }

  276.         /** {@inheritDoc} */
  277.         @Override
  278.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  279.             final LaneBasedGTU predecessorGTU)
  280.         {
  281.             if ((this.overtakingGTUs.contains(GTUType.ALL) || this.overtakingGTUs.contains(gtu.getGTUType())
  282.                 && (this.overtakenGTUs.contains(GTUType.ALL) || this.overtakenGTUs
  283.                     .contains(predecessorGTU.getGTUType()))))
  284.             {
  285.                 return OvertakingDirection.LEFT;
  286.             }
  287.             return OvertakingDirection.NONE;
  288.         }

  289.         /** {@inheritDoc} */
  290.         @Override
  291.         public final String toString()
  292.         {
  293.             return "LeftSet [overtakingGTUs=" + this.overtakingGTUs + ", overtakenGTUs=" + this.overtakenGTUs + "]";
  294.         }
  295.     }

  296.     /**
  297.      * Provide a collection of GTUs that can overtake another collection of GTUs on the right side, but not vice versa. Example:
  298.      * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE, SCOOTER}.
  299.      * In the latter case, cars, trucks and busses can overtake all other GTUs, but bicycles and scooters cannot overtake cars,
  300.      * trucks or busses. Another example is a lane where cars and motors can overtake all other road users, but trucks are not
  301.      * allowed to overtake. In that case, we would allow {CAR, MOTOR} to overtake {ALL} or {CAR, MOTOR} to overtake {CAR, MOTOR,
  302.      * TRUCK} in that lane.
  303.      * <p>
  304.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  305.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  306.      * </p>
  307.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  308.      * initial version Sep 13, 2015
  309.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  310.      */
  311.     public static class RightSet implements OvertakingConditions, Serializable
  312.     {
  313.         /** */
  314.         private static final long serialVersionUID = 20150913L;
  315.        
  316.         /** A collection of GTUs that can overtake another collection of GTUs. */
  317.         private final Collection<GTUType> overtakingGTUs;

  318.         /** A collection of GTUs that can be overtaken by another collection of GTUs. */
  319.         private final Collection<GTUType> overtakenGTUs;

  320.         /**
  321.          * Provide a collection of GTUs that can overtake another collection of GTUs on the right, but not vice versa. Example:
  322.          * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE,
  323.          * SCOOTER}, or {CAR, TRUCK, BUS} can overtake {TRACTOR}.
  324.          * @param overtakingGTUs the GTUs that can overtake a set of other GTUs, e.g., CAR, TRUCK. If overtakingGTUs contains
  325.          *            GTUType.ALL, all GTUs can overtake.
  326.          * @param overtakenGTUs the GTUs that can be overtaken, e.g., BICYCLE, SCOOTER. If overtakenGTUs contains GTUType.ALL,
  327.          *            all GTUs can be overtaken.
  328.          */
  329.         public RightSet(final Collection<GTUType> overtakingGTUs, final Collection<GTUType> overtakenGTUs)
  330.         {
  331.             this.overtakingGTUs = overtakingGTUs;
  332.             this.overtakenGTUs = overtakenGTUs;
  333.         }

  334.         /** {@inheritDoc} */
  335.         @Override
  336.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  337.             final LaneBasedGTU predecessorGTU)
  338.         {
  339.             if ((this.overtakingGTUs.contains(GTUType.ALL) || this.overtakingGTUs.contains(gtu.getGTUType())
  340.                 && (this.overtakenGTUs.contains(GTUType.ALL) || this.overtakenGTUs
  341.                     .contains(predecessorGTU.getGTUType()))))
  342.             {
  343.                 return OvertakingDirection.RIGHT;
  344.             }
  345.             return OvertakingDirection.NONE;
  346.         }

  347.         /** {@inheritDoc} */
  348.         @Override
  349.         public final String toString()
  350.         {
  351.             return "RightSet [overtakingGTUs=" + this.overtakingGTUs + ", overtakenGTUs=" + this.overtakenGTUs + "]";
  352.         }
  353.     }

  354.     /**
  355.      * Provide a collection of GTUs that can overtake another collection of GTUs on the left side, but not vice versa. Example:
  356.      * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE, SCOOTER}.
  357.      * In the latter case, cars, trucks and busses can overtake all other GTUs, but bicycles and scooters cannot overtake cars,
  358.      * trucks or busses. Another example is a lane where cars and motors can overtake all other road users, but trucks are not
  359.      * allowed to overtake. In that case, we would allow {CAR, MOTOR} to overtake {ALL} or {CAR, MOTOR} to overtake {CAR, MOTOR,
  360.      * TRUCK} in that lane. In addition, overtaking on the other side is allowed under a given driving speed.
  361.      * <p>
  362.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  363.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  364.      * </p>
  365.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  366.      * initial version Sep 13, 2015
  367.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  368.      */
  369.     public static class LeftSetRightSpeed implements OvertakingConditions, Serializable
  370.     {
  371.         /** */
  372.         private static final long serialVersionUID = 20150913L;
  373.        
  374.         /** A collection of GTUs that can overtake another collection of GTUs. */
  375.         private final Collection<GTUType> overtakingGTUs;

  376.         /** A collection of GTUs that can be overtaken by another collection of GTUs. */
  377.         private final Collection<GTUType> overtakenGTUs;

  378.         /** The speed under which overtaking on the "wrong" side is allowed. */
  379.         private final Speed rightOvertakingSpeedMax;

  380.         /**
  381.          * Provide a collection of GTUs that can overtake another collection of GTUs on the left, but not vice versa. Example:
  382.          * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE,
  383.          * SCOOTER}, or {CAR, TRUCK, BUS} can overtake {TRACTOR}. In addition, overtaking on the other side is allowed under a
  384.          * given driving speed.
  385.          * @param overtakingGTUs the GTUs that can overtake a set of other GTUs, e.g., CAR, TRUCK. If overtakingGTUs contains
  386.          *            GTUType.ALL, all GTUs can overtake.
  387.          * @param overtakenGTUs the GTUs that can be overtaken, e.g., BICYCLE, SCOOTER. If overtakenGTUs contains GTUType.ALL,
  388.          *            all GTUs can be overtaken.
  389.          * @param rightOvertakingSpeedMax the speed under which overtaking on the "wrong" side is allowed
  390.          */
  391.         public LeftSetRightSpeed(final Collection<GTUType> overtakingGTUs, final Collection<GTUType> overtakenGTUs,
  392.             final Speed rightOvertakingSpeedMax)
  393.         {
  394.             this.overtakingGTUs = overtakingGTUs;
  395.             this.overtakenGTUs = overtakenGTUs;
  396.             this.rightOvertakingSpeedMax = rightOvertakingSpeedMax;
  397.         }

  398.         /** {@inheritDoc} */
  399.         @Override
  400.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  401.             final LaneBasedGTU predecessorGTU)
  402.         {
  403.             boolean left =
  404.                 ((this.overtakingGTUs.contains(GTUType.ALL) || this.overtakingGTUs.contains(gtu.getGTUType())
  405.                     && (this.overtakenGTUs.contains(GTUType.ALL) || this.overtakenGTUs.contains(predecessorGTU
  406.                         .getGTUType()))));
  407.             boolean right = gtu.getSpeed().lt(this.rightOvertakingSpeedMax);
  408.             if (left)
  409.             {
  410.                 return right ? OvertakingDirection.BOTH : OvertakingDirection.LEFT;
  411.             }
  412.             return right ? OvertakingDirection.RIGHT : OvertakingDirection.NONE;
  413.         }

  414.         /** {@inheritDoc} */
  415.         @Override
  416.         public final String toString()
  417.         {
  418.             return "LeftSetRightSpeed [overtakingGTUs=" + this.overtakingGTUs + ", overtakenGTUs=" + this.overtakenGTUs
  419.                     + ", rightOvertakingSpeedMax=" + this.rightOvertakingSpeedMax + "]";
  420.         }
  421.     }

  422.     /**
  423.      * Provide a collection of GTUs that can overtake another collection of GTUs on the right side, but not vice versa. Example:
  424.      * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE, SCOOTER}.
  425.      * In the latter case, cars, trucks and busses can overtake all other GTUs, but bicycles and scooters cannot overtake cars,
  426.      * trucks or busses. Another example is a lane where cars and motors can overtake all other road users, but trucks are not
  427.      * allowed to overtake. In that case, we would allow {CAR, MOTOR} to overtake {ALL} or {CAR, MOTOR} to overtake {CAR, MOTOR,
  428.      * TRUCK} in that lane. In addition, overtaking on the other side is allowed under a given driving speed.
  429.      * <p>
  430.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  431.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  432.      * </p>
  433.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  434.      * initial version Sep 13, 2015
  435.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  436.      */
  437.     public static class RightSetLeftSpeed implements OvertakingConditions, Serializable
  438.     {
  439.         /** */
  440.         private static final long serialVersionUID = 20150913L;
  441.        
  442.         /** A collection of GTUs that can overtake another collection of GTUs. */
  443.         private final Collection<GTUType> overtakingGTUs;

  444.         /** A collection of GTUs that can be overtaken by another collection of GTUs. */
  445.         private final Collection<GTUType> overtakenGTUs;

  446.         /** The speed under which overtaking on the "wrong" side is allowed. */
  447.         private final Speed leftOvertakingSpeedMax;

  448.         /**
  449.          * Provide a collection of GTUs that can overtake another collection of GTUs on the left, but not vice versa. Example:
  450.          * {CAR, TRUCK, BUS} can overtake {BICYCLE, SCOOTER}, or {CAR, TRUCK, BUS} can overtake {CAR, TRUCK, BUS, BICYCLE,
  451.          * SCOOTER}, or {CAR, TRUCK, BUS} can overtake {TRACTOR}. In addition, overtaking on the other side is allowed under a
  452.          * given driving speed.
  453.          * @param overtakingGTUs the GTUs that can overtake a set of other GTUs, e.g., CAR, TRUCK. If overtakingGTUs contains
  454.          *            GTUType.ALL, all GTUs can overtake.
  455.          * @param overtakenGTUs the GTUs that can be overtaken, e.g., BICYCLE, SCOOTER. If overtakenGTUs contains GTUType.ALL,
  456.          *            all GTUs can be overtaken.
  457.          * @param leftOvertakingSpeedMax the speed under which overtaking on the "wrong" side is allowed
  458.          */
  459.         public RightSetLeftSpeed(final Collection<GTUType> overtakingGTUs, final Collection<GTUType> overtakenGTUs,
  460.             final Speed leftOvertakingSpeedMax)
  461.         {
  462.             this.overtakingGTUs = overtakingGTUs;
  463.             this.overtakenGTUs = overtakenGTUs;
  464.             this.leftOvertakingSpeedMax = leftOvertakingSpeedMax;
  465.         }

  466.         /** {@inheritDoc} */
  467.         @Override
  468.         public final OvertakingDirection checkOvertaking(final Lane lane, final LaneBasedGTU gtu,
  469.             final LaneBasedGTU predecessorGTU)
  470.         {
  471.             boolean right =
  472.                 ((this.overtakingGTUs.contains(GTUType.ALL) || this.overtakingGTUs.contains(gtu.getGTUType())
  473.                     && (this.overtakenGTUs.contains(GTUType.ALL) || this.overtakenGTUs.contains(predecessorGTU
  474.                         .getGTUType()))));
  475.             boolean left = gtu.getSpeed().lt(this.leftOvertakingSpeedMax);
  476.             if (right)
  477.             {
  478.                 return left ? OvertakingDirection.BOTH : OvertakingDirection.RIGHT;
  479.             }
  480.             return left ? OvertakingDirection.LEFT : OvertakingDirection.NONE;
  481.         }

  482.         /** {@inheritDoc} */
  483.         @Override
  484.         public final String toString()
  485.         {
  486.             return "RightSetLeftSpeed [overtakingGTUs=" + this.overtakingGTUs + ", overtakenGTUs=" + this.overtakenGTUs
  487.                     + ", leftOvertakingSpeedMax=" + this.leftOvertakingSpeedMax + "]";
  488.         }
  489.     }

  490. }