From 33510d9f1b38a5ada418729cd3ea482115024487 Mon Sep 17 00:00:00 2001 From: "x.ZM" Date: Sat, 3 Nov 2018 16:44:22 +0100 Subject: [PATCH 1/4] fix: segments intersection bug. Unit test also added. --- include/igl/segment_segment_intersect.cpp | 2 +- .../include/igl/segment_segment_intersect.cpp | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/include/igl/segment_segment_intersect.cpp diff --git a/include/igl/segment_segment_intersect.cpp b/include/igl/segment_segment_intersect.cpp index 8438c0496..57a3beea3 100644 --- a/include/igl/segment_segment_intersect.cpp +++ b/include/igl/segment_segment_intersect.cpp @@ -53,7 +53,7 @@ IGL_INLINE bool igl::segments_intersect( t = t1.norm() / rxs.norm(); t = t * sign; - if (t < -eps || fabs(t) < eps) + if ((t - 1.) > eps || t < -eps) return false; a_t = t; diff --git a/tests/include/igl/segment_segment_intersect.cpp b/tests/include/igl/segment_segment_intersect.cpp new file mode 100644 index 000000000..ea2a04c8a --- /dev/null +++ b/tests/include/igl/segment_segment_intersect.cpp @@ -0,0 +1,39 @@ +#include +#include + +TEST_CASE("segment_segment_intersect: examples", "[igl]") +{ + // example 1 + // https://github.com/libigl/libigl/issues/965 + + auto s1 = Eigen::RowVector3d(581, 388, -54); + auto dir1 = Eigen::RowVector3d(0.75, -4.36, 0.64); + + auto s2 = Eigen::RowVector3d(636, 77, -10); + auto dir2 = Eigen::RowVector3d(-2.79, 0.96, 0.39); + + double a1, a2; + bool sect1 = igl::segments_intersect(s1, dir1, s2, dir2, a1, a2); + + bool intersectCondition1 = (a1 >= 0 && a1 <= 1 && a2 >= 0 && a2 <= 1); + + REQUIRE(sect1 == false); + REQUIRE(intersectCondition1 == false); + + // example 2 + // https://github.com/libigl/libigl/issues/957 + + auto s3 = Eigen::RowVector3d(-56.6, 0, -201.7); + auto dir3 = Eigen::RowVector3d(0, 0, 1); + + auto s4 = Eigen::RowVector3d(-65.6, 0, 258.8); + auto dir4 = Eigen::RowVector3d(15.4, 0, 1.9); + + double a3, a4; + bool sect2 = igl::segments_intersect(s3, dir3, s4, dir4, a3, a4); + + bool intersectCondition2 = (a3 >= 0 && a3 <= 1 && a4 >= 0 && a4 <= 1); + + REQUIRE(sect2 == false); + REQUIRE(intersectCondition2 == false); +} From 70ec4474899d1c3ebca53324215f91424fede3af Mon Sep 17 00:00:00 2001 From: Teseo Schneider Date: Tue, 6 Nov 2018 19:10:03 -0500 Subject: [PATCH 2/4] fixed return value bug --- include/igl/segment_segment_intersect.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/igl/segment_segment_intersect.cpp b/include/igl/segment_segment_intersect.cpp index 57a3beea3..8d80690e6 100644 --- a/include/igl/segment_segment_intersect.cpp +++ b/include/igl/segment_segment_intersect.cpp @@ -53,12 +53,12 @@ IGL_INLINE bool igl::segments_intersect( t = t1.norm() / rxs.norm(); t = t * sign; - if ((t - 1.) > eps || t < -eps) - return false; - a_t = t; a_u = u; + if ((t - 1.) > eps || t < -eps) + return false; + return true; }; From de180a073ff066bde35275888c1036c8f356ccd4 Mon Sep 17 00:00:00 2001 From: "x.ZM" Date: Sat, 10 Nov 2018 14:48:57 +0100 Subject: [PATCH 3/4] fix: move return value downwards + change tabs to 2-space. --- include/igl/segment_segment_intersect.cpp | 76 +++++++++---------- .../include/igl/segment_segment_intersect.cpp | 44 +++++------ 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/include/igl/segment_segment_intersect.cpp b/include/igl/segment_segment_intersect.cpp index 8d80690e6..a8569be8c 100644 --- a/include/igl/segment_segment_intersect.cpp +++ b/include/igl/segment_segment_intersect.cpp @@ -11,55 +11,55 @@ template IGL_INLINE bool igl::segments_intersect( - const Eigen::PlainObjectBase &p, - const Eigen::PlainObjectBase &r, - const Eigen::PlainObjectBase &q, - const Eigen::PlainObjectBase &s, - double &a_t, - double &a_u, - double eps + const Eigen::PlainObjectBase &p, + const Eigen::PlainObjectBase &r, + const Eigen::PlainObjectBase &q, + const Eigen::PlainObjectBase &s, + double &a_t, + double &a_u, + double eps ) { - // http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect - // Search intersection between two segments - // p + t*r : t \in [0,1] - // q + u*s : u \in [0,1] + // http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect + // Search intersection between two segments + // p + t*r : t \in [0,1] + // q + u*s : u \in [0,1] - // p + t * r = q + u * s // x s - // t(r x s) = (q - p) x s - // t = (q - p) x s / (r x s) + // p + t * r = q + u * s // x s + // t(r x s) = (q - p) x s + // t = (q - p) x s / (r x s) - // (r x s) ~ 0 --> directions are parallel, they will never cross - Eigen::RowVector3d rxs = r.cross(s); - if (rxs.norm() <= eps) - return false; + // (r x s) ~ 0 --> directions are parallel, they will never cross + Eigen::RowVector3d rxs = r.cross(s); + if (rxs.norm() <= eps) + return false; - int sign; + int sign; - double u; - // u = (q − p) × r / (r × s) - Eigen::RowVector3d u1 = (q - p).cross(r); - sign = ((u1.dot(rxs)) > 0) ? 1 : -1; - u = u1.norm() / rxs.norm(); - u = u * sign; + double u; + // u = (q − p) × r / (r × s) + Eigen::RowVector3d u1 = (q - p).cross(r); + sign = ((u1.dot(rxs)) > 0) ? 1 : -1; + u = u1.norm() / rxs.norm(); + u = u * sign; - if ((u - 1.) > eps || u < -eps) - return false; + double t; + // t = (q - p) x s / (r x s) + Eigen::RowVector3d t1 = (q - p).cross(s); + sign = ((t1.dot(rxs)) > 0) ? 1 : -1; + t = t1.norm() / rxs.norm(); + t = t * sign; - double t; - // t = (q - p) x s / (r x s) - Eigen::RowVector3d t1 = (q - p).cross(s); - sign = ((t1.dot(rxs)) > 0) ? 1 : -1; - t = t1.norm() / rxs.norm(); - t = t * sign; + a_t = t; + a_u = u; - a_t = t; - a_u = u; + if ((u - 1.) > eps || u < -eps) + return false; - if ((t - 1.) > eps || t < -eps) - return false; + if ((t - 1.) > eps || t < -eps) + return false; - return true; + return true; }; #ifdef IGL_STATIC_LIBRARY diff --git a/tests/include/igl/segment_segment_intersect.cpp b/tests/include/igl/segment_segment_intersect.cpp index ea2a04c8a..1899708f2 100644 --- a/tests/include/igl/segment_segment_intersect.cpp +++ b/tests/include/igl/segment_segment_intersect.cpp @@ -3,37 +3,37 @@ TEST_CASE("segment_segment_intersect: examples", "[igl]") { - // example 1 - // https://github.com/libigl/libigl/issues/965 + // example 1 + // https://github.com/libigl/libigl/issues/965 - auto s1 = Eigen::RowVector3d(581, 388, -54); - auto dir1 = Eigen::RowVector3d(0.75, -4.36, 0.64); + auto s1 = Eigen::RowVector3d(581, 388, -54); + auto dir1 = Eigen::RowVector3d(0.75, -4.36, 0.64); - auto s2 = Eigen::RowVector3d(636, 77, -10); - auto dir2 = Eigen::RowVector3d(-2.79, 0.96, 0.39); + auto s2 = Eigen::RowVector3d(636, 77, -10); + auto dir2 = Eigen::RowVector3d(-2.79, 0.96, 0.39); - double a1, a2; - bool sect1 = igl::segments_intersect(s1, dir1, s2, dir2, a1, a2); + double a1, a2; + bool sect1 = igl::segments_intersect(s1, dir1, s2, dir2, a1, a2); - bool intersectCondition1 = (a1 >= 0 && a1 <= 1 && a2 >= 0 && a2 <= 1); + bool intersectCondition1 = (a1 >= 0 && a1 <= 1 && a2 >= 0 && a2 <= 1); - REQUIRE(sect1 == false); - REQUIRE(intersectCondition1 == false); + REQUIRE(sect1 == false); + REQUIRE(intersectCondition1 == false); - // example 2 - // https://github.com/libigl/libigl/issues/957 + // example 2 + // https://github.com/libigl/libigl/issues/957 - auto s3 = Eigen::RowVector3d(-56.6, 0, -201.7); - auto dir3 = Eigen::RowVector3d(0, 0, 1); + auto s3 = Eigen::RowVector3d(-56.6, 0, -201.7); + auto dir3 = Eigen::RowVector3d(0, 0, 1); - auto s4 = Eigen::RowVector3d(-65.6, 0, 258.8); - auto dir4 = Eigen::RowVector3d(15.4, 0, 1.9); + auto s4 = Eigen::RowVector3d(-65.6, 0, 258.8); + auto dir4 = Eigen::RowVector3d(15.4, 0, 1.9); - double a3, a4; - bool sect2 = igl::segments_intersect(s3, dir3, s4, dir4, a3, a4); + double a3, a4; + bool sect2 = igl::segments_intersect(s3, dir3, s4, dir4, a3, a4); - bool intersectCondition2 = (a3 >= 0 && a3 <= 1 && a4 >= 0 && a4 <= 1); + bool intersectCondition2 = (a3 >= 0 && a3 <= 1 && a4 >= 0 && a4 <= 1); - REQUIRE(sect2 == false); - REQUIRE(intersectCondition2 == false); + REQUIRE(sect2 == false); + REQUIRE(intersectCondition2 == false); } From 116d2bc1de7def7ff8be8d2f0cd5d73b7c53ad40 Mon Sep 17 00:00:00 2001 From: "x.ZM" Date: Sat, 10 Nov 2018 15:01:27 +0100 Subject: [PATCH 4/4] fix tab. --- include/igl/segment_segment_intersect.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/igl/segment_segment_intersect.cpp b/include/igl/segment_segment_intersect.cpp index a8569be8c..89d0a412c 100644 --- a/include/igl/segment_segment_intersect.cpp +++ b/include/igl/segment_segment_intersect.cpp @@ -32,7 +32,7 @@ IGL_INLINE bool igl::segments_intersect( // (r x s) ~ 0 --> directions are parallel, they will never cross Eigen::RowVector3d rxs = r.cross(s); if (rxs.norm() <= eps) - return false; + return false; int sign; @@ -54,10 +54,10 @@ IGL_INLINE bool igl::segments_intersect( a_u = u; if ((u - 1.) > eps || u < -eps) - return false; + return false; if ((t - 1.) > eps || t < -eps) - return false; + return false; return true; };