main.cpp 8.9 KB

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