main.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <igl/readOFF.h>
  2. #include <igl/viewer/Viewer.h>
  3. #include <igl/boundary_vertices_sorted.h>
  4. #include <igl/map_vertices_to_circle.h>
  5. #include <igl/harmonic.h>
  6. #include <igl/svd3x3/arap.h>
  7. Eigen::MatrixXd V;
  8. Eigen::MatrixXi F;
  9. Eigen::MatrixXd V_uv;
  10. Eigen::MatrixXd initial_guess;
  11. bool show_uv = false;
  12. bool key_down(igl::Viewer& viewer, unsigned char key, int modifier)
  13. {
  14. if (key == '1')
  15. show_uv = false;
  16. else if (key == '2')
  17. show_uv = true;
  18. if (key == 'q')
  19. V_uv = initial_guess;
  20. if (show_uv)
  21. viewer.set_mesh(V_uv,F);
  22. else
  23. viewer.set_mesh(V,F);
  24. viewer.compute_normals();
  25. return false;
  26. }
  27. int main(int argc, char *argv[])
  28. {
  29. // Load a mesh in OFF format
  30. igl::readOFF("../shared/camelhead.off", V, F);
  31. // Compute the initial solution for ARAP (harmonic parametrization)
  32. Eigen::VectorXi bnd;
  33. igl::boundary_vertices_sorted(V,F,bnd);
  34. Eigen::MatrixXd bnd_uv;
  35. igl::map_vertices_to_circle(V,bnd,bnd_uv);
  36. igl::harmonic(V,F,bnd,bnd_uv,1,initial_guess);
  37. // Add dynamic regularization to avoid to specify boundary conditions
  38. igl::ARAPData arap_data;
  39. arap_data.with_dynamics = true;
  40. Eigen::VectorXi b = Eigen::VectorXi::Zero(0);
  41. Eigen::MatrixXd bc = Eigen::MatrixXd::Zero(0,0);
  42. // Initialize ARAP
  43. arap_data.max_iter = 1000;
  44. Eigen::MatrixXd V_2d = V.block(0,0,V.rows(),2);
  45. arap_precomputation(V_2d,F,b,arap_data);
  46. // Solve arap using the harmonic map as initial guess
  47. V_uv = initial_guess;
  48. arap_solve(bc,arap_data,V_uv);
  49. // Scale UV to make the texture more clear
  50. // V_uv *= 5;
  51. // Plot the mesh
  52. igl::Viewer viewer;
  53. viewer.set_mesh(V, F);
  54. viewer.set_uv(V_uv);
  55. viewer.callback_key_down = &key_down;
  56. // Disable wireframe
  57. viewer.options.show_lines = false;
  58. // Draw checkerboard texture
  59. viewer.options.show_texture = true;
  60. // Launch the viewer
  61. viewer.launch();
  62. }