View Javadoc
1   package org.opentrafficsim.road.gtu.strategical.od;
2   
3   import org.djunits.unit.DurationUnit;
4   import org.djunits.unit.FrequencyUnit;
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Frequency;
7   
8   /**
9    * Interpolation of demand.
10   * <p>
11   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
13   * <p>
14   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 15, 2016 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18   */
19  public enum Interpolation
20  {
21  
22      /** Stepwise interpolation of demand. */
23      STEPWISE
24      {
25          /** {@inheritDoc} */
26          @Override
27          Frequency interpolate(final Frequency frequency0, final Duration time0, final Frequency frequency1,
28                  final Duration time1, final Duration time)
29          {
30              return frequency0;
31          }
32  
33          /** {@inheritDoc} */
34          @Override
35          int integrate(final Frequency frequency0, final Duration time0, final Frequency frequency1, final Duration time1)
36          {
37              return (int) (frequency0.getInUnit(FrequencyUnit.PER_HOUR)
38                      * (time1.getInUnit(DurationUnit.HOUR) - time0.getInUnit(DurationUnit.HOUR)));
39          }
40      },
41  
42      /** Linear interpolation of demand. */
43      LINEAR
44      {
45          /** {@inheritDoc} */
46          @Override
47          Frequency interpolate(final Frequency frequency0, final Duration time0, final Frequency frequency1,
48                  final Duration time1, final Duration time)
49          {
50              return Frequency.interpolate(frequency0, frequency1, (time.si - time0.si) / (time1.si - time0.si));
51          }
52  
53          /** {@inheritDoc} */
54          @Override
55          int integrate(final Frequency frequency0, final Duration time0, final Frequency frequency1, final Duration time1)
56          {
57              return (int) (0.5 * (frequency0.getInUnit(FrequencyUnit.PER_HOUR) + frequency1.getInUnit(FrequencyUnit.PER_HOUR))
58                      * (time1.getInUnit(DurationUnit.HOUR) - time0.getInUnit(DurationUnit.HOUR)));
59          }
60      };
61  
62      /**
63       * Interpolate between given frequencies.
64       * @param frequency0 frequency at {@code time0}
65       * @param time0 time of {@code frequency0} (&le; {@code time})
66       * @param frequency1 frequency at {@code time1}
67       * @param time1 time of {@code frequency1} (&gt; {@code time})
68       * @param time {@code time0} &le; {@code time} &lt; {@code time1}
69       * @return interpolated frequency
70       */
71      abstract Frequency interpolate(Frequency frequency0, Duration time0, Frequency frequency1, Duration time1, Duration time);
72  
73      /**
74       * Integrates to the number of trips in given period.
75       * @param frequency0 frequency at {@code time0}
76       * @param time0 time of {@code frequency0} (&le; {@code time})
77       * @param frequency1 frequency at {@code time1}
78       * @param time1 time of {@code frequency1} (&gt; {@code time})
79       * @return number of trips in given period
80       */
81      abstract int integrate(Frequency frequency0, Duration time0, Frequency frequency1, Duration time1);
82  
83      /**
84       * @return whether this is step-wise interpolation
85       */
86      public boolean isStepWise()
87      {
88          return this.equals(STEPWISE);
89      }
90  
91      /**
92       * @return whether this is linear interpolation
93       */
94      public boolean isLinear()
95      {
96          return this.equals(LINEAR);
97      }
98  
99  }