polyhedron_to_mesh.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[email protected]>
  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. #include "assign_scalar.h"
  11. template <
  12. typename Polyhedron,
  13. typename DerivedV,
  14. typename DerivedF>
  15. IGL_INLINE void igl::copyleft::cgal::polyhedron_to_mesh(
  16. const Polyhedron & poly,
  17. Eigen::PlainObjectBase<DerivedV> & V,
  18. Eigen::PlainObjectBase<DerivedF> & F)
  19. {
  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. assign_scalar(p->point().x(),V(v,0));
  32. assign_scalar(p->point().y(),V(v,1));
  33. assign_scalar(p->point().z(),V(v,2));
  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. // generated by autoexplicit.sh
  63. #include <CGAL/Simple_cartesian.h>
  64. #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
  65. #include <CGAL/Polyhedron_items_with_id_3.h>
  66. template void igl::copyleft::cgal::polyhedron_to_mesh<CGAL::Polyhedron_3<CGAL::Epick, CGAL::Polyhedron_items_3, CGAL::HalfedgeDS_default, std::allocator<int> >, Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(CGAL::Polyhedron_3<CGAL::Epick, CGAL::Polyhedron_items_3, CGAL::HalfedgeDS_default, std::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  67. template void igl::copyleft::cgal::polyhedron_to_mesh<CGAL::Polyhedron_3<CGAL::Epick, CGAL::Polyhedron_items_3, CGAL::HalfedgeDS_default, std::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::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  68. 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::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::allocator<int> > const&,Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0,-1, -1> >&);
  69. #endif