DownstreamNeighborsIterable.java

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

  2. import java.util.LinkedHashSet;
  3. import java.util.Set;

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.opentrafficsim.base.parameters.ParameterException;
  6. import org.opentrafficsim.core.gtu.GTUException;
  7. import org.opentrafficsim.core.gtu.RelativePosition;
  8. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  9. import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.HeadwayGtuType;
  10. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;

  11. /**
  12.  * Iterable to find downstream GTU's.<br>
  13.  * <br>
  14.  * The behavior of this search is slightly altered using {@code boolean ignoreIfUpstream}. This is to deal with the following
  15.  * situations in case a GTU with it's rear upstream of the considered lane is found:<br>
  16.  * <br>
  17.  * Following downstream GTUs ({@code ignoreIfUpstream = true})
  18.  * <ol>
  19.  * <li>From the same direction (or not a merge): the GTU can be ignored as it is also found on the upstream lane.</li>
  20.  * <li>From the other direction of a merge: the GTU can be ignored as it is followed through considering the merge conflict.
  21.  * Note that we cannot follow the GTU in a regular fashion. If the rear of the GTU is upstream of the conflict, the subject GTU
  22.  * can move up to the conflict without hitting the GTU from the other direction. Considering the GTU through the conflict deals
  23.  * with this, and the GTU can be ignored for regular following of downstream GTUs.</li>
  24.  * </ol>
  25.  * <br>
  26.  * GTUs downstream of a conflict ({@code ignoreIfUpstream = false})
  27.  * <ol>
  28.  * <li>From the same direction: the GTU is considered both through the conflict and as a regular downstream GTU.</li>
  29.  * <li>From the other direction of a merge: the GTU needs to be considered.</li>
  30.  * </ol>
  31.  * <p>
  32.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  33.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  34.  * <p>
  35.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 feb. 2018 <br>
  36.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  37.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  38.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  39.  */
  40. public class DownstreamNeighborsIterable extends AbstractPerceptionIterable<HeadwayGTU, LaneBasedGTU, Integer>
  41. {

  42.     /** Margin in case of a left lane. */
  43.     private static final Length LEFT = Length.instantiateSI(-0.000001);

  44.     /** Margin in case of a right lane. */
  45.     private static final Length RIGHT = Length.instantiateSI(0.000001);

  46.     /** Headway GTU type that should be used. */
  47.     private final HeadwayGtuType headwayGtuType;

  48.     /** Added GTU's so far. */
  49.     private final Set<String> ids = new LinkedHashSet<>();

  50.     /**
  51.      * Margin used for neighbor search in some cases to prevent possible deadlock. This does not affect calculated distances to
  52.      * neighbors, but only whether they are considered a leader or follower.
  53.      */
  54.     private final Length margin;

  55.     /** Ignore downstream GTU's if their rear is upstream of the lane start. Note that equal GTU id's are always ignored. */
  56.     private final boolean ignoreIfUpstream;

  57.     /**
  58.      * Constructor.
  59.      * @param perceivingGtu LaneBasedGTU; perceiving GTU
  60.      * @param root LaneRecord&lt;?&gt;; root record
  61.      * @param initialPosition Length; position on the root record
  62.      * @param maxDistance Length; maximum distance to search
  63.      * @param relativePosition RelativePosition; position to which distance are calculated by subclasses
  64.      * @param headwayGtuType HeadwayGtuType; type of HeadwayGTU to return
  65.      * @param lane RelativeLane; relative lane (used for a left/right distinction to prevent dead-locks)
  66.      * @param ignoreIfUpstream boolean; whether to ignore GTU that are partially upstream of a record
  67.      */
  68.     public DownstreamNeighborsIterable(final LaneBasedGTU perceivingGtu, final LaneRecord<?> root, final Length initialPosition,
  69.             final Length maxDistance, final RelativePosition relativePosition, final HeadwayGtuType headwayGtuType,
  70.             final RelativeLane lane, final boolean ignoreIfUpstream)
  71.     {
  72.         super(perceivingGtu, root, initialPosition, true, maxDistance, relativePosition, null);
  73.         this.headwayGtuType = headwayGtuType;
  74.         this.margin = lane.getLateralDirectionality().isLeft() ? LEFT : RIGHT;
  75.         if (perceivingGtu != null)
  76.         {
  77.             this.ids.add(perceivingGtu.getId());
  78.         }
  79.         this.ignoreIfUpstream = ignoreIfUpstream;
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     protected Entry getNext(final LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
  84.     {
  85.         int n;
  86.         LaneBasedGTU next;
  87.         Length pos;
  88.         boolean plus = record.getDirection().isPlus();
  89.         if (counter == null)
  90.         {
  91.             Length searchPos = (plus ? position.plus(this.margin) : position.minus(this.margin));
  92.             next = record.getLane().getGtuAhead(searchPos, record.getDirection(), RelativePosition.FRONT,
  93.                     record.getLane().getParentLink().getSimulator().getSimulatorTime());
  94.             if (next == null)
  95.             {
  96.                 return null;
  97.             }
  98.             n = record.getLane().indexOfGtu(next);
  99.             pos = next.position(record.getLane(), next.getRear());

  100.             if (this.ids.contains(next.getId()))
  101.             {
  102.                 // rear still on previous lane; it is found there, get next gtu
  103.                 pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
  104.                 return getNext(record, pos, n);
  105.             }
  106.             if (this.ignoreIfUpstream)
  107.             {
  108.                 if (plus ? pos.si < 0.0 : pos.si > record.getLane().getLength().si)
  109.                 {
  110.                     pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
  111.                     return getNext(record, pos, n);
  112.                 }
  113.             }
  114.         }
  115.         else
  116.         {
  117.             n = plus ? counter + 1 : counter - 1;
  118.             if (n < 0 || n >= record.getLane().numberOfGtus())
  119.             {
  120.                 return null;
  121.             }
  122.             next = record.getLane().getGtu(n);
  123.             pos = next.position(record.getLane(), next.getRear());
  124.             if (this.ids.contains(next.getId()))
  125.             {
  126.                 // skip self
  127.                 pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
  128.                 return getNext(record, pos, n);
  129.             }
  130.         }
  131.         return new Entry(next, n, pos);
  132.     }

  133.     /** {@inheritDoc} */
  134.     @Override
  135.     protected Length getDistance(final LaneBasedGTU object, final LaneRecord<?> record, final Length position)
  136.     {
  137.         return record.getDistanceToPosition(position).minus(getDx());
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public HeadwayGTU perceive(final LaneBasedGTU perceivingGtu, final LaneBasedGTU object, final Length distance)
  142.             throws GTUException, ParameterException
  143.     {
  144.         return this.headwayGtuType.createDownstreamGtu(perceivingGtu, object, distance);
  145.     }

  146. }