From 6b14e166b827fba493dec4da1a16da5c4865da44 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 28 Jan 2018 02:19:09 -0700 Subject: [PATCH] light: make GLM_InterpolateNormal return failure on degenerate input --- common/mathlib.cc | 3 +++ light/test_light.cc | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/common/mathlib.cc b/common/mathlib.cc index 08704772..3ad61187 100644 --- a/common/mathlib.cc +++ b/common/mathlib.cc @@ -704,6 +704,9 @@ std::pair GLM_InterpolateNormal(const std::vector &points, { Q_assert(points.size() == normals.size()); + if (points.size() < 3) + return make_pair(false, qvec3f(0)); + // Step through the triangles, being careful to handle zero-size ones const qvec3f &p0 = points.at(0); diff --git a/light/test_light.cc b/light/test_light.cc index 35815131..dcf5c8cb 100644 --- a/light/test_light.cc +++ b/light/test_light.cc @@ -319,6 +319,12 @@ TEST(mathlib, ProjectPointOntoPlane) { EXPECT_FLOAT_EQ(10, projected[2]); } +TEST(mathlib, InterpolateNormalsDegenerate) { + EXPECT_FALSE(GLM_InterpolateNormal({}, {}, qvec3f(0,0,0)).first); + EXPECT_FALSE(GLM_InterpolateNormal({qvec3f(0,0,0)}, {qvec3f(0,0,1)}, qvec3f(0,0,0)).first); + EXPECT_FALSE(GLM_InterpolateNormal({qvec3f(0,0,0), qvec3f(10,0,0)}, {qvec3f(0,0,1), qvec3f(0,0,1)}, qvec3f(0,0,0)).first); +} + TEST(mathlib, InterpolateNormals) { // This test relies on the way GLM_InterpolateNormal is implemented