Skip to content

Commit

Permalink
test: random next barycoords3
Browse files Browse the repository at this point in the history
  • Loading branch information
andywiecko committed Oct 18, 2024
1 parent ecd4829 commit 7093402
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
13 changes: 12 additions & 1 deletion Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,18 @@ public static void Run<T>(this Triangulator<T> triangulator) where T : unmanaged
Extensions.Run((dynamic)triangulator);
public static JobHandle Schedule<T>(this Triangulator<T> triangulator, JobHandle dependencies = default) where T : unmanaged =>
Extensions.Schedule((dynamic)triangulator, dependencies);

public static float3 NextBarcoords3(ref this Unity.Mathematics.Random random)
{
var bx = random.NextFloat();
var by = random.NextFloat();
if (bx + by > 1)
{
bx = 1 - bx;
by = 1 - by;
}
var bz = 1 - bx - by;
return new(bx, by, bz);
}
public static float2[] CastToFloat2<T>(this IEnumerable<T> data) where T : unmanaged => data.Select(i => default(T) switch
{
#if UNITY_MATHEMATICS_FIXEDPOINT
Expand Down
16 changes: 4 additions & 12 deletions Tests/UnsafeTriangulatorEditorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,8 @@ public void DynamicInsertTest()
using var halfedges = new NativeList<int>(Allocator.Persistent);
using var constrainedHalfedges = new NativeList<bool>(Allocator.Persistent);
using var constraints = new NativeArray<int>(managedConstraints, Allocator.Persistent);

t.Triangulate(
input: new() { Positions = positions, ConstraintEdges = constraints },
output: new() { Positions = outputPositions, Triangles = triangles, Halfedges = halfedges, ConstrainedHalfedges = constrainedHalfedges },
args: Args.Default(autoHolesAndBoundary: true), Allocator.Persistent
);
var input = new LowLevel.Unsafe.InputData<T> { Positions = positions, ConstraintEdges = constraints };
var output = new LowLevel.Unsafe.OutputData<T> { Positions = outputPositions, Triangles = triangles, Halfedges = halfedges, ConstrainedHalfedges = constrainedHalfedges };

int FindTriangle(ReadOnlySpan<int> initialTriangles, int j)
{
Expand All @@ -571,6 +567,7 @@ int FindTriangle(ReadOnlySpan<int> initialTriangles, int j)
return -1;
}

t.Triangulate(input, output, args: Args.Default(autoHolesAndBoundary: true), Allocator.Persistent);
TestUtils.Draw(outputPositions.AsReadOnly().CastToFloat2(), triangles.AsReadOnly(), Color.red, duration: 5f);

var random = new Unity.Mathematics.Random(seed: 42);
Expand All @@ -582,12 +579,7 @@ int FindTriangle(ReadOnlySpan<int> initialTriangles, int j)
var i = FindTriangle(initialTriangles, j);
if (i != -1)
{
var bar = math.abs(random.NextFloat3Direction());
bar /= bar.x + bar.y + bar.z;
bar.z = 1 - bar.x - bar.y;

var output = new LowLevel.Unsafe.OutputData<T> { Positions = outputPositions, Triangles = triangles, Halfedges = halfedges, ConstrainedHalfedges = constrainedHalfedges };
t.DynamicInsertPoint(output, i, bar, Allocator.Persistent);
t.DynamicInsertPoint(output, tId: i, bar: random.NextBarcoords3(), allocator: Allocator.Persistent);
}
}

Expand Down

0 comments on commit 7093402

Please sign in to comment.