View Javadoc
1   package org.opentrafficsim.graphs;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.djunits.unit.UNITS;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Time;
8   import org.junit.Test;
9   import org.opentrafficsim.core.gtu.GTUException;
10  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
11  import org.opentrafficsim.road.network.lane.Lane;
12  
13  /**
14   * <p>
15   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * <p>
18   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
19   * initial version Aug 22, 2014 <br>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   */
22  public class TrajectoryPlotTest implements UNITS
23  {
24      /** Sample interval for the TrajectoryPlot. */
25      Time.Rel sampleInterval = new Time.Rel(0.25, SECOND);
26  
27      /**
28       * Test the TrajectoryPlot.
29       * @throws Exception which should not happen, but will be treated as an error by the JUnit framework if it does
30       */
31      @Test
32      public final void trajectoryTest() throws Exception
33      {
34          Length.Rel minimumDistance = new Length.Rel(1234, METER);
35          Length.Rel maximumDistance = new Length.Rel(12345, METER);
36  
37          // TODO adapt to new path (List<Lane>) concept
38          /*-
39          TrajectoryPlot tp = new TrajectoryPlot("Trajectory", this.sampleInterval, minimumDistance, maximumDistance);
40          assertTrue("newly created DensityContourPlot should not be null", null != tp);
41          assertEquals("Number of trajectories should initially be 0", 0, tp.getSeriesCount());
42          for (int i = -10; i <= 10; i++)
43          {
44              assertEquals("SeriesKey(" + i + ") should return " + i, i, tp.getSeriesKey(i));
45          }
46          assertEquals("Domain order should be ASCENDING", DomainOrder.ASCENDING, tp.getDomainOrder());
47          // Create a car running 50 km.h
48          Length.Rel initialPosition = new Length.Rel(2000, METER);
49          Speed initialSpeed = new Speed(50, KM_PER_HOUR);
50          GTUType carType = new GTUType("Car");
51          Length.Rel length = new Length.Rel(5.0, METER);
52          Length.Rel width = new Length.Rel(2.0, METER);
53          Map<Lane, Length.Rel> initialLongitudinalPositions = new HashMap<>();
54          Lane lane = CarTest.makeLane();
55          initialLongitudinalPositions.put(lane, initialPosition);
56          OTSDEVSSimulator simulator = CarTest.makeSimulator();
57          // We want to start the car simulation at t=100s; therefore we have to advance the simulator up to that time.
58          simulateUntil(new Time.Abs(100, SECOND), simulator);
59          Speed maxSpeed = new Speed(120, KM_PER_HOUR);
60          Car car =
61              new Car(12345, carType, null, initialLongitudinalPositions, initialSpeed, length, width, maxSpeed,
62                  simulator);
63          // Make the car accelerate with constant acceleration of 0.05 m/s/s for 400 seconds
64          Time.Rel duration = new Time.Rel(400, SECOND);
65          Time.Abs endTime = DoubleScalar.plus(simulator.getSimulatorTime().getTime(), duration);
66          car.setState(new GTUFollowingModelResult(new Acceleration(0.05,
67              METER_PER_SECOND_2), endTime));
68          // System.out.println("Car end position " + car.getPosition(car.getNextEvaluationTime()));
69          tp.addData(car);
70          assertEquals("Number of trajectories should now be 1", 1, tp.getSeriesCount());
71          verifyTrajectory(car, 0, tp);
72          simulateUntil(new Time.Abs(150, SECOND), simulator);
73          Car secondCar =
74              new Car(2, carType, null, initialLongitudinalPositions, initialSpeed, length, width, maxSpeed,
75                  simulator);
76          // Make the second car accelerate with constant acceleration of 0.03 m/s/s for 500 seconds
77          secondCar.setState(new GTUFollowingModelResult(new Acceleration(0.03,
78              METER_PER_SECOND_2), endTime));
79          // System.out.println("Second car end position " + car.getPosition(secondCar.getNextEvaluationTime()));
80          tp.addData(secondCar);
81          assertEquals("Number of trajectories should now be 2", 2, tp.getSeriesCount());
82          verifyTrajectory(car, 0, tp); // first car trajectory should not change by adding the second
83          verifyTrajectory(secondCar, 1, tp);
84          // Check the updateHint method in the PointerHandler
85          // First get the panel that stores the result of updateHint (this is ugly)
86          JLabel hintPanel = null;
87          ChartPanel chartPanel = null;
88          for (Component c0 : tp.getComponents())
89          {
90              for (Component c1 : ((Container) c0).getComponents())
91              {
92                  if (c1 instanceof Container)
93                  {
94                      for (Component c2 : ((Container) c1).getComponents())
95                      {
96                          // System.out.println("c2 is " + c2);
97                          if (c2 instanceof Container)
98                          {
99                              for (Component c3 : ((Container) c2).getComponents())
100                             {
101                                 // System.out.println("c3 is " + c3);
102                                 if (c3 instanceof JLabel)
103                                 {
104                                     if (null == hintPanel)
105                                     {
106                                         hintPanel = (JLabel) c3;
107                                     }
108                                     else
109                                     {
110                                         fail("There should be only one JPanel in a ContourPlot");
111                                     }
112                                 }
113                                 if (c3 instanceof ChartPanel)
114                                 {
115                                     if (null == chartPanel)
116                                     {
117                                         chartPanel = (ChartPanel) c3;
118                                     }
119                                     else
120                                     {
121                                         fail("There should be only one ChartPanel in a ContourPlot");
122                                     }
123                                 }
124                             }
125                         }
126                     }
127                 }
128             }
129         }
130         if (null == hintPanel)
131         {
132             fail("Could not find a JLabel in ContourPlot");
133         }
134         if (null == chartPanel)
135         {
136             fail("Could not find a ChartPanel in ContourPlot");
137         }
138         assertEquals("Initially the text should be a single space", " ", hintPanel.getText());
139         PointerHandler ph = null;
140         for (MouseListener ml : chartPanel.getMouseListeners())
141         {
142             if (ml instanceof PointerHandler)
143             {
144                 if (null == ph)
145                 {
146                     ph = (PointerHandler) ml;
147                 }
148                 else
149                 {
150                     fail("There should be only one PointerHandler on the chartPanel");
151                 }
152             }
153         }
154         if (null == ph)
155         {
156             fail("Could not find the PointerHandler for the chartPanel");
157         }
158         ph.updateHint(1, 2);
159         // System.out.println("Hint text is now " + hintPanel.getText());
160         assertFalse("Hint should not be a single space", " ".equals(hintPanel.getText()));
161         ph.updateHint(Double.NaN, Double.NaN);
162         assertEquals("The text should again be a single space", " ", hintPanel.getText());
163          */
164     }
165 
166     /**
167      * Verify that a sampled trajectory matches the actual trajectory.
168      * @param car Car; the car whose trajectory was sampled
169      * @param series Integer; the series in the TrajectoryPlot that should correspond to the car
170      * @param tp TrajectoryPlot; the TrajectoryPlot that contains the samples
171      * @throws GTUException when car is not on lane anymore
172      */
173     private void verifyTrajectory(final LaneBasedIndividualGTU car, final int series, final TrajectoryPlot tp)
174         throws GTUException
175     {
176         // XXX we take the first (and only) lane on which the vehicle is registered.
177         Lane lane = car.positions(car.getFront()).keySet().iterator().next();
178         Time.Abs initialTime = car.getOperationalPlan().getStartTime();
179         Time.Rel duration = car.getOperationalPlan().getTotalDuration();
180         int expectedNumberOfSamples = (int) (duration.getSI() / this.sampleInterval.getSI());
181         assertEquals("Number of samples in trajectory should be ", expectedNumberOfSamples, tp.getItemCount(series));
182         // Check that the stored trajectory accurately matches the trajectory of the car at all sampling times
183         for (int sample = 0; sample < expectedNumberOfSamples; sample++)
184         {
185             Time.Rel deltaTime = new Time.Rel(this.sampleInterval.getSI() * sample, SECOND);
186             Time.Abs sampleTime = initialTime.plus(deltaTime);
187             double sampledTime = tp.getXValue(series, sample);
188             assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
189             sampledTime = tp.getX(series, sample).doubleValue();
190             assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
191             Length.Rel actualPosition = car.position(lane, car.getFront(), sampleTime);
192             double sampledPosition = tp.getYValue(series, sample);
193             assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition,
194                 0.0001);
195             sampledPosition = tp.getY(series, sample).doubleValue();
196             assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition,
197                 0.0001);
198         }
199     }
200 
201 }