polyhedron_to_mesh.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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 "polyhedron_to_mesh.h"
  9. #include <CGAL/Polyhedron_3.h>
  10. template <typename Polyhedron>
  11. IGL_INLINE void igl::cgal::polyhedron_to_mesh(
  12. const Polyhedron & poly,
  13. Eigen::MatrixXd & V,
  14. Eigen::MatrixXi & F)
  15. {
  16. using namespace std;
  17. V.resize(poly.size_of_vertices(),3);
  18. F.resize(poly.size_of_facets(),3);
  19. typedef typename Polyhedron::Vertex_const_iterator Vertex_iterator;
  20. std::map<Vertex_iterator,size_t> vertex_to_index;
  21. {
  22. size_t v = 0;
  23. for(
  24. typename Polyhedron::Vertex_const_iterator p = poly.vertices_begin();
  25. p != poly.vertices_end();
  26. p++)
  27. {
  28. V(v,0) = p->point().x();
  29. V(v,1) = p->point().y();
  30. V(v,2) = p->point().z();
  31. vertex_to_index[p] = v;
  32. v++;
  33. }
  34. }
  35. {
  36. size_t f = 0;
  37. for(
  38. typename Polyhedron::Facet_const_iterator facet = poly.facets_begin();
  39. facet != poly.facets_end();
  40. ++facet)
  41. {
  42. typename Polyhedron::Halfedge_around_facet_const_circulator he =
  43. facet->facet_begin();
  44. // Facets in polyhedral surfaces are at least triangles.
  45. assert(CGAL::circulator_size(he) == 3 && "Facets should be triangles");
  46. size_t c = 0;
  47. do {
  48. //// This is stooopidly slow
  49. // F(f,c) = std::distance(poly.vertices_begin(), he->vertex());
  50. F(f,c) = vertex_to_index[he->vertex()];
  51. c++;
  52. } while ( ++he != facet->facet_begin());
  53. f++;
  54. }
  55. }
  56. }
  57. #ifdef IGL_STATIC_LIBRARY
  58. // Explicit template specialization
  59. #include <CGAL/Simple_cartesian.h>
  60. #include <CGAL/Polyhedron_items_with_id_3.h>
  61. template void igl::cgal::polyhedron_to_mesh<CGAL::Polyhedron_3<CGAL::Simple_cartesian<double>, CGAL::Polyhedron_items_with_id_3, CGAL::HalfedgeDS_default, std::allocator<int> > >(CGAL::Polyhedron_3<CGAL::Simple_cartesian<double>, CGAL::Polyhedron_items_with_id_3, CGAL::HalfedgeDS_default, std::allocator<int> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<int, -1, -1, 0, -1, -1>&);
  62. #endif