From cd97cad52dd3586efaed73f772c148cd7ecfe257 Mon Sep 17 00:00:00 2001 From: Yannis Guyon Date: Mon, 8 Aug 2022 22:41:05 +0200 Subject: [PATCH 1/3] Use T as temp var type in polyanticlockwise() To match the input data type. Cast inner computation to double to prevent int overflows for any input type. --- modules/mcc/src/common.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/mcc/src/common.hpp b/modules/mcc/src/common.hpp index aa4c383f55..224459eb1a 100644 --- a/modules/mcc/src/common.hpp +++ b/modules/mcc/src/common.hpp @@ -79,10 +79,10 @@ void polyanticlockwise(std::vector &points) // Sort the points in anti-clockwise order // Trace a line between the first and second point. // If the third point is at the right side, then the points are anti-clockwise - cv::Point v1 = points[1] - points[0]; - cv::Point v2 = points[2] - points[0]; + T v1 = points[1] - points[0]; + T v2 = points[2] - points[0]; - double o = (v1.x * v2.y) - (v1.y * v2.x); + double o = ((double)v1.x * v2.y) - ((double)v1.y * v2.x); if (o < 0.0) //if the third point is in the left side, then sort in anti-clockwise order std::swap(points[1], points[3]); @@ -93,10 +93,10 @@ void polyclockwise(std::vector &points) // Sort the points in clockwise order // Trace a line between the first and second point. // If the third point is at the right side, then the points are clockwise - cv::Point v1 = points[1] - points[0]; - cv::Point v2 = points[2] - points[0]; + T v1 = points[1] - points[0]; + T v2 = points[2] - points[0]; - double o = (v1.x * v2.y) - (v1.y * v2.x); + double o = ((double)v1.x * v2.y) - ((double)v1.y * v2.x); if (o > 0.0) //if the third point is in the left side, then sort in clockwise order std::swap(points[1], points[3]); From 0be54f24f27934e4907235a4ff3c32028e4a5f99 Mon Sep 17 00:00:00 2001 From: Yannis Guyon Date: Thu, 1 Sep 2022 11:34:43 +0200 Subject: [PATCH 2/3] Untemplate polyanticlockwise() and polyclockwise() --- modules/mcc/src/common.hpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/modules/mcc/src/common.hpp b/modules/mcc/src/common.hpp index 224459eb1a..175a8e4808 100644 --- a/modules/mcc/src/common.hpp +++ b/modules/mcc/src/common.hpp @@ -73,32 +73,28 @@ void unique(const std::vector &A, std::vector &U) U.push_back(Tm[i]); } -template -void polyanticlockwise(std::vector &points) +void polyanticlockwise(std::vector &points) { // Sort the points in anti-clockwise order // Trace a line between the first and second point. // If the third point is at the right side, then the points are anti-clockwise - T v1 = points[1] - points[0]; - T v2 = points[2] - points[0]; - - double o = ((double)v1.x * v2.y) - ((double)v1.y * v2.x); + cv::Point2f v1 = points[1] - points[0]; + cv::Point2f v2 = points[2] - points[0]; - if (o < 0.0) //if the third point is in the left side, then sort in anti-clockwise order + //if the third point is in the left side, then sort in anti-clockwise order + if ((v1.x * v2.y) - (v1.y * v2.x) < 0.0) std::swap(points[1], points[3]); } -template -void polyclockwise(std::vector &points) +void polyclockwise(std::vector &points) { // Sort the points in clockwise order // Trace a line between the first and second point. // If the third point is at the right side, then the points are clockwise - T v1 = points[1] - points[0]; - T v2 = points[2] - points[0]; - - double o = ((double)v1.x * v2.y) - ((double)v1.y * v2.x); + cv::Point2f v1 = points[1] - points[0]; + cv::Point2f v2 = points[2] - points[0]; - if (o > 0.0) //if the third point is in the left side, then sort in clockwise order + //if the third point is in the left side, then sort in clockwise order + if ((v1.x * v2.y) - (v1.y * v2.x) > 0.0) std::swap(points[1], points[3]); } // Does lexical cast of the input argument to string From 94ca0c5fc0221f8642cd81d862ccc2f2a8a9c070 Mon Sep 17 00:00:00 2001 From: Yannis Guyon Date: Mon, 5 Sep 2022 10:23:32 +0200 Subject: [PATCH 3/3] Move poly*clockwise() definition to common.cpp --- modules/mcc/src/common.cpp | 25 +++++++++++++++++++++++++ modules/mcc/src/common.hpp | 27 +++------------------------ 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/modules/mcc/src/common.cpp b/modules/mcc/src/common.cpp index 71cba1e8bc..1f44273202 100644 --- a/modules/mcc/src/common.cpp +++ b/modules/mcc/src/common.cpp @@ -86,5 +86,30 @@ mace_center(const std::vector &ps) return center; } +void polyanticlockwise(std::vector &points) +{ + // Sort the points in anti-clockwise order + // Trace a line between the first and second point. + // If the third point is at the right side, then the points are anti-clockwise + cv::Point2f v1 = points[1] - points[0]; + cv::Point2f v2 = points[2] - points[0]; + + //if the third point is in the left side, then sort in anti-clockwise order + if ((v1.x * v2.y) - (v1.y * v2.x) < 0.0) + std::swap(points[1], points[3]); +} +void polyclockwise(std::vector &points) +{ + // Sort the points in clockwise order + // Trace a line between the first and second point. + // If the third point is at the right side, then the points are clockwise + cv::Point2f v1 = points[1] - points[0]; + cv::Point2f v2 = points[2] - points[0]; + + //if the third point is in the left side, then sort in clockwise order + if ((v1.x * v2.y) - (v1.y * v2.x) > 0.0) + std::swap(points[1], points[3]); +} + } // namespace mcc } // namespace cv diff --git a/modules/mcc/src/common.hpp b/modules/mcc/src/common.hpp index 175a8e4808..998f8a4c51 100644 --- a/modules/mcc/src/common.hpp +++ b/modules/mcc/src/common.hpp @@ -73,30 +73,9 @@ void unique(const std::vector &A, std::vector &U) U.push_back(Tm[i]); } -void polyanticlockwise(std::vector &points) -{ - // Sort the points in anti-clockwise order - // Trace a line between the first and second point. - // If the third point is at the right side, then the points are anti-clockwise - cv::Point2f v1 = points[1] - points[0]; - cv::Point2f v2 = points[2] - points[0]; - - //if the third point is in the left side, then sort in anti-clockwise order - if ((v1.x * v2.y) - (v1.y * v2.x) < 0.0) - std::swap(points[1], points[3]); -} -void polyclockwise(std::vector &points) -{ - // Sort the points in clockwise order - // Trace a line between the first and second point. - // If the third point is at the right side, then the points are clockwise - cv::Point2f v1 = points[1] - points[0]; - cv::Point2f v2 = points[2] - points[0]; - - //if the third point is in the left side, then sort in clockwise order - if ((v1.x * v2.y) - (v1.y * v2.x) > 0.0) - std::swap(points[1], points[3]); -} +void polyanticlockwise(std::vector &points); +void polyclockwise(std::vector &points); + // Does lexical cast of the input argument to string template std::string ToString(const T &value)