Skip to content

Commit

Permalink
docs: dynamic triangulation
Browse files Browse the repository at this point in the history
  • Loading branch information
andywiecko committed Oct 21, 2024
1 parent 4bb1d63 commit d77ccd1
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 7 deletions.
81 changes: 81 additions & 0 deletions Documentation~/manual/advanced/dynamic-triangulation.md
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*
16 changes: 9 additions & 7 deletions Documentation~/manual/advanced/generic-coordinates.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ See benchmark for the generic coordinates [**here**][benchmark].

<br>

| type | delaunay | constraints | holes | refinement | preprocessors | notes |
| :------------------: | :------: | :---------: | :--------: | :--------: | :---------------: | :--------------------------------------: |
| [`float2`][float2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ | |
| [`Vector2`][Vector2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ | Via [float2] reinterpret |
| [`double2`][double2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ | |
| [`fp2`][fp2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ | Requires additional package[^fixed-math] |
| [`int2`][int2] | ✔️ | ✔️ | 🟡[^holes] ||🟡[^preprocessors] | Support up to $\sim 2^{20}$ |
| <div style="width:70px">type</div> | delaunay | constraints | holes | refinement | preprocessors | <div style="width:100px">dynamic[^dynamic]</div> | <div style="width:100px">notes</div> |
| :------------------: | :------: | :---------: | :--------: | :--------: | :---------------: | :-----: | :--------------------------------------: |
| [`float2`][float2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ |✔️ | |
| [`Vector2`][Vector2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ |✔️ | Via [float2] reinterpret |
| [`double2`][double2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ |✔️ | |
| [`fp2`][fp2] | ✔️ | ✔️ | ✔️ | ✔️ |✔️ |✔️ | Requires additional package[^fixed-math] |
| [`int2`][int2] | ✔️ | ✔️ | 🟡[^holes] ||🟡[^preprocessors]|| Support up to $\sim 2^{20}$ |

[^holes]: In the current implementation, holes are fully supported with [`Settings.AutoHolesAndBoundary`][auto]. However, manual holes with [`int2`][int2] coordinates may not guarantee that the given hole can be created. An additional extension is planned in the future to support holes with manual floating-point precision for [`int2`][int2].
[^preprocessors]: Support for [`Preprocessor.COM`][com] with translation only is available.
[^fixed-math]: This feature is available through an optional dependency. Users must install [`com.danielmansson.mathematics.fixedpoint`][fp2]. See how to install it [**here**](xref:getting-started-md#optional-dependencies).
[^dynamic]: Available only through [`UnsafeTriangulator<T>`][unsafe-triangulator] API.

[auto]: xref:andywiecko.BurstTriangulator.TriangulationSettings.AutoHolesAndBoundary
[com]: xref:andywiecko.BurstTriangulator.Preprocessor.COM
[triangulator]: xref:andywiecko.BurstTriangulator.Triangulator
[triangulatorT2]: xref:andywiecko.BurstTriangulator.Triangulator`1
[unsafe-triangulator]: xref:andywiecko.BurstTriangulator.LowLevel.Unsafe.UnsafeTriangulator`1
[extensions]: xref:andywiecko.BurstTriangulator.Extensions
[float2]: xref:Unity.Mathematics.float2
[Vector2]: xref:UnityEngine.Vector2
Expand Down
4 changes: 4 additions & 0 deletions Documentation~/manual/advanced/unsafe-triangulator.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ t.Triangulate(input, output, Args.Default(restoreBoundary: true), Allocator.Pers
t.RefineMesh(output, Allocator.Persistent, areaThreshold: 1, angleThreshold: 0.5f, constrainBoundary: false);
```

### Dynamic triangulation

The [`UnsafeTriangulator<T>`][unsafe-triangulator] API also offers an option for dynamic triangulation. For more details, refer to the [manual](xref:dynamic-triangulation-manual).

[triangulator]: xref:andywiecko.BurstTriangulator.Triangulator`1
[unsafe-triangulator]: xref:andywiecko.BurstTriangulator.LowLevel.Unsafe.UnsafeTriangulator`1
[n-input-data]: xref:andywiecko.BurstTriangulator.LowLevel.Unsafe.InputData`1
Expand Down
2 changes: 2 additions & 0 deletions Documentation~/manual/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ items:
href: advanced/input-managed.md
- name: Unsafe Triangulator
href: advanced/unsafe-triangulator.md
- name: Dynamic triangulation
href: advanced/dynamic-triangulation.md

- name: Benchmark
href: benchmark.md
Expand Down
16 changes: 16 additions & 0 deletions Documentation~/unity-xrefmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d77ccd1

Please sign in to comment.