main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <igl/readOFF.h>
  2. #include <igl/viewer/Viewer.h>
  3. #include <igl/comiso/miq.h>
  4. #include <igl/barycenter.h>
  5. #include <igl/avg_edge_length.h>
  6. #include <igl/comiso/nrosy.h>
  7. #include <sstream>
  8. #include <igl/rotate_vectors.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. // Cross field
  17. Eigen::MatrixXd X1,X2;
  18. // Bisector field
  19. Eigen::MatrixXd BIS1, BIS2;
  20. // Combed bisector
  21. Eigen::MatrixXd BIS1_combed, BIS2_combed;
  22. // Per-corner, integer mismatches
  23. Eigen::MatrixXi MMatch;
  24. // Field singularities
  25. Eigen::VectorXi isSingularity, singularityIndex;
  26. // Per corner seams
  27. Eigen::MatrixXi Seams;
  28. // Combed field
  29. Eigen::MatrixXd X1_combed, X2_combed;
  30. // Global parametrization
  31. Eigen::MatrixXd UV;
  32. Eigen::MatrixXi FUV;
  33. // Create a texture that hides the integer translation in the parametrization
  34. void line_texture(Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> &texture_R,
  35. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> &texture_G,
  36. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> &texture_B)
  37. {
  38. unsigned size = 128;
  39. unsigned size2 = size/2;
  40. unsigned lineWidth = 3;
  41. texture_R.setConstant(size, size, 255);
  42. for (unsigned i=0; i<size; ++i)
  43. for (unsigned j=size2-lineWidth; j<=size2+lineWidth; ++j)
  44. texture_R(i,j) = 0;
  45. for (unsigned i=size2-lineWidth; i<=size2+lineWidth; ++i)
  46. for (unsigned j=0; j<size; ++j)
  47. texture_R(i,j) = 0;
  48. texture_G = texture_R;
  49. texture_B = texture_R;
  50. }
  51. bool key_down(igl::Viewer& viewer, unsigned char key, int modifier)
  52. {
  53. if (key <'1' || key >'7')
  54. return false;
  55. viewer.clear_mesh();
  56. viewer.options.show_lines = false;
  57. viewer.options.show_texture = false;
  58. if (key == '1')
  59. {
  60. // Cross field
  61. viewer.set_mesh(V, F);
  62. viewer.add_edges (B, B + global_scale*X1 ,Eigen::RowVector3d(1,0,0));
  63. viewer.add_edges (B, B + global_scale*X2 ,Eigen::RowVector3d(0,0,1));
  64. }
  65. if (key == '2')
  66. {
  67. // Bisector field
  68. viewer.set_mesh(V, F);
  69. viewer.add_edges (B, B + global_scale*BIS1 ,Eigen::RowVector3d(1,0,0));
  70. viewer.add_edges (B, B + global_scale*BIS2 ,Eigen::RowVector3d(0,0,1));
  71. }
  72. if (key == '3')
  73. {
  74. // Bisector field combed
  75. viewer.set_mesh(V, F);
  76. viewer.add_edges (B, B + global_scale*BIS1_combed ,Eigen::RowVector3d(1,0,0));
  77. viewer.add_edges (B, B + global_scale*BIS2_combed ,Eigen::RowVector3d(0,0,1));
  78. }
  79. if (key == '4')
  80. {
  81. // Singularities and cuts
  82. viewer.set_mesh(V, F);
  83. // Plot cuts
  84. int l_count = Seams.sum();
  85. Eigen::MatrixXd P1(l_count,3);
  86. Eigen::MatrixXd P2(l_count,3);
  87. for (unsigned i=0; i<Seams.rows(); ++i)
  88. {
  89. for (unsigned j=0; j<Seams.cols(); ++j)
  90. {
  91. if (Seams(i,j) != 0)
  92. {
  93. P1.row(l_count-1) = V.row(F(i,j));
  94. P2.row(l_count-1) = V.row(F(i,(j+1)%3));
  95. l_count--;
  96. }
  97. }
  98. }
  99. viewer.add_edges(P1, P2, Eigen::RowVector3d(1, 0, 0));
  100. // Plot the singularities as colored dots (red for negative, blue for positive)
  101. for (unsigned i=0; i<singularityIndex.size();++i)
  102. {
  103. if (singularityIndex(i) < 2 && singularityIndex(i) > 0)
  104. viewer.add_points(V.row(i),Eigen::RowVector3d(1,0,0));
  105. else if (singularityIndex(i) > 2)
  106. viewer.add_points(V.row(i),Eigen::RowVector3d(0,1,0));
  107. }
  108. }
  109. if (key == '5')
  110. {
  111. // Singularities and cuts, original field
  112. // Singularities and cuts
  113. viewer.set_mesh(V, F);
  114. viewer.add_edges (B, B + global_scale*X1_combed ,Eigen::RowVector3d(1,0,0));
  115. viewer.add_edges (B, B + global_scale*X2_combed ,Eigen::RowVector3d(0,0,1));
  116. // Plot cuts
  117. int l_count = Seams.sum();
  118. Eigen::MatrixXd P1(l_count,3);
  119. Eigen::MatrixXd P2(l_count,3);
  120. for (unsigned i=0; i<Seams.rows(); ++i)
  121. {
  122. for (unsigned j=0; j<Seams.cols(); ++j)
  123. {
  124. if (Seams(i,j) != 0)
  125. {
  126. P1.row(l_count-1) = V.row(F(i,j));
  127. P2.row(l_count-1) = V.row(F(i,(j+1)%3));
  128. l_count--;
  129. }
  130. }
  131. }
  132. viewer.add_edges(P1, P2, Eigen::RowVector3d(1, 0, 0));
  133. // Plot the singularities as colored dots (red for negative, blue for positive)
  134. for (unsigned i=0; i<singularityIndex.size();++i)
  135. {
  136. if (singularityIndex(i) < 2 && singularityIndex(i) > 0)
  137. viewer.add_points(V.row(i),Eigen::RowVector3d(1,0,0));
  138. else if (singularityIndex(i) > 2)
  139. viewer.add_points(V.row(i),Eigen::RowVector3d(0,1,0));
  140. }
  141. }
  142. if (key == '6')
  143. {
  144. // Global parametrization UV
  145. viewer.set_mesh(UV, FUV);
  146. viewer.set_uv(UV);
  147. viewer.options.show_lines = true;
  148. }
  149. if (key == '7')
  150. {
  151. // Global parametrization in 3D
  152. viewer.set_mesh(V, F);
  153. viewer.set_uv(UV,FUV);
  154. viewer.options.show_texture = true;
  155. }
  156. viewer.set_colors(Eigen::RowVector3d(1,1,1));
  157. // Replace the standard texture with an integer shift invariant texture
  158. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> texture_R, texture_G, texture_B;
  159. line_texture(texture_R, texture_G, texture_B);
  160. viewer.set_texture(texture_R, texture_B, texture_G);
  161. return false;
  162. }
  163. int main(int argc, char *argv[])
  164. {
  165. using namespace Eigen;
  166. // Load a mesh in OFF format
  167. igl::readOFF("../shared/3holes.off", V, F);
  168. // Compute face barycenters
  169. igl::barycenter(V, F, B);
  170. // Compute scale for visualizing fields
  171. global_scale = .5*igl::avg_edge_length(V, F);
  172. // Contrain one face
  173. VectorXi b(1);
  174. b << 0;
  175. MatrixXd bc(1,3);
  176. bc << 1, 0, 0;
  177. // Create a smooth 4-RoSy field
  178. VectorXd S;
  179. igl::nrosy(V,F,b,bc,VectorXi(),VectorXd(),MatrixXd(),4,0.5,X1,S);
  180. // Find the the orthogonal vector
  181. MatrixXd B1,B2,B3;
  182. igl::local_basis(V,F,B1,B2,B3);
  183. X2 = igl::rotate_vectors(X1, VectorXd::Constant(1,M_PI/2), B1, B2);
  184. double gradient_size = 50;
  185. double iter = 0;
  186. double stiffness = 5.0;
  187. bool direct_round = 0;
  188. // Always work on the bisectors, it is more general
  189. igl::compute_frame_field_bisectors(V, F, X1, X2, BIS1, BIS2);
  190. // Comb the field, implicitly defining the seams
  191. igl::comb_cross_field(V, F, BIS1, BIS2, BIS1_combed, BIS2_combed);
  192. // Find the integer mismatches
  193. igl::cross_field_missmatch(V, F, BIS1_combed, BIS2_combed, true, MMatch);
  194. // Find the singularities
  195. igl::find_cross_field_singularities(V, F, MMatch, isSingularity, singularityIndex);
  196. // Cut the mesh, duplicating all vertices on the seams
  197. igl::cut_mesh_from_singularities(V, F, MMatch, isSingularity, singularityIndex, Seams);
  198. // Comb the frame-field accordingly
  199. igl::comb_frame_field(V, F, X1, X2, BIS1_combed, BIS2_combed, X1_combed, X2_combed);
  200. // Global parametrization
  201. igl::miq(V,
  202. F,
  203. X1_combed,
  204. X2_combed,
  205. BIS1_combed,
  206. BIS2_combed,
  207. MMatch,
  208. isSingularity,
  209. singularityIndex,
  210. Seams,
  211. UV,
  212. FUV,
  213. gradient_size,
  214. stiffness,
  215. direct_round,
  216. iter,
  217. 5,
  218. true);
  219. // Plot the mesh
  220. igl::Viewer viewer;
  221. // Plot the original mesh with a texture parametrization
  222. key_down(viewer,'7',0);
  223. // Launch the viewer
  224. viewer.callback_key_down = &key_down;
  225. viewer.launch();
  226. }