ear_clipping.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2019 Hanxiao Shen <[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. #ifndef IGL_PREDICATES_EAR_CLIPPING_H
  9. #define IGL_PREDICATES_EAR_CLIPPING_H
  10. #include <Eigen/Core>
  11. #include "../igl_inline.h"
  12. namespace igl
  13. {
  14. namespace predicates
  15. {
  16. /// Implementation of ear clipping triangulation algorithm for a 2D polygon.
  17. /// https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
  18. /// If the polygon is simple and oriented counter-clockwise, all vertices
  19. /// will be clipped and the result mesh is (P,eF) Otherwise, the function
  20. /// will try to clip as many ears as possible.
  21. ///
  22. /// @param[in] P : n*2, size n 2D polygon
  23. /// @param[in] RT: n*1, preserved vertices (do not clip) marked as 1, otherwise 0
  24. /// @param[out] eF: clipped ears, in original index of P
  25. /// @param[out] I : size #nP vector, maps index from nP to P, e.g. nP's ith vertex is origianlly I(i) in P
  26. ///
  27. /// \pre To result in a proper mesh, P should be oriented counter-clockwise
  28. /// with no self-intersections.
  29. ///
  30. /// \note This implementation does not handle polygons with holes.
  31. ///
  32. /// \bug https://github.com/libigl/libigl/issues/1563
  33. template <
  34. typename DerivedP,
  35. typename DerivedRT,
  36. typename DerivedF,
  37. typename DerivedI>
  38. IGL_INLINE void ear_clipping(
  39. const Eigen::MatrixBase<DerivedP>& P,
  40. const Eigen::MatrixBase<DerivedRT>& RT,
  41. Eigen::PlainObjectBase<DerivedF>& eF,
  42. Eigen::PlainObjectBase<DerivedI>& I);
  43. /// \overload
  44. /// \brief Reverses P if necessary. Orientation of output will match input
  45. /// \return true if mesh is proper (should correspond to input being a
  46. /// simple polygon in either orientation), false otherwise.
  47. template <typename DerivedP, typename DerivedF>
  48. IGL_INLINE bool ear_clipping(
  49. const Eigen::MatrixBase<DerivedP>& P,
  50. Eigen::PlainObjectBase<DerivedF>& eF);
  51. }
  52. }
  53. #ifndef IGL_STATIC_LIBRARY
  54. # include "ear_clipping.cpp"
  55. #endif
  56. #endif