main.cpp 2.0 KB

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