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