forked from alecjacobson/geometry-processing-deformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
278 lines (271 loc) · 6.74 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
#include "biharmonic_precompute.h"
#include "biharmonic_solve.h"
#include "arap_precompute.h"
#include "arap_single_iteration.h"
#include <igl/min_quad_with_fixed.h>
#include <igl/read_triangle_mesh.h>
#include <igl/opengl/glfw/Viewer.h>
#include <igl/project.h>
#include <igl/unproject.h>
#include <igl/snap_points.h>
#include <igl/unproject_onto_mesh.h>
#include <Eigen/Core>
#include <iostream>
#include <stack>
// Undoable
struct State
{
// Rest and transformed control points
Eigen::MatrixXd CV, CU;
bool placing_handles = true;
} s;
int main(int argc, char *argv[])
{
// Undo Management
std::stack<State> undo_stack,redo_stack;
const auto push_undo = [&](State & _s=s)
{
undo_stack.push(_s);
// clear
redo_stack = std::stack<State>();
};
const auto undo = [&]()
{
if(!undo_stack.empty())
{
redo_stack.push(s);
s = undo_stack.top();
undo_stack.pop();
}
};
const auto redo = [&]()
{
if(!redo_stack.empty())
{
undo_stack.push(s);
s = redo_stack.top();
redo_stack.pop();
}
};
Eigen::MatrixXd V,U;
Eigen::MatrixXi F;
long sel = -1;
Eigen::RowVector3f last_mouse;
igl::min_quad_with_fixed_data<double> biharmonic_data, arap_data;
Eigen::SparseMatrix<double> arap_K;
// Load input meshes
igl::read_triangle_mesh(
(argc>1?argv[1]:"../shared/data/decimated-knight.off"),V,F);
U = V;
igl::opengl::glfw::Viewer viewer;
std::cout<<R"(
[click] To place new control point
[drag] To move control point
[space] Toggle whether placing control points or deforming
M,m Switch deformation methods
U,u Update deformation (i.e., run another iteration of solver)
R,r Reset control points
⌘ Z Undo
⌘ ⇧ Z Redo
)";
enum Method
{
BIHARMONIC = 0,
ARAP = 1,
NUM_METHODS = 2,
} method = BIHARMONIC;
const auto & update = [&]()
{
// predefined colors
const Eigen::RowVector3d orange(1.0,0.7,0.2);
const Eigen::RowVector3d yellow(1.0,0.9,0.2);
const Eigen::RowVector3d blue(0.2,0.3,0.8);
const Eigen::RowVector3d green(0.2,0.6,0.3);
if(s.placing_handles)
{
viewer.data().set_vertices(V);
viewer.data().set_colors(blue);
viewer.data().set_points(s.CV,orange);
}else
{
// SOLVE FOR DEFORMATION
switch(method)
{
default:
case BIHARMONIC:
{
Eigen::MatrixXd D;
biharmonic_solve(biharmonic_data,s.CU-s.CV,D);
U = V+D;
break;
}
case ARAP:
{
arap_single_iteration(arap_data,arap_K,s.CU,U);
break;
}
}
viewer.data().set_vertices(U);
viewer.data().set_colors(method==BIHARMONIC?orange:yellow);
viewer.data().set_points(s.CU,method==BIHARMONIC?blue:green);
}
viewer.data().compute_normals();
};
viewer.callback_mouse_down =
[&](igl::opengl::glfw::Viewer&, int, int)->bool
{
last_mouse = Eigen::RowVector3f(
viewer.current_mouse_x,viewer.core.viewport(3)-viewer.current_mouse_y,0);
if(s.placing_handles)
{
// Find closest point on mesh to mouse position
int fid;
Eigen::Vector3f bary;
if(igl::unproject_onto_mesh(
last_mouse.head(2),
viewer.core.view,
viewer.core.proj,
viewer.core.viewport,
V, F,
fid, bary))
{
long c;
bary.maxCoeff(&c);
Eigen::RowVector3d new_c = V.row(F(fid,c));
if(s.CV.size()==0 || (s.CV.rowwise()-new_c).rowwise().norm().minCoeff() > 0)
{
push_undo();
s.CV.conservativeResize(s.CV.rows()+1,3);
// Snap to closest vertex on hit face
s.CV.row(s.CV.rows()-1) = new_c;
update();
return true;
}
}
}else
{
// Move closest control point
Eigen::MatrixXf CP;
igl::project(
Eigen::MatrixXf(s.CU.cast<float>()),
viewer.core.view,
viewer.core.proj, viewer.core.viewport, CP);
Eigen::VectorXf D = (CP.rowwise()-last_mouse).rowwise().norm();
sel = (D.minCoeff(&sel) < 30)?sel:-1;
if(sel != -1)
{
last_mouse(2) = CP(sel,2);
push_undo();
update();
return true;
}
}
return false;
};
viewer.callback_mouse_move = [&](igl::opengl::glfw::Viewer &, int,int)->bool
{
if(sel!=-1)
{
Eigen::RowVector3f drag_mouse(
viewer.current_mouse_x,
viewer.core.viewport(3) - viewer.current_mouse_y,
last_mouse(2));
Eigen::RowVector3f drag_scene,last_scene;
igl::unproject(
drag_mouse,
viewer.core.view,
viewer.core.proj,
viewer.core.viewport,
drag_scene);
igl::unproject(
last_mouse,
viewer.core.view,
viewer.core.proj,
viewer.core.viewport,
last_scene);
s.CU.row(sel) += (drag_scene-last_scene).cast<double>();
last_mouse = drag_mouse;
update();
return true;
}
return false;
};
viewer.callback_mouse_up = [&](igl::opengl::glfw::Viewer&, int, int)->bool
{
sel = -1;
return false;
};
viewer.callback_key_pressed =
[&](igl::opengl::glfw::Viewer &, unsigned int key, int mod)
{
switch(key)
{
case 'M':
case 'm':
{
method = (Method)(((int)(method)+1)%((int)(NUM_METHODS)));
break;
}
case 'R':
case 'r':
{
push_undo();
s.CU = s.CV;
break;
}
case 'U':
case 'u':
{
// Just trigger an update
break;
}
case ' ':
push_undo();
s.placing_handles ^= 1;
if(!s.placing_handles && s.CV.rows()>0)
{
// Switching to deformation mode
s.CU = s.CV;
Eigen::VectorXi b;
igl::snap_points(s.CV,V,b);
// PRECOMPUTATION FOR DEFORMATION
biharmonic_precompute(V,F,b,biharmonic_data);
arap_precompute(V,F,b,arap_data,arap_K);
}
break;
default:
return false;
}
update();
return true;
};
// Special callback for handling undo
viewer.callback_key_down =
[&](igl::opengl::glfw::Viewer &, unsigned char key, int mod)->bool
{
if(key == 'Z' && (mod & GLFW_MOD_SUPER))
{
(mod & GLFW_MOD_SHIFT) ? redo() : undo();
update();
return true;
}
return false;
};
viewer.callback_pre_draw =
[&](igl::opengl::glfw::Viewer &)->bool
{
if(viewer.core.is_animating && !s.placing_handles && method == ARAP)
{
arap_single_iteration(arap_data,arap_K,s.CU,U);
update();
}
return false;
};
viewer.data().set_mesh(V,F);
viewer.data().show_lines = false;
viewer.core.is_animating = true;
viewer.data().face_based = true;
update();
viewer.launch();
return EXIT_SUCCESS;
}