LaneStructure.java

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

  2. import java.util.Map;
  3. import java.util.SortedSet;

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.opentrafficsim.core.gtu.GTUException;
  6. import org.opentrafficsim.core.gtu.GTUType;
  7. import org.opentrafficsim.core.gtu.RelativePosition;
  8. import org.opentrafficsim.core.network.route.Route;
  9. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  10. import org.opentrafficsim.road.network.lane.DirectedLanePosition;
  11. import org.opentrafficsim.road.network.lane.object.LaneBasedObject;

  12. /**
  13.  * Interface for lane structures.
  14.  * <p>
  15.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  16.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  17.  * <p>
  18.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 13 aug. 2018 <br>
  19.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  20.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  21.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  22.  */
  23. public interface LaneStructure
  24. {

  25.     /**
  26.      * Updates the underlying structure shifting the root position to the input.
  27.      * @param pos DirectedLanePosition; current position of the GTU
  28.      * @param route Route; current route of the GTU
  29.      * @param gtuType GTUType; GTU type
  30.      * @throws GTUException on a problem while updating the structure
  31.      */
  32.     void update(DirectedLanePosition pos, Route route, GTUType gtuType) throws GTUException;

  33.     /**
  34.      * Returns the root record.
  35.      * @return LaneRecord; root record
  36.      */
  37.     LaneStructureRecord getRootRecord();

  38.     /**
  39.      * Returns the extended cross-section, which includes all lanes for which a first record is present.
  40.      * @return SortedSet; the cross-section
  41.      */
  42.     SortedSet<RelativeLane> getExtendedCrossSection();

  43.     /**
  44.      * Returns the first record on the given lane. This is often a record in the current cross section, but it may be one
  45.      * downstream for a lane that starts further downstream.
  46.      * @param lane RelativeLane; lane
  47.      * @return first record on the given lane, or {@code null} if no such record
  48.      */
  49.     LaneStructureRecord getFirstRecord(RelativeLane lane);

  50.     /**
  51.      * Retrieve objects of a specific type. Returns objects over a maximum length of the look ahead distance downstream from the
  52.      * relative position, or as far as the lane structure goes.
  53.      * @param clazz Class&lt;T&gt;; class of objects to find
  54.      * @param gtu LaneBasedGTU; gtu
  55.      * @param pos RelativePosition.TYPE; relative position to start search from
  56.      * @param <T> type of objects to find
  57.      * @return Map; sorted set of objects of requested type per lane
  58.      * @throws GTUException if lane is not in current set
  59.      */
  60.     <T extends LaneBasedObject> Map<RelativeLane, SortedSet<Entry<T>>> getDownstreamObjects(Class<T> clazz, LaneBasedGTU gtu,
  61.             RelativePosition.TYPE pos) throws GTUException;

  62.     /**
  63.      * Retrieve objects on a lane of a specific type. Returns objects over a maximum length of the look ahead distance
  64.      * downstream from the relative position, or as far as the lane structure goes.
  65.      * @param lane RelativeLane; lane
  66.      * @param clazz Class&lt;T&gt;; class of objects to find
  67.      * @param gtu LaneBasedGTU; gtu
  68.      * @param pos RelativePosition.TYPE; relative position to start search from
  69.      * @param <T> type of objects to find
  70.      * @return SortedSet; sorted set of objects of requested type
  71.      * @throws GTUException if lane is not in current set
  72.      */
  73.     <T extends LaneBasedObject> SortedSet<Entry<T>> getDownstreamObjects(RelativeLane lane, Class<T> clazz, LaneBasedGTU gtu,
  74.             RelativePosition.TYPE pos) throws GTUException;

  75.     /**
  76.      * Retrieve objects of a specific type. Returns objects over a maximum length of the look ahead distance downstream from the
  77.      * relative position, or as far as the lane structure goes. Objects on links not on the route are ignored.
  78.      * @param clazz Class&lt;T&gt;; class of objects to find
  79.      * @param gtu LaneBasedGTU; gtu
  80.      * @param pos RelativePosition.TYPE; relative position to start search from
  81.      * @param <T> type of objects to find
  82.      * @param route Route; the route
  83.      * @return SortedSet; sorted set of objects of requested type per lane
  84.      * @throws GTUException if lane is not in current set
  85.      */
  86.     <T extends LaneBasedObject> Map<RelativeLane, SortedSet<Entry<T>>> getDownstreamObjectsOnRoute(Class<T> clazz,
  87.             LaneBasedGTU gtu, RelativePosition.TYPE pos, Route route) throws GTUException;

  88.     /**
  89.      * Retrieve objects on a lane of a specific type. Returns objects over a maximum length of the look ahead distance
  90.      * downstream from the relative position, or as far as the lane structure goes. Objects on links not on the route are
  91.      * ignored.
  92.      * @param lane RelativeLane; lane
  93.      * @param clazz Class&lt;T&gt;; class of objects to find
  94.      * @param gtu LaneBasedGTU; gtu
  95.      * @param pos RelativePosition.TYPE; relative position to start search from
  96.      * @param <T> type of objects to find
  97.      * @param route Route; the route
  98.      * @return SortedSet; sorted set of objects of requested type
  99.      * @throws GTUException if lane is not in current set
  100.      */
  101.     <T extends LaneBasedObject> SortedSet<Entry<T>> getDownstreamObjectsOnRoute(RelativeLane lane, Class<T> clazz,
  102.             LaneBasedGTU gtu, RelativePosition.TYPE pos, Route route) throws GTUException;

  103.     /**
  104.      * Retrieve objects on a lane of a specific type. Returns upstream objects from the relative position for as far as the lane
  105.      * structure goes. Distances to upstream objects are given as positive values.
  106.      * @param lane RelativeLane; lane
  107.      * @param clazz Class&lt;T&gt;; class of objects to find
  108.      * @param gtu LaneBasedGTU; gtu
  109.      * @param pos RelativePosition.TYPE; relative position to start search from
  110.      * @param <T> type of objects to find
  111.      * @return SortedSet; sorted set of objects of requested type
  112.      * @throws GTUException if lane is not in current set
  113.      */
  114.     <T extends LaneBasedObject> SortedSet<Entry<T>> getUpstreamObjects(RelativeLane lane, Class<T> clazz, LaneBasedGTU gtu,
  115.             RelativePosition.TYPE pos) throws GTUException;

  116.     /**
  117.      * Wrapper to hold lane-based object and it's distance.
  118.      * <p>
  119.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  120.      * <br>
  121.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  122.      * <p>
  123.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 15, 2016 <br>
  124.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  125.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  126.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  127.      * @param <T> class of lane based object contained
  128.      */
  129.     class Entry<T extends LaneBasedObject> implements Comparable<Entry<T>>
  130.     {

  131.         /** Distance to lane based object. */
  132.         private final Length distance;

  133.         /** Lane based object. */
  134.         private final T laneBasedObject;

  135.         /**
  136.          * @param distance Length; distance to lane based object
  137.          * @param laneBasedObject T; lane based object
  138.          */
  139.         public Entry(final Length distance, final T laneBasedObject)
  140.         {
  141.             this.distance = distance;
  142.             this.laneBasedObject = laneBasedObject;
  143.         }

  144.         /**
  145.          * @return distance.
  146.          */
  147.         public final Length getDistance()
  148.         {
  149.             return this.distance;
  150.         }

  151.         /**
  152.          * @return laneBasedObject.
  153.          */
  154.         public final T getLaneBasedObject()
  155.         {
  156.             return this.laneBasedObject;
  157.         }

  158.         /** {@inheritDoc} */
  159.         @Override
  160.         public final int hashCode()
  161.         {
  162.             final int prime = 31;
  163.             int result = 1;
  164.             result = prime * result + ((this.distance == null) ? 0 : this.distance.hashCode());
  165.             result = prime * result + ((this.laneBasedObject == null) ? 0 : this.laneBasedObject.hashCode());
  166.             return result;
  167.         }

  168.         /** {@inheritDoc} */
  169.         @Override
  170.         public final boolean equals(final Object obj)
  171.         {
  172.             if (this == obj)
  173.             {
  174.                 return true;
  175.             }
  176.             if (obj == null)
  177.             {
  178.                 return false;
  179.             }
  180.             if (getClass() != obj.getClass())
  181.             {
  182.                 return false;
  183.             }
  184.             Entry<?> other = (Entry<?>) obj;
  185.             if (this.distance == null)
  186.             {
  187.                 if (other.distance != null)
  188.                 {
  189.                     return false;
  190.                 }
  191.             }
  192.             else if (!this.distance.equals(other.distance))
  193.             {
  194.                 return false;
  195.             }
  196.             if (this.laneBasedObject == null)
  197.             {
  198.                 if (other.laneBasedObject != null)
  199.                 {
  200.                     return false;
  201.                 }
  202.             }
  203.             // laneBasedObject does not implement equals...
  204.             else if (!this.laneBasedObject.equals(other.laneBasedObject))
  205.             {
  206.                 return false;
  207.             }
  208.             return true;
  209.         }

  210.         /** {@inheritDoc} */
  211.         @Override
  212.         public final int compareTo(final Entry<T> arg)
  213.         {
  214.             int d = this.distance.compareTo(arg.distance);
  215.             if (d != 0 || this.laneBasedObject.equals(arg.laneBasedObject))
  216.             {
  217.                 return d; // different distance (-1 or 1), or same distance but also equal lane based object (0)
  218.             }
  219.             return 1; // same distance, unequal lane based object (1)
  220.         }

  221.         /** {@inheritDoc} */
  222.         @Override
  223.         public final String toString()
  224.         {
  225.             return "LaneStructure.Entry [distance=" + this.distance + ", laneBasedObject=" + this.laneBasedObject + "]";
  226.         }

  227.     }

  228. }