qbsp winding: add WindingArea

This commit is contained in:
Eric Wasylishen 2017-07-22 15:08:51 -06:00
parent 5893c16267
commit bca6a55662
3 changed files with 41 additions and 0 deletions

View File

@ -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

View File

@ -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));
}

View File

@ -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;
}