generated from MultiW/libigl-example-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·462 lines (392 loc) · 11.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
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
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <Eigen/Geometry>
#include <imgui/imgui.h>
#include <igl/opengl/glfw/Viewer.h>
#include <igl/unproject_onto_mesh.h>
#include <igl/opengl/glfw/imgui/ImGuiMenu.h>
#include <igl/opengl/glfw/imgui/ImGuiHelpers.h>
#include <igl/bounding_box.h>
#include <igl/grid.h>
#include "include/grid_util.h"
#include "include/inner_void_mesh.h"
#define PI 3.14159265
const bool DEBUG = false;
const std::string DEFAULT_MESH_FILE = "../data/bunny.off";
// Viewer data indices
int mesh_data_id;
int inner_data_mesh_id;
int plane_data_id;
int gravity_data_id;
int carve_plane_data_id;
const Eigen::Vector3d DEFAULT_GRAVITY(0.0,-1.0,0.0);
const float DEFAULT_GRAVITY_YAW = 0.0;
const float DEFAULT_GRAVITY_PITCH = 0.0;
const float DEFAULT_GRAVITY_ROLL = 180.0;
// 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);
const Eigen::RowVector3d black(0.0,0.0,0.0);
// GUI objects
igl::opengl::glfw::Viewer viewer;
igl::opengl::glfw::imgui::ImGuiMenu menu;
// Undeformed objects
Eigen::MatrixXd origV;
Eigen::MatrixXi origF;
struct State
{
// object current position/orientation
Eigen::MatrixXd V;
Eigen::MatrixXi F;
Eigen::MatrixXd innerV;
Eigen::MatrixXi innerF;
Eigen::MatrixXd planeV;
Eigen::MatrixXi planeF;
Eigen::RowVector3d planeCenter;
int balancePointIdx;
// gravity unit vector (relative to original mesh position)
Eigen::Vector3d gravity;
// object orientation
float yaw;
float pitch;
float roll;
// user workflow state
bool selectBalancePoint = true;
bool selectOrientation = false;
bool isCarving = false;
// carving state
InnerVoidMesh *innerMesh = NULL;
} state;
double sin_deg(double angle)
{
return std::sin(angle * PI / 180.0);
}
double cos_deg(double angle)
{
return std::cos(angle * PI / 180.0);
}
Eigen::RowVector3d getBalancePoint()
{
return state.V.row(state.balancePointIdx);
}
// Update display of balance point
void updateBalancePoint()
{
viewer.data(mesh_data_id).clear_points();
viewer.data(mesh_data_id).set_points(getBalancePoint(), state.selectBalancePoint ? yellow : blue);
}
void updateGravityVector() {
// Convert yaw, pitch, roll to vector
// - source: https://stackoverflow.com/questions/1568568/how-to-convert-euler-angles-to-directional-vector
state.gravity(0) = -cos_deg(state.yaw) * sin_deg(state.pitch) * sin_deg(state.roll) - sin_deg(state.yaw) * cos_deg(state.roll);
state.gravity(1) = -sin_deg(state.yaw) * sin_deg(state.pitch) * sin_deg(state.roll) + cos_deg(state.yaw) * cos_deg(state.roll);
state.gravity(2) = cos_deg(state.pitch) * sin_deg(state.roll);
}
void updateGravity()
{
updateGravityVector();
if (DEBUG)
{
viewer.data(gravity_data_id).clear_points();
viewer.data(gravity_data_id).add_points(Eigen::RowVector3d(0.0, 0.0, 0.0), black);
viewer.data(gravity_data_id).add_points(state.gravity.transpose() / 10.0, black);
}
}
void updateMesh()
{
// Rotate mesh in the opposite direction from gravity
// - source: https://stackoverflow.com/questions/1568568/how-to-convert-euler-angles-to-directional-vector
Eigen::Matrix3d R, rollR, pitchR, yawR;
rollR <<
1, 0, 0,
0, cos_deg((double)state.roll-DEFAULT_GRAVITY_ROLL), -sin_deg((double)state.roll-DEFAULT_GRAVITY_ROLL),
0, sin_deg((double)state.roll-DEFAULT_GRAVITY_ROLL), cos_deg((double)state.roll-DEFAULT_GRAVITY_ROLL);
pitchR <<
cos_deg(state.pitch), 0, -sin_deg(state.pitch),
0, 1, 0,
sin_deg(state.pitch), 0, cos_deg(state.pitch);
yawR <<
cos_deg(state.yaw), -sin_deg(state.yaw), 0,
sin_deg(state.yaw), cos_deg(state.yaw), 0,
0, 0, 1;
state.V = origV * rollR.transpose();
state.V *= pitchR.transpose();
state.V *= yawR.transpose();
// Translate mesh to middle of plane
Eigen::RowVector3d translate = state.planeCenter - getBalancePoint();
for (int i = 0; i < state.V.rows(); i++)
{
state.V.row(i) += translate;
}
// Update viewer
viewer.data(mesh_data_id).set_vertices(state.V);
updateBalancePoint();
}
void updateInnerMesh()
{
viewer.data(inner_data_mesh_id).clear();
viewer.data(inner_data_mesh_id).set_mesh(state.innerV, state.innerF);
}
void clearInnerMesh()
{
state.innerV.resize(0,3);
state.innerF.resize(0,3);
viewer.data(inner_data_mesh_id).clear();
if (DEBUG)
{
viewer.data(carve_plane_data_id).clear_points();
}
}
bool findLowestPointIdx(const Eigen::MatrixXd &V, int &lowestIdx)
{
int heightCol = 1;
double minZ = V.col(heightCol).minCoeff();
lowestIdx = -1;
for (int i = 0; i < V.rows(); i++) {
if (V(i, heightCol) == minZ) {
lowestIdx = i;
break;
}
}
if (lowestIdx == -1) {
return false;
}
return true;
}
// Note: nothing related to the viewer yet
void initState(int argc, char *argv[])
{
// Load object
igl::read_triangle_mesh(argc > 1 ? argv[1] : DEFAULT_MESH_FILE, origV, origF);
// Align object to axis planes
Eigen::MatrixXd unalignedV = origV;
alignToAxis(unalignedV, origV);
// Initialize display V and F
state.V = origV;
state.F = origF;
// Create plane from bottom of the object's bounding box
Eigen::AlignedBox3d boundingBox;
createAlignedBox(origV, boundingBox);
state.planeV.resize(4,3);
state.planeV <<
boundingBox.corner(boundingBox.BottomLeftCeil).transpose(),
boundingBox.corner(boundingBox.BottomRightCeil).transpose(),
boundingBox.corner(boundingBox.BottomLeftFloor).transpose(),
boundingBox.corner(boundingBox.BottomRightFloor).transpose();
state.planeF.resize(2,3);
state.planeF <<
0, 1, 2,
2, 1, 3;
state.planeCenter = state.planeV.colwise().sum() / 4.0;
// Gravity points down by default
state.yaw = DEFAULT_GRAVITY_YAW;
state.pitch = DEFAULT_GRAVITY_PITCH;
state.roll = DEFAULT_GRAVITY_ROLL;
updateGravityVector();
// set lowest point as balance point
if (!findLowestPointIdx(origV, state.balancePointIdx)) {
std::cerr << "Failed to find index of lowest mesh point";
exit(-1);
}
}
// == Callback Functions ==
bool mouse_down(igl::opengl::glfw::Viewer& viewer, int x, int y)
{
// Select balance point
// - source: from deformation assignment starter code
Eigen::RowVector3f last_mouse = Eigen::RowVector3f(
viewer.current_mouse_x, viewer.core().viewport(3) - viewer.current_mouse_y, 0);
if (state.selectBalancePoint)
{
// 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,
state.V, state.F,
fid, bary))
{
long c;
bary.maxCoeff(&c);
state.balancePointIdx = state.F(fid, c);
// Snap to closest vertex on hit face
updateBalancePoint();
return true;
}
}
return false;
}
void draw_viewer_menu()
{
// Draw parent menu content
menu.draw_viewer_menu();
}
void draw_workflow_control_window()
{
// Define next window position + size
ImGui::SetNextWindowPos(ImVec2(180.f * menu.menu_scaling(), 10), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(250, 360), ImGuiCond_FirstUseEver);
ImGui::Begin(
"Make It Stand", nullptr,
ImGuiWindowFlags_NoSavedSettings
);
// Balance point selection
ImGui::Text("1. Balance Point");
if (ImGui::Checkbox("Select a balance point", &(state.selectBalancePoint)))
{
if (state.selectBalancePoint) // switching to balance point selection
{
// Set other checkboxes to false
state.selectOrientation = false;
state.isCarving = false;
// Update balance point color
updateBalancePoint();
// Undo carving
clearInnerMesh();
viewer.data(mesh_data_id).show_faces = true;
}
}
if (state.selectBalancePoint)
{
if (ImGui::Button("Update", ImVec2(-1, 0)))
{
updateMesh();
}
}
// Orientation selection
ImGui::Separator();
ImGui::Text("2. Orientation");
if (ImGui::Checkbox("Select an orientation", &(state.selectOrientation)))
{
if (state.selectOrientation) // switching to orientation selection
{
// Set other checkboxes to false
state.selectBalancePoint = false;
state.isCarving = false;
// Undo carving
clearInnerMesh();
viewer.data(mesh_data_id).show_faces = true;
// Align object to its balance point
updateMesh();
}
}
if (state.selectOrientation)
{
ImGui::SliderFloat("Yaw", &(state.yaw), 0.0, 360.0);
if (ImGui::IsItemActive())
{
updateGravity();
updateMesh();
}
ImGui::SliderFloat("Pitch", &(state.pitch), 0.0, 360.0);
if (ImGui::IsItemActive())
{
updateGravity();
updateMesh();
}
ImGui::SliderFloat("Roll", &(state.roll), 0.0, 360.0);
if (ImGui::IsItemActive())
{
updateGravity();
updateMesh();
}
}
// Carving selection
ImGui::Separator();
ImGui::Text("3. Inner Carving");
if (ImGui::Checkbox("Begin carving", &(state.isCarving)))
{
if (state.isCarving) // switching to carving
{
// Set other checkboxes to false
state.selectBalancePoint = false;
state.selectOrientation = false;
// Align object to its balance point
updateMesh();
// Display setup: voxelize inner mesh, make outer mesh transparent
viewer.data(mesh_data_id).show_faces = false;
if (state.innerMesh != NULL)
{
delete state.innerMesh;
}
state.innerMesh = new InnerVoidMesh(state.V, state.F, state.planeV, state.planeF, Eigen::Vector3d(0, -1, 0), getBalancePoint());
state.innerMesh->convertToMesh(state.innerV, state.innerF);
updateInnerMesh();
}
}
if (state.isCarving)
{
if (ImGui::Button("Carve", ImVec2(-1, 0)))
{
if (!state.innerMesh->isOptimized())
{
Eigen::MatrixXd carvePlaneV;
Eigen::MatrixXi carvePlaneF;
Eigen::Vector3d com;
state.innerMesh->carveInnerMeshStep(carvePlaneV, carvePlaneF, com);
state.innerMesh->convertToMesh(state.innerV, state.innerF);
updateInnerMesh();
if (DEBUG)
{
viewer.data(carve_plane_data_id).clear();
viewer.data(carve_plane_data_id).set_mesh(carvePlaneV, carvePlaneF);
viewer.data(carve_plane_data_id).add_points(com.transpose(), orange);
}
}
}
if (ImGui::Button("Finish Carving", ImVec2(-1, 0)))
{
state.innerMesh->carveInnerMesh();
state.innerMesh->convertToMesh(state.innerV, state.innerF);
updateInnerMesh();
}
if (state.innerMesh->isOptimized())
{
ImGui::Text("Completed Carving");
}
}
ImGui::End();
}
int main(int argc, char *argv[])
{
initState(argc, argv);
// == GUI ==
// menu
viewer.plugins.push_back(&menu);
menu.callback_draw_viewer_menu = draw_viewer_menu;
menu.callback_draw_custom_window = draw_workflow_control_window;
// callbacks
viewer.callback_mouse_down = mouse_down;
// == Display Objects ==
// display main mesh
mesh_data_id = viewer.data().id;
viewer.data(mesh_data_id).set_mesh(state.V, state.F);
updateMesh();
// display inner voxelized mesh (representing the empty space)
viewer.append_mesh();
inner_data_mesh_id = viewer.data().id;
// display plane (representing the ground)
viewer.append_mesh();
plane_data_id = viewer.data().id;
viewer.data(plane_data_id).set_mesh(state.planeV, state.planeF);
// gravity vector (for debugging)
if (DEBUG)
{
viewer.append_mesh();
gravity_data_id = viewer.data().id;
updateGravity();
viewer.append_mesh();
carve_plane_data_id = viewer.data().id;
}
// currently selected mesh is the input object
viewer.selected_data_index = viewer.mesh_index(mesh_data_id);
viewer.core().align_camera_center(state.planeV, state.planeF);
viewer.launch();
}