diff -r af6633412a6c -r 180072db4a83 src/geometry.h --- 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 +std::optional calculateNormal(Iter&& begin, Iter&& end) +{ + const long int n = end - begin; + std::optional 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; +}