-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNURBSSurface.cpp
438 lines (384 loc) · 11.7 KB
/
NURBSSurface.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
// This file is part of NURBS, a simple NURBS library.
// github repo: https://github.com/aijm/NURBS
// Copyright (C) 2018 Jiaming Ai <[email protected]>
#include "NURBSSurface.h"
/*input format:
_order : // _order(0):u direction; _order(1): v direction
_controlP: _controlP[i] represents u direction control point ,matrix (m+1) by 3 or 4
v
|
_controlP[n]: | P_0n P_1n ... P_mn
_controlP[i]: | ...
_controlP[1]: | P_01 P_11 ... P_m1
_controlP[0]: | P_00 P_10 ... P_m0
------------------------> u
_uknots : u_0,u_1,...,u_(m+u_order)
_vknots : v_0,v_1,...,v_(n+v_order)
_isRational: */
NURBSSurface::NURBSSurface(
VectorXi _order,
vector<MatrixXd> _controlP,
VectorXd _uknots,
VectorXd _vknots,
bool _isRational)
{
// pass and check the parameters of NURBS surface
u_order = _order(0);
v_order = _order(1);
v_num = _controlP.size() - 1;
assert(u_order >= 1 && v_order >= 1 && v_num >= 1);
u_num = _controlP[0].rows() - 1;
assert(_uknots.size() == u_num + u_order + 1 && _vknots.size() == v_num + v_order + 1);
assert(u_num >= u_order - 1 && v_num >= v_order - 1);
dimension = _controlP[0].cols(); // the dimension of control point 2 or 3 or 4(weighted)
uknots = _uknots;
vknots = _vknots;
controlPw = _controlP;
isRational = _isRational;
if (isRational) { assert(dimension == 4); }
else { assert(dimension == 3); }
}
// load
bool NURBSSurface::loadNURBS(string name){
ifstream in(name);
if(!in){
return false;
}
char sep;
dimension=3;
in>> isRational;
in>> u_num >> v_num;
in>> u_order >> v_order;
in>> dimension;
controlPw = vector<MatrixXd>(v_num+1);
for(int i=0;i<controlPw.size();i++){
controlPw[i] = MatrixXd(u_num+1,dimension);
}
for(int i=0;i<controlPw.size();i++){
for(int j=0;j<=u_num;j++){
for(int k=0;k<dimension;k++){
in>> controlPw[i](j,k);
}
in>>sep;
}
}
uknots = VectorXd(u_num+u_order+1);
vknots = VectorXd(v_num+v_order+1);
for(int i=0;i<uknots.size();i++){
in>>uknots(i);
}
for(int i=0;i<vknots.size();i++){
in>>vknots(i);
}
return true;
}
// save
bool NURBSSurface::saveNURBS(string name){
if(controlPw.size()==0){
cout<< "nothing to save!"<<endl;
return false;
}
if(isRational){
name+=".cptw";
}else{
name+=".cpt";
}
ofstream out(name);
if(!out){
return false;
}
IOFormat outputFmt(4, 0, " ", " ", "", ",");
out<< isRational<<endl;
out<< u_num << " " <<v_num<<endl;
out<< u_order << " "<< v_order<<endl;
out<< controlPw[0].cols()<<endl;
for(int i=0;i<controlPw.size();i++){
for(int j=0;j<=u_num;j++){
out<<controlPw[i].row(j).format(outputFmt);
}
out<<endl;
}
out<<uknots.transpose()<<endl;
out<<vknots.transpose();
return true;
}
// find the knot interval of t by binary searching
int NURBSSurface::find_ind(double t, int k, int n, const VectorXd& knots)
{
if (t == knots(n + 1)) return n;
int low = 0;
int high = n + k;
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;
}
// calculate coordinate of curve point with parameter u & v
MatrixXd NURBSSurface::eval(double u, double v)
{
//Calculating the Control Points of U-direction Isoparametric Line
MatrixXd v_controlP(v_num + 1, dimension);
for (int i = 0; i <= v_num; i++)
{
v_controlP.row(i) = eval(u, controlPw[i], uknots);
}
return eval(v, v_controlP, vknots); // Calculating the coordinate of parameter v
}
MatrixXd NURBSSurface::eval(
double t,
const MatrixXd &_controlP,
const VectorXd &knots)
{
int n = _controlP.rows() - 1;
int k = knots.size() - _controlP.rows();
/*if (t<knots(k - 1) || t>knots(n + 1)) {*/
//cout << "t: " << t << ", left: " << knots(k - 1) << ", right: " << knots(n + 1) << endl;
/*}*/
assert(t>=knots(k-1) && t<=knots(n+1));
// find the knot interval of t by binary searching
int L = find_ind(t, k, n, knots); //[t_L,t_(L+1)]
// P_(L-k+1),..,P_L control the interval [t_L,t_(L+1)]
MatrixXd temp = _controlP.block(L - k + 1, 0, k, _controlP.cols());
for (int r = 1; r <= k - 1; r++)
for (int i = L - k + 1 + r; i <= L; i++)
{
double factor = (t - knots(i)) / (knots(i + k - r) - knots(i));
int start = i - (L - k + 1 + r);
temp.row(start) = (1.0 - factor)*temp.row(start) + factor*temp.row(start + 1);
}
MatrixXd curvePoint = temp.row(0);
return curvePoint;
}
// knot insertion
bool NURBSSurface::insert(double s, char dir){
assert(dir=='u' || dir=='v');
if(dir=='u'){
for(int i=0;i<controlPw.size();i++){
NURBSCurve nurbs(u_num,u_order,controlPw[i],uknots,isRational);
nurbs.insert(s);
controlPw[i]=nurbs.controlPw;
if (i == controlPw.size() - 1) {
uknots = nurbs.knots;
u_num = nurbs.n;
}
}
return true;
}else if(dir=='v'){
vector<MatrixXd> new_controlPw(controlPw.size()+1);
for (int i = 0; i < new_controlPw.size(); i++) {
new_controlPw[i] = MatrixXd(u_num + 1, controlPw[0].cols());
}
MatrixXd v_controlPw(v_num+1,controlPw[0].cols());
for(int i=0;i<=u_num;i++){
for(int j=0;j<=v_num;j++){
v_controlPw.row(j) = controlPw[j].row(i);
}
NURBSCurve nurbs(v_num,v_order,v_controlPw,vknots,isRational);
nurbs.insert(s);
for(int k=0;k<new_controlPw.size();k++){
new_controlPw[k].row(i) = nurbs.controlPw.row(k);
}
if (i == u_num) {
vknots = nurbs.knots;
v_num = nurbs.n;
}
}
controlPw = new_controlPw;
return true;
}else{
cout<< "please input dir as u or v!"<<endl;
return false;
}
}
// kont insertion
bool NURBSSurface::insert(double s, double t){
if(insert(s,'u') && insert(t,'v')){
return true;
}
return false;
}
// draw controlpolygon
void NURBSSurface::drawControlPolygon(igl::opengl::glfw::Viewer &viewer){
//plot control points and control polygon
//plot control points
for (int i = 0; i < controlP.size(); i++)
{
viewer.data().add_points(
controlP[i],
Eigen::RowVector3d(1, 1, 1));
}
// plot control polygon
for (int j = 0; j <= v_num; j++)
for (int i = 0; i <= u_num; i++)
{
if (i != u_num) {
viewer.data().add_edges(
controlP[j].row(i),
controlP[j].row(i + 1),
Eigen::RowVector3d(1, 1, 1));
}
if (j != v_num) {
viewer.data().add_edges(
controlP[j].row(i),
controlP[j + 1].row(i),
Eigen::RowVector3d(1, 1, 1));
}
}
}
// draw NURBS surface
void NURBSSurface::drawSurface(igl::opengl::glfw::Viewer &viewer, double resolution){
// cut apart the parameter domain
double u_low = uknots(u_order - 1);
double u_high = uknots(u_num + 1);
const int uspan = (u_high - u_low) / resolution;
double u_resolution = (u_high - u_low) / uspan;
double v_low = vknots(v_order - 1);
double v_high = vknots(v_num + 1);
const int vspan = (v_high - v_low) / resolution;
double v_resolution = (v_high - v_low) / vspan;
mesh_V = MatrixXd((uspan + 1)*(vspan + 1), 3);
mesh_F = MatrixXi(2 * uspan*vspan, 3);
// discretize NURBS Surface into triangular mesh(V,F) in libigl mesh structure
// calculate mesh_V
for (int j = 0; j <= vspan; j++)
for (int i = 0; i <= uspan; i++)
{
RowVectorXd curvePoint = eval(u_low + i*u_resolution, v_low + j*v_resolution).row(0);
//cout << "curvepoint: " << curvePoint << endl;
if (isRational) { mesh_V.row(j*(uspan + 1) + i) = curvePoint.hnormalized(); }
else { mesh_V.row(j*(uspan + 1) + i) = curvePoint; }
}
for (int j = 0; j<vspan; j++)
for (int i = 0; i < uspan; i++)
{
int V_index = j*(uspan + 1) + i;
int F_index = 2 * j*uspan + 2 * i;
mesh_F.row(F_index) << V_index, V_index + 1, V_index + uspan + 1;
mesh_F.row(F_index + 1) << V_index + uspan + 1, V_index + 1, V_index + uspan + 2;
}
viewer.data().set_mesh(mesh_V, mesh_F);
}
void NURBSSurface::draw(
igl::opengl::glfw::Viewer &viewer,
bool showpolygon,bool showsurface,
double resolution){
if(controlP.size()!=controlPw.size()){
controlP = vector<MatrixXd>(controlPw.size());
}
if(isRational){
for(int i=0;i<controlP.size();i++){
controlP[i] = controlPw[i].rowwise().hnormalized();
}
}else{
controlP = controlPw;
}
if(showpolygon){
drawControlPolygon(viewer);
viewer.core.align_camera_center(controlP[controlP.size()/2]);
}
if(showsurface){
drawSurface(viewer,resolution);
viewer.core.align_camera_center(mesh_V,mesh_F);
}
}
// surface skinning: order is the same for every curves
void NURBSSurface::skinning(const vector<NURBSCurve> &curves,igl::opengl::glfw::Viewer &viewer){
assert(curves.size()>1);
isRational = curves[0].isRational;
// count knots of every curve
vector<map<double,int>> curve_knots(curves.size());
for(int i=0;i<curves.size();i++){
for(int j=0;j<curves[i].knots.size();j++){
if(curve_knots[i].find(curves[i].knots(j))==curve_knots[i].end()){
curve_knots[i][curves[i].knots(j)] = 1;
}else{
curve_knots[i][curves[i].knots(j)] += 1;
}
}
}
// compute merge knots
map<double, int> merge_knots;
for (int i = 0; i < curve_knots.size(); i++) {
for (auto it = curve_knots[i].begin(); it != curve_knots[i].end(); it++) {
if (merge_knots.find(it->first) == merge_knots.end()) {
merge_knots[it->first] = it->second;
}
else if (merge_knots[it->first] < it->second) {
merge_knots[it->first] = it->second;
}
}
}
cout << "merge_knots:\n" << endl;
for (auto it = merge_knots.begin(); it != merge_knots.end(); it++) {
cout << it->first << ", " << it->second << endl;
}
// change every curve to 3D and rational
vector<NURBSCurve> new_curves = curves;
// for every curve, insert knots to make curve knots compatible
for (int i = 0; i < curve_knots.size(); i++) {
for (auto it = merge_knots.begin(); it != merge_knots.end(); it++) {
int insert_num = 0;
if (curve_knots[i].find(it->first) == curve_knots[i].end()) {
insert_num = it->second;
}
else {
insert_num = it->second - curve_knots[i][it->first];
}
// insert
for (int j = 0; j < insert_num; j++) {
new_curves[i].insert(it->first);
}
}
}
for (int i = 0; i < new_curves.size(); i++) {
cout << "curve " << i << ": " << new_curves[i].knots.transpose() << endl;
new_curves[i].draw(viewer);
}
vknots = new_curves[0].knots;
//cout << "after insert knots: " << vknots.transpose() << endl;
// compute u-direction knot vector
VectorXd curves_param = VectorXd::Zero(new_curves.size());
v_num = new_curves[0].n;
v_order = new_curves[0].k;
dimension = new_curves[0].controlPw.cols();
//u_num = new_curves.size() - 1;
u_order = 4;
int curve_num = curves.size() - 1;
u_num = curve_num + 2;
for (int i = 0; i <= v_num; i++) {
MatrixXd u_cpts(curve_num + 1, dimension);
for (int j = 0; j <= curve_num; j++) {
u_cpts.row(j) = new_curves[j].controlPw.row(i);
}
curves_param += NURBSCurve::parameterize(u_cpts);
}
curves_param /= (v_num + 1);
uknots = VectorXd::Zero(curve_num + 7); // u_0,u_0,u_0, u_0,...,u_K, u_K,u_K,u_K
uknots.block(3, 0, curve_num + 1, 1) = curves_param;
uknots(curve_num + 6) = 1.0; uknots(curve_num + 5) = 1.0; uknots(curve_num + 4) = 1.0;
//uknots << 0, 0, 0, 0, 2.0/12, 4.0/12, 5.0/12, 7.0/12, 8.0/12, 11.0/12, 1, 1, 1, 1;
cout << "uknots: " << uknots.transpose() << endl;
// interpolate points to get the NURBS skinning surface control points
controlPw = vector<MatrixXd>(v_num + 1);
for (int i = 0; i < controlPw.size(); i++) {
controlPw[i] = MatrixXd(u_num + 1, dimension);
}
for (int i = 0; i <= v_num; i++) {
MatrixXd u_ctps = MatrixXd::Zero(curve_num + 1, dimension);
for (int j = 0; j <= curve_num; j++) {
u_ctps.row(j) = new_curves[j].controlPw.row(i);
}
//viewer.data().add_points(u_ctps, RowVector3d(0, 1, 0));
NURBSCurve nurbs;
nurbs.interpolate(u_ctps, uknots);
//nurbs.interpolate(u_ctps);
nurbs.draw(viewer, false, true);
controlPw[i] = nurbs.controlPw;
}
cout << "skinning finished!" << endl;
}