1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function Interpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
|
24 |
|
25 | this.parameterPositions = parameterPositions;
|
26 | this._cachedIndex = 0;
|
27 |
|
28 | this.resultBuffer = resultBuffer !== undefined ?
|
29 | resultBuffer : new sampleValues.constructor( sampleSize );
|
30 | this.sampleValues = sampleValues;
|
31 | this.valueSize = sampleSize;
|
32 |
|
33 | }
|
34 |
|
35 | Object.assign( Interpolant.prototype, {
|
36 |
|
37 | evaluate: function ( t ) {
|
38 |
|
39 | var pp = this.parameterPositions,
|
40 | i1 = this._cachedIndex,
|
41 |
|
42 | t1 = pp[ i1 ],
|
43 | t0 = pp[ i1 - 1 ];
|
44 |
|
45 | validate_interval: {
|
46 |
|
47 | seek: {
|
48 |
|
49 | var right;
|
50 |
|
51 | linear_scan: {
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | forward_scan: if ( ! ( t < t1 ) ) {
|
58 |
|
59 | for ( var giveUpAt = i1 + 2; ; ) {
|
60 |
|
61 | if ( t1 === undefined ) {
|
62 |
|
63 | if ( t < t0 ) break forward_scan;
|
64 |
|
65 |
|
66 |
|
67 | i1 = pp.length;
|
68 | this._cachedIndex = i1;
|
69 | return this.afterEnd_( i1 - 1, t, t0 );
|
70 |
|
71 | }
|
72 |
|
73 | if ( i1 === giveUpAt ) break;
|
74 |
|
75 | t0 = t1;
|
76 | t1 = pp[ ++ i1 ];
|
77 |
|
78 | if ( t < t1 ) {
|
79 |
|
80 |
|
81 | break seek;
|
82 |
|
83 | }
|
84 |
|
85 | }
|
86 |
|
87 |
|
88 | right = pp.length;
|
89 | break linear_scan;
|
90 |
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 | if ( ! ( t >= t0 ) ) {
|
96 |
|
97 |
|
98 |
|
99 | var t1global = pp[ 1 ];
|
100 |
|
101 | if ( t < t1global ) {
|
102 |
|
103 | i1 = 2;
|
104 | t0 = t1global;
|
105 |
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | for ( var giveUpAt = i1 - 2; ; ) {
|
111 |
|
112 | if ( t0 === undefined ) {
|
113 |
|
114 |
|
115 |
|
116 | this._cachedIndex = 0;
|
117 | return this.beforeStart_( 0, t, t1 );
|
118 |
|
119 | }
|
120 |
|
121 | if ( i1 === giveUpAt ) break;
|
122 |
|
123 | t1 = t0;
|
124 | t0 = pp[ -- i1 - 1 ];
|
125 |
|
126 | if ( t >= t0 ) {
|
127 |
|
128 |
|
129 | break seek;
|
130 |
|
131 | }
|
132 |
|
133 | }
|
134 |
|
135 |
|
136 | right = i1;
|
137 | i1 = 0;
|
138 | break linear_scan;
|
139 |
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 | break validate_interval;
|
145 |
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 | while ( i1 < right ) {
|
151 |
|
152 | var mid = ( i1 + right ) >>> 1;
|
153 |
|
154 | if ( t < pp[ mid ] ) {
|
155 |
|
156 | right = mid;
|
157 |
|
158 | } else {
|
159 |
|
160 | i1 = mid + 1;
|
161 |
|
162 | }
|
163 |
|
164 | }
|
165 |
|
166 | t1 = pp[ i1 ];
|
167 | t0 = pp[ i1 - 1 ];
|
168 |
|
169 |
|
170 |
|
171 | if ( t0 === undefined ) {
|
172 |
|
173 | this._cachedIndex = 0;
|
174 | return this.beforeStart_( 0, t, t1 );
|
175 |
|
176 | }
|
177 |
|
178 | if ( t1 === undefined ) {
|
179 |
|
180 | i1 = pp.length;
|
181 | this._cachedIndex = i1;
|
182 | return this.afterEnd_( i1 - 1, t0, t );
|
183 |
|
184 | }
|
185 |
|
186 | }
|
187 |
|
188 | this._cachedIndex = i1;
|
189 |
|
190 | this.intervalChanged_( i1, t0, t1 );
|
191 |
|
192 | }
|
193 |
|
194 | return this.interpolate_( i1, t0, t, t1 );
|
195 |
|
196 | },
|
197 |
|
198 | settings: null,
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | DefaultSettings_: {},
|
204 |
|
205 | getSettings_: function () {
|
206 |
|
207 | return this.settings || this.DefaultSettings_;
|
208 |
|
209 | },
|
210 |
|
211 | copySampleValue_: function ( index ) {
|
212 |
|
213 |
|
214 |
|
215 | var result = this.resultBuffer,
|
216 | values = this.sampleValues,
|
217 | stride = this.valueSize,
|
218 | offset = index * stride;
|
219 |
|
220 | for ( var i = 0; i !== stride; ++ i ) {
|
221 |
|
222 | result[ i ] = values[ offset + i ];
|
223 |
|
224 | }
|
225 |
|
226 | return result;
|
227 |
|
228 | },
|
229 |
|
230 |
|
231 |
|
232 | interpolate_: function ( /* i1, t0, t, t1 */ ) {
|
233 |
|
234 | throw new Error( 'call to abstract method' );
|
235 |
|
236 |
|
237 | },
|
238 |
|
239 | intervalChanged_: function ( /* i1, t0, t1 */ ) {
|
240 |
|
241 |
|
242 |
|
243 | }
|
244 |
|
245 | } );
|
246 |
|
247 |
|
248 | Object.assign( Interpolant.prototype, {
|
249 |
|
250 |
|
251 | beforeStart_: Interpolant.prototype.copySampleValue_,
|
252 |
|
253 |
|
254 | afterEnd_: Interpolant.prototype.copySampleValue_,
|
255 |
|
256 | } );
|
257 |
|
258 |
|
259 | export { Interpolant };
|