1 package org.opentrafficsim.draw.graphs;
2
3 import java.awt.Color;
4 import java.util.List;
5
6 import org.djunits.unit.AccelerationUnit;
7 import org.djunits.value.StorageType;
8 import org.djunits.value.ValueException;
9 import org.djunits.value.vdouble.matrix.AccelerationMatrix;
10 import org.djunits.value.vdouble.scalar.Acceleration;
11 import org.djunits.value.vdouble.scalar.Length;
12 import org.djunits.value.vdouble.scalar.Time;
13 import org.opentrafficsim.base.WeightedMeanAndSum;
14 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
15 import org.opentrafficsim.core.egtf.Converter;
16 import org.opentrafficsim.core.egtf.Quantity;
17 import org.opentrafficsim.draw.core.BoundsPaintScale;
18 import org.opentrafficsim.draw.graphs.ContourDataSource.ContourDataType;
19 import org.opentrafficsim.kpi.sampling.Trajectory;
20 import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
21
22
23
24
25
26
27
28
29
30
31
32
33 public class ContourPlotAcceleration extends AbstractContourPlot<Acceleration>
34 {
35
36
37 private static final long serialVersionUID = 20181010L;
38
39
40 private static final Quantity<Acceleration, AccelerationMatrix> QUANTITY =
41 new Quantity<>("acceleration", new Converter<AccelerationMatrix>()
42 {
43
44 @Override
45 public AccelerationMatrix convert(final double[][] filteredData)
46 {
47 try
48 {
49 return new AccelerationMatrix(filteredData, AccelerationUnit.SI, StorageType.DENSE);
50 }
51 catch (ValueException exception)
52 {
53
54 throw new RuntimeException("Unexpected exception while converting acceleration to output format.",
55 exception);
56 }
57 }
58 });
59
60
61 private static final ContourDataType<Acceleration, WeightedMeanAndSum<Double, Double>> CONTOUR_DATA_TYPE =
62 new ContourDataType<Acceleration, WeightedMeanAndSum<Double, Double>>()
63 {
64
65 @Override
66 public WeightedMeanAndSum<Double, Double> identity()
67 {
68 return new WeightedMeanAndSum<>();
69 }
70
71
72 @Override
73 public WeightedMeanAndSum<Double, Double> processSeries(final WeightedMeanAndSum<Double, Double> intermediate,
74 final List<TrajectoryGroup<?>> trajectories, final List<Length> xFrom, final List<Length> xTo,
75 final Time tFrom, final Time tTo)
76 {
77 for (int i = 0; i < trajectories.size(); i++)
78 {
79 TrajectoryGroup<?> trajectoryGroup = trajectories.get(i);
80 for (Trajectory<?> trajectory : trajectoryGroup.getTrajectories())
81 {
82 if (GraphUtil.considerTrajectory(trajectory, tFrom, tTo))
83 {
84 trajectory = trajectory.subSet(xFrom.get(i), xTo.get(i), tFrom, tTo);
85 float[] t = trajectory.getT();
86 float[] a = trajectory.getA();
87 for (int j = 0; j < t.length - 1; j++)
88 {
89 intermediate.add((double) a[j], (double) (t[j + 1] - t[j]));
90 }
91 }
92 }
93 }
94 return intermediate;
95 }
96
97
98 @Override
99 public Acceleration finalize(final WeightedMeanAndSum<Double, Double> intermediate)
100 {
101 return Acceleration.createSI(intermediate.getMean());
102 }
103
104
105 @SuppressWarnings("synthetic-access")
106 @Override
107 public Quantity<Acceleration, ?> getQuantity()
108 {
109 return QUANTITY;
110 }
111
112 };
113
114
115
116
117
118
119
120 public ContourPlotAcceleration(final String caption, final OTSSimulatorInterface simulator,
121 final ContourDataSource<?> dataPool)
122 {
123 super(caption, simulator, dataPool, createPaintScale(), new Acceleration(1.0, AccelerationUnit.SI), "%.0fm/s\u00B2",
124 "acceleration %.2f m/s\u00B2");
125 }
126
127
128
129
130
131 private static BoundsPaintScale createPaintScale()
132 {
133 double[] boundaries = { -3.0, -1.5, 0.0, 1.0, 2.0 };
134 Color[] colorValues = BoundsPaintScale.reverse(BoundsPaintScale.GREEN_RED_DARK);
135 return new BoundsPaintScale(boundaries, colorValues);
136 }
137
138
139 @Override
140 public GraphType getGraphType()
141 {
142 return GraphType.ACCELERATION_CONTOUR;
143 }
144
145
146 @Override
147 protected double scale(final double si)
148 {
149 return si;
150 }
151
152
153 @Override
154 protected double getValue(final int item, final double cellLength, final double cellSpan)
155 {
156 return getDataPool().get(item, CONTOUR_DATA_TYPE);
157 }
158
159
160 @Override
161 protected ContourDataType<Acceleration, WeightedMeanAndSum<Double, Double>> getContourDataType()
162 {
163 return CONTOUR_DATA_TYPE;
164 }
165
166
167 @Override
168 public String toString()
169 {
170 return "ContourPlotAcceleration []";
171 }
172
173 }