View Javadoc
1   package org.opentrafficsim.kpi.sampling;
2   
3   import java.io.Serializable;
4   import java.util.HashSet;
5   import java.util.Iterator;
6   import java.util.Set;
7   
8   import org.djutils.exceptions.Throw;
9   import org.djutils.immutablecollections.ImmutableIterator;
10  import org.opentrafficsim.kpi.interfaces.LaneDataInterface;
11  import org.opentrafficsim.kpi.interfaces.LinkDataInterface;
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-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 29, 2016 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class CrossSection implements Serializable
26  {
27  
28      /** */
29      private static final long serialVersionUID = 20160929L;
30  
31      /** Set of lane locations. */
32      private final Set<KpiDirectedLanePosition> directedLanePositions;
33  
34      /**
35       * Constructor with set of directed lane positions.
36       * @param directedLanePositions Set&lt;KpiDirectedLanePosition&gt;; set of lane locations
37       */
38      public CrossSection(final Set<KpiDirectedLanePosition> directedLanePositions)
39      {
40          Throw.whenNull(directedLanePositions, "Directed lane positions may not be null.");
41          this.directedLanePositions = new HashSet<>(directedLanePositions);
42      }
43  
44      /**
45       * Constructor with link and direction.
46       * @param link LinkDataInterface; link
47       * @param direction KpiGtuDirectionality; direction
48       * @param fraction double; fraction on link
49       * @throws SamplingException if an input is null
50       */
51      public CrossSection(final LinkDataInterface link, final KpiGtuDirectionality direction, final double fraction)
52              throws SamplingException
53      {
54          Throw.whenNull(link, "Link lane positions may not be null.");
55          Throw.whenNull(direction, "Direction may not be null.");
56          this.directedLanePositions = new HashSet<>();
57          for (LaneDataInterface lane : link.getLaneDatas())
58          {
59              KpiDirectedLanePosition directedLanePosition =
60                      new KpiDirectedLanePosition(lane, lane.getLength().multiplyBy(fraction), direction);
61              this.directedLanePositions.add(directedLanePosition);
62          }
63      }
64  
65      /**
66       * @return number of directed lane positions
67       */
68      public final int size()
69      {
70          return this.directedLanePositions.size();
71      }
72  
73      /**
74       * @return safe copy of directed lane positions
75       */
76      public final Set<KpiDirectedLanePosition> getDirectedLanePositions()
77      {
78          return new HashSet<>(this.directedLanePositions);
79      }
80  
81      /**
82       * @return iterator over directed lane positions
83       */
84      public final Iterator<KpiDirectedLanePosition> getIterator()
85      {
86          return new ImmutableIterator<>(this.directedLanePositions.iterator());
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      @SuppressWarnings("checkstyle:designforextension")
92      public String toString()
93      {
94          return "CrossSection [directedLanePositions=" + this.directedLanePositions + "]";
95      }
96  
97  }