mathlib: add GLM_PolyArea
This commit is contained in:
parent
15dd49e093
commit
3e7b4448c1
|
|
@ -440,6 +440,24 @@ glm::vec3 GLM_ProjectPointOntoPlane(const glm::vec4 &plane, const glm::vec3 &poi
|
|||
return point + move;
|
||||
}
|
||||
|
||||
float GLM_PolyArea(const std::vector<glm::vec3> &points)
|
||||
{
|
||||
Q_assert(points.size() >= 3);
|
||||
|
||||
float poly_area = 0;
|
||||
|
||||
const vec3 v0 = points.at(0);
|
||||
for (int i = 2; i < points.size(); i++) {
|
||||
const vec3 v1 = points.at(i-1);
|
||||
const vec3 v2 = points.at(i);
|
||||
|
||||
const float triarea = GLM_TriangleArea(v0, v1, v2);
|
||||
|
||||
poly_area += triarea;
|
||||
}
|
||||
|
||||
return poly_area;
|
||||
}
|
||||
|
||||
glm::vec3 GLM_PolyCentroid(const std::vector<glm::vec3> &points)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -332,6 +332,7 @@ glm::vec3 GLM_TriangleCentroid(const glm::vec3 &v0, const glm::vec3 &v1, const g
|
|||
float GLM_TriangleArea(const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2);
|
||||
float GLM_DistAbovePlane(const glm::vec4 &plane, const glm::vec3 &point);
|
||||
glm::vec3 GLM_ProjectPointOntoPlane(const glm::vec4 &plane, const glm::vec3 &point);
|
||||
float GLM_PolyArea(const std::vector<glm::vec3> &points);
|
||||
glm::vec3 GLM_PolyCentroid(const std::vector<glm::vec3> &points);
|
||||
glm::vec4 GLM_PolyPlane(const std::vector<glm::vec3> &points);
|
||||
/// Returns the index of the polygon edge, and the closest point on that edge, to the given point
|
||||
|
|
|
|||
|
|
@ -187,6 +187,19 @@ TEST(mathlib, PolygonCentroid) {
|
|||
EXPECT_EQ(vec3(32,32,0), GLM_PolyCentroid(poly));
|
||||
}
|
||||
|
||||
TEST(mathlib, PolygonArea) {
|
||||
// poor test.. but at least checks that the colinear point is treated correctly
|
||||
const vector<vec3> poly {
|
||||
{ 0,0,0 },
|
||||
{ 0,32,0 }, // colinear
|
||||
{ 0,64,0 },
|
||||
{ 64,64,0 },
|
||||
{ 64,0,0 }
|
||||
};
|
||||
|
||||
EXPECT_EQ(64.0f * 64.0f, GLM_PolyArea(poly));
|
||||
}
|
||||
|
||||
TEST(mathlib, BarycentricFromPoint) {
|
||||
const tri_t tri = make_tuple<vec3,vec3,vec3>( // clockwise
|
||||
{ 0,0,0 },
|
||||
|
|
|
|||
Loading…
Reference in New Issue