main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <igl/read_triangle_mesh.h>
  2. #include <igl/hessian_energy.h>
  3. #include <igl/curved_hessian_energy.h>
  4. #include <igl/massmatrix.h>
  5. #include <igl/cotmatrix.h>
  6. #include <igl/isolines_map.h>
  7. #include <igl/parula.h>
  8. #include <igl/vertex_components.h>
  9. #include <igl/remove_unreferenced.h>
  10. #include <igl/opengl/glfw/Viewer.h>
  11. #include <igl/heat_geodesics.h>
  12. #include <Eigen/Core>
  13. #include <Eigen/SparseCholesky>
  14. #include <iostream>
  15. #include <set>
  16. #include <limits>
  17. #include <stdlib.h>
  18. int main(int argc, char * argv[])
  19. {
  20. typedef Eigen::SparseMatrix<double> SparseMat;
  21. srand(57);
  22. //Read our mesh
  23. Eigen::MatrixXd V;
  24. Eigen::MatrixXi F;
  25. if(!igl::read_triangle_mesh(
  26. argc>1?argv[1]: TUTORIAL_SHARED_PATH "/beetle.off",V,F)) {
  27. std::cout << "Failed to load mesh." << std::endl;
  28. }
  29. //Constructing an exact function to smooth
  30. igl::HeatGeodesicsData<double> hgData;
  31. igl::heat_geodesics_precompute(V, F, hgData);
  32. Eigen::VectorXd heatDist;
  33. Eigen::VectorXi gamma(1); gamma << 1947; //1631;
  34. igl::heat_geodesics_solve(hgData, gamma, heatDist);
  35. Eigen::VectorXd zexact =
  36. 0.1*(heatDist.array() + (-heatDist.maxCoeff())).pow(2)
  37. + 3*V.block(0,1,V.rows(),1).array().cos();
  38. //Make the exact function noisy
  39. const double s = 0.1*(zexact.maxCoeff() - zexact.minCoeff());
  40. Eigen::VectorXd znoisy = zexact + s*Eigen::VectorXd::Random(zexact.size());
  41. //Constructing the squared Laplacian and squared Hessian energy
  42. SparseMat L, M;
  43. igl::cotmatrix(V, F, L);
  44. igl::massmatrix(V, F, igl::MASSMATRIX_TYPE_BARYCENTRIC, M);
  45. Eigen::SimplicialLDLT<SparseMat> solver(M);
  46. SparseMat MinvL = solver.solve(L);
  47. SparseMat QL = L.transpose()*MinvL;
  48. SparseMat QH;
  49. igl::hessian_energy(V, F, QH);
  50. SparseMat QcH;
  51. igl::curved_hessian_energy(V, F, QcH);
  52. //Solve to find Laplacian-smoothed Hessian-smoothed, and
  53. // curved-Hessian-smoothed solutions
  54. const double al = 3e-7;
  55. Eigen::SimplicialLDLT<SparseMat> lapSolver(al*QL + (1.-al)*M);
  56. Eigen::VectorXd zl = lapSolver.solve(al*M*znoisy);
  57. const double ah = 2e-7;
  58. Eigen::SimplicialLDLT<SparseMat> hessSolver(ah*QH + (1.-ah)*M);
  59. Eigen::VectorXd zh = hessSolver.solve(ah*M*znoisy);
  60. const double ach = 3e-7;
  61. Eigen::SimplicialLDLT<SparseMat> curvedHessSolver(al*QcH + (1.-ach)*M);
  62. Eigen::VectorXd zch = curvedHessSolver.solve(ach*M*znoisy);
  63. //Viewer that shows all functions: zexact, znoisy, zl, zh
  64. igl::opengl::glfw::Viewer viewer;
  65. viewer.data().set_mesh(V,F);
  66. viewer.data().show_lines = false;
  67. viewer.callback_key_down =
  68. [&](igl::opengl::glfw::Viewer & viewer, unsigned char key, int mod)->bool
  69. {
  70. //Graduate result to show isolines, then compute color matrix
  71. const Eigen::VectorXd* z;
  72. switch(key) {
  73. case '1':
  74. z = &zexact;
  75. break;
  76. case '2':
  77. z = &znoisy;
  78. break;
  79. case '3':
  80. z = &zl;
  81. break;
  82. case '4':
  83. z = &zh;
  84. break;
  85. case '5':
  86. z = &zch;
  87. break;
  88. default:
  89. return false;
  90. }
  91. viewer.data().set_data(*z);
  92. return true;
  93. };
  94. std::cout << R"(Smoothing a noisy function.
  95. Usage:
  96. 1 Show original function
  97. 2 Show noisy function
  98. 3 Biharmonic smoothing (zero Neumann boundary)
  99. 4 Biharmonic smoothing (natural planar Hessian boundary)
  100. 5 Biharmonic smoothing (natural curved Hessian boundary)
  101. )";
  102. Eigen::MatrixXd CM;
  103. igl::parula(Eigen::VectorXd::LinSpaced(21,0,1).eval(),false,CM);
  104. igl::isolines_map(Eigen::MatrixXd(CM),CM);
  105. viewer.data().set_colormap(CM);
  106. viewer.data().set_data(znoisy);
  107. viewer.launch();
  108. //Constructing a step function to smooth
  109. Eigen::VectorXd zstep = Eigen::VectorXd::Zero(V.rows());
  110. for(int i=0; i<V.rows(); ++i) {
  111. zstep(i) = V(i,2)<-0.25 ? 1. : (V(i,2)>0.31 ? 2. : 0);
  112. }
  113. //Smooth that function
  114. const double sl = 2e-5;
  115. Eigen::SimplicialLDLT<SparseMat> stepLapSolver(sl*QL + (1.-sl)*M);
  116. Eigen::VectorXd stepzl = stepLapSolver.solve(al*M*zstep);
  117. const double sh = 6e-6;
  118. Eigen::SimplicialLDLT<SparseMat> stepHessSolver(sh*QH + (1.-sh)*M);
  119. Eigen::VectorXd stepzh = stepHessSolver.solve(ah*M*zstep);
  120. const double sch = 2e-5;
  121. Eigen::SimplicialLDLT<SparseMat> stepCurvedHessSolver(sl*QcH + (1.-sch)*M);
  122. Eigen::VectorXd stepzch = stepCurvedHessSolver.solve(ach*M*zstep);
  123. //Display functions
  124. igl::opengl::glfw::Viewer viewer2;
  125. viewer2.data().set_mesh(V,F);
  126. viewer2.data().show_lines = false;
  127. viewer2.callback_key_down =
  128. [&](igl::opengl::glfw::Viewer & viewer, unsigned char key, int mod)->bool
  129. {
  130. //Graduate result to show isolines, then compute color matrix
  131. const Eigen::VectorXd* z;
  132. switch(key) {
  133. case '1':
  134. z = &zstep;
  135. break;
  136. case '2':
  137. z = &stepzl;
  138. break;
  139. case '3':
  140. z = &stepzh;
  141. break;
  142. case '4':
  143. z = &stepzch;
  144. break;
  145. default:
  146. return false;
  147. }
  148. viewer.data().set_data(*z);
  149. return true;
  150. };
  151. std::cout << R"(Smoothing a step function.
  152. Usage:
  153. 1 Show step function
  154. 2 Biharmonic smoothing (zero Neumann boundary)
  155. 3 Biharmonic smoothing (natural planar Hessian boundary)
  156. 4 Biharmonic smoothing (natural curved Hessian boundary)
  157. )";
  158. viewer2.data().set_colormap(CM);
  159. viewer2.data().set_data(zstep);
  160. viewer2.launch();
  161. return 0;
  162. }