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