main.cpp 5.2 KB

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