View Javadoc
1   package org.opentrafficsim.road.gtu.animation;
2   
3   import java.awt.Color;
4   import java.io.Serializable;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import org.djunits.value.vdouble.scalar.Duration;
9   import org.opentrafficsim.base.parameters.ParameterTypes;
10  import org.opentrafficsim.base.parameters.Parameters;
11  import org.opentrafficsim.core.gtu.GTU;
12  import org.opentrafficsim.core.gtu.animation.ColorInterpolator;
13  import org.opentrafficsim.core.gtu.animation.GTUColorer;
14  
15  /**
16   * Color on a scale from Tmin to Tmax parameters, or two given limits.
17   * <p>
18   * Copyright (c) 2013-2018 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 20 apr. 2017 <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 DesiredHeadwayColorer implements GTUColorer, Serializable
27  {
28  
29      /** */
30      private static final long serialVersionUID = 20170420L;
31  
32      /** Minimum value on input scale. */
33      public final Duration tMin;
34  
35      /** Maximum value on input scale. */
36      public final Duration tMax;
37  
38      /** The legend. */
39      private final List<LegendEntry> legend;
40  
41      /** Low color. */
42      private static final Color LOW = Color.RED;
43  
44      /** Middle color. */
45      private static final Color MIDDLE = Color.YELLOW;
46  
47      /** High color. */
48      private static final Color HIGH = Color.GREEN;
49  
50      /** Unknown color. */
51      protected static final Color UNKNOWN = Color.WHITE;
52  
53      /**
54       * Constructor using Tmin and Tmax parameters.
55       */
56      public DesiredHeadwayColorer()
57      {
58          this.legend = new ArrayList<>(4);
59          this.legend.add(new LegendEntry(LOW, "Tmin", "Tmin"));
60          this.legend.add(new LegendEntry(MIDDLE, "Mean", "Mean"));
61          this.legend.add(new LegendEntry(HIGH, "Tmax", "Tmax"));
62          this.legend.add(new LegendEntry(UNKNOWN, "Unknown", "Unknown"));
63          this.tMin = null;
64          this.tMax = null;
65      }
66  
67      /**
68       * Constructor using input Tmin and Tmax.
69       * @param tMin Duration; minimum headway
70       * @param tMax Duration; maximum headway
71       */
72      public DesiredHeadwayColorer(final Duration tMin, final Duration tMax)
73      {
74          String format = "%.2fs";
75          this.legend = new ArrayList<>(4);
76          this.legend.add(new LegendEntry(LOW, String.format(format, tMin.si), "Tmin"));
77          this.legend.add(new LegendEntry(MIDDLE, String.format(format, (tMin.si + tMax.si) / 2.0), "Mean"));
78          this.legend.add(new LegendEntry(HIGH, String.format(format, tMax.si), "Tmax"));
79          this.legend.add(new LegendEntry(UNKNOWN, "Unknown", "Unknown"));
80          this.tMin = tMin;
81          this.tMax = tMax;
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public final Color getColor(final GTU gtu)
87      {
88          Parameters params = gtu.getParameters();
89          Double minT;
90          Double maxT;
91          if (this.tMin == null)
92          {
93              minT = params.getParameterOrNull(ParameterTypes.TMIN).si;
94              maxT = params.getParameterOrNull(ParameterTypes.TMAX).si;
95          }
96          else
97          {
98              minT = this.tMin.si;
99              maxT = this.tMax.si;
100         }
101         Double t = params.getParameterOrNull(ParameterTypes.T).si;
102         if (minT == null || maxT == null || t == null)
103         {
104             return UNKNOWN;
105         }
106         if (t <= minT)
107         {
108             return LOW;
109         }
110         if (t >= maxT)
111         {
112             return HIGH;
113         }
114         double tMean = (minT + maxT) / 2.0;
115         if (t < tMean)
116         {
117             return ColorInterpolator.interpolateColor(LOW, MIDDLE, (t - minT) / (tMean - minT));
118         }
119         return ColorInterpolator.interpolateColor(MIDDLE, HIGH, (t - tMean) / (maxT - tMean));
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public final List<LegendEntry> getLegend()
125     {
126         return this.legend;
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public final String toString()
132     {
133         return "Desired headway";
134     }
135 
136 }