FlowContourPlot.java

  1. package org.opentrafficsim.graphs;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import org.djunits.unit.LengthUnit;
  5. import org.djunits.value.StorageType;
  6. import org.djunits.value.ValueException;
  7. import org.djunits.value.vdouble.scalar.DoubleScalar;
  8. import org.djunits.value.vdouble.vector.MutablePositionVector;
  9. import org.opentrafficsim.road.network.lane.Lane;
  10. import org.opentrafficsim.simulationengine.OTSSimulationException;

  11. /**
  12.  * Flow contour plot.
  13.  * <p>
  14.  * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  16.  * <p>
  17.  * $LastChangedDate: 2015-09-03 13:38:01 +0200 (Thu, 03 Sep 2015) $, @version $Revision: 1378 $, by $Author: averbraeck $,
  18.  * initial version Jul 29, 2014 <br>
  19.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  20.  */
  21. public class FlowContourPlot extends ContourPlot
  22. {
  23.     /** */
  24.     private static final long serialVersionUID = 20140729L;

  25.     /**
  26.      * Create a new FlowContourPlot.
  27.      * @param caption String; text to show above the FlowContourPlot
  28.      * @param path List&lt;Lane&gt;; the series of Lanes that will provide the data for this TrajectoryPlot
  29.      * @throws OTSSimulationException in case of problems initializing the graph
  30.      */
  31.     public FlowContourPlot(final String caption, final List<Lane> path) throws OTSSimulationException
  32.     {
  33.         super(caption, new Axis(INITIALLOWERTIMEBOUND, INITIALUPPERTIMEBOUND, STANDARDTIMEGRANULARITIES,
  34.             STANDARDTIMEGRANULARITIES[STANDARDINITIALTIMEGRANULARITYINDEX], "", "Time", "%.0fs"), path, 2500d, 1500d,
  35.             0d, "flow %.0f veh/h", "%.0f veh/h", 500d);
  36.     }

  37.     /** Storage for the total length traveled in each cell. */
  38.     private ArrayList<MutablePositionVector> cumulativeLengths;

  39.     /** {@inheritDoc} */
  40.     @Override
  41.     public final Comparable<String> getSeriesKey(final int series)
  42.     {
  43.         return "flow";
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public final void extendXRange(final DoubleScalar<?> newUpperLimit)
  48.     {
  49.         if (null == this.cumulativeLengths)
  50.         {
  51.             this.cumulativeLengths = new ArrayList<MutablePositionVector>();
  52.         }
  53.         final int highestBinNeeded =
  54.             (int) Math.floor(this.getXAxis().getRelativeBin(newUpperLimit) * this.getXAxis().getCurrentGranularity()
  55.                 / this.getXAxis().getGranularities()[0]);
  56.         while (highestBinNeeded >= this.cumulativeLengths.size())
  57.         {
  58.             try
  59.             {
  60.                 this.cumulativeLengths.add(new MutablePositionVector(new double[this.getYAxis().getBinCount()],
  61.                     LengthUnit.METER, StorageType.DENSE));
  62.             }
  63.             catch (ValueException exception)
  64.             {
  65.                 exception.printStackTrace();
  66.             }
  67.         }
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public final void incrementBinData(final int timeBin, final int distanceBin, final double duration,
  72.         final double distanceCovered, final double acceleration)
  73.     {
  74.         if (timeBin < 0 || distanceBin < 0 || 0 == duration || distanceBin >= this.getYAxis().getBinCount())
  75.         {
  76.             return;
  77.         }
  78.         while (timeBin >= this.cumulativeLengths.size())
  79.         {
  80.             try
  81.             {
  82.                 this.cumulativeLengths.add(new MutablePositionVector(new double[this.getYAxis().getBinCount()],
  83.                     LengthUnit.METER, StorageType.DENSE));
  84.             }
  85.             catch (ValueException exception)
  86.             {
  87.                 exception.printStackTrace();
  88.             }
  89.         }
  90.         MutablePositionVector values = this.cumulativeLengths.get(timeBin);
  91.         try
  92.         {
  93.             values.setSI(distanceBin, values.getSI(distanceBin) + distanceCovered);
  94.         }
  95.         catch (ValueException exception)
  96.         {
  97.             System.err.println("Error in incrementData:");
  98.             exception.printStackTrace();
  99.         }
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public final double computeZValue(final int firstTimeBin, final int endTimeBin, final int firstDistanceBin,
  104.         final int endDistanceBin)
  105.     {
  106.         double cumulativeLengthInSI = 0;
  107.         if (firstTimeBin >= this.cumulativeLengths.size())
  108.         {
  109.             return Double.NaN;
  110.         }
  111.         try
  112.         {
  113.             for (int timeBinIndex = firstTimeBin; timeBinIndex < endTimeBin; timeBinIndex++)
  114.             {
  115.                 if (timeBinIndex >= this.cumulativeLengths.size())
  116.                 {
  117.                     break;
  118.                 }
  119.                 MutablePositionVector values = this.cumulativeLengths.get(timeBinIndex);
  120.                 for (int distanceBinIndex = firstDistanceBin; distanceBinIndex < endDistanceBin; distanceBinIndex++)
  121.                 {
  122.                     cumulativeLengthInSI += values.getSI(distanceBinIndex);
  123.                 }
  124.             }
  125.         }
  126.         catch (ValueException exception)
  127.         {
  128.             System.err.println(String.format("Error in getZValue(timeBinRange=[%d-%d], distanceBinRange=[%d-%d]",
  129.                 firstTimeBin, endTimeBin, firstDistanceBin, endDistanceBin));
  130.             exception.printStackTrace();
  131.         }
  132.         return 3600 * cumulativeLengthInSI / this.getXAxis().getCurrentGranularity()
  133.             / this.getYAxis().getCurrentGranularity();
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     public final String toString()
  138.     {
  139.         return "FlowContourPlot [cumulativeLengths.size=" + this.cumulativeLengths.size() + "]";
  140.     }

  141. }