main.cpp 6.4 KB

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