main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <igl/readOBJ.h>
  2. #include <igl/readDMAT.h>
  3. #include <igl/viewer/Viewer.h>
  4. #include <igl/barycenter.h>
  5. #include <igl/avg_edge_length.h>
  6. #include <vector>
  7. #include <igl/n_polyvector.h>
  8. #include <stdlib.h>
  9. // Input mesh
  10. Eigen::MatrixXd V;
  11. Eigen::MatrixXi F;
  12. // Face barycenters
  13. Eigen::MatrixXd B;
  14. // Scale for visualizing the fields
  15. double global_scale;
  16. // Input constraints
  17. Eigen::VectorXi isConstrained;
  18. std::vector<Eigen::MatrixXd> constraints;
  19. bool key_down(igl::Viewer& viewer, unsigned char key, int modifier)
  20. {
  21. using namespace std;
  22. using namespace Eigen;
  23. if (key <'1' || key >'4')
  24. return false;
  25. viewer.clear_mesh();
  26. viewer.set_mesh(V, F);
  27. viewer.options.show_lines = false;
  28. viewer.options.show_texture = false;
  29. int num = key - '0';
  30. // Interpolate
  31. cerr<<"Interpolating for n = "<<num<<"... ";
  32. // Interpolated polyVector field
  33. Eigen::MatrixXd pvf;
  34. igl::n_polyvector(V, F, isConstrained, constraints[num-1], pvf);
  35. cerr<<"done." <<endl;
  36. // Highlight in red the constrained faces
  37. MatrixXd C = MatrixXd::Constant(F.rows(),3,1);
  38. for (unsigned i=0; i<F.rows();++i)
  39. if (isConstrained[i])
  40. C.row(i) << 1, 0, 0;
  41. viewer.set_colors(C);
  42. for (int n =0; n<num; ++n)
  43. {
  44. // Frame field constraints
  45. MatrixXd F_t = MatrixXd::Zero(F.rows(),3);
  46. for (unsigned i=0; i<F.rows();++i)
  47. if (isConstrained[i])
  48. F_t.row(i) = constraints[num-1].block(i,n*3,1,3);
  49. const Eigen::MatrixXd &pvf_t = pvf.block(0,n*3,F.rows(),3);
  50. viewer.add_edges (B - global_scale*F_t, B + global_scale*F_t , Eigen::RowVector3d(0,0,1));
  51. viewer.add_edges (B - global_scale*pvf_t, B + global_scale*pvf_t , Eigen::RowVector3d(0,1,0));
  52. }
  53. return false;
  54. }
  55. int main(int argc, char *argv[])
  56. {
  57. using namespace Eigen;
  58. using namespace std;
  59. // Load a mesh in OBJ format
  60. igl::readOBJ("../shared/snail.obj", V, F);
  61. // Compute face barycenters
  62. igl::barycenter(V, F, B);
  63. // Compute scale for visualizing fields
  64. global_scale = .2*igl::avg_edge_length(V, F);
  65. // Allocate constraints and polyvector field
  66. constraints.resize(4);
  67. // Load constraints
  68. MatrixXd temp;
  69. for (int n =0; n<=3; ++n)
  70. {
  71. char cfile[1024]; sprintf(cfile, "/Users/olkido/Desktop/snail%d.dmat",n+1);
  72. igl::readDMAT(cfile,temp);
  73. if (n == 0)
  74. isConstrained = temp.block(0,0,temp.rows(),1).cast<int>();
  75. constraints[n] = temp.block(0,1,temp.rows(),temp.cols()-1);
  76. }
  77. igl::Viewer viewer;
  78. // Plot the original mesh with a texture parametrization
  79. key_down(viewer,'1',0);
  80. // Launch the viewer
  81. viewer.callback_key_down = &key_down;
  82. viewer.launch();
  83. }