main.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <igl/avg_edge_length.h>
  2. #include <igl/barycenter.h>
  3. #include <igl/local_basis.h>
  4. #include <igl/readOFF.h>
  5. #include <igl/comiso/nrosy.h>
  6. #include <igl/viewer/Viewer.h>
  7. #include "tutorial_shared_path.h"
  8. // Mesh
  9. Eigen::MatrixXd V;
  10. Eigen::MatrixXi F;
  11. // Constrained faces id
  12. Eigen::VectorXi b;
  13. // Cosntrained faces representative vector
  14. Eigen::MatrixXd bc;
  15. // Degree of the N-RoSy field
  16. int N = 4;
  17. // Converts a representative vector per face in the full set of vectors that describe
  18. // an N-RoSy field
  19. void representative_to_nrosy(
  20. const Eigen::MatrixXd& V,
  21. const Eigen::MatrixXi& F,
  22. const Eigen::MatrixXd& R,
  23. const int N,
  24. Eigen::MatrixXd& Y)
  25. {
  26. using namespace Eigen;
  27. using namespace std;
  28. MatrixXd B1, B2, B3;
  29. igl::local_basis(V,F,B1,B2,B3);
  30. Y.resize(F.rows()*N,3);
  31. for (unsigned i=0;i<F.rows();++i)
  32. {
  33. double x = R.row(i) * B1.row(i).transpose();
  34. double y = R.row(i) * B2.row(i).transpose();
  35. double angle = atan2(y,x);
  36. for (unsigned j=0; j<N;++j)
  37. {
  38. double anglej = angle + 2*M_PI*double(j)/double(N);
  39. double xj = cos(anglej);
  40. double yj = sin(anglej);
  41. Y.row(i*N+j) = xj * B1.row(i) + yj * B2.row(i);
  42. }
  43. }
  44. }
  45. // Plots the mesh with an N-RoSy field and its singularities on top
  46. // The constrained faces (b) are colored in red.
  47. void plot_mesh_nrosy(
  48. igl::viewer::Viewer& viewer,
  49. Eigen::MatrixXd& V,
  50. Eigen::MatrixXi& F,
  51. int N,
  52. Eigen::MatrixXd& PD1,
  53. Eigen::VectorXd& S,
  54. Eigen::VectorXi& b)
  55. {
  56. using namespace Eigen;
  57. using namespace std;
  58. // Clear the mesh
  59. viewer.data.clear();
  60. viewer.data.set_mesh(V,F);
  61. // Expand the representative vectors in the full vector set and plot them as lines
  62. double avg = igl::avg_edge_length(V, F);
  63. MatrixXd Y;
  64. representative_to_nrosy(V, F, PD1, N, Y);
  65. MatrixXd B;
  66. igl::barycenter(V,F,B);
  67. MatrixXd Be(B.rows()*N,3);
  68. for(unsigned i=0; i<B.rows();++i)
  69. for(unsigned j=0; j<N; ++j)
  70. Be.row(i*N+j) = B.row(i);
  71. viewer.data.add_edges(Be,Be+Y*(avg/2),RowVector3d(0,0,1));
  72. // Plot the singularities as colored dots (red for negative, blue for positive)
  73. for (unsigned i=0; i<S.size();++i)
  74. {
  75. if (S(i) < -0.001)
  76. viewer.data.add_points(V.row(i),RowVector3d(1,0,0));
  77. else if (S(i) > 0.001)
  78. viewer.data.add_points(V.row(i),RowVector3d(0,1,0));
  79. }
  80. // Highlight in red the constrained faces
  81. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  82. for (unsigned i=0; i<b.size();++i)
  83. C.row(b(i)) << 1, 0, 0;
  84. viewer.data.set_colors(C);
  85. }
  86. // It allows to change the degree of the field when a number is pressed
  87. bool key_down(igl::viewer::Viewer& viewer, unsigned char key, int modifier)
  88. {
  89. using namespace Eigen;
  90. using namespace std;
  91. if (key >= '1' && key <= '9')
  92. N = key - '0';
  93. MatrixXd R;
  94. VectorXd S;
  95. igl::comiso::nrosy(V,F,b,bc,VectorXi(),VectorXd(),MatrixXd(),N,0.5,R,S);
  96. plot_mesh_nrosy(viewer,V,F,N,R,S,b);
  97. return false;
  98. }
  99. int main(int argc, char *argv[])
  100. {
  101. using namespace std;
  102. using namespace Eigen;
  103. // Load a mesh in OFF format
  104. igl::readOFF(TUTORIAL_SHARED_PATH "/bumpy.off", V, F);
  105. // Threshold faces with high anisotropy
  106. b.resize(1);
  107. b << 0;
  108. bc.resize(1,3);
  109. bc << 1,1,1;
  110. igl::viewer::Viewer viewer;
  111. // Interpolate the field and plot
  112. key_down(viewer, '4', 0);
  113. // Plot the mesh
  114. viewer.data.set_mesh(V, F);
  115. viewer.callback_key_down = &key_down;
  116. // Disable wireframe
  117. viewer.core.show_lines = false;
  118. // Launch the viewer
  119. viewer.launch();
  120. }