main.cpp 4.5 KB

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