main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/writeDMAT.h>
  14. #include <igl/opengl/glfw/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. // read the mesh, if the code is prepared outside of tutorial, the TUTORIAL_SHARED_PATH
  31. // should be the data folder
  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. // this will create a vector from 0 to V.rows()-1 where the gap is 1
  45. Eigen::VectorXi J = Eigen::VectorXi::LinSpaced(high.V.rows(),0,high.V.rows()-1);
  46. Eigen::VectorXd sqrD;
  47. Eigen::MatrixXd _2;
  48. cout<<"Finding closest points..."<<endl;
  49. // using J which is N by 1 instead of a matrix that represents faces of N by 3
  50. // so that we will find the closest vertices istead of closest point on the face
  51. // so far the two meshes are not seperated. So what we are really doing here
  52. // is computing handles from low resolution and use that for the high resolution one
  53. igl::point_mesh_squared_distance(low.V,high.V,J,sqrD,b,_2);
  54. assert(sqrD.minCoeff() < 1e-7 && "low.V should exist in high.V");
  55. }
  56. // force perfect positioning, rather have popping in low-res than high-res.
  57. // The correct/elaborate thing to do is express original low.V in terms of
  58. // linear interpolation (or extrapolation) via elements in (high.V,high.F)
  59. // this is to replace the vertices on low resolution
  60. // with the vertices in high resolution. b is the list of vertices
  61. // corresponding to the indices in high resolution which has closest
  62. // distance to the points in low resolution
  63. low.V = high.V(b,Eigen::placeholders::all);
  64. // list of points --> list of singleton lists
  65. std::vector<std::vector<int> > S;
  66. // S will hav size of low.V.rows() and each list inside will have 1 element
  67. igl::matrix_to_list(b,S);
  68. cout<<"Computing weights for "<<b.size()<<
  69. " handles at "<<high.V.rows()<<" vertices..."<<endl;
  70. // Technically k should equal 3 for smooth interpolation in 3d, but 2 is
  71. // faster and looks OK
  72. const int k = 2;
  73. // using all the points in low resolution as handles for the region
  74. // it will be too expansive to use all the points in high reolution as handles
  75. // but since low and high resembles the same thing, using points in low reesolution
  76. // will give you similar performance
  77. igl::biharmonic_coordinates(high.V,high.T,S,k,W);
  78. cout<<"Reindexing..."<<endl;
  79. // Throw away interior tet-vertices, keep weights and indices of boundary
  80. VectorXi I,J;
  81. igl::remove_unreferenced(high.V.rows(),high.F,I,J);
  82. for_each(high.F.data(),high.F.data()+high.F.size(),[&I](int & a){a=I(a);});
  83. for_each(b.data(),b.data()+b.size(),[&I](int & a){a=I(a);});
  84. high.V = high.V(J,Eigen::placeholders::all).eval();
  85. W = W(J,Eigen::placeholders::all).eval();
  86. }
  87. // Resize low res (high res will also be resized by affine precision of W)
  88. low.V.rowwise() -= low.V.colwise().mean();
  89. low.V /= (low.V.maxCoeff()-low.V.minCoeff());
  90. low.V.rowwise() += RowVector3d(0,1,0);
  91. low.U = low.V;
  92. high.U = high.V;
  93. arap_data.with_dynamics = true;
  94. arap_data.max_iter = 10;
  95. arap_data.energy = ARAP_ENERGY_TYPE_DEFAULT;
  96. arap_data.h = 0.01;
  97. arap_data.ym = 0.001;
  98. if(!arap_precomputation(low.V,low.T,3,VectorXi(),arap_data))
  99. {
  100. cerr<<"arap_precomputation failed."<<endl;
  101. return EXIT_FAILURE;
  102. }
  103. // Constant gravitational force
  104. Eigen::SparseMatrix<double> M;
  105. igl::massmatrix(low.V,low.T,igl::MASSMATRIX_TYPE_DEFAULT,M);
  106. const size_t n = low.V.rows();
  107. // f = ma
  108. arap_data.f_ext = M * RowVector3d(0,-9.8,0).replicate(n,1);
  109. // Random initial velocities to wiggle things
  110. arap_data.vel = MatrixXd::Random(n,3);
  111. igl::opengl::glfw::Viewer viewer;
  112. // Create one huge mesh containing both meshes
  113. igl::cat(1,low.U,high.U,scene.U);
  114. // need to remap the indices since we cat the V matrices
  115. igl::cat(1,low.F,MatrixXi(high.F.array()+low.V.rows()),scene.F);
  116. // Color each mesh
  117. viewer.data().set_mesh(scene.U,scene.F);
  118. MatrixXd C(scene.F.rows(),3);
  119. C<<
  120. RowVector3d(0.8,0.5,0.2).replicate(low.F.rows(),1),
  121. RowVector3d(0.3,0.4,1.0).replicate(high.F.rows(),1);
  122. viewer.data().set_colors(C);
  123. viewer.callback_key_pressed =
  124. [&](igl::opengl::glfw::Viewer & viewer,unsigned int key,int mods)->bool
  125. {
  126. switch(key)
  127. {
  128. default:
  129. return false;
  130. case ' ':
  131. viewer.core().is_animating = !viewer.core().is_animating;
  132. return true;
  133. case 'r':
  134. low.U = low.V;
  135. return true;
  136. }
  137. };
  138. viewer.callback_pre_draw = [&](igl::opengl::glfw::Viewer & viewer)->bool
  139. {
  140. glEnable(GL_CULL_FACE);
  141. if(viewer.core().is_animating)
  142. {
  143. arap_solve(MatrixXd(0,3),arap_data,low.U);
  144. for(int v = 0;v<low.U.rows();v++)
  145. {
  146. // collide with y=0 plane
  147. const int y = 1;
  148. if(low.U(v,y) < 0)
  149. {
  150. low.U(v,y) = -low.U(v,y);
  151. // ~ coefficient of restitution
  152. const double cr = 1.1;
  153. arap_data.vel(v,y) = - arap_data.vel(v,y) / cr;
  154. }
  155. }
  156. scene.U.block(0,0,low.U.rows(),low.U.cols()) = low.U;
  157. high.U = W * (low.U.rowwise() + RowVector3d(1,0,0));
  158. scene.U.block(low.U.rows(),0,high.U.rows(),high.U.cols()) = high.U;
  159. viewer.data().set_vertices(scene.U);
  160. viewer.data().compute_normals();
  161. }
  162. return false;
  163. };
  164. viewer.data().show_lines = false;
  165. viewer.core().is_animating = true;
  166. viewer.core().animation_max_fps = 30.;
  167. viewer.data().set_face_based(true);
  168. cout<<R"(
  169. [space] to toggle animation
  170. 'r' to reset positions
  171. )";
  172. viewer.core().rotation_type =
  173. igl::opengl::ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP;
  174. viewer.launch();
  175. }