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