polyhedron_to_mesh.cpp 3.0 KB

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