mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-25 11:17:40 -05:00
Best-fit slide distances rebuilt their band indexes for every offset: over half of a slide's time and nearly all of its allocation (5.8 GB for the 225,684 slides of 51 drawings). CpuDistanceComputer now slides a batch through Core's SlideBatch, which packs the two boundaries and builds their vertices, contact topology and one band index per boundary and push axis once. The pair search prepares each pair once for its first slides and its settling slides (IPreparedDistanceComputer). For pushes along an axis a slide also passes over pairs that cannot change its answer: a vertex or curve outside the other boundary's whole extent across the push, which every band test rejects, and, while the nearest contact is searched for, a pair whose gap along the push already reaches the nearest distance found, which the search discards. Both keep margins for the kernels' own tolerances; a ray hits a line up to epsilon of its length past its ends. Every distance is bit for bit what the per-slide query returns: CpuDistanceComputerEquivalenceTests compares the batch with a frozen copy of the old computer on hand-built boundaries, grazing and touching starts and the pair search's own batches. The per-slide sources and SlideResolver are unchanged and remain what every other caller uses. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Engine.BestFit
|
|
{
|
|
/// <summary>
|
|
/// Slides one boundary against another from every offset of a batch. The boundaries are
|
|
/// prepared once (<see cref="SlideBatch"/>); each slide then returns what
|
|
/// <see cref="SlideResolver.FirstBlocking"/> returns for that offset, bit for bit.
|
|
/// </summary>
|
|
public class CpuDistanceComputer : IPreparedDistanceComputer
|
|
{
|
|
public double[] ComputeDistances(
|
|
List<Line> stationaryLines,
|
|
List<Line> movingTemplateLines,
|
|
SlideOffset[] offsets
|
|
)
|
|
{
|
|
return Slide(SlideBatch.ForLines(movingTemplateLines, stationaryLines), offsets);
|
|
}
|
|
|
|
public double[] ComputeDistances(
|
|
List<Entity> stationaryEntities,
|
|
List<Entity> movingEntities,
|
|
SlideOffset[] offsets
|
|
)
|
|
{
|
|
return Slide(SlideBatch.ForEntities(movingEntities, stationaryEntities), offsets);
|
|
}
|
|
|
|
public IPreparedDistances Prepare(List<Entity> stationaryEntities, List<Entity> movingEntities)
|
|
{
|
|
return new Prepared(SlideBatch.ForEntities(movingEntities, stationaryEntities));
|
|
}
|
|
|
|
private static double[] Slide(SlideBatch batch, SlideOffset[] offsets)
|
|
{
|
|
var results = new double[offsets.Length];
|
|
|
|
for (var i = 0; i < offsets.Length; i++)
|
|
batch.Cover(offsets[i].Dx, offsets[i].Dy, offsets[i].DirX, offsets[i].DirY);
|
|
batch.Index();
|
|
|
|
// All vertices participate: a leading-half filter can miss the next contact
|
|
// after sliding past an initial touch on a concave boundary.
|
|
System.Threading.Tasks.Parallel.For(0, offsets.Length, i =>
|
|
{
|
|
var offset = offsets[i];
|
|
results[i] = batch.FirstBlocking(offset.Dx, offset.Dy, offset.DirX, offset.DirY);
|
|
});
|
|
return results;
|
|
}
|
|
|
|
private sealed class Prepared : IPreparedDistances
|
|
{
|
|
private readonly SlideBatch batch;
|
|
|
|
public Prepared(SlideBatch batch)
|
|
{
|
|
this.batch = batch;
|
|
}
|
|
|
|
public double[] ComputeDistances(SlideOffset[] offsets) => Slide(batch, offsets);
|
|
}
|
|
}
|
|
}
|