main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <igl/serialize.h>
  17. #include <igl/xml/serialize_xml.h>
  18. // Input mesh
  19. Eigen::MatrixXd V;
  20. Eigen::MatrixXi F;
  21. // Face barycenters
  22. Eigen::MatrixXd B;
  23. // Scale for visualizing the fields
  24. double global_scale;
  25. bool extend_arrows = false;
  26. // Cross field
  27. Eigen::MatrixXd X1,X2;
  28. // Bisector field
  29. Eigen::MatrixXd BIS1, BIS2;
  30. // Combed bisector
  31. Eigen::MatrixXd BIS1_combed, BIS2_combed;
  32. // Per-corner, integer mismatches
  33. Eigen::MatrixXi MMatch;
  34. // Field singularities
  35. Eigen::VectorXi isSingularity, singularityIndex;
  36. // Per corner seams
  37. Eigen::MatrixXi Seams;
  38. // Combed field
  39. Eigen::MatrixXd X1_combed, X2_combed;
  40. // Global parametrization (with seams)
  41. Eigen::MatrixXd UV_seams;
  42. Eigen::MatrixXi FUV_seams;
  43. // Global parametrization
  44. Eigen::MatrixXd UV;
  45. Eigen::MatrixXi FUV;
  46. // Serialization state
  47. struct MIQState : public igl::Serializable
  48. {
  49. Eigen::MatrixXd UV;
  50. Eigen::MatrixXi FUV;
  51. // You have to define this function to
  52. // register the fields you want to serialize
  53. void InitSerialization()
  54. {
  55. this->Add(UV , "UV");
  56. this->Add(FUV , "FUV");
  57. }
  58. virtual ~MIQState(){}
  59. void printDiff(const MIQState &other, std::ostream& out = std::cout){
  60. std::cout << "Start Diff..\n";
  61. out << "Checking UV..\n";
  62. int nErrorsUV = 0;
  63. for(int i = 0; i < UV.rows(); i++){
  64. if(UV.row(i) != other.UV.row(i)){
  65. if(nErrorsUV == 0)
  66. out << "Index\tthisUV\totherUV\n";
  67. out << i << UV(i,0) << "\t" << UV(i,1) << "\t" << other.UV(i,0) << "\t" << other.UV(i,1) << std::endl;
  68. nErrorsUV++;
  69. }
  70. }
  71. out << "Checking FUV..\n";
  72. int nErrorsFUV = 0;
  73. for(int i = 0; i < FUV.rows(); i++){
  74. if(FUV.row(i) != other.FUV.row(i)){
  75. if(nErrorsFUV == 0)
  76. out << "Index\tthisFUV\totherFUV\n";
  77. out << i << FUV(i,0) << "\t" << FUV(i,1) << "\t" << other.FUV(i,0) << "\t" << other.FUV(i,1) << std::endl;
  78. nErrorsFUV++;
  79. }
  80. }
  81. std::cout << "Finished.\n" << nErrorsUV << " errors in UV.\n" << nErrorsFUV << " errors in FUV.\n";
  82. }
  83. };
  84. // Create a texture that hides the integer translation in the parametrization
  85. void line_texture(Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_R,
  86. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_G,
  87. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_B)
  88. {
  89. unsigned size = 128;
  90. unsigned lineWidth = 3;
  91. texture_R.setConstant(size, size, 255);
  92. for (unsigned i=0; i<size; ++i){
  93. for (unsigned j=0; j<lineWidth; ++j)
  94. texture_R(i,j) = 0;
  95. for (unsigned j=size-lineWidth; j<size; ++j)
  96. texture_R(i,j) = 0;
  97. }
  98. for (unsigned j=0; j<size; ++j){
  99. for (unsigned i=0; i<lineWidth; ++i)
  100. texture_R(i,j) = 0;
  101. for (unsigned i=size-lineWidth; i<size; ++i)
  102. texture_R(i,j) = 0;
  103. }
  104. texture_G = texture_R;
  105. texture_B = texture_R;
  106. }
  107. bool key_down(igl::viewer::Viewer& viewer, unsigned char key, int modifier)
  108. {
  109. if (key == 'E')
  110. {
  111. extend_arrows = !extend_arrows;
  112. }
  113. if (key <'1' || key >'8')
  114. return false;
  115. viewer.data.clear();
  116. viewer.core.show_lines = false;
  117. viewer.core.show_texture = false;
  118. if (key == '1')
  119. {
  120. // Cross field
  121. viewer.data.set_mesh(V, F);
  122. viewer.data.add_edges(extend_arrows ? B - global_scale*X1 : B, B + global_scale*X1 ,Eigen::RowVector3d(1,0,0));
  123. viewer.data.add_edges(extend_arrows ? B - global_scale*X2 : B, B + global_scale*X2 ,Eigen::RowVector3d(0,0,1));
  124. }
  125. if (key == '2')
  126. {
  127. // Bisector field
  128. viewer.data.set_mesh(V, F);
  129. viewer.data.add_edges(extend_arrows ? B - global_scale*BIS1 : B, B + global_scale*BIS1 ,Eigen::RowVector3d(1,0,0));
  130. viewer.data.add_edges(extend_arrows ? B - global_scale*BIS2 : B, B + global_scale*BIS2 ,Eigen::RowVector3d(0,0,1));
  131. }
  132. if (key == '3')
  133. {
  134. // Bisector field combed
  135. viewer.data.set_mesh(V, F);
  136. viewer.data.add_edges(extend_arrows ? B - global_scale*BIS1_combed : B, B + global_scale*BIS1_combed ,Eigen::RowVector3d(1,0,0));
  137. viewer.data.add_edges(extend_arrows ? B - global_scale*BIS2_combed : B, B + global_scale*BIS2_combed ,Eigen::RowVector3d(0,0,1));
  138. }
  139. if (key == '4')
  140. {
  141. // Singularities and cuts
  142. viewer.data.set_mesh(V, F);
  143. // Plot cuts
  144. int l_count = Seams.sum();
  145. Eigen::MatrixXd P1(l_count,3);
  146. Eigen::MatrixXd P2(l_count,3);
  147. for (unsigned i=0; i<Seams.rows(); ++i)
  148. {
  149. for (unsigned j=0; j<Seams.cols(); ++j)
  150. {
  151. if (Seams(i,j) != 0)
  152. {
  153. P1.row(l_count-1) = V.row(F(i,j));
  154. P2.row(l_count-1) = V.row(F(i,(j+1)%3));
  155. l_count--;
  156. }
  157. }
  158. }
  159. viewer.data.add_edges(P1, P2, Eigen::RowVector3d(1, 0, 0));
  160. // Plot the singularities as colored dots (red for negative, blue for positive)
  161. for (unsigned i=0; i<singularityIndex.size();++i)
  162. {
  163. if (singularityIndex(i) < 2 && singularityIndex(i) > 0)
  164. viewer.data.add_points(V.row(i),Eigen::RowVector3d(1,0,0));
  165. else if (singularityIndex(i) > 2)
  166. viewer.data.add_points(V.row(i),Eigen::RowVector3d(0,1,0));
  167. }
  168. }
  169. if (key == '5')
  170. {
  171. // Singularities and cuts, original field
  172. // Singularities and cuts
  173. viewer.data.set_mesh(V, F);
  174. viewer.data.add_edges(extend_arrows ? B - global_scale*X1_combed : B, B + global_scale*X1_combed ,Eigen::RowVector3d(1,0,0));
  175. viewer.data.add_edges(extend_arrows ? B - global_scale*X2_combed : B, B + global_scale*X2_combed ,Eigen::RowVector3d(0,0,1));
  176. // Plot cuts
  177. int l_count = Seams.sum();
  178. Eigen::MatrixXd P1(l_count,3);
  179. Eigen::MatrixXd P2(l_count,3);
  180. for (unsigned i=0; i<Seams.rows(); ++i)
  181. {
  182. for (unsigned j=0; j<Seams.cols(); ++j)
  183. {
  184. if (Seams(i,j) != 0)
  185. {
  186. P1.row(l_count-1) = V.row(F(i,j));
  187. P2.row(l_count-1) = V.row(F(i,(j+1)%3));
  188. l_count--;
  189. }
  190. }
  191. }
  192. viewer.data.add_edges(P1, P2, Eigen::RowVector3d(1, 0, 0));
  193. // Plot the singularities as colored dots (red for negative, blue for positive)
  194. for (unsigned i=0; i<singularityIndex.size();++i)
  195. {
  196. if (singularityIndex(i) < 2 && singularityIndex(i) > 0)
  197. viewer.data.add_points(V.row(i),Eigen::RowVector3d(1,0,0));
  198. else if (singularityIndex(i) > 2)
  199. viewer.data.add_points(V.row(i),Eigen::RowVector3d(0,1,0));
  200. }
  201. }
  202. if (key == '6')
  203. {
  204. // Global parametrization UV
  205. viewer.data.set_mesh(UV, FUV);
  206. viewer.data.set_uv(UV);
  207. viewer.core.show_lines = true;
  208. }
  209. if (key == '7')
  210. {
  211. // Global parametrization in 3D
  212. viewer.data.set_mesh(V, F);
  213. viewer.data.set_uv(UV,FUV);
  214. viewer.core.show_texture = true;
  215. }
  216. if (key == '8')
  217. {
  218. // Global parametrization in 3D with seams
  219. viewer.data.set_mesh(V, F);
  220. viewer.data.set_uv(UV_seams,FUV_seams);
  221. viewer.core.show_texture = true;
  222. }
  223. viewer.data.set_colors(Eigen::RowVector3d(1,1,1));
  224. // Replace the standard texture with an integer shift invariant texture
  225. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_R, texture_G, texture_B;
  226. line_texture(texture_R, texture_G, texture_B);
  227. viewer.data.set_texture(texture_R, texture_B, texture_G);
  228. viewer.core.align_camera_center(viewer.data.V,viewer.data.F);
  229. return false;
  230. }
  231. int main(int argc, char *argv[])
  232. {
  233. using namespace Eigen;
  234. // Load a mesh in OFF format
  235. igl::readOFF("../shared/3holes.off", V, F);
  236. // Compute face barycenters
  237. igl::barycenter(V, F, B);
  238. // Compute scale for visualizing fields
  239. global_scale = .5*igl::avg_edge_length(V, F);
  240. // Contrain one face
  241. VectorXi b(1);
  242. b << 0;
  243. MatrixXd bc(1,3);
  244. bc << 1, 0, 0;
  245. // Create a smooth 4-RoSy field
  246. VectorXd S;
  247. igl::comiso::nrosy(V,F,b,bc,VectorXi(),VectorXd(),MatrixXd(),4,0.5,X1,S);
  248. // Find the the orthogonal vector
  249. MatrixXd B1,B2,B3;
  250. igl::local_basis(V,F,B1,B2,B3);
  251. X2 = igl::rotate_vectors(X1, VectorXd::Constant(1,M_PI/2), B1, B2);
  252. double gradient_size = 50;
  253. double iter = 0;
  254. double stiffness = 5.0;
  255. bool direct_round = 0;
  256. // Always work on the bisectors, it is more general
  257. igl::compute_frame_field_bisectors(V, F, X1, X2, BIS1, BIS2);
  258. // Comb the field, implicitly defining the seams
  259. igl::comb_cross_field(V, F, BIS1, BIS2, BIS1_combed, BIS2_combed);
  260. // Find the integer mismatches
  261. igl::cross_field_missmatch(V, F, BIS1_combed, BIS2_combed, true, MMatch);
  262. // Find the singularities
  263. igl::find_cross_field_singularities(V, F, MMatch, isSingularity, singularityIndex);
  264. // Cut the mesh, duplicating all vertices on the seams
  265. igl::cut_mesh_from_singularities(V, F, MMatch, Seams);
  266. // Comb the frame-field accordingly
  267. igl::comb_frame_field(V, F, X1, X2, BIS1_combed, BIS2_combed, X1_combed, X2_combed);
  268. // Global parametrization
  269. igl::comiso::miq(V,
  270. F,
  271. X1_combed,
  272. X2_combed,
  273. MMatch,
  274. isSingularity,
  275. Seams,
  276. UV,
  277. FUV,
  278. gradient_size,
  279. stiffness,
  280. direct_round,
  281. iter,
  282. 5,
  283. true);
  284. // Global parametrization (with seams, only for demonstration)
  285. igl::comiso::miq(V,
  286. F,
  287. X1_combed,
  288. X2_combed,
  289. MMatch,
  290. isSingularity,
  291. Seams,
  292. UV_seams,
  293. FUV_seams,
  294. gradient_size,
  295. stiffness,
  296. direct_round,
  297. iter,
  298. 5,
  299. false);
  300. // Plot the mesh
  301. igl::viewer::Viewer viewer;
  302. // Plot the original mesh with a texture parametrization
  303. key_down(viewer,'7',0);
  304. //vertex to face adjacency for MIQ V to UV conversion
  305. std::vector<std::vector<int> > VF, VFi;
  306. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  307. //vertex to face adjacency for MIQ UV to V conversion
  308. std::vector<std::vector<int> > UVF, UVFi;
  309. igl::vertex_triangle_adjacency(UV, FUV, UVF, UVFi);
  310. // Add MIQ-Tools menu
  311. int vertexIndex = 0;
  312. bool addPoints = false;
  313. viewer.callback_init = [&](igl::viewer::Viewer& viewer)
  314. {
  315. viewer.ngui->addNewWindow(Eigen::Vector2i(200,10),"MIQ-Tools");
  316. viewer.ngui->addNewGroup("Find vertex");
  317. viewer.ngui->addVariable(vertexIndex,"Vertex Index", true);
  318. viewer.ngui->addVariable(addPoints, "add points");
  319. viewer.ngui->addButton("Find (UV)!", [&](){
  320. if(vertexIndex < 0 || vertexIndex > UV.rows()){
  321. std::cerr << "Vertex index " << vertexIndex << " not in range of UV.\n";
  322. return;
  323. }
  324. if(addPoints)
  325. viewer.data.add_points(UV.row(vertexIndex),Eigen::RowVector3d(1,1,0));
  326. else
  327. viewer.data.set_points(UV.row(vertexIndex),Eigen::RowVector3d(1,1,0));
  328. });
  329. viewer.ngui->addButton("Find (V)!", [&](){
  330. if(vertexIndex < 0 || vertexIndex > V.rows()){
  331. std::cerr << "Vertex index " << vertexIndex << " not in range of V.\n";
  332. return;
  333. }
  334. if(addPoints)
  335. viewer.data.add_points(V.row(vertexIndex),Eigen::RowVector3d(1,1,0));
  336. else
  337. viewer.data.set_points(V.row(vertexIndex),Eigen::RowVector3d(1,1,0));
  338. });
  339. viewer.ngui->addButton("Find converted UV->V", [&](){
  340. if(vertexIndex < 0 || vertexIndex > UV.rows()){
  341. std::cerr << "Vertex index " << vertexIndex << " not in range of UV.\n";
  342. return;
  343. }
  344. int vIndex = F(UVF[vertexIndex][0], UVFi[vertexIndex][0]);
  345. if(addPoints)
  346. viewer.data.add_points(V.row(vIndex), Eigen::RowVector3d(1,1,0));
  347. else
  348. viewer.data.set_points(V.row(vIndex), Eigen::RowVector3d(1,1,0));
  349. std::cout << "Index " << vertexIndex << "(UV) is index " << vIndex << "(V)\n";
  350. });
  351. viewer.ngui->addButton("Find converted V->UV", [&](){
  352. if(vertexIndex < 0 || vertexIndex > V.rows()){
  353. std::cerr << "Vertex index " << vertexIndex << " not in range of V.\n";
  354. return;
  355. }
  356. std::vector<int> uvIndices;
  357. for(int i = 0; i < VF[vertexIndex].size(); i++){
  358. uvIndices.push_back(FUV(VF[vertexIndex][i], VFi[vertexIndex][i]));
  359. }
  360. std::sort(uvIndices.begin(), uvIndices.end());
  361. for(auto it = uvIndices.begin(); it != uvIndices.end(); ++it){
  362. // Ignore duplicates
  363. if(it != uvIndices.begin())
  364. if(*it == *(it-1))
  365. continue;
  366. if(addPoints)
  367. viewer.data.add_points(UV.row(*it) ,Eigen::RowVector3d(1,1,0));
  368. else
  369. viewer.data.set_points(UV.row(*it) ,Eigen::RowVector3d(1,1,0));
  370. std::cout << "Index " << vertexIndex << "(V) is index " << *it << "(UV)\n";
  371. }
  372. });
  373. viewer.ngui->addNewGroup("Check results");
  374. viewer.ngui->addButton("Serialize results", [&](){
  375. auto path = nanogui::file_dialog({}, true);
  376. if(path != ""){
  377. MIQState miqstate;
  378. miqstate.UV = UV;
  379. miqstate.FUV = FUV;
  380. // Serialize the state of the application
  381. igl::serialize(miqstate,"MIQState",path,true);
  382. std::cout << "Serialized UV(" << UV.rows() << " rows) and FUV(" << FUV.rows() << " rows)\n";
  383. }
  384. });
  385. viewer.ngui->addButton("Check results with reference", [&](){
  386. auto path = nanogui::file_dialog({ {"*", "Any file"}, }, true);
  387. if(path != ""){
  388. MIQState miqstate_ref, miqstate;
  389. miqstate.UV = UV;
  390. miqstate.FUV = FUV;
  391. // Serialize the state of the application
  392. igl::deserialize(miqstate_ref,"MIQState",path);
  393. std::cout << "this = reference\n";
  394. miqstate_ref.printDiff(miqstate);
  395. }
  396. });
  397. viewer.ngui->layout();
  398. return false;
  399. };
  400. // Launch the viewer
  401. viewer.callback_key_down = &key_down;
  402. viewer.launch();
  403. }