From d10dec18034bd17f01d43f0029113258b69a6776 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Fri, 12 Jan 2018 00:36:12 -0700 Subject: [PATCH] light: make GLM_PolyCentroid accept degenerate faces (todo: must rename these) --- common/mathlib.cc | 8 ++++++++ light/test_light.cc | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/common/mathlib.cc b/common/mathlib.cc index 477b8f0f..08704772 100644 --- a/common/mathlib.cc +++ b/common/mathlib.cc @@ -24,6 +24,7 @@ #include #include +#include #include @@ -596,6 +597,13 @@ float GLM_PolyArea(const std::vector &points) qvec3f GLM_PolyCentroid(const std::vector &points) { + if (points.size() == 0) + return qvec3f(NAN); + else if (points.size() == 1) + return points.at(0); + else if (points.size() == 2) + return (points.at(0) + points.at(1)) / 2.0; + Q_assert(points.size() >= 3); qvec3f poly_centroid(0); diff --git a/light/test_light.cc b/light/test_light.cc index 8eeb2874..35815131 100644 --- a/light/test_light.cc +++ b/light/test_light.cc @@ -181,6 +181,22 @@ TEST(mathlib, ClosestPointOnPolyBoundary) { EXPECT_EQ(make_pair(0, qvec3f(0,0,0)), GLM_ClosestPointOnPolyBoundary(poly, qvec3f(-1,-1,0))); } +TEST(mathlib, PolygonCentroid_empty) { + const qvec3f res = GLM_PolyCentroid({}); + + for (int i=0; i<3; i++) { + EXPECT_TRUE(std::isnan(res[i])); + } +} + +TEST(mathlib, PolygonCentroid_point) { + EXPECT_EQ(qvec3f(1,1,1), GLM_PolyCentroid({qvec3f(1,1,1)})); +} + +TEST(mathlib, PolygonCentroid_line) { + EXPECT_EQ(qvec3f(1,1,1), GLM_PolyCentroid({qvec3f(0,0,0), qvec3f(2,2,2)})); +} + TEST(mathlib, PolygonCentroid) { // poor test.. but at least checks that the colinear point is treated correctly const vector poly {