This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBSpline.js
125 lines (118 loc) · 3.96 KB
/
BSpline.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
var BSpline = function(points,degree,copy){
if(copy){
this.points = []
for(var i = 0;i<points.length;i++){
this.points.push(points[i]);
}
}else{
this.points = points;
}
this.degree = degree;
this.dimension = points[0].length;
if(degree == 2){
this.baseFunc = this.basisDeg2;
this.baseFuncRangeInt = 2;
}else if(degree == 3){
this.baseFunc = this.basisDeg3;
this.baseFuncRangeInt = 2;
}else if(degree == 4){
this.baseFunc = this.basisDeg4;
this.baseFuncRangeInt = 3;
}else if(degree == 5){
this.baseFunc = this.basisDeg5;
this.baseFuncRangeInt = 3;
}
};
BSpline.prototype.seqAt = function(dim){
var points = this.points;
var margin = this.degree + 1;
return function(n){
if(n < margin){
return points[0][dim];
}else if(points.length + margin <= n){
return points[points.length-1][dim];
}else{
return points[n-margin][dim];
}
};
};
BSpline.prototype.basisDeg2 = function(x){
if(-0.5 <= x && x < 0.5){
return 0.75 - x*x;
}else if(0.5 <= x && x <= 1.5){
return 1.125 + (-1.5 + x/2.0)*x;
}else if(-1.5 <= x && x < -0.5){
return 1.125 + (1.5 + x/2.0)*x;
}else{
return 0;
}
};
BSpline.prototype.basisDeg3 = function(x){
if(-1 <= x && x < 0){
return 2.0/3.0 + (-1.0 - x/2.0)*x*x;
}else if(1 <= x && x <= 2){
return 4.0/3.0 + x*(-2.0 + (1.0 - x/6.0)*x);
}else if(-2 <= x && x < -1){
return 4.0/3.0 + x*(2.0 + (1.0 + x/6.0)*x);
}else if(0 <= x && x < 1){
return 2.0/3.0 + (-1.0 + x/2.0)*x*x;
}else{
return 0;
}
};
BSpline.prototype.basisDeg4 = function(x){
if(-1.5 <= x && x < -0.5){
return 55.0/96.0 + x*(-(5.0/24.0) + x*(-(5.0/4.0) + (-(5.0/6.0) - x/6.0)*x));
}else if(0.5 <= x && x < 1.5){
return 55.0/96.0 + x*(5.0/24.0 + x*(-(5.0/4.0) + (5.0/6.0 - x/6.0)*x));
}else if(1.5 <= x && x <= 2.5){
return 625.0/384.0 + x*(-(125.0/48.0) + x*(25.0/16.0 + (-(5.0/12.0) + x/24.0)*x));
}else if(-2.5 <= x && x <= -1.5){
return 625.0/384.0 + x*(125.0/48.0 + x*(25.0/16.0 + (5.0/12.0 + x/24.0)*x));
}else if(-1.5 <= x && x < 1.5){
return 115.0/192.0 + x*x*(-(5.0/8.0) + x*x/4.0);
}else{
return 0;
}
};
BSpline.prototype.basisDeg5 = function(x){
if(-2 <= x && x < -1){
return 17.0/40.0 + x*(-(5.0/8.0) + x*(-(7.0/4.0) + x*(-(5.0/4.0) + (-(3.0/8.0) - x/24.0)*x)));
}else if(0 <= x && x < 1){
return 11.0/20.0 + x*x*(-(1.0/2.0) + (1.0/4.0 - x/12.0)*x*x);
}else if(2 <= x && x <= 3){
return 81.0/40.0 + x*(-(27.0/8.0) + x*(9.0/4.0 + x*(-(3.0/4.0) + (1.0/8.0 - x/120.0)*x)));
}else if(-3 <= x && x < -2){
return 81.0/40.0 + x*(27.0/8.0 + x*(9.0/4.0 + x*(3.0/4.0 + (1.0/8.0 + x/120.0)*x)));
}else if(1 <= x && x < 2){
return 17.0/40.0 + x*(5.0/8.0 + x*(-(7.0/4.0) + x*(5.0/4.0 + (-(3.0/8.0) + x/24.0)*x)));
}else if(-1 <= x && x < 0){
return 11.0/20.0 + x*x*(-(1.0/2.0) + (1.0/4.0 + x/12.0)*x*x);
}else{
return 0;
}
};
BSpline.prototype.getInterpol = function(seq,t){
var f = this.baseFunc;
var rangeInt = this.baseFuncRangeInt;
var tInt = Math.floor(t);
var result = 0;
for(var i = tInt - rangeInt;i <= tInt + rangeInt;i++){
result += seq(i)*f(t-i);
}
return result;
};
BSpline.prototype.calcAt = function(t){
t = t*((this.degree+1)*2+this.points.length);//t must be in [0,1]
if(this.dimension == 2){
return [this.getInterpol(this.seqAt(0),t),this.getInterpol(this.seqAt(1),t)];
}else if(this.dimension == 3){
return [this.getInterpol(this.seqAt(0),t),this.getInterpol(this.seqAt(1),t),this.getInterpol(this.seqAt(2),t)];
}else{
var res = [];
for(var i = 0;i<this.dimension;i++){
res.push(this.getInterpol(this.seqAt(i),t));
}
return res;
}
};