-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bb1d63
commit d77ccd1
Showing
5 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
uid: dynamic-triangulation-manual | ||
--- | ||
|
||
# Dynamic triangulation | ||
|
||
Using the [`UnsafeTriangulator<T>`][unsafe-triangulator] 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][dynamic-insert-point] | ✔️ | | ||
| DynamicSplitHalfedge | ❌ | | ||
| DynamicRemovePoint | ❌ | | ||
|
||
## DynamicInsertPoint | ||
|
||
The [DynamicInsertPoint][dynamic-insert-point] 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][dynamic-insert-point] 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 triangle `tId`. | ||
|
||
> [!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: | ||
|
||
```csharp | ||
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): | ||
> | ||
> ```csharp | ||
> 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. | ||
[unsafe-triangulator]: xref:andywiecko.BurstTriangulator.LowLevel.Unsafe.UnsafeTriangulator`1 | ||
[dynamic-insert-point]: xref:andywiecko.BurstTriangulator.LowLevel.Unsafe.Extensions.DynamicInsertPoint* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,18 +28,34 @@ references: | |
name: float2 | ||
href: https://docs.unity3d.com/Packages/[email protected]/api/Unity.Mathematics.float2.html | ||
|
||
- uid: Unity.Mathematics.float3 | ||
name: float3 | ||
href: https://docs.unity3d.com/Packages/[email protected]/api/Unity.Mathematics.float3.html | ||
|
||
- uid: UnityEngine.Vector2 | ||
name: Vector2 | ||
href: https://docs.unity3d.com/ScriptReference/Vector2.html | ||
|
||
- uid: UnityEngine.Vector3 | ||
name: Vector3 | ||
href: https://docs.unity3d.com/ScriptReference/Vector3.html | ||
|
||
- uid: Unity.Mathematics.double2 | ||
name: double2 | ||
href: https://docs.unity3d.com/Packages/[email protected]/api/Unity.Mathematics.double2.html | ||
|
||
- uid: Unity.Mathematics.double3 | ||
name: double3 | ||
href: https://docs.unity3d.com/Packages/[email protected]/api/Unity.Mathematics.double3.html | ||
|
||
- uid: Unity.Mathematics.int2 | ||
name: int2 | ||
href: https://docs.unity3d.com/Packages/[email protected]/api/Unity.Mathematics.int2.html | ||
|
||
- uid: Unity.Mathematics.int3 | ||
name: int3 | ||
href: https://docs.unity3d.com/Packages/[email protected]/api/Unity.Mathematics.int3.html | ||
|
||
- uid: UnityEngine.MonoBehaviour | ||
name: MonoBehaviour | ||
href: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html |