Skip to content

Commit

Permalink
refactor: merge cleanup jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
andywiecko committed Oct 19, 2023
1 parent 988e77c commit fc0ee6d
Showing 1 changed file with 34 additions and 48 deletions.
82 changes: 34 additions & 48 deletions Runtime/Triangulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ public JobHandle Schedule(JobHandle dependencies = default)
dependencies = new PlantingSeedsJob<PlantHoles>(this).Schedule(dependencies);
}

dependencies = new CleanupTrianglesJob(this).Schedule(this, dependencies);
dependencies = new CleanupPositionsJob(this).Schedule(dependencies);
dependencies = new CleanupJob(this).Schedule(dependencies);

dependencies = Settings.Preprocessor switch
{
Expand Down Expand Up @@ -2390,83 +2389,70 @@ private void GeneratePointsOffset()
}

[BurstCompile]
private unsafe struct CleanupTrianglesJob : IJobParallelForDefer
private unsafe struct CleanupJob : IJob
{
private NativeReference<Status>.ReadOnly status;
[ReadOnly]
private NativeList<Triangle> triangles;
[ReadOnly]
private NativeList<float2> outputPositions;
[WriteOnly]
private NativeList<int>.ParallelWriter outputTriangles;
private NativeList<int> outputTriangles;
[ReadOnly]
private NativeArray<int> pointsOffset;
private NativeReference<Status>.ReadOnly status;

public CleanupTrianglesJob(Triangulator triangulator)
private NativeList<float2> positions;
[ReadOnly]
private NativeArray<int> pointsToRemove;

public CleanupJob(Triangulator triangulator)
{
status = triangulator.status.AsReadOnly();
triangles = triangulator.triangles;
outputPositions = triangulator.outputPositions;
outputTriangles = triangulator.outputTriangles.AsParallelWriter();
outputTriangles = triangulator.outputTriangles;
pointsOffset = triangulator.pointsOffset.AsDeferredJobArray();
status = triangulator.status.AsReadOnly();
}

public JobHandle Schedule(Triangulator triangulator, JobHandle dependencies)
{
return this.Schedule(triangulator.triangles, triangulator.Settings.BatchCount, dependencies);
positions = triangulator.outputPositions;
pointsToRemove = triangulator.pointsToRemove.AsDeferredJobArray();
}

public void Execute(int i)
public void Execute()
{
if (status.Value != Status.OK)
{
return;
}

var t = triangles[i];
if (t.ContainsCommonPointWith(SuperTriangle))
{
return;
}
t = t.GetSignedArea2(outputPositions) < 0 ? t : t.Flip();
var (idA, idB, idC) = t;
t = t.WithShift(-3); // Due to supertriangle verticies.
t = t.WithShift(pointsOffset[idA], pointsOffset[idB], pointsOffset[idC]);
var ptr = UnsafeUtility.AddressOf(ref t);
outputTriangles.AddRangeNoResize(ptr, 3);
}
}

[BurstCompile]
private struct CleanupPositionsJob : IJob
{
private NativeList<float2> positions;
[ReadOnly]
private NativeArray<int> pointsToRemove;
private NativeReference<Status>.ReadOnly status;

public CleanupPositionsJob(Triangulator triangulator)
{
positions = triangulator.outputPositions;
pointsToRemove = triangulator.pointsToRemove.AsDeferredJobArray();
status = triangulator.status.AsReadOnly();
CleanupTriangles();
CleanupPositions();
}

public void Execute()
private void CleanupTriangles()
{
if (status.Value != Status.OK)
for (int i = 0; i < triangles.Length; i++)
{
return;
var t = triangles[i];
if (t.ContainsCommonPointWith(SuperTriangle))
{
continue;
}
t = t.GetSignedArea2(positions) < 0 ? t : t.Flip();
var (idA, idB, idC) = t;
t = t.WithShift(-3); // Due to supertriangle verticies.
t = t.WithShift(pointsOffset[idA], pointsOffset[idB], pointsOffset[idC]);
var ptr = UnsafeUtility.AddressOf(ref t);
outputTriangles.AddRangeNoResize(ptr, 3);
}
}

private void CleanupPositions()
{
for (int i = pointsToRemove.Length - 1; i >= 0; i--)
{
positions.RemoveAt(pointsToRemove[i]);
}

// Remove Super Triangle positions
positions.RemoveAt(0);
positions.RemoveAt(0);
positions.RemoveAt(2);
positions.RemoveAt(1);
positions.RemoveAt(0);
}
}
Expand Down

0 comments on commit fc0ee6d

Please sign in to comment.