diff --git a/common/mathlib.cc b/common/mathlib.cc index 77dd6b45..bab49ab4 100644 --- a/common/mathlib.cc +++ b/common/mathlib.cc @@ -400,6 +400,14 @@ float GLM_DistAbovePlane(const glm::vec4 &plane, const glm::vec3 &point) return dot(vec3(plane), point) - plane.w; } +glm::vec3 GLM_ProjectPointOntoPlane(const glm::vec4 &plane, const glm::vec3 &point) +{ + float dist = GLM_DistAbovePlane(plane, point); + vec3 move = -dist * vec3(plane); + return point + move; +} + + glm::vec3 GLM_PolyCentroid(const std::vector &points) { Q_assert(points.size() >= 3); diff --git a/include/common/mathlib.hh b/include/common/mathlib.hh index 23eec72c..64154ad1 100644 --- a/include/common/mathlib.hh +++ b/include/common/mathlib.hh @@ -329,6 +329,7 @@ float GLM_EdgePlanes_PointInsideDist(const std::vector &edgeplanes, c glm::vec3 GLM_TriangleCentroid(const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2); float GLM_TriangleArea(const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2); float GLM_DistAbovePlane(const glm::vec4 &plane, const glm::vec3 &point); +glm::vec3 GLM_ProjectPointOntoPlane(const glm::vec4 &plane, const glm::vec3 &point); glm::vec3 GLM_PolyCentroid(const std::vector &points); glm::vec4 GLM_PolyPlane(const std::vector &points); /// Returns the index of the polygon edge, and the closest point on that edge, to the given point diff --git a/light/test_light.cc b/light/test_light.cc index 7435c2b0..ba96e1c3 100644 --- a/light/test_light.cc +++ b/light/test_light.cc @@ -402,6 +402,22 @@ TEST(mathlib, BarycentricRandom) { } } +TEST(mathlib, DistAbovePlane) { + vec4 plane(0, 0, 1, 10); + vec3 point(100, 100, 100); + EXPECT_FLOAT_EQ(90, GLM_DistAbovePlane(plane, point)); +} + +TEST(mathlib, ProjectPointOntoPlane) { + vec4 plane(0, 0, 1, 10); + vec3 point(100, 100, 100); + + vec3 projected = GLM_ProjectPointOntoPlane(plane, point); + EXPECT_FLOAT_EQ(100, projected.x); + EXPECT_FLOAT_EQ(100, projected.y); + EXPECT_FLOAT_EQ(10, projected.z); +} + TEST(mathlib, InterpolateNormals) { // This test relies on the way GLM_InterpolateNormal is implemented