main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "tutorial_shared_path.h"
  2. #include <igl/read_triangle_mesh.h>
  3. #include <igl/triangulated_grid.h>
  4. #include <igl/heat_geodesics.h>
  5. #include <igl/unproject_onto_mesh.h>
  6. #include <igl/avg_edge_length.h>
  7. #include <igl/isolines_map.h>
  8. #include <igl/opengl/glfw/Viewer.h>
  9. #include <igl/opengl/create_shader_program.h>
  10. #include <igl/opengl/destroy_shader_program.h>
  11. #include <iostream>
  12. void set_colormap(igl::opengl::glfw::Viewer & viewer)
  13. {
  14. const int num_intervals = 30;
  15. Eigen::MatrixXd CM(num_intervals,3);
  16. // Colormap texture
  17. for(int i = 0;i<num_intervals;i++)
  18. {
  19. double t = double(num_intervals - i - 1)/double(num_intervals-1);
  20. CM(i,0) = std::max(std::min(2.0*t-0.0,1.0),0.0);
  21. CM(i,1) = std::max(std::min(2.0*t-1.0,1.0),0.0);
  22. CM(i,2) = std::max(std::min(6.0*t-5.0,1.0),0.0);
  23. }
  24. igl::isolines_map(Eigen::MatrixXd(CM),CM);
  25. viewer.data().set_colormap(CM);
  26. }
  27. int main(int argc, char *argv[])
  28. {
  29. // Create the peak height field
  30. Eigen::MatrixXi F;
  31. Eigen::MatrixXd V;
  32. igl::read_triangle_mesh( argc>1?argv[1]: TUTORIAL_SHARED_PATH "/beetle.off",V,F);
  33. // Precomputation
  34. igl::HeatGeodesicsData<double> data;
  35. double t = std::pow(igl::avg_edge_length(V,F),2);
  36. const auto precompute = [&]()
  37. {
  38. if(!igl::heat_geodesics_precompute(V,F,t,data))
  39. {
  40. std::cerr<<"Error: heat_geodesics_precompute failed."<<std::endl;
  41. exit(EXIT_FAILURE);
  42. };
  43. };
  44. precompute();
  45. igl::opengl::glfw::Viewer viewer;
  46. bool down_on_mesh = false;
  47. const auto update = [&]()->bool
  48. {
  49. int fid;
  50. Eigen::Vector3f bc;
  51. // Cast a ray in the view direction starting from the mouse position
  52. double x = viewer.current_mouse_x;
  53. double y = viewer.core().viewport(3) - viewer.current_mouse_y;
  54. if(igl::unproject_onto_mesh(Eigen::Vector2f(x,y), viewer.core().view,
  55. viewer.core().proj, viewer.core().viewport, V, F, fid, bc))
  56. {
  57. Eigen::VectorXd D;
  58. // if big mesh, just use closest vertex. Otherwise, blend distances to
  59. // vertices of face using barycentric coordinates.
  60. if(F.rows()>100000)
  61. {
  62. // 3d position of hit
  63. const Eigen::RowVector3d m3 =
  64. V.row(F(fid,0))*bc(0) + V.row(F(fid,1))*bc(1) + V.row(F(fid,2))*bc(2);
  65. int cid = 0;
  66. Eigen::Vector3d(
  67. (V.row(F(fid,0))-m3).squaredNorm(),
  68. (V.row(F(fid,1))-m3).squaredNorm(),
  69. (V.row(F(fid,2))-m3).squaredNorm()).minCoeff(&cid);
  70. const int vid = F(fid,cid);
  71. igl::heat_geodesics_solve(data,(Eigen::VectorXi(1,1)<<vid).finished(),D);
  72. }else
  73. {
  74. D = Eigen::VectorXd::Zero(V.rows());
  75. for(int cid = 0;cid<3;cid++)
  76. {
  77. const int vid = F(fid,cid);
  78. Eigen::VectorXd Dc;
  79. igl::heat_geodesics_solve(data,(Eigen::VectorXi(1,1)<<vid).finished(),Dc);
  80. D += Dc*bc(cid);
  81. }
  82. }
  83. viewer.data().set_data(D);
  84. return true;
  85. }
  86. return false;
  87. };
  88. viewer.callback_mouse_down =
  89. [&](igl::opengl::glfw::Viewer& viewer, int, int)->bool
  90. {
  91. if(update())
  92. {
  93. down_on_mesh = true;
  94. return true;
  95. }
  96. return false;
  97. };
  98. viewer.callback_mouse_move =
  99. [&](igl::opengl::glfw::Viewer& viewer, int, int)->bool
  100. {
  101. if(down_on_mesh)
  102. {
  103. update();
  104. return true;
  105. }
  106. return false;
  107. };
  108. viewer.callback_mouse_up =
  109. [&down_on_mesh](igl::opengl::glfw::Viewer& viewer, int, int)->bool
  110. {
  111. down_on_mesh = false;
  112. return false;
  113. };
  114. std::cout<<R"(Usage:
  115. [click] Click on shape to pick new geodesic distance source
  116. ,/. Decrease/increase t by factor of 10.0
  117. D,d Toggle using intrinsic Delaunay discrete differential operators
  118. )";
  119. viewer.callback_key_pressed =
  120. [&](igl::opengl::glfw::Viewer& /*viewer*/, unsigned int key, int mod)->bool
  121. {
  122. switch(key)
  123. {
  124. default:
  125. return false;
  126. case 'D':
  127. case 'd':
  128. data.use_intrinsic_delaunay = !data.use_intrinsic_delaunay;
  129. std::cout<<(data.use_intrinsic_delaunay?"":"not ")<<
  130. "using intrinsic delaunay..."<<std::endl;
  131. precompute();
  132. update();
  133. break;
  134. case '.':
  135. case ',':
  136. t *= (key=='.'?10.0:0.1);
  137. precompute();
  138. update();
  139. std::cout<<"t: "<<t<<std::endl;
  140. break;
  141. }
  142. return true;
  143. };
  144. // Show mesh
  145. viewer.data().set_mesh(V, F);
  146. viewer.data().set_data(Eigen::VectorXd::Zero(V.rows()));
  147. set_colormap(viewer);
  148. viewer.data().show_lines = false;
  149. viewer.launch();
  150. }