Triangles are a very common modeling primitive in many graphics applications. The libgfx library provides a package of routines for computing various geometric properties of triangles. To use this package you must include the standard header file
#include <gfx/geom3d.h>Note that all of these functions assume that the corners of the triangle are listed in counter-clockwise order around the outward pointing normal.
You can compute the (signed) area of a triangle using the following function. Note that the area will be negative if the vertices are listed in clockwise order.
double triangle_area(const Vec3&, const Vec3&, const Vec3&);
The following functions compute the plane defined by a triangle, and the corresponding normal vector. The standard versions always use unit normal vectors while the "raw" versions will use unscaled normals.
Vec3 triangle_normal(const Vec3&, const Vec3&, const Vec3&); Vec4 triangle_plane(const Vec3&, const Vec3&, const Vec3&); Vec3 triangle_raw_normal(const Vec3&, const Vec3&, const Vec3&); Vec4 triangle_raw_plane(const Vec3&, const Vec3&, const Vec3&);
For some meshing applications, it may on occasion be necessary to assess the aspect ratio of a triangle.
double triangle_compactness(const Vec3&, const Vec3&, const Vec3&);This function computes the "compactness" of a triangle. The returned value will be between 0 and 1, with 0 meaning a degenerate (area=0) triangle and 1 meaning an equilateral triangle. The formula used, originally suggested by Andre Gueziec, is 4*sqrt(3) * Area / (L1 + L2 + L3) where Li is the squared length of side i of the triangle.