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. import org.opentrafficsim.road.gtu.lane.perception.structure.LaneRecordInterface;

  12. /**
  13.  * Iterable to find downstream GTU's.<br>
  14.  * <br>
  15.  * The behavior of this search is slightly altered using {@code boolean ignoreIfUpstream}. This is to deal with the following
  16.  * situations in case a GTU with it's rear upstream of the considered lane is found:<br>
  17.  * <br>
  18.  * Following downstream GTUs ({@code ignoreIfUpstream = true})
  19.  * <ol>
  20.  * <li>From the same direction (or not a merge): the GTU can be ignored as it is also found on the upstream lane.</li>
  21.  * <li>From the other direction of a merge: the GTU can be ignored as it is followed through considering the merge conflict.
  22.  * 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
  23.  * can move up to the conflict without hitting the GTU from the other direction. Considering the GTU through the conflict deals
  24.  * with this, and the GTU can be ignored for regular following of downstream GTUs.</li>
  25.  * </ol>
  26.  * <br>
  27.  * GTUs downstream of a conflict ({@code ignoreIfUpstream = false})
  28.  * <ol>
  29.  * <li>From the same direction: the GTU is considered both through the conflict and as a regular downstream GTU.</li>
  30.  * <li>From the other direction of a merge: the GTU needs to be considered.</li>
  31.  * </ol>
  32.  * <p>
  33.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  34.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  35.  * </p>
  36.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  37.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  38.  * @author <a href="https://github.com/wjschakel">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 LaneRecordInterface<?> root,
  69.             final Length initialPosition, final Length maxDistance, final RelativePosition relativePosition,
  70.             final HeadwayGtuType headwayGtuType, 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 LaneRecordInterface<?> record, final Length position, final Integer counter)
  84.             throws GtuException
  85.     {
  86.         int n;
  87.         LaneBasedGtu next;
  88.         Length pos;
  89.         if (counter == null)
  90.         {
  91.             Length searchPos = position.plus(this.margin);
  92.             next = record.getLane().getGtuAhead(searchPos, RelativePosition.FRONT,
  93.                     record.getLane().getLink().getSimulator().getSimulatorAbsTime());
  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 = pos.plus(next.getLength());
  104.                 return getNext(record, pos, n);
  105.             }
  106.             if (this.ignoreIfUpstream)
  107.             {
  108.                 if (pos.si < 0.0)
  109.                 {
  110.                     pos = pos.plus(next.getLength());
  111.                     return getNext(record, pos, n);
  112.                 }
  113.             }
  114.         }
  115.         else
  116.         {
  117.             n = 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 = pos.plus(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 LaneRecordInterface<?> 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. }