main.cpp 3.3 KB

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