complex_to_mesh.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "complex_to_mesh.h"
  9. #include <igl/centroid.h>
  10. #include <igl/remove_unreferenced.h>
  11. #include <CGAL/Surface_mesh_default_triangulation_3.h>
  12. #include <CGAL/Delaunay_triangulation_cell_base_with_circumcenter_3.h>
  13. #include <set>
  14. #include <stack>
  15. template <typename Tr, typename DerivedV, typename DerivedF>
  16. IGL_INLINE bool igl::complex_to_mesh(
  17. const CGAL::Complex_2_in_triangulation_3<Tr> & c2t3,
  18. Eigen::PlainObjectBase<DerivedV> & V,
  19. Eigen::PlainObjectBase<DerivedF> & F)
  20. {
  21. using namespace igl;
  22. using namespace Eigen;
  23. // CGAL/IO/Complex_2_in_triangulation_3_file_writer.h
  24. using CGAL::Surface_mesher::number_of_facets_on_surface;
  25. typedef typename CGAL::Complex_2_in_triangulation_3<Tr> C2t3;
  26. typedef typename Tr::Finite_facets_iterator Finite_facets_iterator;
  27. typedef typename Tr::Finite_vertices_iterator Finite_vertices_iterator;
  28. typedef typename Tr::Facet Facet;
  29. typedef typename Tr::Edge Edge;
  30. typedef typename Tr::Vertex_handle Vertex_handle;
  31. // Header.
  32. const Tr& tr = c2t3.triangulation();
  33. bool success = true;
  34. const int n = tr.number_of_vertices();
  35. const int m = c2t3.number_of_facets();
  36. assert(m == number_of_facets_on_surface(tr));
  37. // Finite vertices coordinates.
  38. std::map<Vertex_handle, int> v2i;
  39. V.resize(n,3);
  40. {
  41. int v = 0;
  42. for(Finite_vertices_iterator vit = tr.finite_vertices_begin();
  43. vit != tr.finite_vertices_end();
  44. ++vit)
  45. {
  46. V(v,0) = vit->point().x();
  47. V(v,1) = vit->point().y();
  48. V(v,2) = vit->point().z();
  49. v2i[vit] = v++;
  50. }
  51. }
  52. {
  53. Finite_facets_iterator fit = tr.finite_facets_begin();
  54. std::set<Facet> oriented_set;
  55. std::stack<Facet> stack;
  56. while ((int)oriented_set.size() != m)
  57. {
  58. while ( fit->first->is_facet_on_surface(fit->second) == false ||
  59. oriented_set.find(*fit) != oriented_set.end() ||
  60. oriented_set.find(c2t3.opposite_facet(*fit)) !=
  61. oriented_set.end() )
  62. {
  63. ++fit;
  64. }
  65. oriented_set.insert(*fit);
  66. stack.push(*fit);
  67. while(! stack.empty() )
  68. {
  69. Facet f = stack.top();
  70. stack.pop();
  71. for(int ih = 0 ; ih < 3 ; ++ih)
  72. {
  73. const int i1 = tr.vertex_triple_index(f.second, tr. cw(ih));
  74. const int i2 = tr.vertex_triple_index(f.second, tr.ccw(ih));
  75. const typename C2t3::Face_status face_status
  76. = c2t3.face_status(Edge(f.first, i1, i2));
  77. if(face_status == C2t3::REGULAR)
  78. {
  79. Facet fn = c2t3.neighbor(f, ih);
  80. if (oriented_set.find(fn) == oriented_set.end())
  81. {
  82. if(oriented_set.find(c2t3.opposite_facet(fn)) == oriented_set.end())
  83. {
  84. oriented_set.insert(fn);
  85. stack.push(fn);
  86. }else {
  87. success = false; // non-orientable
  88. }
  89. }
  90. }else if(face_status != C2t3::BOUNDARY)
  91. {
  92. success = false; // non manifold, thus non-orientable
  93. }
  94. } // end "for each neighbor of f"
  95. } // end "stack non empty"
  96. } // end "oriented_set not full"
  97. F.resize(m,3);
  98. int f = 0;
  99. for(typename std::set<Facet>::const_iterator fit =
  100. oriented_set.begin();
  101. fit != oriented_set.end();
  102. ++fit)
  103. {
  104. const typename Tr::Cell_handle cell = fit->first;
  105. const int& index = fit->second;
  106. const int index1 = v2i[cell->vertex(tr.vertex_triple_index(index, 0))];
  107. const int index2 = v2i[cell->vertex(tr.vertex_triple_index(index, 1))];
  108. const int index3 = v2i[cell->vertex(tr.vertex_triple_index(index, 2))];
  109. // This order is flipped
  110. F(f,0) = index1;
  111. F(f,1) = index2;
  112. F(f,2) = index3;
  113. f++;
  114. }
  115. assert(f == m);
  116. } // end if(facets must be oriented)
  117. // This CGAL code seems to randomly assign the global orientation
  118. // Flip based on the signed volume.
  119. Eigen::Vector3d cen;
  120. double vol;
  121. igl::centroid(V,F,cen,vol);
  122. if(vol < 0)
  123. {
  124. // Flip
  125. F = F.rowwise().reverse().eval();
  126. }
  127. // CGAL code somehow can end up with unreferenced vertices
  128. VectorXi _1;
  129. remove_unreferenced( MatrixXd(V), MatrixXi(F), V,F,_1);
  130. return success;
  131. }
  132. #ifdef IGL_STATIC_LIBRARY
  133. template bool igl::complex_to_mesh<CGAL::Delaunay_triangulation_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_data_structure_3<CGAL::Surface_mesh_vertex_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_vertex_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_ds_vertex_base_3<void> > >, CGAL::Delaunay_triangulation_cell_base_with_circumcenter_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Surface_mesh_cell_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_cell_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_ds_cell_base_3<void> > > >, CGAL::Sequential_tag>, CGAL::Default, CGAL::Default>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(CGAL::Complex_2_in_triangulation_3<CGAL::Delaunay_triangulation_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_data_structure_3<CGAL::Surface_mesh_vertex_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_vertex_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_ds_vertex_base_3<void> > >, CGAL::Delaunay_triangulation_cell_base_with_circumcenter_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Surface_mesh_cell_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_cell_base_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, CGAL::Triangulation_ds_cell_base_3<void> > > >, CGAL::Sequential_tag>, CGAL::Default, CGAL::Default>, void> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  134. #endif