TaskSaturationColorer.java

  1. package org.opentrafficsim.road.gtu.animation;

  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import java.util.List;

  5. import org.opentrafficsim.core.gtu.GTU;
  6. import org.opentrafficsim.core.gtu.animation.ColorInterpolator;
  7. import org.opentrafficsim.core.gtu.animation.GTUColorer;
  8. import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller;

  9. /**
  10.  * Displays task saturation.
  11.  * <p>
  12.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  14.  * <p>
  15.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 apr. 2018 <br>
  16.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  18.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  19.  */
  20. public class TaskSaturationColorer implements GTUColorer
  21. {

  22.     /** Full. */
  23.     static final Color MAX = Color.RED;
  24.    
  25.     /** Medium. */
  26.     static final Color MID = Color.YELLOW;
  27.    
  28.     /** Zero. */
  29.     static final Color SUBCRIT = Color.GREEN;
  30.    
  31.     /** Not available. */
  32.     static final Color NA = Color.WHITE;
  33.    
  34.     /** Legend. */
  35.     static final List<LegendEntry> LEGEND;
  36.    
  37.     static
  38.     {
  39.         LEGEND = new ArrayList<>();
  40.         LEGEND.add(new LegendEntry(SUBCRIT, "sub-critical", "sub-critical task saturation"));
  41.         LEGEND.add(new LegendEntry(MID, "medium", "medium task saturation"));
  42.         LEGEND.add(new LegendEntry(MAX, "max", "max task saturation"));
  43.         LEGEND.add(new LegendEntry(NA, "N/A", "N/A"));
  44.     }
  45.    
  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public Color getColor(final GTU gtu)
  49.     {
  50.         Double ts = gtu.getParameters().getParameterOrNull(Fuller.TS);
  51.         Double tsCrit = gtu.getParameters().getParameterOrNull(Fuller.TS_CRIT);
  52.         Double tsMax = gtu.getParameters().getParameterOrNull(Fuller.TS_MAX);
  53.         if (ts == null || tsCrit == null || tsMax == null)
  54.         {
  55.             return NA;
  56.         }
  57.         if (ts < tsCrit)
  58.         {
  59.             return SUBCRIT;
  60.         }
  61.         else if (ts > tsMax)
  62.         {
  63.             return MAX;
  64.         }
  65.         double range = .5 * (tsMax - tsCrit);
  66.         double mid = tsCrit + range;
  67.         if (ts < mid)
  68.         {
  69.             return ColorInterpolator.interpolateColor(SUBCRIT, MID, (ts - tsCrit) / range);
  70.         }
  71.         return ColorInterpolator.interpolateColor(MID, MAX, (tsMax - ts) / range);
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public List<LegendEntry> getLegend()
  76.     {
  77.         return LEGEND;
  78.     }
  79.    
  80.     /** {@inheritDoc} */
  81.     @Override
  82.     public String toString()
  83.     {
  84.         return "Task saturation";
  85.     }

  86. }