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