1 2 /** 3 * @class 4 */ 5 function Interpolation(){ 6 7 } 8 9 Interpolation.NEAREST = 0; 10 Interpolation.LINEAR = 1; 11 //Interpolation.SPLINE = 2; 12 Interpolation.BSPLINE = 3; 13 14 /** 15 * @class 16 */ 17 function BSpline(){ 18 19 } 20 21 /** 22 * B-Spline coefficient matrix 23 */ 24 BSpline.BM = [ -1, 3, -3, 1, 3, -6, 3, 0, -3, 0, 3, 0, 1, 4, 1, 0 ]; 25 26 27 /** 28 * Takes a set of x/y observations, evaluates the b-spline and returns samples of the b-spline 29 * 30 * @param x x coordinates array 31 * @param y y coordinates array 32 */ 33 BSpline.evaluate = function(x, y){ 34 var n = x.length; 35 var nCurves = n + 1; 36 var bm = new Float32Array(16); 37 var steps = 15; 38 var numSamples = nCurves * (steps+1); 39 var ex = new Float32Array(numSamples); 40 var ey = new Float32Array(numSamples); 41 for(var i = 0; i < bm.length; i++){ 42 bm[i] = BSpline.BM[i] / 6; 43 } 44 45 var k = 0; 46 for(var i = 0; i < nCurves; i++){ 47 var ip1 = Math.max(i-2, 0); 48 var ip2 = Math.max(i-1, 0); 49 var ip3 = Math.min(i, n-1); 50 var ip4 = Math.min(i+1, n-1); 51 var p1 = [x[ip1], y[ip1] ]; 52 var p2 = [x[ip2], y[ip2] ]; 53 var p3 = [x[ip3], y[ip3] ]; 54 var p4 = [x[ip4], y[ip4] ]; 55 56 for(var j = 0; j <= steps; j++){ 57 var t = j/10; 58 var b = [ t*t*t, t*t, t, 1]; 59 60 var p1x = bm[0]*p1[0] + bm[1]*p2[0] + bm[2]*p3[0] + bm[3]*p4[0]; 61 var p1y = bm[0]*p1[1] + bm[1]*p2[1] + bm[2]*p3[1] + bm[3]*p4[1]; 62 63 var p2x = bm[4]*p1[0] + bm[5]*p2[0] + bm[6]*p3[0] + bm[7]*p4[0]; 64 var p2y = bm[4]*p1[1] + bm[5]*p2[1] + bm[6]*p3[1] + bm[7]*p4[1]; 65 66 var p3x = bm[8]*p1[0] + bm[9]*p2[0] + bm[10]*p3[0] + bm[11]*p4[0]; 67 var p3y = bm[8]*p1[1] + bm[9]*p2[1] + bm[10]*p3[1] + bm[11]*p4[1]; 68 69 var p4x = bm[12]*p1[0] + bm[13]*p2[0] + bm[14]*p3[0] + bm[15]*p4[0]; 70 var p4y = bm[12]*p1[1] + bm[13]*p2[1] + bm[14]*p3[1] + bm[15]*p4[1]; 71 72 var pcx = p1x*b[0] + p2x*b[1] + p3x*b[2] + p4x*b[3]; 73 var pcy = p1y*b[0] + p2y*b[1] + p3y*b[2] + p4y*b[3]; 74 75 ex[k] = pcx; 76 ey[k] = pcy; 77 k++; 78 } 79 } 80 81 return {"x": ex, "y": ey}; 82 83 };