LaneRecord.java

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

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

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.opentrafficsim.core.network.Link;
  6. import org.opentrafficsim.core.network.route.Route;
  7. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  8. import org.opentrafficsim.road.network.lane.Lane;

  9. /**
  10.  * Record of a lane within the lane structure.
  11.  * <p>
  12.  * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  14.  * </p>
  15.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  16.  */
  17. public class LaneRecord implements LaneRecordInterface<LaneRecord>
  18. {

  19.     /** Lane. */
  20.     private final Lane lane;

  21.     /** Relative lane. */
  22.     private final RelativeLane relativeLane;

  23.     /** Start distance. */
  24.     private final Length startDistance;

  25.     /** Merge distance, i.e. the distance after which this road merges with the road the GTU is at. */
  26.     private final Length mergeDistance;

  27.     /** Set of downstream records. */
  28.     private final Set<LaneRecord> next = new LinkedHashSet<>();

  29.     /** Set of upstream records. */
  30.     private final Set<LaneRecord> prev = new LinkedHashSet<>();

  31.     /** Set of lateral records. */
  32.     private final Set<LaneRecord> lat = new LinkedHashSet<>();

  33.     /**
  34.      * Constructor.
  35.      * @param lane lane.
  36.      * @param relativeLane relative lane.
  37.      * @param startDistance start distance.
  38.      * @param mergeDistance merge distance, i.e. the distance after which this road merges with the road the GTU is at.
  39.      */
  40.     public LaneRecord(final Lane lane, final RelativeLane relativeLane, final Length startDistance, final Length mergeDistance)
  41.     {
  42.         this.lane = lane;
  43.         this.relativeLane = relativeLane;
  44.         this.startDistance = startDistance;
  45.         this.mergeDistance = mergeDistance;
  46.     }

  47.     /**
  48.      * Returns the lane.
  49.      * @return lane.
  50.      */
  51.     @Override
  52.     public Lane getLane()
  53.     {
  54.         return this.lane;
  55.     }

  56.     /**
  57.      * Returns the relative lane.
  58.      * @return relative lane.
  59.      */
  60.     public RelativeLane getRelativeLane()
  61.     {
  62.         return this.relativeLane;
  63.     }

  64.     /**
  65.      * Returns the start distance. This value is negative for anything upstream of the reference point of the GTU.
  66.      * @return start distance.
  67.      */
  68.     @Override
  69.     public Length getStartDistance()
  70.     {
  71.         return this.startDistance;
  72.     }

  73.     /**
  74.      * Returns the end distance. This value is negative for anything upstream of the reference point of the GTU.
  75.      * @return end distance.
  76.      */
  77.     public Length getEndDistance()
  78.     {
  79.         return this.startDistance.plus(this.lane.getLength());
  80.     }

  81.     @Override
  82.     public Length getMergeDistance()
  83.     {
  84.         return this.mergeDistance;
  85.     }

  86.     /**
  87.      * Add downstream lane.
  88.      * @param downstream downstream lane.
  89.      */
  90.     public void addNext(final LaneRecord downstream)
  91.     {
  92.         this.next.add(downstream);
  93.     }

  94.     @Override
  95.     public Set<LaneRecord> getNext()
  96.     {
  97.         return this.next;
  98.     }

  99.     /**
  100.      * Add downstream lane.
  101.      * @param upstream downstream lane.
  102.      */
  103.     public void addPrev(final LaneRecord upstream)
  104.     {
  105.         this.prev.add(upstream);
  106.     }

  107.     @Override
  108.     public Set<LaneRecord> getPrev()
  109.     {
  110.         return this.prev;
  111.     }

  112.     /**
  113.      * Add lateral lane.
  114.      * @param lateral downstream lane.
  115.      */
  116.     public void addLateral(final LaneRecord lateral)
  117.     {
  118.         this.lat.add(lateral);
  119.     }

  120.     @Override
  121.     public Set<LaneRecord> lateral()
  122.     {
  123.         return this.lat;
  124.     }

  125.     /**
  126.      * Returns whether the record is on the route.
  127.      * @param route route.
  128.      * @return whether the record is on the route
  129.      */
  130.     public boolean isOnRoute(final Route route)
  131.     {
  132.         if (route == null)
  133.         {
  134.             return true;
  135.         }
  136.         Link link = getLane().getLink();
  137.         int from;
  138.         int to;
  139.         from = route.indexOf(link.getStartNode());
  140.         to = route.indexOf(link.getEndNode());
  141.         return from != -1 && to != -1 && to - from == 1;
  142.     }

  143.     @Override
  144.     public boolean isDownstreamBranch()
  145.     {
  146.         return this.mergeDistance.eq0() || this.getEndDistance().gt0();
  147.     }

  148.     @Override
  149.     public String toString()
  150.     {
  151.         return "LaneRecord [lane=" + this.lane + ", relativeLane=" + this.relativeLane + ", startDistance=" + this.startDistance
  152.                 + ", mergeDistance=" + this.mergeDistance + "]";
  153.     }

  154. }