main.cpp 2.1 KB

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