-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutility.cpp
485 lines (430 loc) · 13.4 KB
/
utility.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "utility.h"
namespace t_mesh {
// the index of parameter t in knot vector
// example knots=[0, 0, 0, 0, 0.4, 0.6, 1, 1, 1, 1]; t= 0.5, n = 5, p = 3
// return 4
int FindSpan(const Eigen::MatrixXd &knots, double t, int p) {
const int n = knots.size() - p - 2;
if (t == knots(n + 1)) return n;
int low = p;
int high = n+1;
assert(t >= knots(low) && t < knots(high));
int mid = (low + high) / 2;
while (t < knots(mid) || t >= knots(mid + 1))
{
if (t < knots(mid)) high = mid;
else low = mid;
mid = (low + high) / 2;
}
return mid;
}
// Blending function N[s0,s1,s2,s3,s4](t)
double Basis(const Eigen::MatrixXd &knots, double t, int i, int p)
{
const int m = knots.size() - 1;
// 特殊情况
if (i == 0) {
int count = 0;
for (int j = 0; j <= p; j++) {
if (abs(knots(j) - t) <= 0.0001) {
count++;
}
}
if (count == p + 1) {
return 1.0;
}
}
if (i == m - p - 1) {
int count = 0;
for (int j = 0; j <= p; j++) {
if (abs(knots(i+j+1) - t) <= 0.0001) {
count++;
}
}
if (count == p + 1) {
return 1.0;
}
}
/*if ((i == 0 && t == knots(0)) ||
(i == m - p - 1 && t == knots(m))) {
return 1.0;
}*/
// 根据局部性
if (t < knots(i) || t >= knots(i + p + 1)) {
return 0.0;
}
Eigen::VectorXd N(p + 1);
// 初始化0次的基函数
for (int j = 0; j <= p; j++) {
if (t >= knots(i + j) && t < knots(i + j + 1)) N(j) = 1.0;
else N(j) = 0.0;
}
//cout << "N: \n" << N << endl;
// 计算三角形表
for (int k = 1; k <= p; k++) {
//cout << "k=1:" << endl;
double saved = 0.0;
if (N(0) == 0.0) saved = 0.0;
else saved = (t - knots(i)) * N(0) / (knots(i + k) - knots(i));
for (int j = 0; j < p - k + 1; j++) {
double Uleft = knots(i + j + 1);
double Uright = knots(i + j + k + 1);
if (N(j + 1) == 0.0) {
N(j) = saved;
saved = 0.0;
}
else {
double temp = N(j + 1) / (Uright - Uleft);
N(j) = saved + (Uright - t) * temp;
saved = (t - Uleft) * temp;
}
//cout << N(j) << endl;
}
}
return N(0);
}
// Berivative of Blending function N[s0,s1,s2,s3,s4](t)
Eigen::RowVectorXd DersBasis(const Eigen::MatrixXd &knots, double t, int i, int p)
{
if (t == 0.0) {
t = 0.0001;
}
if (t == 1.0) {
t = 0.9999;
}
const int m = knots.size() - 1;
Eigen::RowVectorXd ders = Eigen::RowVectorXd::Zero(p + 1); // k阶导数, k= 0,1,2,...,p
// 根据局部性
if (t < knots(i) || t >= knots(i + p + 1)) {
for (int k = 0; k <= p; k++) ders(k) = 0.0;
return ders;
}
Eigen::MatrixXd N = Eigen::MatrixXd::Zero(p + 1, p + 1);
// 初始化0次的基函数
for (int j = 0; j <= p; j++) {
if (t >= knots(i + j) && t < knots(i + j + 1)) N(j, 0) = 1.0;
else N(j, 0) = 0.0;
}
// 计算三角形表
for (int k = 1; k <= p; k++) {
double saved = 0.0;
if (N(0,k-1) == 0.0) saved = 0.0;
else saved = (t - knots(i)) * N(0,k-1) / (knots(i + k) - knots(i));
for (int j = 0; j < p - k + 1; j++) {
double Uleft = knots(i + j + 1);
double Uright = knots(i + j + k + 1);
if (N(j + 1, k - 1) == 0.0) {
N(j, k) = saved;
saved = 0.0;
}
else {
double temp = N(j + 1, k - 1) / (Uright - Uleft);
N(j, k) = saved + (Uright - t) * temp;
saved = (t - Uleft) * temp;
}
}
}
//cout << "N: \n" << N << endl;
ders(0) = N(0, p); // 函数值
// 计算导数
for (int k = 1; k <= p; k++) {
Eigen::VectorXd ND = N.col(p - k); // 载入正确的列
for (int jj = 1; jj <= k; jj++) {
double saved = 0.0;
if (ND(0) == 0.0) saved = 0.0;
else saved = ND(0) / (knots(i + p - k + jj) - knots(i));
for (int j = 0; j < k - jj + 1; j++) {
double Uleft = knots(i + j + 1);
double Uright = knots(i + j + p - k + jj + 1);
if (ND(j + 1) == 0.0) {
ND(j) = (p - k + jj) * saved;
saved = 0.0;
}
else {
double temp = ND(j + 1) / (Uright - Uleft);
ND(j) = (p - k + jj)*(saved - temp);
saved = temp;
}
}
}
ders(k) = ND(0);
}
return ders;
}
// Blending function N[s0,s1,s2,s3,s4](p)
double Basis1(const Eigen::MatrixXd &knotvector, double t, int i, int p)
{
//cout << "knotvector:\n" << knotvector.transpose() << endl;
//int p = knotvector.size() - 1;
assert(p >= 1);
if (p == 1) {
if (t >= knotvector(i) && t < knotvector(i + 1)) {
return 1.0;
}
else {
return 0.0;
}
}
double a = knotvector(i + p - 1) - knotvector(i);
double b = knotvector(i + p) - knotvector(i + 1);
a = (a == 0.0) ? 0.0 : (t - knotvector(i)) / a;
b = (b == 0.0) ? 0.0 : (knotvector(i + p) - t) / b;
return a*Basis(knotvector, t, i, p - 1) + b*Basis(knotvector, t, i + 1, p - 1);
}
bool loadpoints(std::string name, Eigen::MatrixXd &mat) {
ifstream in(name);
if (!in.is_open()) {
cout << "error: can't open file: " + name << endl;
return false;
}
int rows = 0;
int cols = 0;
in >> rows >> cols;
mat = Eigen::MatrixXd(rows, cols);
for (int i = 0; i < mat.rows(); i++) {
for (int j = 0; j < mat.cols(); j++) {
in >> mat(i, j);
}
}
//cout << "matrix: \n" << mat << endl;
return true;
}
bool savepoints(string name, const Eigen::MatrixXd &mat) {
name += ".dat";
ofstream out(name);
if (!out.is_open()) {
cout << "error: can't open file: " + name << endl;
return false;
}
out << mat.rows() << " " << mat.cols() << endl;
out << mat;
//cout << "matrix: \n" << mat << endl;
return true;
}
void vec_insert(Eigen::VectorXd &vec, double t) {
assert(t > vec(0) && t < vec(vec.size() - 1));
Eigen::VectorXd temp = vec;
vec.resize(temp.size() + 1);
int i = 0;
while (i < temp.size() && temp(i) <= t) {
vec(i) = temp(i);
i++;
}
vec(i) = t;
while (i < temp.size()) {
vec(i + 1) = temp(i);
i++;
}
}
void TsplineSimplify(const NURBSSurface & surface, Mesh3d & tspline, int maxIterNum, double eps)
{
assert(surface.dimension == 3 && surface.u_order == 4 && surface.v_order == 4);
// create a tspline format of surface
Mesh3d origin;
Eigen::VectorXd u_knots = surface.uknots;
Eigen::VectorXd v_knots = surface.vknots;
u_knots(3) = 0.0001; u_knots(u_knots.size() - 4) = 0.9999;
v_knots(3) = 0.0001; v_knots(v_knots.size() - 4) = 0.9999;
map<double, int> u_map;
map<double, int> v_map;
for (int i = 2; i <= u_knots.size()-3; i++) {
u_map[u_knots(i)] = i;
}
for (int i = 2; i <= v_knots.size() - 3; i++) {
v_map[v_knots(i)] = i;
}
// 控制顶点对应的参数点的高斯曲率绝对值
map<double, map<double, double>> curvature;
double max_curvature = -1;
double min_curvature = 1e10;
for (int i = 0; i <= surface.v_num; i++) {
for (int j = 0; j <= surface.u_num; j++) {
double u = u_knots(j + 2);
double v = v_knots(i + 2);
// 取绝对值后取对数拉伸范围
double h = log10((abs(surface.guassian_curvature(u, v)) + 1));
//cout << h << endl;
curvature[u][v] = h;
if (h < min_curvature) min_curvature = h;
if (h > max_curvature) max_curvature = h;
origin.insert_helper(u, v, false);
auto node = origin.get_node(u, v);
node->data.fromVectorXd(surface.controlPw[i].row(j));
}
}
cout << "size of original tspline: " << origin.get_num() <<"*************************"<< endl;
cout << "\n\n";
/*cout << "hmin: " << min_curvature << ", " << "hmax : " << max_curvature << endl;
cout << "origin pool size : " << origin.pool.size() << endl;*/
origin.pool.clear();
if (!origin.check_valid()) {
cout << "error: invalid tspline mesh!" << endl;
return;
}
//origin.saveMesh("../out/tspline/origin");
// create an initial tspline patch
Eigen::VectorXd init_knots(4);
init_knots << 0, 0.0001, 0.9999, 1.0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
double u = init_knots(i);
double v = init_knots(j);
tspline.insert_helper(u, v, false);
tspline.s_map[u][v]->data = origin.s_map[u][v]->data;
}
}
//cout << "pool size **********************: " << tspline.pool.size() << endl;
tspline.pool.clear();
if (!tspline.check_valid()) {
cout << "error: invalid tspline mesh!" << endl;
return;
}
// split the rectangle defined by the parameter
auto split = [&](const std::tuple<double,double,double,double>& rect) {
// 其实只是分割区域,是拓扑层面的操作,控制点坐标不需要考虑
int u0 = u_map[get<0>(rect)]; // ulow
int u1 = u_map[get<1>(rect)]; // uhigh
int v0 = v_map[get<2>(rect)]; // vlow
int v1 = v_map[get<3>(rect)]; // vhigh
if (u1 - u0 >= v1 - v0) {
int u_insert = (u0 + u1) / 2;
tspline.insert_helper(u_knots( u_insert), v_knots(v0),false);
tspline.merge_all();
tspline.insert_helper(u_knots(u_insert), v_knots(v1),false);
tspline.merge_all();
}
else {
int v_insert = (v0 + v1) / 2;
tspline.insert_helper(u_knots(u0), v_knots(v_insert),false);
tspline.merge_all();
tspline.insert_helper(u_knots(u1), v_knots(v_insert),false);
tspline.merge_all();
}
};
int i = 0;
while(true) {
/*cout << i << " ***********************************************************" << endl;
cout << "size of nodes: " << tspline.get_num() << endl;*/
// 节点插入加细到与B样条曲面一致
//Viewer viewer;
//Mesh3d tspline_copy(tspline);
//for (auto node : tspline.nodes) {
// double u = node->s[2];
// double v = node->t[2];
// if (origin.get_node(u, v) == 0 || origin.get_node(u,v)->data != node->data) {
// cout << "error: *********************" << endl;
// return;
// }
//}
//tspline_copy.setViewer(&viewer);
//tspline_copy.draw(false, true, true);
///*for (auto node : origin.nodes) {
// MatrixXd P;
// array2matrixd(node->data, P);
// viewer.data().add_points(P, green);
//}*/
//viewer.launch();
Mesh3d mesh(tspline);
//cout << "size of mesh: " << mesh.get_num() << endl;
for (auto node : origin.nodes) {
if (mesh.get_node(node->s[2], node->t[2]) == 0) {
mesh.insert(node->s[2], node->t[2]);
}
}
/*mesh.setViewer(&viewer);
mesh.draw(false, true, true);
for (auto node : mesh.nodes) {
auto node_origin = origin.get_node(node->s[2], node->t[2]);
MatrixXd P1, P2;
array2matrixd(node->data, P1);
array2matrixd(node_origin->data, P2);
viewer.data().add_edges(P1, P2, yellow);
viewer.data().add_points(P2, green);
}
viewer.launch();*/
vector<pair<tuple<double, double, double, double>, double>> regions;
// 先整体计算误差,取出需要split的区域
for (auto node : mesh.nodes) {
double u = node->s[2];
double v = node->t[2];
double error = (node->data - origin.s_map[u][v]->data).toVectorXd().norm();
double factor = (max_curvature - curvature[u][v]) / (max_curvature - min_curvature);
//cout << "factor: " << factor << endl;
if (factor < 0.4) {
factor = 0.7*factor;
}
else {
factor = (1 + factor) / 2;
}
if (isnan(factor)) {
factor = 1.0;
}
factor = max(factor, 0.05);
//cout << "factor: " << factor << endl;
if (error < factor*eps) {
continue;
}
auto rects = tspline.region(u, v);
for (auto rect : rects) {
//cout << "error: " << error << ", factor*eps: " << error - factor*eps << endl;
regions.push_back(make_pair(rect, error - factor*eps));
}
}
if (regions.empty()) {
break;
}
if (tspline.nodes.size() < origin.nodes.size() * 0.2) {
// 再按误差排序
sort(regions.begin(), regions.end(),
[](const pair<tuple<double, double, double, double>, double>& a,
const pair<tuple<double, double, double, double>, double>& b) {return a.second > b.second; });
cout << "iter "<<i <<" ,max(error-eps): " << regions[0].second << endl;
for (auto rect : regions) {
split(rect.first);
}
}
else {
// 去除regions的重复元素
sort(regions.begin(), regions.end(),
[](const pair<tuple<double, double, double, double>, double>& a,
const pair<tuple<double, double, double, double>, double>& b) {return a.first < b.first; });
regions.erase(unique(regions.begin(), regions.end(),
[](const pair<tuple<double, double, double, double>, double>& a,
const pair<tuple<double, double, double, double>, double>& b) {return a.first == b.first; }), regions.end());
// 再按误差排序
sort(regions.begin(), regions.end(),
[](const pair<tuple<double, double, double, double>, double>& a,
const pair<tuple<double, double, double, double>, double>& b) {return a.second > b.second; });
cout << "iter " << i << " ,max(error-eps): " << regions[0].second << endl;
//cout << "total region size: "<<regions.size() << endl;
// 每次选择误差最大的5个split
for (int j = 0; j < 10; j++) {
if (j == regions.size()) {
break;
}
else {
/*cout << "误差:" << regions[j].second << endl;
cout << "split: " << get<0>(regions[j].first)<<", " << get<1>(regions[j].first) << ", "
<< get<2>(regions[j].first) << ", " << get<3>(regions[j].first) << endl;*/
split(regions[j].first);
}
}
}
tspline.improve();
// 更新 tspline
for (auto node : tspline.nodes) {
node->data = origin.s_map[node->s[2]][node->t[2]]->data;
}
//cout << "tspline pool size after split: "<<tspline.pool.size()<<"************************** "<< endl;
tspline.pool.clear();
if (!tspline.check_valid()) {
cout << "error: tspline is not valid!!!" << endl;
}
i++;
cout << "size of nodes: " << tspline.get_num() << endl;
}
//cout << "size of nodes: " << tspline.get_num() << endl;
cout << "simplify finished. *****************************" << endl;
cout << "\n\n";
}
};