src/geometry.cpp

changeset 369
57de8fab2237
parent 297
bc92f97498f7
child 371
171d3f9638a9
--- 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]);
 }
 
 /**

mercurial