main.cpp 9.2 KB

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