View Javadoc
1   package org.opentrafficsim.road.gtu.colorer;
2   
3   import java.awt.Color;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.opentrafficsim.core.animation.ColorInterpolator;
8   import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
9   import org.opentrafficsim.core.gtu.GTU;
10  import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller;
11  
12  /**
13   * Displays task saturation.
14   * <p>
15   * Copyright (c) 2013-2019 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/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 apr. 2018 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   */
23  public class TaskSaturationColorer implements GTUColorer
24  {
25  
26      /** Full. */
27      static final Color MAX = Color.RED;
28  
29      /** Medium. */
30      static final Color MID = Color.YELLOW;
31  
32      /** Zero. */
33      static final Color SUBCRIT = Color.GREEN;
34  
35      /** Not available. */
36      static final Color NA = Color.WHITE;
37  
38      /** Legend. */
39      static final List<LegendEntry> LEGEND;
40  
41      static
42      {
43          LEGEND = new ArrayList<>();
44          LEGEND.add(new LegendEntry(SUBCRIT, "sub-critical", "sub-critical task saturation"));
45          LEGEND.add(new LegendEntry(MID, "medium", "medium task saturation"));
46          LEGEND.add(new LegendEntry(MAX, "max", "max task saturation"));
47          LEGEND.add(new LegendEntry(NA, "N/A", "N/A"));
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public Color getColor(final GTU gtu)
53      {
54          Double ts = gtu.getParameters().getParameterOrNull(Fuller.TS);
55          Double tsCrit = gtu.getParameters().getParameterOrNull(Fuller.TS_CRIT);
56          Double tsMax = gtu.getParameters().getParameterOrNull(Fuller.TS_MAX);
57          if (ts == null || tsCrit == null || tsMax == null)
58          {
59              return NA;
60          }
61          if (ts < tsCrit)
62          {
63              return SUBCRIT;
64          }
65          else if (ts > tsMax)
66          {
67              return MAX;
68          }
69          double range = .5 * (tsMax - tsCrit);
70          double mid = tsCrit + range;
71          if (ts < mid)
72          {
73              return ColorInterpolator.interpolateColor(SUBCRIT, MID, (ts - tsCrit) / range);
74          }
75          return ColorInterpolator.interpolateColor(MAX, MID, (tsMax - ts) / range);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public List<LegendEntry> getLegend()
81      {
82          return LEGEND;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public String toString()
88      {
89          return "Task saturation";
90      }
91  
92  }