main.cpp 3.5 KB

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