using System.Collections.Generic; using OpenNest.Geometry; namespace OpenNest.Engine.BestFit { /// /// Slides one boundary against another from every offset of a batch. The boundaries are /// prepared once (); each slide then returns what /// returns for that offset, bit for bit. /// public class CpuDistanceComputer : IPreparedDistanceComputer { public double[] ComputeDistances( List stationaryLines, List movingTemplateLines, SlideOffset[] offsets ) { return Slide(SlideBatch.ForLines(movingTemplateLines, stationaryLines), offsets); } public double[] ComputeDistances( List stationaryEntities, List movingEntities, SlideOffset[] offsets ) { return Slide(SlideBatch.ForEntities(movingEntities, stationaryEntities), offsets); } public IPreparedDistances Prepare(List stationaryEntities, List 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); } } }