View Javadoc
1   package org.opentrafficsim.kpi.sampling;
2   
3   import java.io.Serializable;
4   import java.util.Iterator;
5   import java.util.LinkedHashSet;
6   import java.util.Set;
7   
8   import org.djutils.exceptions.Throw;
9   import org.djutils.immutablecollections.ImmutableIterator;
10  import org.opentrafficsim.kpi.interfaces.LaneData;
11  import org.opentrafficsim.kpi.interfaces.LinkData;
12  
13  /**
14   * A cross sections contains locations on lanes that together make up a cross section. It is not required that this is on a
15   * single road, i.e. the cross section may any section in space.
16   * <p>
17   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
22   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23   */
24  public class CrossSection implements Serializable
25  {
26  
27      /** */
28      private static final long serialVersionUID = 20160929L;
29  
30      /** Set of lane locations. */
31      private final Set<LanePosition> lanePositions;
32  
33      /**
34       * Constructor with set of lane positions.
35       * @param lanePositions Set&lt;LanePosition&gt;; set of lane locations
36       */
37      public CrossSection(final Set<LanePosition> lanePositions)
38      {
39          Throw.whenNull(lanePositions, "Lane positions may not be null.");
40          this.lanePositions = new LinkedHashSet<>(lanePositions);
41      }
42  
43      /**
44       * Constructor with link and fraction.
45       * @param link LinkData&lt;?&gt;; link
46       * @param fraction double; fraction on link
47       * @throws SamplingException if an input is null
48       */
49      public CrossSection(final LinkData<?> link, final double fraction) throws SamplingException
50      {
51          Throw.whenNull(link, "Link lane positions may not be null.");
52          this.lanePositions = new LinkedHashSet<>();
53          for (LaneData<?> lane : link.getLaneDatas())
54          {
55              LanePosition lanePosition = new LanePosition(lane, lane.getLength().times(fraction));
56              this.lanePositions.add(lanePosition);
57          }
58      }
59  
60      /**
61       * Returns the number of lane positions.
62       * @return number of directed lane positions
63       */
64      public final int size()
65      {
66          return this.lanePositions.size();
67      }
68  
69      /**
70       * Returns a safe copy of the lane positions.
71       * @return safe copy of lane positions
72       */
73      public final Set<LanePosition> getLanePositions()
74      {
75          return new LinkedHashSet<>(this.lanePositions);
76      }
77  
78      /**
79       * Returns an iterator over the lane positions.
80       * @return iterator over lane positions
81       */
82      public final Iterator<LanePosition> getIterator()
83      {
84          return new ImmutableIterator<>(this.lanePositions.iterator());
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      @SuppressWarnings("checkstyle:designforextension")
90      public String toString()
91      {
92          return "CrossSection [lanePositions=" + this.lanePositions + "]";
93      }
94  
95  }