main.cpp 5.3 KB

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