Skip to content

Commit 24b6ea8

Browse files
JimmyCushnieandywiecko
authored andcommitted
Add ability to ignore some positions
When creating a new triangulation job, you can now optionally pass a NativeArray<bool> called IgnorePositions. This array should be the same length as the Positions array. Any indexes that are true it IngorePositions will not be used in the triangulation. Example use case: regenerate a mesh's triangles but only use a subset of its vertices, without having to change the mesh's vertices array.
1 parent ae48385 commit 24b6ea8

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

Runtime/Triangulator.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ public class TriangulationSettings
231231
public class InputData
232232
{
233233
public NativeArray<float2> Positions { get; set; }
234+
235+
public NativeArray<bool> IgnorePositions { get; set; }
234236
public NativeArray<int> ConstraintEdges { get; set; }
235237
public NativeArray<float2> HoleSeeds { get; set; }
236238
}
@@ -309,6 +311,8 @@ public struct InputData
309311
{
310312
public NativeArray<float2> Positions;
311313
[NativeDisableContainerSafetyRestriction]
314+
public NativeArray<bool> IgnorePositions;
315+
[NativeDisableContainerSafetyRestriction]
312316
public NativeArray<int> ConstraintEdges;
313317
[NativeDisableContainerSafetyRestriction]
314318
public NativeArray<float2> HoleSeeds;
@@ -336,6 +340,7 @@ public TriangulationJob(Triangulator triangulator)
336340
input = new()
337341
{
338342
Positions = triangulator.Input.Positions,
343+
IgnorePositions = triangulator.Input.IgnorePositions,
339344
ConstraintEdges = triangulator.Input.ConstraintEdges,
340345
HoleSeeds = triangulator.Input.HoleSeeds,
341346
};
@@ -397,6 +402,7 @@ public void Execute()
397402
{
398403
status = output.Status,
399404
positions = localPositions.AsArray(),
405+
ignorePositions = input.IgnorePositions,
400406
triangles = triangles,
401407
halfedges = halfedges,
402408
hullStart = int.MaxValue,
@@ -603,9 +609,11 @@ private struct DistComparer : IComparer<int>
603609
public NativeList<int> triangles;
604610

605611
public NativeList<int> halfedges;
606-
612+
613+
[NativeDisableContainerSafetyRestriction]
614+
public NativeArray<bool> ignorePositions;
607615
[NativeDisableContainerSafetyRestriction]
608-
private NativeArray<int> ids;
616+
private NativeList<int> ids;
609617
[NativeDisableContainerSafetyRestriction]
610618
private NativeArray<float> dists;
611619
[NativeDisableContainerSafetyRestriction]
@@ -661,25 +669,29 @@ public void Execute()
661669

662670
var min = (float2)float.MaxValue;
663671
var max = (float2)float.MinValue;
664-
672+
665673
for (int i = 0; i < positions.Length; i++)
666674
{
675+
if (ignorePositions.IsCreated && ignorePositions[i])
676+
continue;
677+
667678
var p = positions[i];
668679
min = math.min(min, p);
669680
max = math.max(max, p);
670-
ids[i] = i;
681+
ids.Add(i);
671682
}
672683

673684
var center = 0.5f * (min + max);
674685

675686
int i0 = int.MaxValue, i1 = int.MaxValue, i2 = int.MaxValue;
676687
var minDistSq = float.MaxValue;
677-
for (int i = 0; i < positions.Length; i++)
688+
for (int i = 0; i < ids.Length; i++)
678689
{
679-
var distSq = math.distancesq(center, positions[i]);
690+
var id = ids[i];
691+
var distSq = math.distancesq(center, positions[id]);
680692
if (distSq < minDistSq)
681693
{
682-
i0 = i;
694+
i0 = id;
683695
minDistSq = distSq;
684696
}
685697
}
@@ -688,13 +700,14 @@ public void Execute()
688700
var p0 = positions[i0];
689701

690702
minDistSq = float.MaxValue;
691-
for (int i = 0; i < positions.Length; i++)
703+
for (int i = 0; i < ids.Length; i++)
692704
{
693-
if (i == i0) continue;
694-
var distSq = math.distancesq(p0, positions[i]);
705+
var id = ids[i];
706+
if (id == i0) continue;
707+
var distSq = math.distancesq(p0, positions[id]);
695708
if (distSq < minDistSq)
696709
{
697-
i1 = i;
710+
i1 = id;
698711
minDistSq = distSq;
699712
}
700713
}
@@ -703,14 +716,15 @@ public void Execute()
703716
var p1 = positions[i1];
704717

705718
var minRadius = float.MaxValue;
706-
for (int i = 0; i < positions.Length; i++)
719+
for (int i = 0; i < ids.Length; i++)
707720
{
708-
if (i == i0 || i == i1) continue;
709-
var p = positions[i];
721+
var id = ids[i];
722+
if (id == i0 || id == i1) continue;
723+
var p = positions[id];
710724
var r = CircumRadiusSq(p0, p1, p);
711725
if (r < minRadius)
712726
{
713-
i2 = i;
727+
i2 = id;
714728
minRadius = r;
715729
}
716730
}
@@ -739,9 +753,10 @@ public void Execute()
739753

740754
// Sort all other vertices by their distance to the circumcenter of the initial triangle
741755
c = CircumCenter(p0, p1, p2);
742-
for (int i = 0; i < positions.Length; i++)
756+
for (int i = 0; i < ids.Length; i++)
743757
{
744-
dists[i] = math.distancesq(c, positions[i]);
758+
var id = ids[i];
759+
dists[id] = math.distancesq(c, positions[id]);
745760
}
746761

747762
ids.Sort(new DistComparer(dists));

0 commit comments

Comments
 (0)