ear_clipping.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <test_common.h>
  2. #include <igl/predicates/ear_clipping.h>
  3. TEST_CASE("ear_clipping: simple poly 1", "[igl/predicates]")
  4. {
  5. // Example1: simple polygon
  6. Eigen::MatrixXd polygon(10,2);
  7. polygon<<2,-3,4,1,5.5,-2,6,2.5,5,1,4,5,3,0,1,1,1,5,0,0;
  8. Eigen::VectorXi RT,nR,I;
  9. Eigen::MatrixXi eF;
  10. RT.setZero(polygon.rows());
  11. igl::predicates::ear_clipping(polygon,RT,eF,I);
  12. REQUIRE(I.size() == 0);
  13. Eigen::MatrixXi eF1;
  14. REQUIRE(igl::predicates::ear_clipping(polygon,eF1));
  15. REQUIRE(eF1.rows() == polygon.rows()-2);
  16. // Check that reverse also works
  17. Eigen::MatrixXd polygon2 = polygon.colwise().reverse();
  18. Eigen::MatrixXi eF2;
  19. REQUIRE(igl::predicates::ear_clipping(polygon2,eF2));
  20. REQUIRE(eF2.rows() == polygon2.rows()-2);
  21. }
  22. TEST_CASE("ear_clipping: simple poly 2", "[igl/predicates]")
  23. {
  24. using namespace std;
  25. const Eigen::MatrixXd P = (Eigen::MatrixXd(6,2)<<
  26. 0,-0.212132034355964,
  27. 0.212132034355964, 0,
  28. 0.106066017177982, 0.106066017177982,
  29. 0, 0,
  30. -0.106066017177982, 0.106066017177982,
  31. -0.212132034355964, 0
  32. ).finished();
  33. const Eigen::VectorXi RT = Eigen::VectorXi::Zero(P.rows(),1);
  34. Eigen::VectorXi I;
  35. Eigen::MatrixXi eF;
  36. igl::predicates::ear_clipping(P,RT,eF,I);
  37. Eigen::MatrixXi ans(4, 3);
  38. ans << 0,1,2,0,2,3,5,0,3,5,3,4;
  39. REQUIRE(ans == eF);
  40. REQUIRE(I.size() ==0);
  41. }
  42. TEST_CASE("ear_clipping: simple poly 3", "[igl/predicates]")
  43. {
  44. using namespace std;
  45. const Eigen::MatrixXd P = (Eigen::MatrixXd(3,2)<<
  46. 0, 0,
  47. 1, 0,
  48. 0, 1
  49. ).finished();
  50. const Eigen::VectorXi RT = Eigen::VectorXi::Zero(P.rows(),1);
  51. Eigen::VectorXi I;
  52. Eigen::MatrixXi eF;
  53. igl::predicates::ear_clipping(P,RT,eF,I);
  54. Eigen::MatrixXi ans(1, 3);
  55. ans << 2, 0, 1;
  56. REQUIRE(ans == eF);
  57. REQUIRE(I.size() == 0);
  58. }