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
17
18
19
20
21
22
23
24
25
26 public class MetaDataCrossSections extends MetaDataType<CrossSection>
27 {
28
29
30
31
32 public MetaDataCrossSections()
33 {
34 super("crossSection");
35 }
36
37
38 @Override
39 public final CrossSection getValue(final GtuDataInterface gtu)
40 {
41 return null;
42 }
43
44
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
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
73 for (int i = 0; i < trajectoryAcceptList.size(); i++)
74 {
75 TrajectoryGroup trajectoryGroup = trajectoryAcceptList.getTrajectoryGroup(i);
76
77 Iterator<CrossSection> crossSectionIterator = querySet.iterator();
78 while (crossSectionIterator.hasNext())
79 {
80 CrossSection crossSection = crossSectionIterator.next();
81
82 Iterator<KpiDirectedLanePosition> directedLanePositionIterator = crossSection.getIterator();
83 while (directedLanePositionIterator.hasNext())
84 {
85 KpiDirectedLanePosition directedLanePosition = directedLanePositionIterator.next();
86
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
98 crossedCrossSections.add(crossSection);
99 }
100 }
101 }
102 }
103 }
104 if (querySet.equals(crossedCrossSections))
105 {
106 trajectoryAcceptList.acceptAll();
107 }
108 }
109
110
111 @Override
112 @SuppressWarnings("checkstyle:designforextension")
113 public String toString()
114 {
115 return "MetaDataCrossSections: [id=" + getId() + "]";
116 }
117
118 }