main.cpp 8.9 KB

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