main.cpp 9.0 KB

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