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.LanePerception;
11  import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller;
12  import org.opentrafficsim.road.gtu.lane.perception.mental.Mental;
13  import org.opentrafficsim.road.gtu.lane.perception.mental.Task;
14  
15  /**
16   * Colorer for task demand with anticipation reliance indicated by interpolating towards white, for a specific task.
17   * <p>
18   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 30 jan. 2019 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class TaskColorer implements GTUColorer
27  {
28  
29      /** Full. */
30      static final Color HIGH = Color.RED;
31  
32      /** Medium. */
33      static final Color MID = Color.YELLOW;
34  
35      /** Zero. */
36      static final Color LOW = Color.GREEN;
37  
38      /** Fully compensated by anticipation reliance. */
39      static final Color AR = Color.WHITE;
40  
41      /** Not available. */
42      static final Color NA = Color.BLUE;
43  
44      /** Legend. */
45      static final List<LegendEntry> LEGEND;
46  
47      static
48      {
49          LEGEND = new ArrayList<>();
50          LEGEND.add(new LegendEntry(LOW, "low", "low task load"));
51          LEGEND.add(new LegendEntry(MID, "medium", "medium task load"));
52          LEGEND.add(new LegendEntry(HIGH, "high", "high task load"));
53          LEGEND.add(new LegendEntry(AR, "fully anticipation reliant", "task load compensated with anticipation reliance"));
54          LEGEND.add(new LegendEntry(NA, "N/A", "N/A"));
55      }
56  
57      /** Id. */
58      final String id;
59  
60      /**
61       * Constructor.
62       * @param id String; id
63       */
64      public TaskColorer(final String id)
65      {
66          this.id = id;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public Color getColor(final GTU gtu)
72      {
73          if (gtu.getTacticalPlanner().getPerception() instanceof LanePerception)
74          {
75              Mental mental = ((LanePerception) gtu.getTacticalPlanner().getPerception()).getMental();
76              if (mental != null && mental instanceof Fuller)
77              {
78                  for (Task task : ((Fuller) mental).getTasks())
79                  {
80                      if (task.getId().equals(this.id))
81                      {
82                          double taskDemand = task.getTaskDemand();
83                          if (taskDemand == 0.0)
84                          {
85                              return LOW;
86                          }
87                          double anticipationReliance = task.getAnticipationReliance();
88                          Color base = taskDemand < 0.5 ? ColorInterpolator.interpolateColor(LOW, MID, taskDemand / 0.5)
89                                  : ColorInterpolator.interpolateColor(MID, HIGH, (taskDemand - 0.5) / 0.5);
90                          return ColorInterpolator.interpolateColor(base, AR, anticipationReliance / taskDemand);
91                      }
92                  }
93              }
94          }
95          return NA;
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public List<LegendEntry> getLegend()
101     {
102         return LEGEND;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public String toString()
108     {
109         return "Task load (" + this.id + ")";
110     }
111 
112 }