polyhedron_to_mesh.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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::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. }