View Javadoc
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   * Contour plot for acceleration.
24   * <p>
25   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
27   * <p>
28   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 okt. 2018 <br>
29   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
31   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
32   */
33  public class ContourPlotAcceleration extends AbstractContourPlot<Acceleration>
34  {
35  
36      /** */
37      private static final long serialVersionUID = 20181010L;
38  
39      /** Quantity for the EGTF. */
40      private static final Quantity<Acceleration, AccelerationMatrix> QUANTITY =
41              new Quantity<>("acceleration", new Converter<AccelerationMatrix>()
42              {
43                  /** {@inheritDoc} */
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                          // should not happen as filtered data comes from the EGTF
54                          throw new RuntimeException("Unexpected exception while converting acceleration to output format.",
55                                  exception);
56                      }
57                  }
58              });
59  
60      /** Contour data type. */
61      private static final ContourDataType<Acceleration, WeightedMeanAndSum<Double, Double>> CONTOUR_DATA_TYPE =
62              new ContourDataType<Acceleration, WeightedMeanAndSum<Double, Double>>()
63              {
64                  /** {@inheritDoc} */
65                  @Override
66                  public WeightedMeanAndSum<Double, Double> identity()
67                  {
68                      return new WeightedMeanAndSum<>();
69                  }
70  
71                  /** {@inheritDoc} */
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                  /** {@inheritDoc} */
98                  @Override
99                  public Acceleration finalize(final WeightedMeanAndSum<Double, Double> intermediate)
100                 {
101                     return Acceleration.createSI(intermediate.getMean());
102                 }
103 
104                 /** {@inheritDoc} */
105                 @SuppressWarnings("synthetic-access")
106                 @Override
107                 public Quantity<Acceleration, ?> getQuantity()
108                 {
109                     return QUANTITY;
110                 }
111 
112             };
113 
114     /**
115      * Constructor.
116      * @param caption String; caption
117      * @param simulator OTSSimulatorInterface; simulator
118      * @param dataPool ContourDataSource&lt;?&gt;; data pool
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      * Creates a paint scale from red, via yellow to green.
129      * @return ContinuousColorPaintScale; paint scale
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     /** {@inheritDoc} */
139     @Override
140     public GraphType getGraphType()
141     {
142         return GraphType.ACCELERATION_CONTOUR;
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     protected double scale(final double si)
148     {
149         return si;
150     }
151 
152     /** {@inheritDoc} */
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     /** {@inheritDoc} */
160     @Override
161     protected ContourDataType<Acceleration, WeightedMeanAndSum<Double, Double>> getContourDataType()
162     {
163         return CONTOUR_DATA_TYPE;
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public String toString()
169     {
170         return "ContourPlotAcceleration []";
171     }
172 
173 }