main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <igl/opengl/gl.h>
  2. #include <igl/arap.h>
  3. #include <igl/biharmonic_coordinates.h>
  4. #include <igl/cat.h>
  5. #include <igl/cotmatrix.h>
  6. #include <igl/massmatrix.h>
  7. #include <igl/matrix_to_list.h>
  8. #include <igl/parula.h>
  9. #include <igl/point_mesh_squared_distance.h>
  10. #include <igl/readDMAT.h>
  11. #include <igl/readMESH.h>
  12. #include <igl/remove_unreferenced.h>
  13. #include <igl/slice.h>
  14. #include <igl/writeDMAT.h>
  15. #include <igl/opengl/glfw/Viewer.h>
  16. #include <Eigen/Sparse>
  17. #include <iostream>
  18. #include <queue>
  19. #include "tutorial_shared_path.h"
  20. struct Mesh
  21. {
  22. Eigen::MatrixXd V,U;
  23. Eigen::MatrixXi T,F;
  24. } low,high,scene;
  25. Eigen::MatrixXd W;
  26. igl::ARAPData arap_data;
  27. int main(int argc, char * argv[])
  28. {
  29. using namespace Eigen;
  30. using namespace std;
  31. using namespace igl;
  32. if(!readMESH(TUTORIAL_SHARED_PATH "/octopus-low.mesh",low.V,low.T,low.F))
  33. {
  34. cout<<"failed to load mesh"<<endl;
  35. }
  36. if(!readMESH(TUTORIAL_SHARED_PATH "/octopus-high.mesh",high.V,high.T,high.F))
  37. {
  38. cout<<"failed to load mesh"<<endl;
  39. }
  40. // Precomputation
  41. {
  42. Eigen::VectorXi b;
  43. {
  44. Eigen::VectorXi J = Eigen::VectorXi::LinSpaced(high.V.rows(),0,high.V.rows()-1);
  45. Eigen::VectorXd sqrD;
  46. Eigen::MatrixXd _2;
  47. cout<<"Finding closest points..."<<endl;
  48. igl::point_mesh_squared_distance(low.V,high.V,J,sqrD,b,_2);
  49. assert(sqrD.minCoeff() < 1e-7 && "low.V should exist in high.V");
  50. }
  51. // force perfect positioning, rather have popping in low-res than high-res.
  52. // The correct/elaborate thing to do is express original low.V in terms of
  53. // linear interpolation (or extrapolation) via elements in (high.V,high.F)
  54. igl::slice(high.V,b,1,low.V);
  55. // list of points --> list of singleton lists
  56. std::vector<std::vector<int> > S;
  57. igl::matrix_to_list(b,S);
  58. cout<<"Computing weights for "<<b.size()<<
  59. " handles at "<<high.V.rows()<<" vertices..."<<endl;
  60. // Technically k should equal 3 for smooth interpolation in 3d, but 2 is
  61. // faster and looks OK
  62. const int k = 2;
  63. igl::biharmonic_coordinates(high.V,high.T,S,k,W);
  64. cout<<"Reindexing..."<<endl;
  65. // Throw away interior tet-vertices, keep weights and indices of boundary
  66. VectorXi I,J;
  67. igl::remove_unreferenced(high.V.rows(),high.F,I,J);
  68. for_each(high.F.data(),high.F.data()+high.F.size(),[&I](int & a){a=I(a);});
  69. for_each(b.data(),b.data()+b.size(),[&I](int & a){a=I(a);});
  70. igl::slice(MatrixXd(high.V),J,1,high.V);
  71. igl::slice(MatrixXd(W),J,1,W);
  72. }
  73. // Resize low res (high res will also be resized by affine precision of W)
  74. low.V.rowwise() -= low.V.colwise().mean();
  75. low.V /= (low.V.maxCoeff()-low.V.minCoeff());
  76. low.V.rowwise() += RowVector3d(0,1,0);
  77. low.U = low.V;
  78. high.U = high.V;
  79. arap_data.with_dynamics = true;
  80. arap_data.max_iter = 10;
  81. arap_data.energy = ARAP_ENERGY_TYPE_DEFAULT;
  82. arap_data.h = 0.01;
  83. arap_data.ym = 0.001;
  84. if(!arap_precomputation(low.V,low.T,3,VectorXi(),arap_data))
  85. {
  86. cerr<<"arap_precomputation failed."<<endl;
  87. return EXIT_FAILURE;
  88. }
  89. // Constant gravitational force
  90. Eigen::SparseMatrix<double> M;
  91. igl::massmatrix(low.V,low.T,igl::MASSMATRIX_TYPE_DEFAULT,M);
  92. const size_t n = low.V.rows();
  93. arap_data.f_ext = M * RowVector3d(0,-9.8,0).replicate(n,1);
  94. // Random initial velocities to wiggle things
  95. arap_data.vel = MatrixXd::Random(n,3);
  96. igl::opengl::glfw::Viewer viewer;
  97. // Create one huge mesh containing both meshes
  98. igl::cat(1,low.U,high.U,scene.U);
  99. igl::cat(1,low.F,MatrixXi(high.F.array()+low.V.rows()),scene.F);
  100. // Color each mesh
  101. viewer.data().set_mesh(scene.U,scene.F);
  102. MatrixXd C(scene.F.rows(),3);
  103. C<<
  104. RowVector3d(0.8,0.5,0.2).replicate(low.F.rows(),1),
  105. RowVector3d(0.3,0.4,1.0).replicate(high.F.rows(),1);
  106. viewer.data().set_colors(C);
  107. viewer.callback_key_pressed =
  108. [&](igl::opengl::glfw::Viewer & viewer,unsigned int key,int mods)->bool
  109. {
  110. switch(key)
  111. {
  112. default:
  113. return false;
  114. case ' ':
  115. viewer.core().is_animating = !viewer.core().is_animating;
  116. return true;
  117. case 'r':
  118. low.U = low.V;
  119. return true;
  120. }
  121. };
  122. viewer.callback_pre_draw = [&](igl::opengl::glfw::Viewer & viewer)->bool
  123. {
  124. glEnable(GL_CULL_FACE);
  125. if(viewer.core().is_animating)
  126. {
  127. arap_solve(MatrixXd(0,3),arap_data,low.U);
  128. for(int v = 0;v<low.U.rows();v++)
  129. {
  130. // collide with y=0 plane
  131. const int y = 1;
  132. if(low.U(v,y) < 0)
  133. {
  134. low.U(v,y) = -low.U(v,y);
  135. // ~ coefficient of restitution
  136. const double cr = 1.1;
  137. arap_data.vel(v,y) = - arap_data.vel(v,y) / cr;
  138. }
  139. }
  140. scene.U.block(0,0,low.U.rows(),low.U.cols()) = low.U;
  141. high.U = W * (low.U.rowwise() + RowVector3d(1,0,0));
  142. scene.U.block(low.U.rows(),0,high.U.rows(),high.U.cols()) = high.U;
  143. viewer.data().set_vertices(scene.U);
  144. viewer.data().compute_normals();
  145. }
  146. return false;
  147. };
  148. viewer.data().show_lines = false;
  149. viewer.core().is_animating = true;
  150. viewer.core().animation_max_fps = 30.;
  151. viewer.data().set_face_based(true);
  152. cout<<R"(
  153. [space] to toggle animation
  154. 'r' to reset positions
  155. )";
  156. viewer.core().rotation_type =
  157. igl::opengl::ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP;
  158. viewer.launch();
  159. }