main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2019 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include <Eigen/Core>
  9. #include <Eigen/Geometry>
  10. #include <igl/read_triangle_mesh.h>
  11. #include <igl/opengl/glfw/Viewer.h>
  12. #include <igl/iterative_closest_point.h>
  13. #include <igl/random_dir.h>
  14. #include <igl/PI.h>
  15. #include <igl/AABB.h>
  16. #include <igl/per_face_normals.h>
  17. #include <igl/avg_edge_length.h>
  18. #include <igl/per_vertex_normals.h>
  19. #include <igl/barycenter.h>
  20. #include <igl/slice.h>
  21. #include <igl/slice_mask.h>
  22. #include <iostream>
  23. int main(int argc, char * argv[])
  24. {
  25. Eigen::MatrixXd OVX,VX,VY;
  26. Eigen::MatrixXi FX,FY;
  27. igl::read_triangle_mesh( argc>1?argv[1]: TUTORIAL_SHARED_PATH "/decimated-max.obj",VY,FY);
  28. const double bbd = (VY.colwise().maxCoeff()-VY.colwise().minCoeff()).norm();
  29. FX = FY;
  30. {
  31. // sprinkle a noise so that we can see z-fighting when the match is perfect.
  32. const double h = igl::avg_edge_length(VY,FY);
  33. OVX = VY + 1e-2*h*Eigen::MatrixXd::Random(VY.rows(),VY.cols());
  34. }
  35. VX = OVX;
  36. igl::AABB<Eigen::MatrixXd,3> Ytree;
  37. Ytree.init(VY,FY);
  38. Eigen::MatrixXd NY;
  39. igl::per_face_normals(VY,FY,NY);
  40. igl::opengl::glfw::Viewer v;
  41. std::cout<<R"(
  42. [space] conduct a single iterative closest point iteration
  43. R,r reset to a random orientation and offset
  44. )";
  45. const auto apply_random_rotation = [&]()
  46. {
  47. const Eigen::Matrix3d R = Eigen::AngleAxisd(
  48. 2.*igl::PI*(double)rand()/RAND_MAX*0.3, igl::random_dir()).matrix();
  49. const Eigen::RowVector3d cen =
  50. 0.5*(VY.colwise().maxCoeff()+VY.colwise().minCoeff());
  51. VX = ((OVX*R).rowwise()+(cen-cen*R)).eval();
  52. };
  53. const auto single_iteration = [&]()
  54. {
  55. ////////////////////////////////////////////////////////////////////////
  56. // Perform single iteration of ICP method
  57. ////////////////////////////////////////////////////////////////////////
  58. Eigen::Matrix3d R;
  59. Eigen::RowVector3d t;
  60. igl::iterative_closest_point(VX,FX,VY,FY,Ytree,NY,1000,1,R,t);
  61. VX = ((VX*R).rowwise()+t).eval();
  62. v.data().set_mesh(VX,FX);
  63. v.data().compute_normals();
  64. };
  65. v.callback_pre_draw = [&](igl::opengl::glfw::Viewer &)->bool
  66. {
  67. if(v.core().is_animating)
  68. {
  69. single_iteration();
  70. }
  71. return false;
  72. };
  73. v.callback_key_pressed =
  74. [&](igl::opengl::glfw::Viewer &,unsigned char key,int)->bool
  75. {
  76. switch(key)
  77. {
  78. case ' ':
  79. {
  80. v.core().is_animating = false;
  81. single_iteration();
  82. return true;
  83. }
  84. case 'R':
  85. case 'r':
  86. // Random rigid transformation
  87. apply_random_rotation();
  88. v.data().set_mesh(VX,FX);
  89. v.data().compute_normals();
  90. return true;
  91. break;
  92. }
  93. return false;
  94. };
  95. v.data().set_mesh(VY,FY);
  96. v.data().set_colors(Eigen::RowVector3d(1,1,1));
  97. v.data().show_lines = false;
  98. v.append_mesh();
  99. v.data().set_mesh(VX,FX);
  100. v.data().show_lines = false;
  101. v.launch();
  102. }