light: make GLM_PolyCentroid accept degenerate faces (todo: must rename these)

This commit is contained in:
Eric Wasylishen 2018-01-12 00:36:12 -07:00
parent 82e3aef283
commit d10dec1803
2 changed files with 24 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include <tuple>
#include <map>
#include <cmath>
#include <common/qvec.hh>
@ -596,6 +597,13 @@ float GLM_PolyArea(const std::vector<qvec3f> &points)
qvec3f GLM_PolyCentroid(const std::vector<qvec3f> &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);

View File

@ -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<qvec3f> poly {