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-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 MetaDataCrossSections extends MetaDataType<CrossSection>
27  {
28  
29      /**
30       * 
31       */
32      public MetaDataCrossSections()
33      {
34          super("crossSection");
35      }
36  
37      /** {@inheritDoc} */
38      @Override
39      public final CrossSection getValue(final GtuDataInterface gtu)
40      {
41          return null;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public String formatValue(String format, CrossSection value)
47      {
48          StringBuilder str = new StringBuilder();
49          str.append("[");
50          String delimiter = "";
51          for (KpiDirectedLanePosition kpiDirectedLanePosition : value.getDirectedLanePositions())
52          {
53              str.append(delimiter);
54              delimiter = "|";
55              str.append(kpiDirectedLanePosition.getLaneData().getId());
56              str.append(kpiDirectedLanePosition.getKpiGtuDirection().isPlus() ? "+" : "-");
57              str.append(String.format(format, kpiDirectedLanePosition.getPosition().si));
58          }
59          str.append("]");
60          return str.toString();
61      }
62  
63      /**
64       * Accepts all trajectory's or rejects all trajectory's depending on whether all cross sections have been crossed.
65       */
66      @Override
67      public final void accept(final TrajectoryAcceptList trajectoryAcceptList, final Set<CrossSection> querySet)
68      {
69          Throw.whenNull(trajectoryAcceptList, "Trajectory accept list may not be null.");
70          Throw.whenNull(querySet, "Qeury set may not be null.");
71          Set<CrossSection> crossedCrossSections = new HashSet<>();
72          // Loop over trajectoryList/trajectoryGroupList combo
73          for (int i = 0; i < trajectoryAcceptList.size(); i++)
74          {
75              TrajectoryGroup trajectoryGroup = trajectoryAcceptList.getTrajectoryGroup(i);
76              // Loop over cross sections
77              Iterator<CrossSection> crossSectionIterator = querySet.iterator();
78              while (crossSectionIterator.hasNext())
79              {
80                  CrossSection crossSection = crossSectionIterator.next();
81                  // Loop over lanes in cross section
82                  Iterator<KpiDirectedLanePosition> directedLanePositionIterator = crossSection.getIterator();
83                  while (directedLanePositionIterator.hasNext())
84                  {
85                      KpiDirectedLanePosition directedLanePosition = directedLanePositionIterator.next();
86                      // If Trajectories is of same lane and direction, check position
87                      if (trajectoryGroup.getLaneDirection().getLaneData().equals(directedLanePosition.getLaneData())
88                              && trajectoryGroup.getLaneDirection().getKpiDirection()
89                                      .equals(directedLanePosition.getKpiGtuDirection()))
90                      {
91                          double position = directedLanePosition.getPosition().si;
92                          float[] x = trajectoryAcceptList.getTrajectory(i).getX();
93                          double xStart = x[0];
94                          double xEnd = x[x.length - 1];
95                          if ((xStart < position && position < xEnd) || (xEnd < position && position < xStart))
96                          {
97                              // Trajectory was up- and downstream of the location, so the location was crossed
98                              crossedCrossSections.add(crossSection);
99                          }
100                     }
101                 }
102             }
103         }
104         if (querySet.equals(crossedCrossSections))
105         {
106             trajectoryAcceptList.acceptAll();
107         }
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     @SuppressWarnings("checkstyle:designforextension")
113     public String toString()
114     {
115         return "MetaDataCrossSections: [id=" + getId() + "]";
116     }
117 
118 }