main.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. using namespace std;
  30. // Load a mesh in OFF format
  31. igl::readOFF("../shared/camelhead.off", V, F);
  32. // Compute the initial solution for ARAP (harmonic parametrization)
  33. Eigen::VectorXi bnd;
  34. igl::boundary_vertices_sorted(V,F,bnd);
  35. Eigen::MatrixXd bnd_uv;
  36. igl::map_vertices_to_circle(V,bnd,bnd_uv);
  37. igl::harmonic(V,F,bnd,bnd_uv,1,initial_guess);
  38. // Add dynamic regularization to avoid to specify boundary conditions
  39. igl::ARAPData arap_data;
  40. arap_data.with_dynamics = true;
  41. Eigen::VectorXi b = Eigen::VectorXi::Zero(0);
  42. Eigen::MatrixXd bc = Eigen::MatrixXd::Zero(0,0);
  43. // Initialize ARAP
  44. arap_data.max_iter = 100;
  45. // 2 means that we're going to *solve* in 2d
  46. arap_precomputation(V,F,2,b,arap_data);
  47. // Solve arap using the harmonic map as initial guess
  48. V_uv = initial_guess;
  49. arap_solve(bc,arap_data,V_uv);
  50. // Scale UV to make the texture more clear
  51. V_uv *= 20;
  52. // Plot the mesh
  53. igl::Viewer viewer;
  54. viewer.set_mesh(V, F);
  55. viewer.set_uv(V_uv);
  56. viewer.callback_key_down = &key_down;
  57. // Disable wireframe
  58. viewer.options.show_lines = false;
  59. // Draw checkerboard texture
  60. viewer.options.show_texture = true;
  61. // Launch the viewer
  62. viewer.launch();
  63. }