1 package org.opentrafficsim.road.od;
2
3 import org.djunits.unit.FrequencyUnit;
4 import org.djunits.unit.TimeUnit;
5 import org.djunits.value.ValueRuntimeException;
6 import org.djunits.value.vdouble.scalar.Frequency;
7 import org.djunits.value.vdouble.scalar.Time;
8 import org.djunits.value.vdouble.vector.FrequencyVector;
9 import org.djunits.value.vdouble.vector.TimeVector;
10
11
12
13
14
15
16
17
18
19
20
21 public enum Interpolation
22 {
23
24
25 STEPWISE
26 {
27
28 @Override
29 Frequency interpolate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1,
30 final Time time)
31 {
32 return frequency0;
33 }
34
35
36 @Override
37 int integrate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1)
38 {
39 return (int) (frequency0.getInUnit(FrequencyUnit.PER_HOUR)
40 * (time1.getInUnit(TimeUnit.BASE_HOUR) - time0.getInUnit(TimeUnit.BASE_HOUR)));
41 }
42
43
44 @Override
45 public String toString()
46 {
47 return "STEPWISE";
48 }
49 },
50
51
52 LINEAR
53 {
54
55 @Override
56 Frequency interpolate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1,
57 final Time time)
58 {
59 return Frequency.interpolate(frequency0, frequency1, (time.si - time0.si) / (time1.si - time0.si));
60 }
61
62
63 @Override
64 int integrate(final Frequency frequency0, final Time time0, final Frequency frequency1, final Time time1)
65 {
66 return (int) (0.5 * (frequency0.getInUnit(FrequencyUnit.PER_HOUR) + frequency1.getInUnit(FrequencyUnit.PER_HOUR))
67 * (time1.getInUnit(TimeUnit.BASE_HOUR) - time0.getInUnit(TimeUnit.BASE_HOUR)));
68 }
69
70
71 @Override
72 public String toString()
73 {
74 return "LINEAR";
75 }
76 };
77
78
79
80
81
82
83
84
85
86
87 abstract Frequency interpolate(Frequency frequency0, Time time0, Frequency frequency1, Time time1, Time time);
88
89
90
91
92
93
94
95
96
97 abstract int integrate(Frequency frequency0, Time time0, Frequency frequency1, Time time1);
98
99
100
101
102 public boolean isStepWise()
103 {
104 return this.equals(STEPWISE);
105 }
106
107
108
109
110 public boolean isLinear()
111 {
112 return this.equals(LINEAR);
113 }
114
115
116
117
118
119
120
121
122
123 public final Frequency interpolateVector(final Time time, final FrequencyVector demandVector, final TimeVector timeVector,
124 final boolean sliceStart)
125 {
126 try
127 {
128
129
130
131
132
133 if (timeVector.size() == 0 || (sliceStart ? time.lt(timeVector.get(0)) : time.le(timeVector.get(0))) || (sliceStart
134 ? time.ge(timeVector.get(timeVector.size() - 1)) : time.gt(timeVector.get(timeVector.size() - 1))))
135 {
136 return new Frequency(0.0, FrequencyUnit.PER_HOUR);
137 }
138
139 for (int i = 0; i < timeVector.size() - 1; i++)
140 {
141
142
143
144 if (sliceStart ? timeVector.get(i + 1).gt(time) : timeVector.get(i + 1).ge(time))
145 {
146 return interpolate(demandVector.get(i), timeVector.get(i), demandVector.get(i + 1), timeVector.get(i + 1),
147 time);
148 }
149 }
150 }
151 catch (ValueRuntimeException ve)
152 {
153
154 throw new RuntimeException("Index out of bounds.", ve);
155 }
156
157 throw new RuntimeException("Demand interpolation failed.");
158 }
159
160 }