using System;
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.Engine.Jobs;
/// Shared numerical contract for layout validation.
/// Only material contours (SpecialLayers.IsMaterial) count for bounds, clearance and scoring.
/// Scribe/etch marks never affect these checks or salvage envelopes, even outside the sheet.
public static class NestTolerances
{
/// Arc chord tolerance of the spacing outline (perimeter arcs circumscribed,
/// cutouts inscribed) and of the placement validator's overlap check (arcs inscribed).
/// Spacing candidates the outline flags are confirmed on .
public const double ValidationOutline = SpacingOutline.ChordTolerance;
/// How far under the part spacing a gap may fall and still pass both validators
/// and the pre-post spacing review; see .
public const double SpacingSlack = SpacingOutline.SpacingSlack;
/// How far material may extend past the work area and still pass the layout check's
/// bounds test. Matches the general geometry tolerance, so a part drawn a few millionths
/// larger than its sheet's work area (as exported dimensions often are) is still accepted.
public const double WorkAreaSlack = Tolerance.Epsilon;
/// Clipper decimal precision (a 1e-4 coordinate grid).
public const int ClipperPrecision = ClipperBridge.Precision;
/// Extra pair clearance for an engine whose outline error is bounded by t.
/// Each of two boundaries contributes ValidationOutline from validator flattening,
/// one Clipper grid unit from rounding, and t from engine flattening: therefore
/// 2 * ValidationOutline + 2 * 10^(-ClipperPrecision) + 2 * t.
/// This budget assumes valid material geometry and bounded chord error on both sides.
/// Tolerance is negative or non-finite.
public static double SafeClearanceMargin(double engineChordTolerance)
{
if (!double.IsFinite(engineChordTolerance) || engineChordTolerance < 0)
throw new ArgumentOutOfRangeException(nameof(engineChordTolerance));
return 2 * ValidationOutline + 2 * System.Math.Pow(10, -ClipperPrecision)
+ 2 * engineChordTolerance;
}
}