mesh_to_polyhedron.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "mesh_to_polyhedron.h"
  9. #include <CGAL/Polyhedron_3.h>
  10. #include <CGAL/Polyhedron_incremental_builder_3.h>
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename Polyhedron>
  15. IGL_INLINE bool igl::copyleft::cgal::mesh_to_polyhedron(
  16. const Eigen::MatrixBase<DerivedV>& V,
  17. const Eigen::MatrixBase<DerivedF>& F,
  18. Polyhedron& poly)
  19. {
  20. typedef typename Polyhedron::HalfedgeDS HalfedgeDS;
  21. // Postcondition: hds is a valid polyhedral surface.
  22. CGAL::Polyhedron_incremental_builder_3<HalfedgeDS> B(poly.hds());
  23. B.begin_surface(V.rows(),F.rows());
  24. typedef typename HalfedgeDS::Vertex Vertex;
  25. typedef typename Vertex::Point Point;
  26. assert(V.cols() == 3 && "V must be #V by 3");
  27. for(int v = 0;v<V.rows();v++)
  28. {
  29. B.add_vertex(Point(V(v,0),V(v,1),V(v,2)));
  30. }
  31. assert(F.cols() == 3 && "F must be #F by 3");
  32. for(int f=0;f<F.rows();f++)
  33. {
  34. B.begin_facet();
  35. for(int c = 0;c<3;c++)
  36. {
  37. B.add_vertex_to_facet(F(f,c));
  38. }
  39. B.end_facet();
  40. }
  41. if(B.error())
  42. {
  43. B.rollback();
  44. return false;
  45. }
  46. B.end_surface();
  47. return poly.is_valid();
  48. }
  49. #ifdef IGL_STATIC_LIBRARY
  50. // Explicit template instantiation
  51. #include <CGAL/Simple_cartesian.h>
  52. #include <CGAL/Polyhedron_items_with_id_3.h>
  53. #endif