fast_find_intersections.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <test_common.h>
  2. #include <igl/tri_tri_intersect.h>
  3. #include <igl/fast_find_intersections.h>
  4. #include <igl/fast_find_self_intersections.h>
  5. #include <igl/combine.h>
  6. TEST_CASE("tri_tri_intersection_test_3d intersect", "[igl]")
  7. {
  8. // try with oblique interecion plane
  9. for(double shift=-1000;shift<=1000;shift+=100.0)
  10. {
  11. Eigen::RowVector3d p1(0,0,0), q1(1,0,0),r1(0,1,0);
  12. Eigen::RowVector3d p2(shift,0,1),q2(1,1,0),r2(-shift,0,-1);
  13. // should intersect along the vector (0,0,0) -> (0.5,0.5,0)
  14. Eigen::RowVector3d s,t;
  15. bool coplanar;
  16. REQUIRE( igl::tri_tri_intersection_test_3d(p1,q1,r1, p2,q2,r2, coplanar, s, t) );
  17. REQUIRE( !coplanar);
  18. if(s.norm()<1e-5)
  19. {
  20. Eigen::RowVector3d t_correct(0.5,0.5,0);
  21. Eigen::RowVector3d s_correct(0,0,0);
  22. test_common::assert_near( t, t_correct, 1e-10);
  23. test_common::assert_near( s, s_correct, 1e-10);
  24. } else {
  25. Eigen::RowVector3d s_correct(0.5,0.5,0);
  26. Eigen::RowVector3d t_correct(0,0,0);
  27. test_common::assert_near( t, t_correct, 1e-10);
  28. test_common::assert_near( s, s_correct, 1e-10);
  29. }
  30. }
  31. }
  32. TEST_CASE("tri_tri_intersection_test_3d not intersect", "[igl]")
  33. {
  34. // positive test that two triangles intersect
  35. Eigen::RowVector3d p1(0,0,0),q1(1,0,0),r1(0,1,0);
  36. Eigen::RowVector3d p2(0,0,1),q2(1,0,1),r2(0,1,1);
  37. // should intersect along the vector (0,0,0) -> (sqrt(2),sqrt(2),0)
  38. Eigen::RowVector3d s,t;
  39. bool coplanar;
  40. REQUIRE( !igl::tri_tri_intersection_test_3d(p1,q1,r1,p2,q2,r2, coplanar, s, t) );
  41. }
  42. TEST_CASE("tri_tri_intersection_test_3d coplanar", "[igl]")
  43. {
  44. // positive test that two triangles intersect
  45. Eigen::RowVector3d p1(0,0,0),q1(1,0,0),r1(0,1,0);
  46. Eigen::RowVector3d p2(0,0,0),q2(0.5,0,0),r2(0,0.5,0);
  47. // should intersect along the vector (0,0,0) -> (sqrt(2),sqrt(2),0)
  48. Eigen::RowVector3d s,t;
  49. bool coplanar;
  50. REQUIRE( igl::tri_tri_intersection_test_3d(p1,q1,r1,p2,q2,r2, coplanar, s, t) );
  51. REQUIRE(coplanar);
  52. }
  53. TEST_CASE("fast_find_self_intersections: negative", "[igl]")
  54. {
  55. Eigen::MatrixXd V;
  56. Eigen::MatrixXi F;
  57. Eigen::VectorXi I;
  58. igl::read_triangle_mesh(test_common::data_path("cube.obj"), V, F);
  59. REQUIRE (! igl::fast_find_self_intersections(V,F,I) );
  60. REQUIRE ( I.sum()==0);
  61. }
  62. TEST_CASE("fast_find_self_intersections: positive", "[igl]")
  63. {
  64. Eigen::MatrixXd V;
  65. Eigen::MatrixXi F;
  66. igl::read_triangle_mesh(test_common::data_path("cube.obj"), V, F);
  67. auto dims=(V.colwise().maxCoeff()-V.colwise().minCoeff())*2;
  68. auto cz=(V.col(2).minCoeff()+V.col(2).maxCoeff())/2;
  69. // add a triangle intersecting cube
  70. Eigen::MatrixXd Vp(3,3);
  71. Vp << V.col(0).minCoeff()-dims(0), V.col(1).minCoeff()-dims(1), cz,
  72. V.col(0).minCoeff()-dims(0), V.col(1).maxCoeff()+dims(1)*3, cz,
  73. V.col(0).maxCoeff()+dims(0)*3, V.col(1).maxCoeff()-dims(1), cz;
  74. Eigen::MatrixXi Fp(1,3);
  75. Fp << 0,1,2;
  76. Eigen::MatrixXd V_;
  77. Eigen::MatrixXi F_;
  78. igl::combine<Eigen::MatrixXd,Eigen::MatrixXi>({V,Vp},{F,Fp}, V_,F_);
  79. Eigen::VectorXi I;
  80. Eigen::MatrixXd edges;
  81. REQUIRE ( igl::fast_find_self_intersections(V_,F_,I,edges) );
  82. // should have 9 triangles that are intersecting each other
  83. REQUIRE ( I.sum()==9);
  84. // and 16 line edges
  85. REQUIRE ( edges.rows()==16);
  86. // plane that we added intersects other triangles
  87. REQUIRE ( I(F_.rows()-1)!=0 );
  88. // TODO: check if the edges are at the right place (?)
  89. }
  90. TEST_CASE("fast_find_intersections:", "[igl]")
  91. {
  92. Eigen::MatrixXd V;
  93. Eigen::MatrixXi F;
  94. igl::read_triangle_mesh(test_common::data_path("cube.obj"), V, F);
  95. auto dims=(V.colwise().maxCoeff()-V.colwise().minCoeff())*2;
  96. auto cz=(V.col(2).minCoeff()+V.col(2).maxCoeff())/2;
  97. // add a triangle intersecting cube
  98. Eigen::MatrixXd Vp(3,3);
  99. Vp << V.col(0).minCoeff()-dims(0), V.col(1).minCoeff()-dims(1), cz,
  100. V.col(0).minCoeff()-dims(0), V.col(1).maxCoeff()+dims(1)*3, cz,
  101. V.col(0).maxCoeff()+dims(0)*3, V.col(1).maxCoeff()-dims(1), cz;
  102. Eigen::MatrixXi Fp(1,3);
  103. Fp << 0,1,2;
  104. Eigen::MatrixXi I;
  105. Eigen::MatrixXd edges;
  106. igl::fast_find_intersections(V,F,Vp,Fp,I,edges);
  107. // should have 8 triangles that are intersecting plane
  108. REQUIRE ( I.rows()==8);
  109. // all triangles from the cube intersect the same plane
  110. REQUIRE( (I.col(1).array()==0).all());
  111. // and 16 line edges
  112. REQUIRE ( edges.rows()==16);
  113. // TODO: check if the edges are at the right place
  114. }