main.cpp 8.1 KB

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