isConvex renamed to quadrilateral_convexity, refactor, now returns an enum instead of bool

Mon, 10 Apr 2023 12:40:49 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 10 Apr 2023 12:40:49 +0300
changeset 369
57de8fab2237
parent 368
9444de9762c3
child 370
b2f9ded235a6

isConvex renamed to quadrilateral_convexity, refactor, now returns an enum instead of bool

src/geometry.cpp file | annotate | diff | comparison | revisions
src/geometry.h file | annotate | diff | comparison | revisions
src/triangulate.cpp file | annotate | diff | comparison | revisions
--- a/src/geometry.cpp	Sun Apr 09 17:05:40 2023 +0300
+++ b/src/geometry.cpp	Mon Apr 10 12:40:49 2023 +0300
@@ -198,23 +198,33 @@
 	};
 }
 
-bool isConvex(const Quadrilateral& quad)
+static convexity_e determine_convexity(const glm::vec3* begin, const glm::vec3* end)
 {
-	glm::vec3 crosses[4] = {
+	auto test_concave_dot_product = [begin](const glm::vec3& vector) {
+		return glm::dot(*begin, vector) < 1e-6;
+	};
+	if (std::any_of(begin + 1, end, test_concave_dot_product))
+	{
+		return convexity_e::concave;
+	}
+	else
+	{
+		return convexity_e::convex;
+	}
+}
+
+convexity_e quadrilateral_convexity(const Quadrilateral& quad)
+{
+	const glm::vec3 crosses[4] = {
 		glm::cross(quad.p4 - quad.p1, quad.p2 - quad.p1),
 		glm::cross(quad.p1 - quad.p2, quad.p3 - quad.p2),
 		glm::cross(quad.p2 - quad.p3, quad.p4 - quad.p3),
 		glm::cross(quad.p3 - quad.p4, quad.p1 - quad.p4),
 	};
-	return not std::any_of(
-		&crosses[1],
-		&crosses[4],
-		[&crosses](const glm::vec3& vector) {
-			return glm::dot(crosses[0], vector) < 1e-6;
-		});
+	return determine_convexity(&crosses[0], &crosses[4]);
 }
 
-bool isConvex(const std::vector<glm::vec3>& polygon)
+convexity_e polygon_convexity(const std::vector<glm::vec3>& polygon)
 {
 	const std::size_t n = polygon.size();
 	std::vector<glm::vec3> crosses;
@@ -226,13 +236,7 @@
 		const glm::vec3 v3 = polygon[(i + 1) % n];
 		crosses[i] = glm::cross(v1 - v2, v3 - v2);
 	}
-	return not std::any_of(
-		crosses.begin() + 1,
-		crosses.end(),
-		[&crosses](const glm::vec3& vector)
-		{
-			return glm::dot(crosses[0], vector) < 1e-6;
-		});
+	return determine_convexity(&crosses[0], &crosses[n]);
 }
 
 /**
--- a/src/geometry.h	Sun Apr 09 17:05:40 2023 +0300
+++ b/src/geometry.h	Mon Apr 10 12:40:49 2023 +0300
@@ -138,8 +138,9 @@
 LineSegment2D bottom(const QRectF& rectangle);
 LineSegment2D left(const QRectF& rectangle);
 LineSegment2D right(const QRectF& rectangle);
-bool isConvex(const Quadrilateral& quad);
-bool isConvex(const std::vector<glm::vec3>& polygon);
+enum class convexity_e { concave, convex };
+convexity_e quadrilateral_convexity(const Quadrilateral& quad);
+convexity_e polygon_convexity(const std::vector<glm::vec3>& polygon);
 Winding winding(const QPolygonF& polygon);
 struct ScalingExtract
 {
--- a/src/triangulate.cpp	Sun Apr 09 17:05:40 2023 +0300
+++ b/src/triangulate.cpp	Mon Apr 10 12:40:49 2023 +0300
@@ -96,7 +96,7 @@
 			*(polyBegin + shared_boundary.second),
 			*(polyBegin + earcut_triangle_1.third_vertex),
 		};
-		if (isConvex(quad)) {
+		if (quadrilateral_convexity(quad) == convexity_e::convex) {
 			result.quadrilaterals.push_back(quad);
 			result.cutTriangles.insert(earcut_triangle_1.triangleid);
 			result.cutTriangles.insert(earcut_triangle_2.triangleid);

mercurial