View Javadoc
1   package org.opentrafficsim.kpi.sampling.meta;
2   
3   import java.util.HashSet;
4   import java.util.Iterator;
5   import java.util.Set;
6   
7   import org.opentrafficsim.kpi.interfaces.GtuDataInterface;
8   import org.opentrafficsim.kpi.sampling.CrossSection;
9   import org.opentrafficsim.kpi.sampling.KpiDirectedLanePosition;
10  import org.opentrafficsim.kpi.sampling.TrajectoryAcceptList;
11  import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
12  
13  import nl.tudelft.simulation.language.Throw;
14  
15  /**
16   * Accepts trajectories that have passed all cross sections as defined in a query.
17   * <p>
18   * Copyright (c) 2013-2016 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 MetaDataCrossSections extends MetaDataType<CrossSection>
27  {
28  
29      /**
30       * @param id id
31       */
32      public MetaDataCrossSections(final String id)
33      {
34          super(id);
35      }
36  
37      /** {@inheritDoc} */
38      @Override
39      public final CrossSection getValue(final GtuDataInterface gtu)
40      {
41          return null;
42      }
43  
44      /**
45       * Accepts all trajectory's or rejects all trajectory's depending on whether all cross sections have been crossed.
46       */
47      @Override
48      public final void accept(final TrajectoryAcceptList trajectoryAcceptList, final Set<CrossSection> querySet)
49      {
50          Throw.whenNull(trajectoryAcceptList, "Trajectory accept list may not be null.");
51          Throw.whenNull(querySet, "Qeury set may not be null.");
52          Set<CrossSection> crossedCrossSections = new HashSet<>();
53          // Loop over trajectoryList/trajectoryGroupList combo
54          for (int i = 0; i < trajectoryAcceptList.size(); i++)
55          {
56              TrajectoryGroup trajectoryGroup = trajectoryAcceptList.getTrajectoryGroup(i);
57              // Loop over cross sections
58              Iterator<CrossSection> crossSectionIterator = querySet.iterator();
59              while (crossSectionIterator.hasNext())
60              {
61                  CrossSection crossSection = crossSectionIterator.next();
62                  // Loop over lanes in cross section
63                  Iterator<KpiDirectedLanePosition> directedLanePositionIterator = crossSection.getIterator();
64                  while (directedLanePositionIterator.hasNext())
65                  {
66                      KpiDirectedLanePosition directedLanePosition = directedLanePositionIterator.next();
67                      // If Trajectories is of same lane and direction, check position
68                      if (trajectoryGroup.getLaneDirection().getLaneData().equals(directedLanePosition.getLaneData())
69                              && trajectoryGroup.getLaneDirection().getKpiDirection().equals(directedLanePosition.getKpiGtuDirection()))
70                      {
71                          double position = directedLanePosition.getPosition().si;
72                          float[] x = trajectoryAcceptList.getTrajectory(i).getX();
73                          double xStart = x[0];
74                          double xEnd = x[x.length - 1];
75                          if ((xStart < position && position < xEnd) || (xEnd < position && position < xStart))
76                          {
77                              // Trajectory was up- and downstream of the location, so the location was crossed
78                              crossedCrossSections.add(crossSection);
79                          }
80                      }
81                  }
82              }
83          }
84          if (querySet.equals(crossedCrossSections))
85          {
86              trajectoryAcceptList.acceptAll();
87          }
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      @SuppressWarnings("checkstyle:designforextension")
93      public String toString()
94      {
95          return "MetaDataCrossSections: [id=" + getId() + "]";
96      }
97  
98  }