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