View Javadoc
1   package org.opentrafficsim.road.gtu.colorer;
2   
3   import java.awt.Color;
4   import java.io.Serializable;
5   import java.util.ArrayList;
6   import java.util.Collections;
7   import java.util.LinkedHashMap;
8   import java.util.List;
9   import java.util.Map;
10  
11  import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
12  import org.opentrafficsim.core.animation.gtu.colorer.IDGTUColorer;
13  import org.opentrafficsim.core.gtu.GTU;
14  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
15  import org.opentrafficsim.road.gtu.lane.perception.mental.Fuller;
16  import org.opentrafficsim.road.gtu.lane.perception.mental.Mental;
17  import org.opentrafficsim.road.gtu.lane.perception.mental.Task;
18  import org.opentrafficsim.road.gtu.lane.perception.mental.sdm.DefaultDistraction;
19  
20  /**
21   * <p>
22   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 6 nov. 2018 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
29   */
30  public class DistractionColorer implements GTUColorer, Serializable
31  {
32  
33      /** */
34      private static final long serialVersionUID = 20181106L;
35  
36      /** None color. */
37      private static final Color NONE = Color.MAGENTA.darker().darker();
38  
39      /** The legend. */
40      private final ArrayList<LegendEntry> legend = new ArrayList<>();
41  
42      /** Colors. */
43      private final Map<String, Color> colors = new LinkedHashMap<>();
44  
45      /**
46       * Constructor.
47       * @param distractions DefaultDistraction...; DefaultDistraction... distractions to color
48       */
49      public DistractionColorer(final DefaultDistraction... distractions)
50      {
51          this.legend.add(new LegendEntry(NONE, "None", "No distraction"));
52          for (int i = 0; i < distractions.length; i++)
53          {
54              Color c = IDGTUColorer.LEGEND.get(i % 10).getColor();
55              this.colors.put(distractions[i].getId(), c);
56              this.legend.add(new LegendEntry(c, distractions[i].getDescription(), distractions[i].getDescription()));
57          }
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public Color getColor(final GTU gtu)
63      {
64          if (gtu.getTacticalPlanner().getPerception() instanceof LanePerception)
65          {
66              Mental mental = ((LanePerception) gtu.getTacticalPlanner().getPerception()).getMental();
67              if (mental != null && mental instanceof Fuller)
68              {
69                  for (Task task : ((Fuller) mental).getTasks())
70                  {
71                      String id = task.getId();
72                      if (this.colors.containsKey(id))
73                      {
74                          return this.colors.get(id);
75                      }
76                  }
77              }
78          }
79          return NONE;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final List<LegendEntry> getLegend()
85      {
86          return Collections.unmodifiableList(this.legend);
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final String toString()
92      {
93          return "Distraction";
94      }
95  
96  }