-
Notifications
You must be signed in to change notification settings - Fork 3
/
fvec.js
291 lines (274 loc) · 7.17 KB
/
fvec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict'
module.exports = createFilteredVector
var cubicHermite = require('cubic-hermite')
var bsearch = require('binary-search-bounds')
function clamp(lo, hi, x) {
return Math.min(hi, Math.max(lo, x))
}
function FilteredVector(state0, velocity0, t0) {
this.dimension = state0.length
this.bounds = [ new Array(this.dimension), new Array(this.dimension) ]
for(var i=0; i<this.dimension; ++i) {
this.bounds[0][i] = -Infinity
this.bounds[1][i] = Infinity
}
this._state = state0.slice().reverse()
this._velocity = velocity0.slice().reverse()
this._time = [ t0 ]
this._scratch = [ state0.slice(), state0.slice(), state0.slice(), state0.slice(), state0.slice() ]
}
var proto = FilteredVector.prototype
proto.flush = function(t) {
var idx = bsearch.gt(this._time, t) - 1
if(idx <= 0) {
return
}
this._time.splice(0, idx)
this._state.splice(0, idx * this.dimension)
this._velocity.splice(0, idx * this.dimension)
}
proto.curve = function(t) {
var time = this._time
var n = time.length
var idx = bsearch.le(time, t)
var result = this._scratch[0]
var state = this._state
var velocity = this._velocity
var d = this.dimension
var bounds = this.bounds
if(idx < 0) {
var ptr = d-1
for(var i=0; i<d; ++i, --ptr) {
result[i] = state[ptr]
}
} else if(idx >= n-1) {
var ptr = state.length-1
var tf = t - time[n-1]
for(var i=0; i<d; ++i, --ptr) {
result[i] = state[ptr] + tf * velocity[ptr]
}
} else {
var ptr = d * (idx+1) - 1
var t0 = time[idx]
var t1 = time[idx+1]
var dt = (t1 - t0) || 1.0
var x0 = this._scratch[1]
var x1 = this._scratch[2]
var v0 = this._scratch[3]
var v1 = this._scratch[4]
var steady = true
for(var i=0; i<d; ++i, --ptr) {
x0[i] = state[ptr]
v0[i] = velocity[ptr] * dt
x1[i] = state[ptr+d]
v1[i] = velocity[ptr+d] * dt
steady = steady && (x0[i] === x1[i] && v0[i] === v1[i] && v0[i] === 0.0)
}
if(steady) {
for(var i=0; i<d; ++i) {
result[i] = x0[i]
}
} else {
cubicHermite(x0, v0, x1, v1, (t-t0)/dt, result)
}
}
var lo = bounds[0]
var hi = bounds[1]
for(var i=0; i<d; ++i) {
result[i] = clamp(lo[i], hi[i], result[i])
}
return result
}
proto.dcurve = function(t) {
var time = this._time
var n = time.length
var idx = bsearch.le(time, t)
var result = this._scratch[0]
var state = this._state
var velocity = this._velocity
var d = this.dimension
if(idx >= n-1) {
var ptr = state.length-1
var tf = t - time[n-1]
for(var i=0; i<d; ++i, --ptr) {
result[i] = velocity[ptr]
}
} else {
var ptr = d * (idx+1) - 1
var t0 = time[idx]
var t1 = time[idx+1]
var dt = (t1 - t0) || 1.0
var x0 = this._scratch[1]
var x1 = this._scratch[2]
var v0 = this._scratch[3]
var v1 = this._scratch[4]
var steady = true
for(var i=0; i<d; ++i, --ptr) {
x0[i] = state[ptr]
v0[i] = velocity[ptr] * dt
x1[i] = state[ptr+d]
v1[i] = velocity[ptr+d] * dt
steady = steady && (x0[i] === x1[i] && v0[i] === v1[i] && v0[i] === 0.0)
}
if(steady) {
for(var i=0; i<d; ++i) {
result[i] = 0.0
}
} else {
cubicHermite.derivative(x0, v0, x1, v1, (t-t0)/dt, result)
for(var i=0; i<d; ++i) {
result[i] /= dt
}
}
}
return result
}
proto.lastT = function() {
var time = this._time
return time[time.length-1]
}
proto.stable = function() {
var velocity = this._velocity
var ptr = velocity.length
for(var i=this.dimension-1; i>=0; --i) {
if(velocity[--ptr]) {
return false
}
}
return true
}
proto.jump = function(t) {
var t0 = this.lastT()
var d = this.dimension
if(t < t0 || arguments.length !== d+1) {
return
}
var state = this._state
var velocity = this._velocity
var ptr = state.length-this.dimension
var bounds = this.bounds
var lo = bounds[0]
var hi = bounds[1]
this._time.push(t0, t)
for(var j=0; j<2; ++j) {
for(var i=0; i<d; ++i) {
state.push(state[ptr++])
velocity.push(0)
}
}
this._time.push(t)
for(var i=d; i>0; --i) {
state.push(clamp(lo[i-1], hi[i-1], arguments[i]))
velocity.push(0)
}
}
proto.push = function(t) {
var t0 = this.lastT()
var d = this.dimension
if(t < t0 || arguments.length !== d+1) {
return
}
var state = this._state
var velocity = this._velocity
var ptr = state.length-this.dimension
var dt = t - t0
var bounds = this.bounds
var lo = bounds[0]
var hi = bounds[1]
var sf = (dt > 1e-6) ? 1/dt : 0
this._time.push(t)
for(var i=d; i>0; --i) {
var xc = clamp(lo[i-1], hi[i-1], arguments[i])
state.push(xc)
velocity.push((xc - state[ptr++]) * sf)
}
}
proto.set = function(t) {
var d = this.dimension
if(t < this.lastT() || arguments.length !== d+1) {
return
}
var state = this._state
var velocity = this._velocity
var bounds = this.bounds
var lo = bounds[0]
var hi = bounds[1]
this._time.push(t)
for(var i=d; i>0; --i) {
state.push(clamp(lo[i-1], hi[i-1], arguments[i]))
velocity.push(0)
}
}
proto.move = function(t) {
var t0 = this.lastT()
var d = this.dimension
if(t <= t0 || arguments.length !== d+1) {
return
}
var state = this._state
var velocity = this._velocity
var statePtr = state.length - this.dimension
var bounds = this.bounds
var lo = bounds[0]
var hi = bounds[1]
var dt = t - t0
var sf = (dt > 1e-6) ? 1/dt : 0.0
this._time.push(t)
for(var i=d; i>0; --i) {
var dx = arguments[i]
state.push(clamp(lo[i-1], hi[i-1], state[statePtr++] + dx))
velocity.push(dx * sf)
}
}
proto.idle = function(t) {
var t0 = this.lastT()
if(t < t0) {
return
}
var d = this.dimension
var state = this._state
var velocity = this._velocity
var statePtr = state.length-d
var bounds = this.bounds
var lo = bounds[0]
var hi = bounds[1]
var dt = t - t0
this._time.push(t)
for(var i=d-1; i>=0; --i) {
state.push(clamp(lo[i], hi[i], state[statePtr] + dt * velocity[statePtr]))
velocity.push(0)
statePtr += 1
}
}
function getZero(d) {
var result = new Array(d)
for(var i=0; i<d; ++i) {
result[i] = 0.0
}
return result
}
function createFilteredVector(initState, initVelocity, initTime) {
switch(arguments.length) {
case 0:
return new FilteredVector([0], [0], 0)
case 1:
if(typeof initState === 'number') {
var zero = getZero(initState)
return new FilteredVector(zero, zero, 0)
} else {
return new FilteredVector(initState, getZero(initState.length), 0)
}
case 2:
if(typeof initVelocity === 'number') {
var zero = getZero(initState.length)
return new FilteredVector(initState, zero, +initVelocity)
} else {
initTime = 0
}
case 3:
if(initState.length !== initVelocity.length) {
throw new Error('state and velocity lengths must match')
}
return new FilteredVector(initState, initVelocity, initTime)
}
}