main.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <igl/readOFF.h>
  2. #include <igl/readDMAT.h>
  3. #include <igl/cotmatrix.h>
  4. #include <igl/massmatrix.h>
  5. #include <igl/jet.h>
  6. #include <igl/per_vertex_normals.h>
  7. #include <igl/viewer/Viewer.h>
  8. #include <iostream>
  9. Eigen::MatrixXd V,U;
  10. Eigen::MatrixXi F;
  11. Eigen::SparseMatrix<double> L;
  12. igl::Viewer viewer;
  13. int main(int argc, char *argv[])
  14. {
  15. using namespace Eigen;
  16. using namespace std;
  17. // Load a mesh in OFF format
  18. igl::readOFF("../shared/cow.off", V, F);
  19. // Compute Laplace-Beltrami operator: #V by #V
  20. igl::cotmatrix(V,F,L);
  21. const auto &key_down = [](igl::Viewer &viewer,unsigned char key,int mod)->bool
  22. {
  23. switch(key)
  24. {
  25. case 'r':
  26. case 'R':
  27. U = V;
  28. break;
  29. case ' ':
  30. {
  31. // Recompute just mass matrix on each step
  32. SparseMatrix<double> M;
  33. igl::massmatrix(U,F,igl::MASSMATRIX_VORONOI,M);
  34. // Solve (M-delta*L) U = M*U
  35. const auto & S = (M - 0.001*L);
  36. Eigen::SimplicialLLT<Eigen::SparseMatrix<double > > solver(S);
  37. assert(solver.info() == Eigen::Success);
  38. U = solver.solve(M*U).eval();
  39. // Normalize to unit surface area (important for numerics)
  40. U.array() /= sqrt(M.diagonal().array().sum());
  41. break;
  42. }
  43. default:
  44. return false;
  45. }
  46. // Send new positions, update normals, recenter
  47. viewer.set_vertices(U);
  48. viewer.compute_normals();
  49. viewer.align_camera_center();
  50. return true;
  51. };
  52. // Use original normals as pseudo-colors
  53. MatrixXd N;
  54. igl::per_vertex_normals(V,F,N);
  55. MatrixXd C = N.rowwise().normalized().array()*0.5+0.5;
  56. // Initialize smoothing with base mesh
  57. U = V;
  58. viewer.set_mesh(U, F);
  59. viewer.set_colors(C);
  60. viewer.callback_key_down = key_down;
  61. cout<<"Press [space] to smooth."<<endl;;
  62. cout<<"Press [r] to reset."<<endl;;
  63. viewer.launch();
  64. }