From bca6a5566207abce005767bd329feee2f5319e5c Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sat, 22 Jul 2017 15:08:51 -0600 Subject: [PATCH] qbsp winding: add WindingArea --- include/qbsp/winding.hh | 2 ++ qbsp/test_qbsp.cc | 16 ++++++++++++++++ qbsp/winding.cc | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/include/qbsp/winding.hh b/include/qbsp/winding.hh index a8a29f16..db91a9eb 100644 --- a/include/qbsp/winding.hh +++ b/include/qbsp/winding.hh @@ -47,4 +47,6 @@ void MidpointWinding(const winding_t *w, vec3_t v); void CalcSides(const winding_t *in, const qbsp_plane_t *split, int *sides, vec_t *dists, int counts[3]); +vec_t WindingArea(const winding_t * w); + #endif diff --git a/qbsp/test_qbsp.cc b/qbsp/test_qbsp.cc index 730107f2..1bb15e51 100644 --- a/qbsp/test_qbsp.cc +++ b/qbsp/test_qbsp.cc @@ -120,3 +120,19 @@ TEST(qbsp, duplicatePlanes) { EXPECT_EQ(6, Brush_NumFaces(brush)); FreeMem(brush, BRUSH, 1); } + +TEST(mathlib, WindingArea) { + winding_t w; + w.numpoints = 5; + + // poor test.. but at least checks that the colinear point is treated correctly + VectorSet(w.points[0], 0,0,0); + VectorSet(w.points[1], 0,32,0); // colinear + VectorSet(w.points[2], 0,64,0); + VectorSet(w.points[3], 64,64,0); + VectorSet(w.points[4], 64,0,0); + + + EXPECT_EQ(64.0f * 64.0f, WindingArea(&w)); +} + diff --git a/qbsp/winding.cc b/qbsp/winding.cc index 7a2f91ac..f2724719 100644 --- a/qbsp/winding.cc +++ b/qbsp/winding.cc @@ -371,3 +371,26 @@ MidpointWinding(const winding_t *w, vec3_t v) if (w->numpoints) VectorScale(v, 1.0 / w->numpoints, v); } + +/* +================== +WindingArea +================== +*/ +vec_t +WindingArea(const winding_t * w) +{ + int i; + vec3_t d1, d2, cross; + vec_t total; + + total = 0; + for (i = 2; i < w->numpoints; i++) { + VectorSubtract(w->points[i - 1], w->points[0], d1); + VectorSubtract(w->points[i], w->points[0], d2); + CrossProduct(d1, d2, cross); + vec_t triarea = 0.5 * VectorLength(cross); + total += triarea; + } + return total; +}