CrossSection.java

  1. package org.opentrafficsim.kpi.sampling;

  2. import java.io.Serializable;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashSet;
  5. import java.util.Set;

  6. import org.djunits.value.vdouble.scalar.Length;
  7. import org.djutils.exceptions.Throw;
  8. import org.djutils.immutablecollections.ImmutableIterator;
  9. import org.opentrafficsim.kpi.interfaces.LaneData;
  10. import org.opentrafficsim.kpi.interfaces.LinkData;
  11. import org.opentrafficsim.kpi.sampling.CrossSection.LanePosition;

  12. /**
  13.  * A cross sections contains locations on lanes that together make up a cross section. It is not required that this is on a
  14.  * single road, i.e. the cross section may be any section in space.
  15.  * <p>
  16.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  17.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  18.  * </p>
  19.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  20.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  21.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  22.  */
  23. public class CrossSection implements Serializable, Iterable<LanePosition>
  24. {

  25.     /** */
  26.     private static final long serialVersionUID = 20160929L;

  27.     /** Set of lane locations. */
  28.     private final Set<LanePosition> lanePositions;

  29.     /**
  30.      * Constructor with set of lane positions.
  31.      * @param lanePositions set of lane locations
  32.      */
  33.     public CrossSection(final Set<LanePosition> lanePositions)
  34.     {
  35.         Throw.whenNull(lanePositions, "Lane positions may not be null.");
  36.         this.lanePositions = new LinkedHashSet<>(lanePositions);
  37.     }

  38.     /**
  39.      * Constructor with link and fraction. Note that the fraction is used with the length of each lane. For curved links this
  40.      * fraction may not point to locations on the lane that are perfectly laterally adjacent. GTUs can thus possibly change lane
  41.      * at these gaps and be missed.
  42.      * @param link link
  43.      * @param fraction fraction on link
  44.      */
  45.     public CrossSection(final LinkData<?> link, final double fraction)
  46.     {
  47.         Throw.whenNull(link, "Link lane positions may not be null.");
  48.         this.lanePositions = new LinkedHashSet<>();
  49.         for (LaneData<?> lane : link.getLanes())
  50.         {
  51.             LanePosition lanePosition = new LanePosition(lane, lane.getLength().times(fraction));
  52.             this.lanePositions.add(lanePosition);
  53.         }
  54.     }

  55.     /**
  56.      * Returns the number of lane positions.
  57.      * @return number of directed lane positions
  58.      */
  59.     public final int size()
  60.     {
  61.         return this.lanePositions.size();
  62.     }

  63.     /**
  64.      * Returns a safe copy of the lane positions.
  65.      * @return safe copy of lane positions
  66.      */
  67.     public final Set<LanePosition> getLanePositions()
  68.     {
  69.         return new LinkedHashSet<>(this.lanePositions);
  70.     }

  71.     /**
  72.      * Returns an iterator over the lane positions.
  73.      * @return iterator over lane positions
  74.      */
  75.     @Override
  76.     public final Iterator<LanePosition> iterator()
  77.     {
  78.         return new ImmutableIterator<>(this.lanePositions.iterator());
  79.     }

  80.     @Override
  81.     public String toString()
  82.     {
  83.         return "CrossSection [lanePositions=" + this.lanePositions + "]";
  84.     }

  85.     /**
  86.      * Position on a lane.
  87.      * @param lane lane
  88.      * @param position position
  89.      */
  90.     @SuppressWarnings("javadoc") // @param's present but warning still given, bug?
  91.     public record LanePosition(LaneData<?> lane, Length position)
  92.     {
  93.         /**
  94.          * Construct a new LanePosition.
  95.          */
  96.         public LanePosition
  97.         {
  98.             Throw.whenNull(lane, "lane is null");
  99.             Throw.whenNull(position, "position is null");
  100.         }
  101.     }

  102. }