LaneBasedObjectIterable.java

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

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

  5. import org.djunits.value.vdouble.scalar.Length;
  6. import org.opentrafficsim.core.gtu.RelativePosition;
  7. import org.opentrafficsim.core.network.route.Route;
  8. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  9. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
  10. import org.opentrafficsim.road.network.lane.object.LaneBasedObject;

  11. /**
  12.  * Iterable that searches downstream for a certain type of lane based object.
  13.  * <p>
  14.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  16.  * <p>
  17.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 16 feb. 2018 <br>
  18.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  19.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  20.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  21.  * @param <H> headway type
  22.  * @param <L> lane based object type
  23.  */
  24. public abstract class LaneBasedObjectIterable<H extends Headway, L extends LaneBasedObject>
  25.         extends AbstractPerceptionIterable<H, L, Void>
  26. {

  27.     /** Class of lane based objects to return. */
  28.     private final Class<L> clazz;

  29.     /**
  30.      * Constructor.
  31.      * @param perceivingGtu LaneBasedGTU; perceiving GTU
  32.      * @param clazz Class&lt;H&gt;; class of lane based objects to return
  33.      * @param root LaneRecord; root record
  34.      * @param initialPosition Length; initial position
  35.      * @param maxDistance Length; max distance to search
  36.      * @param relativePosition RelativePosition; relative position
  37.      * @param route Route; route of the GTU, may be {@code null}
  38.      */
  39.     public LaneBasedObjectIterable(final LaneBasedGTU perceivingGtu, final Class<L> clazz, final LaneRecord<?> root,
  40.             final Length initialPosition, final Length maxDistance, final RelativePosition relativePosition, final Route route)
  41.     {
  42.         super(perceivingGtu, root, initialPosition, true, maxDistance, relativePosition, route);
  43.         this.clazz = clazz;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @SuppressWarnings("unchecked")
  47.     @Override
  48.     protected Entry getNext(final LaneRecord<?> record, final Length position, final Void counter)
  49.     {
  50.         if (!record.isDownstreamBranch())
  51.         {
  52.             return null;
  53.         }
  54.         List<LaneBasedObject> list = record.getLane().getObjectAhead(position, record.getDirection());
  55.         while (list != null)
  56.         {
  57.             Set<L> set = new LinkedHashSet<>();
  58.             Length pos = list.get(0).getLongitudinalPosition();
  59.             for (LaneBasedObject object : list)
  60.             {
  61.                 if (this.clazz.isAssignableFrom(object.getClass()))
  62.                 {
  63.                     // is assignable, so safe cast
  64.                     set.add((L) object);
  65.                 }
  66.             }
  67.             if (!set.isEmpty())
  68.             {
  69.                 if (set.size() == 1)
  70.                 {
  71.                     return new Entry(set.iterator().next(), null, pos);
  72.                 }
  73.                 return new Entry(set, null, pos);
  74.             }
  75.             list = record.getLane().getObjectAhead(pos, record.getDirection());
  76.         }
  77.         return null;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     protected final Length getDistance(final L object, final LaneRecord<?> record, final Length position)
  82.     {
  83.         return record.getDistanceToPosition(position).minus(getDx());
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     public String toString()
  88.     {
  89.         return "LaneBasedObjectIterable [class=" + this.clazz + "]";
  90.     }

  91. }