main.cpp 5.2 KB

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