src/geometry.h

changeset 321
180072db4a83
parent 223
ce81db996275
child 322
a39f454a3d7f
--- a/src/geometry.h	Sun Jul 03 14:35:06 2022 +0300
+++ b/src/geometry.h	Sun Jul 03 15:59:22 2022 +0300
@@ -198,3 +198,26 @@
 
 glm::vec3 pointOnCurve(const BezierCurve& curve, float t);
 glm::vec3 derivativeOnCurve(const BezierCurve& curve, float t);
+
+template<typename Iter>
+std::optional<glm::mat3> calculateNormal(Iter&& begin, Iter&& end)
+{
+	const long int n = end - begin;
+	std::optional<glm::mat3> result;
+	for (long int i = 0; i < n; ++i) {
+		const glm::vec3& v1 = *(begin + (i + n - 1) % n);
+		const glm::vec3& v2 = *(begin + i);
+		const glm::vec3& v3 = *(begin + (i + 1) % n);
+		const glm::vec3 xvec = v1 - v2;
+		const glm::vec3 yvec = v3 - v2;
+		constexpr float threshold = 1e-6f;
+		if (glm::length(xvec) > threshold and glm::length(yvec) > threshold) {
+			const glm::vec3 zvec = glm::cross(glm::normalize(xvec), glm::normalize(yvec));
+			if (glm::length(zvec) > threshold) {
+				result = {xvec, yvec, zvec};
+				break;
+			}
+		}
+	}
+	return result;
+}

mercurial