Deriving and Implementing Integer Barycentric Coordinates for Triangle Mesh Grids
This article explains how to compute integer barycentric coordinates for points on a mesh of equilateral triangles. It covers the cell layout and coordinate convention, derives coordinates for upward and downward triangles from a single comparison, and shows how to assign a linear triangle index. It includes a C++ implementation, hand-checked test cases, common mistakes, and notes on spherical meshes and the limits of the method.
1. Scope and Terminology
This article covers triangle mesh grids, which are flat meshes made of equilateral triangles. Each hexagon in a hexagonal grid can be formed by six triangles arranged around a center point, so a hexagonal grid can also be built on a triangle mesh. This article focuses only on the triangles themselves and uses the term “triangle” throughout.
Readers should understand basic C++ syntax and the concept of barycentric coordinates. The code logic has been checked against the manual test cases below. It has not been measured on a specific compiler or in a specific performance environment.
2. Cell Layout and Coordinate Convention
Let N be the side length of a triangle, meaning each edge is divided into N segments. The plane is divided into N × N parallelogram cells. Each cell is split along a diagonal into two triangles: an upward-pointing triangle △ and a downward-pointing triangle ▽.
Inside a cell, local coordinates (i, j) are used. The four corners are (0,0), (N,0), (0,N), and (N,N). The diagonal connects (N,0) and (0,N), which is the line i + j = N.
Global grid point coordinates (r, c) must be non-negative integers. The cell index and the offset within the cell are computed as follows:
- Cell row index
I = r / Nand cell column indexJ = c / N(integer division) - Offset within the cell
i = r % Nandj = c % N, where0 ≤ i, j < N
Because the input is non-negative, the calculation never produces negative indices. If coordinates can be negative, shift the grid origin into the non-negative range first.
3. Deriving the Barycentric Coordinates
For local coordinates (i, j), determining which triangle a point belongs to requires only one comparison between i + j and N:
- △ region:
i + j ≤ N. Points on the diagonal also belong to △. - ▽ region:
i + j > N.
The integer barycentric coordinates for △ are:
(u, v, w) = (N - i - j, i, j)
The three corners correspond to: (0,0) as the u corner, (N,0) as the v corner, and (0,N) as the w corner. The sum is (N - i - j) + i + j = N, and all three components are non-negative inside △.
The integer barycentric coordinates for ▽ are:
(u, v, w) = (N - i, N - j, i + j - N)
The three corners correspond to: (0,N) as the u corner, (N,0) as the v corner, and (N,N) as the w corner. The sum is (N - i) + (N - j) + (i + j - N) = N. Inside ▽, i + j > N and i, j < N, so all three components are positive.
Note that the same component refers to different corners in △ and ▽. If higher-level code needs a consistent meaning for each corner, convert the values explicitly at the interface. Do not mix them directly.
4. Linear Index
Mesh storage usually requires a unique one-dimensional ID for each triangle. This article uses the following scheme:
tri_id = 2 * (I * cells_per_row + J) + parity
Here, parity is 0 for △ and 1 for ▽. The two triangles in the same cell have adjacent IDs, so reading the data for one cell accesses a contiguous range. This is a property of the memory layout. How much cache benefit it provides depends on the target hardware and must be measured with real data. This article does not give figures.
5. C++ Implementation
#include <array>#include <cassert>// N: side length of the triangle, must be a positive integer// r, c: global grid coordinates, must be non-negativestd::array<int, 3> get_uvw(int r, int c, int N) {assert(N > 0 && r >= 0 && c >= 0);int i = r % N;int j = c % N;if (i + j <= N) { // △, including points on the diagonalreturn {N - i - j, i, j};}return {N - i, N - j, i + j - N}; // ▽}// cells_per_row: number of cells in each rowint tri_id(int r, int c, int N, int cells_per_row) {assert(N > 0 && r >= 0 && c >= 0 && cells_per_row > 0);int cell = (r / N) * cells_per_row + (c / N);int parity = ((r % N) + (c % N) <= N) ? 0 : 1;return 2 * cell + parity;}
The function contains only one comparison branch. The rest consists of modulo, integer division, addition, and subtraction. The % and / operators have the intended meaning only for non-negative inputs, so the assert at the beginning must not be removed.
6. Test Cases
With N = 4, the results of get_uvw are shown below. Each set of components should sum to 4.
Input (r, c) |
Local (i, j) |
Triangle | Result (u, v, w) |
|---|---|---|---|
| (0, 0) | (0, 0) | △ | (4, 0, 0) |
| (1, 1) | (1, 1) | △ | (2, 1, 1) |
| (3, 1) | (3, 1) | △ (on the diagonal) | (0, 3, 1) |
| (3, 3) | (3, 3) | ▽ | (1, 1, 2) |
| (2, 3) | (2, 3) | ▽ | (2, 1, 1) |
| (4, 4) | (0, 0) | △ (next cell) | (4, 0, 0) |
With N = 4 and cells_per_row = 3, the results of tri_id are shown below.
Input (r, c) |
Result |
|---|---|
| (0, 0) | 0 |
| (2, 3) | 1 |
| (3, 3) | 1 |
| (0, 4) | 2 |
| (4, 0) | 6 |
These cases cover both triangle types, points on the diagonal, and cell boundaries. In practice, also check random grid points to confirm that u + v + w == N, and compare the results with a floating-point implementation of barycentric coordinates.
7. Common Mistakes
- Negative input. In C++, the
%result for a negative number is still negative, which produces out-of-range components. Check the input at the entry point, or shift the coordinates to the non-negative range first. - Inconsistent diagonal assignment. If some code assigns
i + j == Nto ▽ while other code assigns it to △, the same point gets stored in two triangles. Use the≤ Nconvention from this article everywhere. - Inconsistent
Norcells_per_row. Mesh generation and index queries must use the same parameters. Otherwise, the IDs will not match. - Mixing corner labels. The same component refers to different corners in △ and ▽. Using the values directly for interpolation produces wrong results.
- Using floating-point numbers to determine the region. Floating-point error makes the region assignment of points on the diagonal unstable. This method uses only integers, so this problem does not occur.
8. Pole Handling and Scope
On a spherical mesh, a pole is a vertex shared by several triangles and does not belong to the interior of any cell. This article treats the pole as a separate vertex and returns a corner coordinate of one of the triangles that contains it, for example (N, 0, 0). This is a convention, not a result derived from geometry.
The original approach assumed that the Cap regions at both poles could reuse the same formula. Whether this holds depends on how the Cap is constructed. Reuse is valid if the Cap is also made of same-sized △ and ▽ triangles, and if the triangles around the pole are connected the same way as in the equatorial band. This article does not verify that construction, so it does not guarantee the result.
The method has the following scope:
- All triangles have the same side length, and
Nis a fixed positive integer. - Coordinates are non-negative integers, and the grid starts at the origin.
- It applies only to rectangular cell layouts. If the mesh boundary is not rectangular, the regions outside the cells must be handled separately.
9. Implementation Checklist
- Set the triangle side length
Nand the number of cells per rowcells_per_row. Use the same values when generating the mesh and when querying indices. - Check that input coordinates are non-negative. Protect this check with
assertin debug builds. - Verify that
u + v + w == Nfor every query result. - Write unit tests with the cases above, then compare the implementation with a floating-point version using random grid points.
- If you need to evaluate performance, measure the integer version and the existing implementation on the target hardware before deciding which to use.
