Dynamic triangulation
Using the UnsafeTriangulator<T>
API, you can perform dynamic triangulation.
This feature is especially useful in scenarios like path-finding in RTS games, where recalculating only a small portion of the mesh, instead of the entire mesh, can significantly improve efficiency.
Method | Implemented |
---|---|
DynamicInsertPoint | ✔️ |
DynamicSplitHalfedge | ❌ |
DynamicRemovePoint | ❌ |
DynamicInsertPoint
The DynamicInsertPoint method allows you to insert a point into a specified triangle using barycentric coordinates. This method only supports point insertion within the original triangulation domain and cannot be used to insert points outside the existing mesh.
Inserting a point at specific T2 p
coordinates can be computationally expensive since it requires locating the triangle that contains the point p
.
This package does not include acceleration structures, as it assumes the user will implement this based on their specific requirements.
It is recommended to use structures such as bounding volume hierarchies, 2D trees, grids, or buckets for efficient point lookup (p
\(\to \triangle\)).
The most suitable acceleration structure may vary depending on the use case.
The DynamicInsertPoint method accepts the following parameters (in addition to output
and allocator
):
tId
the index of the triangle where the point should be inserted.bar
the barycentric coordinates of the point inside triangletId
.
Important
All \(\texttt{bar}\) coordinates must be valid barycentric coordinates inside the triangle, meaning
- \(\forall_i\,\,\texttt{bar}[i]\in(0, 1)\);
- \(\sum_i \, \texttt{bar}[i] = 1\).
Here is an example of how to use the API:
var t = new UnsafeTriangulator<float2>();
using var positions = new NativeArray<float2>(..., Allocator.Persistent);
using var constraints = new NativeArray<int>(..., Allocator.Persistent);
var input = new InputData<float2> { Positions = positions, ConstraintEdges = constraints };
using var outputPositions = new NativeList<float2>(Allocator.Persistent);
using var triangles = new NativeList<int>(Allocator.Persistent);
using var halfedges = new NativeList<int>(Allocator.Persistent);
using var constrainedHalfedges = new NativeList<bool>(Allocator.Persistent);
var output = new OutputData<float2> { Positions = outputPositions, Triangles = triangles, Halfedges = halfedges, ConstrainedHalfedges = constrainedHalfedges };
t.Triangulate(input, output, args: Args.Default(autoHolesAndBoundary: true), Allocator.Persistent);
// Insert a new point in triangle with index 42 at the center (barycentric coordinates: [⅓, ⅓, ⅓]).
t.DynamicInsertPoint(output, tId: 42, bar: 1f / 3, allocator: Allocator.Persistent);
Note
To quickly calculate the corresponding bar
for a given p
inside a triangle (a, b, c)
, you can use the following utility function (not included in the package):
static float3 Bar(float2 p, float2 a, float2 b, float2 c)
{
float cross(float2 x, float2 y) => x.x * y.y - x.y * y.x;
var (v0, v1, v2) = (b - a, c - a, p - a);
var v = cross(v2, v1) / cross(v0, v1);
var w = cross(v0, v2) / cross(v0, v1);
var u = 1 - v - w;
return math.float3(u, v, w);
}
DynamicSplitHalfedge
Not implemented.
DynamicRemovePoint
Not implemented.