-
Notifications
You must be signed in to change notification settings - Fork 0
/
spline.cpp
284 lines (229 loc) · 6.54 KB
/
spline.cpp
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
#include <spline.h>
// class Spline
Spline::Spline()
{
}
Spline::Spline(vector<Vector3f>& vlist)
{
_vector_list = vlist;
}
Vector3f Spline::getSpline(float t, string spline_t, bool closed)
{
if(spline_t == "CATMULL_ROM")
{
return _catmullRom(t, closed);
}
else if(spline_t == "BSPLINE")
{
return _bspline(t);
}
else
{
cout << "Wrong spline type provided." << endl;
}
Vector3f default_vec(0.0,0.0,0.0);
return default_vec;
}
/*
Spline::~Spline()
{
for(int i = 0; i < _vector_list.size(); i++)
delete &(_vector_list[i]);
delete &(_vector_list);
}
*/
Spline::~Spline()
{
}
Vector3f Spline::_catmullRom(float t, bool closed)
{
if(t < 0.0) t = 0.0;
int curve_index = (int) t;
float curve_value = t - (float)curve_index;
int section_num = _vector_list.size();
if(t >= (float)section_num)
{
curve_index = section_num - 1;
curve_value = 1.0;
}
if(!closed)
{
if(curve_index < 1)
{
curve_index = 1;
curve_value = 0.0;
}
else if (curve_index >= section_num-2)
{
curve_index = section_num-3;
curve_value = 1.0;
}
}
// Matrix multiplication for CATMULL_ROM
Vector3f b0 = _vector_list[curve_index];
Vector3f b3 = _vector_list[(curve_index+1) % section_num];
Vector3f p_n = _vector_list[(curve_index+2) % section_num];
Vector3f p_p = _vector_list[(curve_index-1+section_num) % section_num];
Vector3f b1 = b0 + ((1.0/6.0) * (b3 - p_p));
Vector3f b2 = b3 - ((1.0/6.0) * (p_n - b0));
Matrix<float, 3, 4> control_point_mat;
control_point_mat.col(0) << b0;
control_point_mat.col(1) << b1;
control_point_mat.col(2) << b2;
control_point_mat.col(3) << b3;
Matrix4f bernstein_mat;
bernstein_mat << -1.0, 3.0, -3.0, 1.0,
3.0, -6.0, 3.0, 0.0,
-3.0, 3.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0;
Vector4f monomial(curve_value*curve_value*curve_value,
curve_value*curve_value,
curve_value,
1.0
);
Vector3f result = (control_point_mat * bernstein_mat) * monomial;
return result;
}
Vector3f Spline::_bspline(float t)
{
if(t < 0.0) t = 0.0;
int curve_index = (int) t;
float curve_value = t - (float)curve_index;
int section_num = _vector_list.size();
if(t >= (float)section_num)
{
curve_index = section_num - 1;
curve_value = 1.0;
}
// Matrix multiplication for BSPLINE
Vector3f b0 = _vector_list[curve_index];
Vector3f b1 = _vector_list[(curve_index+1) % section_num];
Vector3f b2 = _vector_list[(curve_index+2) % section_num];
Vector3f b3 = _vector_list[(curve_index+3) % section_num];
Matrix<float, 3, 4> control_point_mat;
control_point_mat.col(0) << b0;
control_point_mat.col(1) << b1;
control_point_mat.col(2) << b2;
control_point_mat.col(3) << b3;
Matrix4f basis_mat;
basis_mat << -1.0, 3.0, -3.0, 1.0,
3.0, -6.0, 0.0, 4.0,
-3.0, 3.0, 3.0, 1.0,
1.0, 0.0, 0.0, 0.0;
Vector4f monomial(curve_value*curve_value*curve_value,
curve_value*curve_value,
curve_value,
1.0
);
Vector3f result = (control_point_mat * basis_mat) * monomial;
return result;
}
// class FloatSpline
FloatSpline::FloatSpline(vector<float>& vlist)
{
_val_list = vlist;
}
float FloatSpline::getSpline(float t, string spline_t, bool closed)
{
return _catmullRom(t);
}
float FloatSpline::_catmullRom(float t, bool closed)
{
int section_num = _val_list.size();
if(t < 0.0) t = 0.0;
int curve_index = (int) t;
float curve_value = t - (float)curve_index;
if(t >= (float)section_num)
{
curve_index = section_num - 1;
curve_value = 1.0;
}
if(!closed)
{
if(curve_index < 1)
{
curve_index = 1;
curve_value = 0.0;
}
else if (curve_index >= section_num-2)
{
curve_index = section_num-3;
curve_value = 1.0;
}
}
float b0 = _val_list[curve_index];
float b3 = _val_list[(curve_index+1) % section_num];
float p_n = _val_list[(curve_index+2) % section_num];
float p_p = _val_list[(curve_index-1+section_num) % section_num];
float b1 = b0 + ((1.0/6.0) * (b3 - p_p));
float b2 = b3 - ((1.0/6.0) * (p_n - b0));
Matrix<float, 1, 4> control_point_mat;
control_point_mat << b0, b1, b2, b3;
Matrix4f bernstein_mat;
bernstein_mat << -1.0, 3.0, -3.0, 1.0,
3.0, -6.0, 3.0, 0.0,
-3.0, 3.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0;
Vector4f monomial(curve_value*curve_value*curve_value,
curve_value*curve_value,
curve_value,
1.0
);
Matrix<float, 1, 1> result = (control_point_mat * bernstein_mat) * monomial;
return result[0];
}
// class QuaternionSpline
QuaternionSpline::QuaternionSpline(vector<Quaternionf>& vlist)
{
_quat_list = vlist;
}
Quaternionf QuaternionSpline::getSpline(float t, string spline_t, bool closed)
{
return _catmullRom(t);
}
Quaternionf QuaternionSpline::_catmullRom(float t, bool closed)
{
int section_num = _quat_list.size();
if(t < 0.0) t = 0.0;
int curve_index = (int) t;
float curve_value = t - (float)curve_index;
if(curve_index < 1)
{
curve_index = 1;
curve_value = 0.0;
}
else if (curve_index >= section_num-2)
{
curve_index = section_num-3;
curve_value = 1.0;
}
float _norm = 0.0;
Quaternionf result;
do {
Quaternionf b0 = _quat_list[curve_index];
Quaternionf b3 = _quat_list[curve_index+1];
Quaternionf q_n = _quat_list[curve_index+2];
Quaternionf q_p = _quat_list[curve_index-1];
Quaternionf b1 = b0 * q_p.conjugate() * safe_slerp(1.0/6.0, q_p, b3);
Quaternionf b2 = b3 * q_n.conjugate() * safe_slerp(1.0/6.0, q_n, b0);
// cout << "norms : " << b0.norm() << ", " << b1.norm() << ", " << b2.norm() << ", " << b3.norm() << endl;
// De Casteljau Algorithm
Quaternionf l1_0 = safe_slerp(curve_value, b0, b1);
Quaternionf l1_1 = safe_slerp(curve_value, b1, b2);
Quaternionf l1_2 = safe_slerp(curve_value, b2, b3);
Quaternionf l2_0 = safe_slerp(curve_value, l1_0, l1_1);
Quaternionf l2_1 = safe_slerp(curve_value, l1_1, l1_2);
result = safe_slerp(curve_value, l2_0, l2_1);
_norm = result.norm();
// cout << "norm : " << _norm << endl;
} while (_norm != _norm); // checks NaN value
return result;
}
Quaternionf QuaternionSpline::safe_slerp(float t, Quaternionf& from, Quaternionf& to)
{
Quaternionf checker = from.inverse() * to;
if(checker.w() > 0.9999)
return from;
else
return from.slerp(t, to);
}