mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-25 16:57: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>
70 lines
3.2 KiB
C#
70 lines
3.2 KiB
C#
#nullable disable
|
|
// Frozen, test-only copy of OpenNest.Engine/BestFit/CpuDistanceComputer.cs at commit 6067d0c8
|
|
// (before a batch prepared its boundaries once and skipped pairs that cannot change a slide).
|
|
// It runs Core's per-slide sources (EntitySlideEvents, LineSlideEvents) and SlideResolver,
|
|
// which that change left as they were. Only #nullable disable, the namespace, type name and
|
|
// visibility differ from the original.
|
|
// Do not edit: differential tests use it as the independent pre-change oracle.
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenNest.Engine.BestFit;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Tests.BestFit
|
|
{
|
|
internal class LegacyCpuDistanceComputer : IDistanceComputer
|
|
{
|
|
public double[] ComputeDistances(
|
|
List<Line> stationaryLines,
|
|
List<Line> movingTemplateLines,
|
|
SlideOffset[] offsets
|
|
)
|
|
{
|
|
var results = new double[offsets.Length];
|
|
var movingVertices = Vertices(movingTemplateLines);
|
|
var stationaryVertices = Vertices(stationaryLines);
|
|
var contacts = SlideContactClassifier.FromLines(
|
|
movingTemplateLines, Vector.Zero, stationaryLines, Vector.Zero).Prepare();
|
|
|
|
System.Threading.Tasks.Parallel.For(0, offsets.Length, i =>
|
|
{
|
|
var offset = offsets[i];
|
|
var source = new LineSlideEvents(
|
|
movingTemplateLines, movingVertices, offset.Dx, offset.Dy,
|
|
stationaryLines, stationaryVertices, offset.DirX, offset.DirY);
|
|
results[i] = SlideResolver.FirstBlocking(ref source,
|
|
contacts.At(new Vector(offset.Dx, offset.Dy), Vector.Zero), offset.DirX, offset.DirY);
|
|
});
|
|
return results;
|
|
}
|
|
|
|
public double[] ComputeDistances(
|
|
List<Entity> stationaryEntities,
|
|
List<Entity> movingEntities,
|
|
SlideOffset[] offsets
|
|
)
|
|
{
|
|
var results = new double[offsets.Length];
|
|
var movingVertices = SpatialQuery.ExtractEntityVertices(movingEntities);
|
|
var stationaryVertices = SpatialQuery.ExtractEntityVertices(stationaryEntities);
|
|
var contacts = new SlideContactClassifier(movingEntities, stationaryEntities).Prepare();
|
|
|
|
// 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];
|
|
var source = new EntitySlideEvents(
|
|
movingEntities, movingVertices, offset.Dx, offset.Dy,
|
|
stationaryEntities, stationaryVertices, offset.DirX, offset.DirY, arcToLine: true);
|
|
results[i] = SlideResolver.FirstBlocking(ref source,
|
|
contacts.At(new Vector(offset.Dx, offset.Dy), Vector.Zero), offset.DirX, offset.DirY);
|
|
});
|
|
return results;
|
|
}
|
|
|
|
private static Vector[] Vertices(List<Line> lines) =>
|
|
lines.SelectMany(line => new[] { line.StartPoint, line.EndPoint }).Distinct().ToArray();
|
|
}
|
|
}
|