is_self_intersecting.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "is_self_intersecting.h"
  2. #include "../../placeholders.h"
  3. #include "../../find.h"
  4. #include "../../doublearea.h"
  5. #include "../../remove_unreferenced.h"
  6. #include "RemeshSelfIntersectionsParam.h"
  7. #include "remesh_self_intersections.h"
  8. #include "../../collapse_edge.h"
  9. template <
  10. typename DerivedV,
  11. typename DerivedF>
  12. bool igl::copyleft::cgal::is_self_intersecting(
  13. const Eigen::PlainObjectBase<DerivedV> & V,
  14. const Eigen::PlainObjectBase<DerivedF> & F)
  15. {
  16. assert(V.cols() == 3);
  17. assert(F.cols() == 3);
  18. using MatrixV = Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3>;
  19. using MatrixF = Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, 3>;
  20. const auto valid =
  21. igl::find((F.array() != IGL_COLLAPSE_EDGE_NULL).rowwise().any().eval());
  22. // Extract only the valid faces
  23. MatrixF FF = F(valid, igl::placeholders::all);
  24. // Remove unreferneced vertices
  25. MatrixV VV;
  26. {
  27. Eigen::VectorXi I;
  28. igl::remove_unreferenced(V,MatrixF(FF),VV,FF,I);
  29. }
  30. Eigen::Matrix<typename DerivedV::Scalar,Eigen::Dynamic,1> A;
  31. igl::doublearea(VV,FF,A);
  32. if(A.minCoeff() <= 0)
  33. {
  34. return true;
  35. }
  36. if(
  37. (FF.array().col(0) == FF.array().col(1)).any() ||
  38. (FF.array().col(1) == FF.array().col(2)).any() ||
  39. (FF.array().col(2) == FF.array().col(0)).any())
  40. {
  41. return true;
  42. }
  43. // check for self-intersections VV,FF
  44. igl::copyleft::cgal::RemeshSelfIntersectionsParam params;
  45. params.detect_only = true;
  46. params.first_only = true;
  47. Eigen::MatrixXi IF;
  48. Eigen::VectorXi J,IM;
  49. {
  50. MatrixV tempV;
  51. MatrixF tempF;
  52. igl::copyleft::cgal::remesh_self_intersections(
  53. V,F,params,tempV,tempF,IF,J,IM);
  54. }
  55. return IF.rows() > 0;
  56. }
  57. #ifdef IGL_STATIC_LIBRARY
  58. // Explicit template specialization
  59. template bool igl::copyleft::cgal::is_self_intersecting<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  60. #endif