Calculating the areas of triangles on a mesh

Metadata
aliases: []
shorthands: {}
created: 2022-02-28 15:33:21
modified: 2022-02-28 15:33:21

Let's say we have a triangle mesh: a set of vertices (points in 3D Euclidean space) and triangles between them, described by a set of index triplets and we want to calculate the areas of these triangles.

The area of a triangle (with corners , , ) can be calculated the following way:

This can be done on any of the triangles of the mesh.

Implementation

The following function implements this in Python, using the separate arrays convention for the mesh. It receives the mesh and returns an array with the triangle areas in the same order as the indices were sent.

def triangle_areas(x, y, z, i, j, k):
    x = np.array(x)
    y = np.array(y)
    z = np.array(z)
    i = np.array(i)
    j = np.array(j)
    k = np.array(k)

    # Calculate side vectors
    v1x = x[j] - x[i]
    v2x = x[k] - x[i]
    v1y = y[j] - y[i]
    v2y = y[k] - y[i]
    v1z = z[j] - z[i]
    v2z = z[k] - z[i]

    # Perform cross product
    crossx = v1y * v2z - v1z * v2y
    crossy = v1z * v2x - v1x * v2z
    crossz = v1x * v2y - v1y * v2x

    # Take the norm and divide by 2
    areas = 0.5 * np.sqrt(crossx**2 + crossy**2 + crossz**2)
    return areas

Examples

The areas for the figures in the following articles were done using this triangle_areas function.