forked from scijs/ndarray-warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp.js
66 lines (60 loc) · 1.59 KB
/
warp.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
'use strict'
var interp = require('ndarray-linear-interpolate')
var cwise = require('cwise')
var do_warp = cwise({
args: ['index', 'array', 'scalar', 'scalar', 'scalar'],
pre: function(idx, dest, func, interp, n) {
this.warped = new Array(n)
},
body: function warpND(idx, dest, func, interp, n) {
func(this.warped, idx)
dest = interp.apply(undefined, this.warped)
}
})
var do_warp_1 = cwise({
args: ['index', 'array', 'scalar', 'scalar', 'scalar'],
pre: function() {
this.warped = [0]
},
body: function warp1D(idx, dest, func, interp, src) {
func(this.warped, idx)
dest = interp(src, this.warped[0])
}
})
var do_warp_2 = cwise({
args: ['index', 'array', 'scalar', 'scalar', 'scalar'],
pre: function() {
this.warped = [0, 0]
},
body: function warp2D(idx, dest, func, interp, src) {
func(this.warped, idx)
dest = interp(src, this.warped[0], this.warped[1])
}
})
var do_warp_3 = cwise({
args: ['index', 'array', 'scalar', 'scalar', 'scalar'],
pre: function() {
this.warped = [0, 0, 0]
},
body: function warp3D(idx, dest, func, interp, src) {
func(this.warped, idx)
dest = interp(src, this.warped[0], this.warped[1], this.warped[2])
}
})
module.exports = function warp(dest, src, func) {
switch(src.shape.length) {
case 1:
do_warp_1(dest, func, interp.d1, src)
break
case 2:
do_warp_2(dest, func, interp.d2, src)
break
case 3:
do_warp_3(dest, func, interp.d3, src)
break
default:
do_warp(dest, func, interp.bind(undefined, src), src.shape.length)
break
}
return dest
}