convex_hull.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 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 "convex_hull.h"
  9. #include "../../ismember.h"
  10. #include "polyhedron_to_mesh.h"
  11. #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
  12. #include <CGAL/Polyhedron_3.h>
  13. #include <CGAL/Surface_mesh.h>
  14. #include <CGAL/convex_hull_3.h>
  15. #include <vector>
  16. template <
  17. typename DerivedV,
  18. typename DerivedW,
  19. typename DerivedG>
  20. IGL_INLINE void igl::copyleft::cgal::convex_hull(
  21. const Eigen::MatrixBase<DerivedV> & V,
  22. Eigen::PlainObjectBase<DerivedW> & W,
  23. Eigen::PlainObjectBase<DerivedG> & G)
  24. {
  25. typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
  26. typedef K::Point_3 Point_3;
  27. //typedef CGAL::Delaunay_triangulation_3<K> Delaunay;
  28. //typedef Delaunay::Vertex_handle Vertex_handle;
  29. //typedef CGAL::Surface_mesh<Point_3> Surface_mesh;
  30. typedef CGAL::Polyhedron_3<K> Polyhedron_3;
  31. std::vector<Point_3> points(V.rows());
  32. for(int i = 0;i<V.rows();i++)
  33. {
  34. points[i] = Point_3(V(i,0),V(i,1),V(i,2));
  35. }
  36. Polyhedron_3 poly;
  37. CGAL::convex_hull_3(points.begin(),points.end(),poly);
  38. assert(poly.is_pure_triangle() && "Assuming CGAL outputs a triangle mesh");
  39. polyhedron_to_mesh(poly,W,G);
  40. }
  41. template <
  42. typename DerivedV,
  43. typename DerivedF>
  44. IGL_INLINE void igl::copyleft::cgal::convex_hull(
  45. const Eigen::MatrixBase<DerivedV> & V,
  46. Eigen::PlainObjectBase<DerivedF> & F)
  47. {
  48. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, Eigen::Dynamic> W;
  49. Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, Eigen::Dynamic> G;
  50. convex_hull(V,W,G);
  51. // This is a lazy way to reindex into the original mesh
  52. Eigen::Matrix<bool,Eigen::Dynamic,1> I;
  53. Eigen::VectorXi J;
  54. igl::ismember_rows(W,V,I,J);
  55. assert(I.all() && "Should find all W in V");
  56. F.resizeLike(G);
  57. for(int f = 0;f<G.rows();f++)
  58. {
  59. for(int c = 0;c<3;c++)
  60. {
  61. F(f,c) = J(G(f,c));
  62. }
  63. }
  64. }
  65. #ifdef IGL_STATIC_LIBRARY
  66. // Explicit template instantiation
  67. // generated by autoexplicit.sh
  68. template void igl::copyleft::cgal::convex_hull<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  69. #endif