From e2ce4c811a5e32b7513ae0c20a836e4a08814c6d Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Fri, 1 Sep 2023 19:22:42 +0300 Subject: [PATCH 1/3] Less multiplications in intersectionLines --- modules/objdetect/src/qrcode.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/modules/objdetect/src/qrcode.cpp b/modules/objdetect/src/qrcode.cpp index 9f64c6446259..c7417e13df5d 100644 --- a/modules/objdetect/src/qrcode.cpp +++ b/modules/objdetect/src/qrcode.cpp @@ -72,15 +72,8 @@ static Point2f intersectionLines(Point2f a1, Point2f a2, Point2f b1, Point2f b2) const float eps = 0.001f; if (abs(divisor) < eps) return a2; - Point2f result_square_angle( - ((a1.x * a2.y - a1.y * a2.x) * (b1.x - b2.x) - - (b1.x * b2.y - b1.y * b2.x) * (a1.x - a2.x)) / - divisor, - ((a1.x * a2.y - a1.y * a2.x) * (b1.y - b2.y) - - (b1.x * b2.y - b1.y * b2.x) * (a1.y - a2.y)) / - divisor - ); - return result_square_angle; + const float u = ((b2.x - a2.x) * (b1.y - b2.y) + (b1.x - b2.x) * (a2.y - b2.y)) / divisor; + return a2 + u * (a1 - a2); } // / | b From 0b70ddc7c4a2364977297969a2f19e52ebf5df33 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Sat, 2 Sep 2023 20:58:14 +0300 Subject: [PATCH 2/3] Less multiplications in intersectLineSegments --- modules/imgproc/src/geometry.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/imgproc/src/geometry.cpp b/modules/imgproc/src/geometry.cpp index 701c3a647f03..77d3d16e02c0 100644 --- a/modules/imgproc/src/geometry.cpp +++ b/modules/imgproc/src/geometry.cpp @@ -328,17 +328,16 @@ static LineSegmentIntersection parallelInt( Point2f a, Point2f b, Point2f c, Poi static LineSegmentIntersection intersectLineSegments( Point2f a, Point2f b, Point2f c, Point2f d, Point2f& p, Point2f& q ) { - double denom = a.x * (double)(d.y - c.y) + b.x * (double)(c.y - d.y) + - d.x * (double)(b.y - a.y) + c.x * (double)(a.y - b.y); + double denom = (a.x - b.x) * (double)(d.y - c.y) - (a.y - b.y) * (double)(d.x - c.x); // If denom is zero, then segments are parallel: handle separately. if( denom == 0. ) return parallelInt(a, b, c, d, p, q); - double num = a.x * (double)(d.y - c.y) + c.x * (double)(a.y - d.y) + d.x * (double)(c.y - a.y); + double num = (d.y - a.y) * (double)(a.x - c.x) + (a.x - d.x) * (double)(a.y - c.y); double s = num / denom; - num = a.x * (double)(b.y - c.y) + b.x * (double)(c.y - a.y) + c.x * (double)(a.y - b.y); + num = (b.y - a.y) * (double)(a.x - c.x) + (c.y - a.y) * (double)(b.x - a.x); double t = num / denom; p.x = (float)(a.x + s*(b.x - a.x)); From a4b63191b916933c3294fa7b95e0ff66aa8a0b30 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Tue, 5 Sep 2023 09:07:57 +0300 Subject: [PATCH 3/3] Add comment --- modules/objdetect/src/qrcode.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/objdetect/src/qrcode.cpp b/modules/objdetect/src/qrcode.cpp index c7417e13df5d..1df46a9bb5ff 100644 --- a/modules/objdetect/src/qrcode.cpp +++ b/modules/objdetect/src/qrcode.cpp @@ -68,6 +68,8 @@ static void updatePointsResult(OutputArray points_, const vector& point static Point2f intersectionLines(Point2f a1, Point2f a2, Point2f b1, Point2f b2) { + // Try to solve a two lines intersection (a1, a2) and (b1, b2) as a system of equations: + // a2 + u * (a1 - a2) = b2 + v * (b1 - b2) const float divisor = (a1.x - a2.x) * (b1.y - b2.y) - (a1.y - a2.y) * (b1.x - b2.x); const float eps = 0.001f; if (abs(divisor) < eps)