-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMinJaeMethod.cpp
More file actions
311 lines (253 loc) · 9.8 KB
/
MinJaeMethod.cpp
File metadata and controls
311 lines (253 loc) · 9.8 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "MinJaeMethod.h"
void MinJaeMethod::init()
{
// 2. construct basis T-mesh
//add 0 and 1
for (int j = 0; j <= curves[0].n; j++) {
double vknot = curves[0].knots(j + 2);
if (j == 1) { vknot = 0.0001; }
if (j == curves[0].n - 1) { vknot = 0.9999; }
tspline.insert_helper(0.0, vknot, false);
auto node = tspline.get_node(0.0, vknot);
(node->data).fromVectorXd(curves[0].controlPw.row(j));
/*(node->data).output(cout);
cout << endl;*/
}
for (int i = 0; i < curves_num; i++) {
for (int j = 0; j <= curves[i].n; j++) {
double vknot = curves[i].knots(j + 2);
if (j == 1) { vknot = 0.0001; }
if (j == curves[i].n - 1) { vknot = 0.9999; }
tspline.insert_helper(s_knots(i), vknot, false);
auto node = tspline.get_node(s_knots(i), vknot);
(node->data).fromVectorXd(curves[i].controlPw.row(j));
/*(node->data).output(cout);
cout << endl;*/
//merge_all();
}
}
for (int j = 0; j <= curves[curves_num - 1].n; j++) {
double vknot = curves[curves_num - 1].knots(j + 2);
if (j == 1) { vknot = 0.0001; }
if (j == curves[curves_num - 1].n - 1) { vknot = 0.9999; }
tspline.insert_helper(1.0, vknot, false);
auto node = tspline.get_node(1.0, vknot);
(node->data).fromVectorXd(curves[curves_num - 1].controlPw.row(j));
/*(node->data).output(cout);
cout << endl;*/
}
cout << "pool size:" << tspline.pool.size() << endl;
tspline.pool.clear();
if (!tspline.check_valid()) {
cout << "skinning: invalid T-mesh!" << endl;
return;
}
}
void MinJaeMethod::insert()
{
// 3. insert intermediate vertices
// the coordinate of vertices is the midpoint of the corresponding points in C_r and C_(r+1)
assert(curves_num >= 3);
for (int i = 0; i <= curves_num - 1; i++) {
double s_now = s_knots(i);
auto s_nodes = tspline.s_map[s_now];
if (i == 0) {
for (auto it = s_nodes.begin(); it != s_nodes.end(); ++it) {
double s_insert = 2.0 / 3 * s_now + 1.0 / 3 * s_knots(i + 1);
tspline.insert_helper(s_insert, it->first, false);
MatrixXd position = 2.0 / 3 * curves[i].eval(it->first) + 1.0 / 3 * curves[i + 1].eval(it->first);
(tspline.s_map[s_insert][it->first]->data).fromVectorXd(position.row(0).transpose());
}
}
else if (i == curves_num - 1) {
for (auto it = s_nodes.begin(); it != s_nodes.end(); ++it) {
double s_insert = 2.0 / 3 * s_now + 1.0 / 3 * s_knots(i - 1);
tspline.insert_helper(s_insert, it->first, false);
MatrixXd position = 2.0 / 3 * curves[i].eval(it->first) + 1.0 / 3 * curves[i - 1].eval(it->first);
(tspline.s_map[s_insert][it->first]->data).fromVectorXd(position.row(0).transpose());
}
}
else {
for (auto it = s_nodes.begin(); it != s_nodes.end(); ++it) {
double s_left = 2.0 / 3 * s_now + 1.0 / 3 * s_knots(i - 1);
double s_right = 2.0 / 3 * s_now + 1.0 / 3 * s_knots(i + 1);
tspline.insert_helper(s_left, it->first, false);
MatrixXd position = 2.0 / 3 * curves[i].eval(it->first) + 1.0 / 3 * curves[i - 1].eval(it->first);
(tspline.s_map[s_left][it->first]->data).fromVectorXd(position.row(0).transpose());
tspline.insert_helper(s_right, it->first, false);
position = 2.0 / 3 * curves[i].eval(it->first) + 1.0 / 3 * curves[i + 1].eval(it->first);
(tspline.s_map[s_right][it->first]->data).fromVectorXd(position.row(0).transpose());
}
}
}
tspline.pool.clear();
if (!tspline.check_valid()) {
cout << "Skinning: invalid T-mesh!" << endl;
return;
}
}
void MinJaeMethod::calculate()
{
// 1. compute s-knot for curves
parameterize();
// 2. construct basis T-mesh
init();
// 3. insert intermediate vertices
// the coordinate of vertices is the midpoint of the corresponding points in C_r and C_(r+1)
insert();
// 4. 初始化中间点坐标
inter_init();
update();
// 5. 迭代更新中间点坐标
double error = 1.0;
for (int i = 0; i < maxIterNum; i++) {
error = inter_update();
update();
cout << "*******************iter " << i+1 << ": ,error " << error << "********************" << endl;
if (error < eps) {
break;
}
}
}
/**
1. calculate sample points by linear interpolate
2. fit sample points by B-spline using PIA method
3. the control points of B-Spline is the initial X,Y
*/
void MinJaeMethod::inter_init()
{
const int dimension = curves[0].controlPw.cols();
assert(curves_num >= 2);
for (int i = 0; i <= curves_num - 2; i++) {
double s_now = s_knots(i);
double s_next = s_knots(i + 1);
auto node_now = (tspline.s_map[s_now].begin())->second;
auto node_next = (tspline.s_map[s_next].begin())->second;
double s_inter1 = node_now->adj[1]->s[2];
double s_inter2 = node_next->adj[3]->s[2];
MatrixXd sample_inter1(sampleNum + 1, dimension);
MatrixXd sample_inter2(sampleNum + 1, dimension);
VectorXd params(sampleNum + 1);
// calculate sample points by linear interpolate
for (int j = 0; j <= sampleNum; j++) {
params(j) = 1.0*j / sampleNum;
RowVectorXd now_coor = curves[i].eval(params(j));
RowVectorXd next_coor = curves[i + 1].eval(params(j));
sample_inter1.row(j) = 2.0 / 3 * now_coor + 1.0 / 3 * next_coor;
sample_inter2.row(j) = 2.0 / 3 * next_coor + 1.0 / 3 * now_coor;
}
//(*viewer).data().add_points(sample_inter1, green);
//(*viewer).data().add_points(sample_inter2, green);
// fit sample points by B-spline using LSPIA with appointed knot vector
// the control points of B-Spline is the initial X,Y
VectorXd knots1 = curves[i].knots;
knots1(3) = 0.0001; knots1(curves[i].n + 1) = 0.9999;
VectorXd knots2 = curves[i + 1].knots;
knots2(3) = 0.0001; knots2(curves[i+1].n + 1) = 0.9999;
MatrixXd cpts1(curves[i].n + 1, 3);
MatrixXd cpts2(curves[i+1].n + 1, 3);
for (int j = 0; j <= curves[i].n; j++) {
cpts1.row(j) = tspline.s_map[s_inter1][knots1(j+2)]->data.toVectorXd();
}
for (int j = 0; j <= curves[i+1].n; j++) {
cpts2.row(j) = tspline.s_map[s_inter2][knots2(j+2)]->data.toVectorXd();
}
NURBSCurve inter1, inter2;
inter1.lspiafit(sample_inter1, params, cpts1, curves[i].knots, 100);
inter2.lspiafit(sample_inter2, params, cpts2, curves[i + 1].knots, 100);
/*inter1.draw(*viewer, false, true, 0.001);
inter2.draw(*viewer, false, true, 0.001);*/
inter1.knots(3) = 0.0001; inter1.knots(inter1.n + 1) = 0.9999;
inter2.knots(3) = 0.0001; inter2.knots(inter2.n + 1) = 0.9999;
// update coordinate of intermediate control points and save as inital_cpts
for (int j = 0; j <= inter1.n; j++) {
Point3d temp;
temp.fromVectorXd(inter1.controlPw.row(j));
initial_cpts[s_inter1][inter1.knots(j + 2)] = temp;
tspline.s_map[s_inter1][inter1.knots(j + 2)]->data = temp;
}
for (int j = 0; j <= inter2.n; j++) {
Point3d temp;
temp.fromVectorXd(inter2.controlPw.row(j));
initial_cpts[s_inter2][inter2.knots(j + 2)] = temp;
tspline.s_map[s_inter2][inter2.knots(j + 2)]->data = temp;
}
}
cout << "finished inter_init()!" << endl;
}
/**
1. calculate sample points on T-spline surface
2. fit sample points by B-spline using PIA method
3. update coordinate of intermediate control points
*/
double MinJaeMethod::inter_update()
{
const int dimension = curves[0].controlPw.cols();
assert(curves_num >= 2);
map<double, map<double, Point3d>> T_cpts;
for (int i = 0; i <= curves_num - 2; i++) {
double s_now = s_knots(i);
double s_next = s_knots(i + 1);
auto node_now = (tspline.s_map[s_now].begin())->second;
auto node_next = (tspline.s_map[s_next].begin())->second;
double s_inter1 = node_now->adj[1]->s[2];
double s_inter2 = node_next->adj[3]->s[2];
MatrixXd T_inter1(sampleNum + 1, dimension);
MatrixXd T_inter2(sampleNum + 1, dimension);
VectorXd params(sampleNum + 1);
// 1. calculate sample points on T - spline surface
for (int j = 0; j <= sampleNum; j++) {
params(j) = 1.0*j / sampleNum;
if (j == 0) {
T_inter1.row(j) = tspline.eval(s_inter1, 0.0001).toVectorXd();
T_inter2.row(j) = tspline.eval(s_inter2, 0.0001).toVectorXd();
}
else if (j == sampleNum) {
T_inter1.row(j) = tspline.eval(s_inter1, 0.9999).toVectorXd();
T_inter2.row(j) = tspline.eval(s_inter2, 0.9999).toVectorXd();
}
else {
T_inter1.row(j) = tspline.eval(s_inter1, params(j)).toVectorXd();
T_inter2.row(j) = tspline.eval(s_inter2, params(j)).toVectorXd();
}
}
// fit sample points by B-spline using LSPIA with appointed knot vector
VectorXd knots1 = curves[i].knots;
knots1(3) = 0.0001; knots1(curves[i].n + 1) = 0.9999;
VectorXd knots2 = curves[i + 1].knots;
knots2(3) = 0.0001; knots2(curves[i + 1].n + 1) = 0.9999;
MatrixXd cpts1(curves[i].n + 1, 3);
MatrixXd cpts2(curves[i + 1].n + 1, 3);
for (int j = 0; j <= curves[i].n; j++) {
cpts1.row(j) = tspline.s_map[s_inter1][knots1(j + 2)]->data.toVectorXd();
}
for (int j = 0; j <= curves[i + 1].n; j++) {
cpts2.row(j) = tspline.s_map[s_inter2][knots2(j + 2)]->data.toVectorXd();
}
NURBSCurve inter1, inter2;
inter1.lspiafit(T_inter1, params, cpts1, curves[i].knots, 100);
inter2.lspiafit(T_inter2, params, cpts2, curves[i + 1].knots, 100);
inter1.knots(3) = 0.0001; inter1.knots(inter1.n + 1) = 0.9999;
inter2.knots(3) = 0.0001; inter2.knots(inter2.n + 1) = 0.9999;
// update coordinate of intermediate control points
for (int j = 0; j <= inter1.n; j++) {
T_cpts[s_inter1][inter1.knots(j + 2)].fromVectorXd(inter1.controlPw.row(j).transpose());
}
for (int j = 0; j <= inter2.n; j++) {
T_cpts[s_inter2][inter2.knots(j + 2)].fromVectorXd(inter2.controlPw.row(j).transpose());
}
}
// update by T_cpts and initial_cpts
double error = 0.0;
int count = 0;
for (auto it = T_cpts.begin(); it != T_cpts.end(); ++it) {
for (auto it1 = (it->second).begin(); it1 != (it->second).end(); ++it1) {
count++;
Point3d delta = (initial_cpts[it->first][it1->first] - (it1->second));
error += delta.toVectorXd().norm();
(tspline.s_map[it->first][it1->first]->data).add(delta);
}
}
cout << "finished inter_update()!" << endl;
return error / count;
}