main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #define IGL_HEADER_ONLY
  2. #include <igl/readOBJ.h>
  3. #include <igl/viewer/Viewer.h>
  4. #include <igl/mixed_integer_quadrangulate.h>
  5. #include <igl/barycenter.h>
  6. #include <igl/avg_edge_length.h>
  7. #include <sstream>
  8. void line_texture(Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> &texture_R,
  9. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> &texture_G,
  10. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> &texture_B)
  11. {
  12. unsigned size = 128;
  13. unsigned size2 = size/2;
  14. unsigned lineWidth = 3;
  15. texture_R.setConstant(size, size, 255);
  16. for (unsigned i=0; i<size; ++i)
  17. for (unsigned j=size2-lineWidth; j<=size2+lineWidth; ++j)
  18. texture_R(i,j) = 0;
  19. for (unsigned i=size2-lineWidth; i<=size2+lineWidth; ++i)
  20. for (unsigned j=0; j<size; ++j)
  21. texture_R(i,j) = 0;
  22. texture_G = texture_R;
  23. texture_B = texture_R;
  24. }
  25. bool readPolyVf(const char *fname,
  26. Eigen::VectorXi &isConstrained,
  27. std::vector<Eigen::MatrixXd> &polyVF)
  28. {
  29. FILE *fp = fopen(fname,"r");
  30. if (!fp)
  31. return false;
  32. int degree, numF;
  33. if (fscanf(fp,"%d %d", &degree, &numF) !=2)
  34. return false;
  35. polyVF.resize(degree, Eigen::MatrixXd::Zero(numF, 3));
  36. isConstrained.setZero(numF,1);
  37. int vali; float u0,u1,u2;
  38. for (int i = 0; i<numF; ++i)
  39. {
  40. if (fscanf(fp,"%d", &vali)!=1)
  41. return false;
  42. isConstrained[i] = vali;
  43. for (int j = 0; j<degree; ++j)
  44. {
  45. if (fscanf(fp,"%g %g %g", &u0, &u1, &u2) !=3)
  46. return false;
  47. polyVF[j](i,0) = u0;
  48. polyVF[j](i,1) = u1;
  49. polyVF[j](i,2) = u2;
  50. }
  51. }
  52. fclose(fp);
  53. return true;
  54. }
  55. void writePolyVf(const char *fname,
  56. const Eigen::VectorXi &isConstrained,
  57. const std::vector<Eigen::MatrixXd> &polyVF)
  58. {
  59. int numF = polyVF[0].rows();
  60. int degree = polyVF.size();
  61. FILE *fp = fopen(fname,"w");
  62. fprintf(fp,"%d %d\n", degree,numF);
  63. for (int i = 0; i<numF; ++i)
  64. {
  65. fprintf(fp,"%d ", isConstrained[i]);
  66. for (int j = 0; j<degree; ++j)
  67. fprintf(fp,"%.15g %.15g %.15g ", polyVF[j](i,0), polyVF[j](i,1), polyVF[j](i,2));
  68. fprintf(fp, "\n");
  69. }
  70. fclose(fp);
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. Eigen::MatrixXd V;
  75. Eigen::MatrixXi F;
  76. // Load a mesh in OFF format
  77. igl::readOBJ("../shared/lilium.obj", V, F);
  78. // Load a frame field
  79. Eigen::VectorXi isConstrained;
  80. std::vector<Eigen::MatrixXd> polyVF;
  81. readPolyVf("../shared/lilium.crossfield", isConstrained, polyVF);
  82. Eigen::MatrixXd UV;
  83. Eigen::MatrixXi FUV;
  84. double gradientSize = 50;
  85. double quadIter = 0;
  86. double stiffness = 5.0;
  87. bool directRound = 1;
  88. igl::mixed_integer_quadrangulate(V,
  89. F,
  90. polyVF[0],
  91. polyVF[1],
  92. UV,
  93. FUV,
  94. gradientSize,
  95. stiffness,
  96. directRound,
  97. quadIter);
  98. // Face barycenters
  99. Eigen::MatrixXd MF;
  100. igl::barycenter(V, F, MF);
  101. double scale = .5*igl::avg_edge_length(V, F);
  102. // Plot the mesh
  103. igl::Viewer viewer;
  104. viewer.set_mesh(V, F);
  105. // Plot the field
  106. viewer.add_edges (MF, MF+scale*polyVF[0],Eigen::RowVector3d(1,0,1));
  107. viewer.add_edges (MF, MF+scale*polyVF[1],Eigen::RowVector3d(1,0,1));
  108. viewer.set_uv(UV,FUV);
  109. viewer.options.show_texture = true;
  110. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> texture_R, texture_G, texture_B;
  111. line_texture(texture_R, texture_G, texture_B);
  112. viewer.set_texture(texture_R, texture_B, texture_G);
  113. // Increase the thickness of the lines
  114. viewer.options.line_width = 2.0f;
  115. // Launch the viewer
  116. viewer.launch();
  117. }