-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
379 lines (326 loc) · 12.7 KB
/
main.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
#include <igl/unproject_onto_mesh.h>
#include <igl/opengl/glfw/Viewer.h>
#include <igl/readDMAT.h>
#include <hedra/edge_mesh.h>
#include <hedra/polygonal_read_OFF.h>
#include <hedra/polygonal_write_OFF.h>
#include <hedra/triangulate_mesh.h>
#include <hedra/polygonal_edge_topology.h>
#include <hedra/point_spheres.h>
#include <hedra/concyclity.h>
#include <hedra/dc_error.h>
#include <hedra/ia_error.h>
#include <hedra/qc_error.h>
#include <hedra/concyclity.h>
#include <hedra/scalar2RGB.h>
#include <hedra/complex_moebius_deform.h>
#include <hedra/quat_moebius_deform.h>
#include <algorithm>
using namespace Eigen;
using namespace std;
igl::opengl::glfw::Viewer viewer;
typedef enum {DC_ERROR, IA_ERROR, FACE_CONCYCLITY, QC_ERROR} ViewingModes;
ViewingModes viewingMode=FACE_CONCYCLITY;
bool showDeformed=true;
std::vector<int> handles;
std::vector<Eigen::RowVector3d> handlePoses;
int currHandle;
Eigen::MatrixXd origV, deformV, currV;
Eigen::MatrixXd edgeOrigV, edgeDeformV;
Eigen::MatrixXi F, edgeT, currT, polyT;
Eigen::VectorXi D, polyTF,edgeTE, currTF;
Eigen::MatrixXi EV, EF, FE, EFi, I4;
Eigen::MatrixXd FEs;
Eigen::VectorXi innerEdges;
Eigen::Vector3d spans;
bool isExactMC=false;
bool isExactIAP=false;
bool isQuat = false;
std::string outputFileName;
bool editing=false;
bool choosingHandleMode=false;
double currWinZ;
hedra::ComplexMoebiusData cmdata;
hedra::QuatMoebiusData qmdata;
double rigidityFactor=0.1; //inversion control ratio
double DCSensitivity=0.01; //ratio of abs(cr)
double IASensitivity=1.0; //circle IA degrees difference between faces
double QCSensitivity=0.2; //abs(max_sing/min_sing)
double circSensitivity=5.0; //circle IA difference
void choose_current_mesh(){
//choosing mesh
switch(viewingMode){
case FACE_CONCYCLITY:
case QC_ERROR:
currV=(showDeformed ? deformV : origV);
currT=polyT;
currTF=polyTF;
break;
case DC_ERROR:
case IA_ERROR:
currV=(showDeformed ? edgeDeformV : edgeOrigV);
currT=edgeT;
currTF=edgeTE;
break;
}
}
bool UpdateCurrentView()
{
choose_current_mesh();
MatrixXd bc(handles.size(),currV.cols());
for (int i=0;i<handles.size();i++)
bc.row(i)=handlePoses[i].transpose();
Eigen::MatrixXd C, TC;
VectorXd scalarMeasure;
switch(viewingMode){
case FACE_CONCYCLITY:
hedra::concyclity(deformV,D,F,scalarMeasure);
scalarMeasure/=circSensitivity;
break;
case QC_ERROR:
hedra::qc_error(origV, deformV,D,F,scalarMeasure);
scalarMeasure/=QCSensitivity;
break;
case DC_ERROR:
hedra::dc_error(origV, deformV, D, F, EV, EF, EFi, innerEdges, scalarMeasure);
scalarMeasure/=DCSensitivity;
break;
case IA_ERROR:
hedra::ia_error(origV, deformV, D, F, EV, EF, EFi, innerEdges, scalarMeasure);
scalarMeasure/=IASensitivity;
break;
}
hedra::scalar2RGB(scalarMeasure, 0.0,1.0, C);
TC.resize(currT.rows(),3);
for (int i=0;i<currTF.rows();i++)
TC.row(i)=C.row(currTF(i));
double sphereRadius=spans.sum()/200.0;
MatrixXd sphereGreens(handles.size(),3);
sphereGreens.col(0).setZero();
sphereGreens.col(1).setOnes();
sphereGreens.col(2).setZero();
MatrixXd bigV=currV;
MatrixXi bigT=currT;
MatrixXd bigTC=TC;
/*point_spheres(const Eigen::MatrixXd& points,
const double& radius,
const Eigen::MatrixXd& colors,
const int res,
Eigen::MatrixXd& V,
Eigen::MatrixXi& T,
Eigen::MatrixXd& C)*/
if (bc.rows()!=0)
hedra::point_spheres(bc, sphereRadius, sphereGreens, 10, bigV, bigT, bigTC);
viewer.data().show_lines = false;
Eigen::MatrixXd OrigEdgeColors(EV.rows(), 3);
OrigEdgeColors.col(0) = Eigen::VectorXd::Constant(EV.rows(), 0.0);
OrigEdgeColors.col(1) = Eigen::VectorXd::Constant(EV.rows(), 0.0);
OrigEdgeColors.col(2) = Eigen::VectorXd::Constant(EV.rows(), 0.0);
viewer.data(0).clear();
viewer.data(0).set_mesh(currV, currT);
viewer.data(0).set_colors(TC);
viewer.data(0).compute_normals();
viewer.data(0).set_edges(currV, EV, OrigEdgeColors);
viewer.data(0).show_lines = false;
viewer.data(1).clear();
viewer.data(1).set_mesh(bigV, bigT);
viewer.data(1).show_lines=false;
viewer.data(0).set_colors(bigTC);
return true;
}
bool mouse_move(igl::opengl::glfw::Viewer& viewer, int mouse_x, int mouse_y)
{
if (!editing)
return false;
double x = viewer.current_mouse_x;
double y = viewer.core().viewport(3) - viewer.current_mouse_y;
Eigen::RowVector3f NewPos=igl::unproject<float>(Eigen::Vector3f(x,y,currWinZ),
viewer.core().view,
viewer.core().proj,
viewer.core().viewport);
handlePoses[handlePoses.size()-1]=NewPos.cast<double>();
Eigen::MatrixXd A(3*F.rows(),3);
for (int i=0;i<F.rows();i++)
A.block(3*i,0,3,3)=Eigen::Matrix3d::Identity();
Eigen::MatrixXd bc(handles.size(),currV.cols());
for (int i=0;i<handles.size();i++)
bc.row(i)=handlePoses[i].transpose();
if (isQuat)
hedra::quat_moebius_deform(qmdata, 1.0, rigidityFactor, isExactMC, bc, true, deformV);
else
hedra::complex_moebius_deform(cmdata, bc,150, deformV);
hedra::edge_mesh(deformV,D,F,EV, EF, edgeDeformV, edgeT,edgeTE);
UpdateCurrentView();
return true;
}
bool mouse_up(igl::opengl::glfw::Viewer& viewer, int button, int modifier)
{
if (((igl::opengl::glfw::Viewer::MouseButton)button==igl::opengl::glfw::Viewer::MouseButton::Left))
return false;
editing=false;
return true;
}
bool mouse_down(igl::opengl::glfw::Viewer& viewer, int button, int modifier)
{
if (((igl::opengl::glfw::Viewer::MouseButton)button==igl::opengl::glfw::Viewer::MouseButton::Left))
return false;
int vid, fid;
Eigen::Vector3f bc;
if (!choosingHandleMode){
editing=true;
return false;
}
double x = viewer.current_mouse_x;
double y = viewer.core().viewport(3) - viewer.current_mouse_y;
if(igl::unproject_onto_mesh(Eigen::Vector2f(x,y), viewer.core().view,
viewer.core().proj, viewer.core().viewport, currV, currT, fid, bc)){
//add the closest vertex to the handles
Eigen::MatrixXf::Index maxRow, maxCol;
bc.maxCoeff(&maxRow);
int CurrVertex=currT(fid, maxRow);
bool Found=false;
for (int i=0;i<handles.size();i++)
if (handles[i]==CurrVertex){
CurrVertex=handles[i];
Found=true;
}
if (!Found){
handles.push_back(CurrVertex);
handlePoses.push_back(currV.row(CurrVertex));
}
Eigen::Vector3f WinCoords=igl::project<float>(currV.row(CurrVertex).cast<float>(),
viewer.core().view,
viewer.core().proj,
viewer.core().viewport);
currWinZ=WinCoords(2);
std::cout<<"Choosing Vertex :"<<CurrVertex<<std::endl;
Eigen::VectorXi b(handles.size());
for (int i=0;i<handles.size();i++)
b(i)=handles[i];
if (isQuat)
hedra::quat_moebius_precompute(b, qmdata);
else
hedra::complex_moebius_precompute(b, isExactMC, isExactIAP, rigidityFactor, cmdata);
//hedra::affine_maps_precompute(V,D,F,EV,EF,EFi, FE, b, 3, affine_data);
UpdateCurrentView();
}
return true;
}
bool key_up(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifiers)
{
switch(key)
{
case '1': choosingHandleMode=false;
break;
}
return false;
}
bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifiers)
{
switch(key)
{
case '1':{
choosingHandleMode=true;
break;
}
case '2': {
viewingMode=(ViewingModes)(((int)viewingMode+1)%4);
UpdateCurrentView();
switch(viewingMode){
case DC_ERROR: cout<<"Showing discrete conformal error"<<endl; break;
case IA_ERROR: cout<<"Showing intersection-angle error"<<endl; break;
case FACE_CONCYCLITY: cout<<"Showing face concyclity error"<<endl; break;
case QC_ERROR: cout<<"Showing quasiconformal error"<<endl; break;
}
break;
}
case '3': {
showDeformed=!showDeformed;
UpdateCurrentView();
break;
}
case '5': {
switch(viewingMode){
case DC_ERROR: DCSensitivity+=0.01 ;cout<<"discrete conformal error range: [0,"<<DCSensitivity<<"]"<<endl; break;
case IA_ERROR: IASensitivity+=1.0 ;cout<<"intersection-angle error range: [0,"<<IASensitivity<<"]"<<endl; break;
case FACE_CONCYCLITY: circSensitivity+=1.0 ;cout<<"concyclity error range: [0,"<<circSensitivity<<"]"<<endl; break;
case QC_ERROR: QCSensitivity+=0.05 ;cout<<"quasiconformal error range: [0,"<<QCSensitivity<<"]"<<endl; break;
}
UpdateCurrentView();
break;
}
case '4': {
switch(viewingMode){
case DC_ERROR: if (DCSensitivity>0.01) DCSensitivity-=0.01; cout<<"discrete conformal error range: [0,"<<DCSensitivity<<"]"<<endl; break;
case IA_ERROR: if (IASensitivity>1.0) IASensitivity-=1.0 ;cout<<"intersection-angle error range: [0,"<<IASensitivity<<"]"<<endl; break;
case FACE_CONCYCLITY: if (circSensitivity>1.0) circSensitivity-=1.0 ;cout<<"concyclity error range: [0,"<<circSensitivity<<"]"<<endl; break;
case QC_ERROR: if (QCSensitivity>0.05) QCSensitivity-=0.05 ;cout<<"quasiconformal error range: [0,"<<QCSensitivity<<"]"<<endl; break;
}
UpdateCurrentView();
break;
}
case '6': {
isExactMC = !isExactMC;
cout<<"Computing with Exact CETM (in next deformation): "<<isExactMC<<endl;
break;
}
case '7': {
rigidityFactor=rigidityFactor*2.0;
cout<<"Rigidity factor (in next deformation): "<<rigidityFactor<<endl;
break;
}
case '8': {
rigidityFactor=rigidityFactor/2.0;
cout<<"Rigidity factor (in next deformation): "<<rigidityFactor<<endl;
break;
}
case 'W':{
hedra::polygonal_write_OFF(outputFileName, currV, D, F);
break;
}
}
return false;
}
int main(int argc, char *argv[])
{
if (argc < 3){
cout<<"1st argument should be complete file path and 2nd argument for output file!"<<endl;
return 0;
}
cout<<"press 1+right button to select new handles"<<endl;
cout<<"press 2 button to toggle between different quality measures"<<endl;
cout<<"press 3 to toggle between original and deformed mesh"<<endl;
cout<<"press 4-5 to change the sensitivity of the quality measurement"<<endl;
cout<<"press 6 to toggle exact CETM"<<endl;
cout<<"press 7-8 to change the rigidity factor"<<endl;
cout<<"Press W to save the output file"<<endl;
cout<<"press the right button and drag the current handle for deformation"<<endl;
// Load a mesh in OFF format
outputFileName = std::string(argv[2]);
hedra::polygonal_read_OFF(std::string(argv[1]), origV, D, F);
if (origV.col(2).maxCoeff()-origV.col(2).minCoeff()>10e-5) { //if it's a 2D mesh or not
isQuat = true;
rigidityFactor = 0.5; //initial
}
hedra::polygonal_edge_topology(D, F, EV, FE, EF,EFi,FEs,innerEdges);
hedra::triangulate_mesh(D, F, polyT, polyTF);
if (isQuat)
hedra::quat_moebius_setup(origV,D,F,polyTF,EV,EF,EFi,FE,FEs, innerEdges, qmdata);
else
hedra::complex_moebius_setup(origV,D,F,polyTF,EV,EF,EFi,FE,FEs, innerEdges, cmdata);
hedra::edge_mesh(origV, D, F, EV, EF, edgeOrigV, edgeT, edgeTE);
currV=origV;
deformV=origV;
edgeDeformV=edgeOrigV;
spans=currV.colwise().maxCoeff()-currV.colwise().minCoeff();
//handles mesh
viewer.append_mesh();
viewer.callback_mouse_down = &mouse_down;
viewer.callback_mouse_move = &mouse_move;
viewer.callback_mouse_up=&mouse_up;
viewer.callback_key_down=&key_down;
viewer.callback_key_up=&key_up;
viewer.core().background_color<<0.75,0.75,0.75,1.0;
UpdateCurrentView();
viewer.launch();
}