Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: random next barycoords3 #281

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading