@automovie/recovery 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/lib/compilation/index.d.ts +2 -2
  2. package/lib/compilation/index.js +16 -5
  3. package/lib/compilation/index.js.map +1 -1
  4. package/lib/curves/AutoMovieRecoveryCompactApproximation.d.ts +125 -0
  5. package/lib/curves/AutoMovieRecoveryCompactApproximation.js +62 -0
  6. package/lib/curves/AutoMovieRecoveryCompactApproximation.js.map +1 -0
  7. package/lib/curves/AutoMovieRecoveryConicApproximation.d.ts +218 -0
  8. package/lib/curves/AutoMovieRecoveryConicApproximation.js +113 -0
  9. package/lib/curves/AutoMovieRecoveryConicApproximation.js.map +1 -0
  10. package/lib/curves/approximateAutoMovieRecoveryCompactCurves.d.ts +30 -0
  11. package/lib/curves/approximateAutoMovieRecoveryCompactCurves.js +303 -0
  12. package/lib/curves/approximateAutoMovieRecoveryCompactCurves.js.map +1 -0
  13. package/lib/curves/approximateAutoMovieRecoveryConicsAhn.d.ts +39 -0
  14. package/lib/curves/approximateAutoMovieRecoveryConicsAhn.js +289 -0
  15. package/lib/curves/approximateAutoMovieRecoveryConicsAhn.js.map +1 -0
  16. package/lib/curves/approximateAutoMovieRecoveryConicsGhosh.d.ts +32 -0
  17. package/lib/curves/approximateAutoMovieRecoveryConicsGhosh.js +350 -0
  18. package/lib/curves/approximateAutoMovieRecoveryConicsGhosh.js.map +1 -0
  19. package/lib/curves/findAutoMovieRecoveryCubicCriticalParameters.d.ts +29 -0
  20. package/lib/curves/findAutoMovieRecoveryCubicCriticalParameters.js +77 -0
  21. package/lib/curves/findAutoMovieRecoveryCubicCriticalParameters.js.map +1 -0
  22. package/lib/curves/index.d.ts +6 -1
  23. package/lib/curves/index.js +20 -3
  24. package/lib/curves/index.js.map +1 -1
  25. package/lib/fitting/index.d.ts +2 -2
  26. package/lib/fitting/index.js +16 -5
  27. package/lib/fitting/index.js.map +1 -1
  28. package/lib/index.d.ts +6 -57
  29. package/lib/index.js +6 -57
  30. package/lib/index.js.map +1 -1
  31. package/lib/internal/GhoshConicApproximationBalance.d.ts +25 -0
  32. package/lib/internal/GhoshConicApproximationBalance.js +177 -0
  33. package/lib/internal/GhoshConicApproximationBalance.js.map +1 -0
  34. package/lib/internal/GhoshConicApproximationDisplacement.d.ts +23 -0
  35. package/lib/internal/GhoshConicApproximationDisplacement.js +77 -0
  36. package/lib/internal/GhoshConicApproximationDisplacement.js.map +1 -0
  37. package/lib/internal/index.d.ts +183 -0
  38. package/lib/internal/index.js +200 -0
  39. package/lib/internal/index.js.map +1 -0
  40. package/lib/internal/mesh-observations/index.d.ts +1 -2
  41. package/lib/internal/mesh-observations/index.js +1 -2
  42. package/lib/internal/mesh-observations/index.js.map +1 -1
  43. package/lib/observation/index.d.ts +15 -15
  44. package/lib/observation/index.js +29 -31
  45. package/lib/observation/index.js.map +1 -1
  46. package/lib/reconstruction/index.d.ts +10 -10
  47. package/lib/reconstruction/index.js +24 -21
  48. package/lib/reconstruction/index.js.map +1 -1
  49. package/lib/structures/curve/index.d.ts +15 -15
  50. package/lib/structures/curve/index.js +29 -0
  51. package/lib/structures/curve/index.js.map +1 -1
  52. package/lib/structures/domain/index.d.ts +45 -45
  53. package/lib/structures/domain/index.js +59 -0
  54. package/lib/structures/domain/index.js.map +1 -1
  55. package/lib/structures/image/index.d.ts +45 -45
  56. package/lib/structures/image/index.js +59 -0
  57. package/lib/structures/image/index.js.map +1 -1
  58. package/lib/structures/index.d.ts +1 -0
  59. package/lib/structures/index.js +1 -0
  60. package/lib/structures/index.js.map +1 -1
  61. package/lib/structures/internal/index.d.ts +58 -0
  62. package/lib/structures/internal/index.js +75 -0
  63. package/lib/structures/internal/index.js.map +1 -0
  64. package/lib/structures/mesh/index.d.ts +19 -19
  65. package/lib/structures/mesh/index.js +33 -0
  66. package/lib/structures/mesh/index.js.map +1 -1
  67. package/lib/structures/node/index.d.ts +23 -23
  68. package/lib/structures/node/index.js +37 -0
  69. package/lib/structures/node/index.js.map +1 -1
  70. package/lib/structures/observation/index.d.ts +15 -15
  71. package/lib/structures/observation/index.js +29 -0
  72. package/lib/structures/observation/index.js.map +1 -1
  73. package/lib/structures/operation/index.d.ts +10 -10
  74. package/lib/structures/operation/index.js +24 -0
  75. package/lib/structures/operation/index.js.map +1 -1
  76. package/lib/typings/index.d.ts +6 -6
  77. package/lib/typings/index.js +20 -0
  78. package/lib/typings/index.js.map +1 -1
  79. package/package.json +6 -6
  80. package/src/compilation/index.ts +2 -2
  81. package/src/curves/AutoMovieRecoveryCompactApproximation.ts +149 -0
  82. package/src/curves/AutoMovieRecoveryConicApproximation.ts +282 -0
  83. package/src/curves/approximateAutoMovieRecoveryCompactCurves.ts +285 -0
  84. package/src/curves/approximateAutoMovieRecoveryConicsAhn.ts +315 -0
  85. package/src/curves/approximateAutoMovieRecoveryConicsGhosh.ts +444 -0
  86. package/src/curves/findAutoMovieRecoveryCubicCriticalParameters.ts +76 -0
  87. package/src/curves/index.ts +6 -1
  88. package/src/fitting/index.ts +2 -2
  89. package/src/index.ts +6 -57
  90. package/src/internal/GhoshConicApproximationBalance.ts +184 -0
  91. package/src/internal/GhoshConicApproximationDisplacement.ts +83 -0
  92. package/src/internal/index.ts +183 -0
  93. package/src/internal/mesh-observations/index.ts +1 -2
  94. package/src/observation/index.ts +15 -15
  95. package/src/reconstruction/index.ts +10 -10
  96. package/src/structures/curve/index.ts +15 -15
  97. package/src/structures/domain/index.ts +45 -45
  98. package/src/structures/image/index.ts +45 -45
  99. package/src/structures/index.ts +1 -0
  100. package/src/structures/internal/index.ts +58 -0
  101. package/src/structures/mesh/index.ts +19 -19
  102. package/src/structures/node/index.ts +23 -23
  103. package/src/structures/observation/index.ts +15 -15
  104. package/src/structures/operation/index.ts +10 -10
  105. package/src/typings/index.ts +6 -6
@@ -0,0 +1,315 @@
1
+ import { AutoMovieRecoveryConicApproximation as C } from "./AutoMovieRecoveryConicApproximation";
2
+
3
+ /**
4
+ * Construct G1 bitangent conics using Ahn's control-triangle subdivision scheme.
5
+ *
6
+ * Basis: Y. J. Ahn, Conic approximation of planar curves, CAD 33 (2001),
7
+ * 867-872, doi:10.1016/S0010-4485(00)00110-X. The public primary abstract and
8
+ * section preview establish endpoint tangent interpolation, positive fullness
9
+ * factor and subdivision into a containing tangent triangle. The inaccessible
10
+ * characterization theorem and its specific error function are NOT reproduced.
11
+ * This explicitly labelled adaptation minimizes a sampled radial-correspondence
12
+ * maximum distance instead: each source point and conic point lie on the same
13
+ * ray from the tangent intersection. Its signed displacement increases with the
14
+ * weight, so bisection balances positive and negative extreme discrepancies.
15
+ *
16
+ * Samples at odd 1/32 intervals validate weights fitted on even intervals.
17
+ * Their distance is an observable discrepancy, never a continuous Hausdorff
18
+ * bound. A complete result only covers the supplied chart within this sampled
19
+ * criterion. Inflections and inadmissible triangles cause bisection; exhausted
20
+ * budgets omit unresolved intervals and return partial. Finite straight charts
21
+ * admit an explicit collinear quadratic, the conic's degenerate case: exactly
22
+ * collinear to roundoff, or, where the control triangle refuses a chart whose
23
+ * curvature keeps its sign, when its samples advance along its chord within
24
+ * toleranceMm of it (a bend below its coordinates' roundoff, between close
25
+ * inflections, refused at every depth otherwise). Such a primitive's end
26
+ * tangents are its chord's, so G1 holds there only to that tolerance's bend.
27
+ * Source callbacks are borrowed and must
28
+ * be deterministic; callback failures or nonfinite data refuse the whole call.
29
+ * Incident endpoint jets request exact right/left limits so supplied breaks
30
+ * preserve genuine source corners without finite-difference displacements.
31
+ *
32
+ * @evidence contracts/development/common.md#meaningful-documentation Documents the paper and inaccessible theorem, alternative error objective, parameterization, sampled validation, straight degeneracy within roundoff or the caller's tolerance with its G1 limit, and refusal/budget effects.
33
+ * @evidence contracts/development/implementation.md#principled-implementation Endpoint tangent intersection fixes the rational quadratic control triangle. Ahn's published triangle subdivision supplies the construction; this declared adaptation uses monotone radial displacement rather than claiming the unavailable characterization or certified bound. A line is the conic's degenerate case (weight one, collinear controls), admitted only where the triangle construction refuses a chart of one curvature sign whose samples lie within the approximation's own tolerance of the chord, so the result stays inside the sampled contract; its end tangents follow the chord, a stated G1 limitation.
34
+ * @evidence contracts/development/implementation.md#clear-and-simple-structure One pure operation owns admission, coefficient fitting and bounded interval subdivision, using the shared recovery contract and no source format or renderer.
35
+ * @evidence contracts/development/implementation.md#prohibited-implementation-shortcuts No source file, specimen coordinates, fallback mesh, least-squares replacement or unlabelled theorem claim enters the computation. Binary64 roundoff thresholds depend on local scale; tolerance and work budgets come from the caller, and the line admission uses that same tolerance, no constant of its own.
36
+ * @evidence contracts/development/implementation.md#efficient-algorithms With L visited leaves, constant 33-point observation, 48-step scalar bisection and fixed-size validation cost O(L) time; the arc and interval population is bounded by maximumArcs and depth by maximumDepth.
37
+ * @evidence contracts/development/implementation.md#reuse-equivalent-work Each interval's points and derivatives are observed once and reused during all weight iterations; no cross-call cache assumes callback equivalence.
38
+ * @evidence contracts/development/implementation.md#bound-retention-and-release-resources Source callbacks and temporary arrays remain call-local; the returned coefficients contain no source samples, and no file, task or process is acquired.
39
+ */
40
+ export function approximateAutoMovieRecoveryConicsAhn(
41
+ curve: C.ICurve,
42
+ options: C.IOptions,
43
+ ): C.IResult {
44
+ const method: string = "Ahn 2001 triangle construction / sampled radial-minimax adaptation";
45
+ const diagnostics: string[] = C.validate(curve, options);
46
+ const arcs: C.IArc[] = [];
47
+ let queries: number = 0;
48
+ let unresolved: number = 0;
49
+ const reasons: Map<string, number> = new Map();
50
+
51
+ /** Assemble evidence without retaining callback closures or observed samples. */
52
+ const result: (status: C.IResult["status"]) => C.IResult = (
53
+ status: C.IResult["status"],
54
+ ): C.IResult => ({
55
+ method,
56
+ status,
57
+ arcs: status === "refused" ? [] : arcs,
58
+ diagnostics,
59
+ maximumSampleErrorMm: status === "refused" ? 0 : arcs.reduce(
60
+ (maximum: number, arc: C.IArc): number => Math.max(maximum, arc.maximumSampleErrorMm),
61
+ 0,
62
+ ),
63
+ certified: false,
64
+ queries,
65
+ });
66
+ if (diagnostics.length !== 0) return result("refused");
67
+ diagnostics.push("Numerical adaptation: Ahn's full characterization theorem/error function was unavailable; radial-correspondence samples do not certify Hausdorff distance or global minimality.");
68
+
69
+ /** Reject corrupt observations rather than creating substitute coordinates. */
70
+ const observe: (t: number, order?: 1 | 2 | 3, side?: "left" | "right") => C.Point = (
71
+ t: number,
72
+ order?: 1 | 2 | 3,
73
+ side?: "left" | "right",
74
+ ): C.Point => {
75
+ ++queries;
76
+ const p: C.Point = order === undefined ? curve.evaluate(t) : curve.derivative(t, order, side);
77
+ if (p.length !== 2 || !Number.isFinite(p[0]) || !Number.isFinite(p[1]))
78
+ throw new Error(`Nonfinite source ${order === undefined ? "point" : "derivative"} at parameter ${t}.`);
79
+ // A source evaluator may reuse a scratch tuple. Snapshot the value now so
80
+ // later source queries cannot change observed points or returned controls.
81
+ return [p[0], p[1]];
82
+ };
83
+
84
+ /** Source-minus-origin vectors keep determinants translation invariant. */
85
+ const minus: (a: C.Point, b: C.Point) => C.Point = (a: C.Point, b: C.Point): C.Point => [a[0] - b[0], a[1] - b[1]];
86
+
87
+ /** A work item retains only source interval, subdivision depth and its cause. */
88
+ interface Interval {
89
+ a: number;
90
+ b: number;
91
+ depth: number;
92
+ }
93
+
94
+ /** A radial correspondence needs no nearest-point solve during weight search. */
95
+ interface Radial {
96
+ u: number;
97
+ apex: number;
98
+ scale: number;
99
+ idealWeight: number;
100
+ }
101
+
102
+ /**
103
+ * Attempt one admissible triangle. Undefined requests source subdivision;
104
+ * nonfinite observations throw and are reported at the operation boundary.
105
+ */
106
+ const fit: (interval: Interval) => C.IArc | undefined = (
107
+ interval: Interval,
108
+ ): C.IArc | undefined => {
109
+ const points: C.Point[] = [];
110
+ const first: C.Point = observe(interval.a, 1, "right");
111
+ const last: C.Point = observe(interval.b, 1, "left");
112
+ const firstNorm: number = Math.hypot(...first);
113
+ const lastNorm: number = Math.hypot(...last);
114
+ if (firstNorm === 0 || lastNorm === 0) return undefined;
115
+ let curvatureSign: number = 0;
116
+ for (let i: number = 0; i <= 32; ++i) {
117
+ const t: number = interval.a + (interval.b - interval.a) * (i / 32);
118
+ points.push(observe(t));
119
+ if (i % 4 === 0) {
120
+ const d1: C.Point = i === 0 ? first : i === 32 ? last : observe(t, 1);
121
+ const d2: C.Point = observe(t, 2, i === 0 ? "right" : i === 32 ? "left" : undefined);
122
+ const turn: number = C.cross(d1, d2);
123
+ const endpoint: boolean = i === 0 || i === 32;
124
+ const jerk: C.Point | undefined = endpoint ? observe(t, 3, i === 0 ? "right" : "left") : undefined;
125
+
126
+ // A numerical critical root can leave a one-ulp parameter residual at
127
+ // the endpoint. The jerk term bounds the resulting acceleration error,
128
+ // including a large original parameter origin; source jets stay intact.
129
+ const allowance: number = endpoint ? 128 * Number.EPSILON * Math.hypot(...d1) *
130
+ (Math.hypot(...d2) + Math.max(Math.abs(t), curve.domain[1] - curve.domain[0]) * Math.hypot(...jerk!))
131
+ : 64 * Number.EPSILON * (Math.abs(d1[0] * d2[1]) + Math.abs(d1[1] * d2[0]));
132
+ if (Math.abs(turn) > allowance) {
133
+ const sign: number = Math.sign(turn);
134
+ if (curvatureSign !== 0 && sign !== curvatureSign) return undefined;
135
+ curvatureSign = sign;
136
+ }
137
+ }
138
+ }
139
+ const p0: C.Point = points[0]!;
140
+ const p2: C.Point = points[32]!;
141
+ const chord: C.Point = minus(p2, p0);
142
+ const chordSquared: number = chord[0] * chord[0] + chord[1] * chord[1];
143
+ const length: number = Math.hypot(...chord);
144
+ if (length === 0) return undefined;
145
+ const determinant: number = C.cross(first, last);
146
+ const relativeRoundoff: number = 64 * Number.EPSILON;
147
+
148
+ // A conic's degenerate case, the line, within the caller's tolerance:
149
+ // the chart is admitted as the collinear primitive when both endpoint
150
+ // tangents point forward along its chord, its samples advance along the
151
+ // chord, and each lies within toleranceMm of it. It is tried only where
152
+ // the conic construction refuses, so no conic admitted otherwise changes.
153
+ // A chart that bends by less than its coordinates' roundoff, such as a
154
+ // near-flat stretch between two close inflections far from the origin,
155
+ // has samples either side of its chord by roundoff, outside every control
156
+ // triangle at every depth, while its end tangents still differ by more
157
+ // than roundoff; without this it was refused and bisected until the arc
158
+ // budget ran out. The primitive's end tangents are the chord's: they
159
+ // differ from the source's by its bend, which the tolerance bounds through
160
+ // the samples' distance from the chord, not by an angle.
161
+ const straight: () => C.IArc | undefined = (): C.IArc | undefined => {
162
+ if (first[0] * chord[0] + first[1] * chord[1] <= 0 || last[0] * chord[0] + last[1] * chord[1] <= 0) return undefined;
163
+ let previous: number = -Infinity;
164
+ let maximum: number = 0;
165
+ for (const p of points) {
166
+ const offset: C.Point = minus(p, p0);
167
+ const along: number = (offset[0] * chord[0] + offset[1] * chord[1]) / chordSquared;
168
+ maximum = Math.max(maximum, Math.abs(C.cross(offset, chord)) / length);
169
+ if (along < previous || along < 0 || along > 1 || maximum > options.toleranceMm) return undefined;
170
+ previous = along;
171
+ }
172
+ return {
173
+ domain: [interval.a, interval.b],
174
+ controls: [p0, [(p0[0] + p2[0]) / 2, (p0[1] + p2[1]) / 2], p2],
175
+ weight: 1,
176
+ maximumSampleErrorMm: maximum,
177
+ };
178
+ };
179
+
180
+ // Parallel tangents admit a line only when the entire sampled chart and
181
+ // both forward endpoint tangents are collinear to binary64 roundoff, or
182
+ // within the tolerance of its chord (`straight`).
183
+ if (Math.abs(determinant) <= relativeRoundoff * firstNorm * lastNorm) {
184
+ const scale: number = Math.max(length, ...points.map((p: C.Point): number => C.distance(p, p0)));
185
+ const allowance: number = relativeRoundoff * scale;
186
+ if (
187
+ first[0] * chord[0] + first[1] * chord[1] <= 0 ||
188
+ last[0] * chord[0] + last[1] * chord[1] <= 0 ||
189
+ Math.abs(C.cross(first, chord)) > relativeRoundoff * firstNorm * length ||
190
+ Math.abs(C.cross(last, chord)) > relativeRoundoff * lastNorm * length
191
+ ) return straight();
192
+ let previous: number = -Infinity;
193
+ let maximum: number = 0;
194
+ for (const p of points) {
195
+ const offset: C.Point = minus(p, p0);
196
+ const along: number = (offset[0] * chord[0] + offset[1] * chord[1]) / chordSquared;
197
+ maximum = Math.max(maximum, Math.abs(C.cross(offset, chord)) / length);
198
+ if (along < previous || along < 0 || along > 1 || maximum > allowance) return straight();
199
+ previous = along;
200
+ }
201
+ return {
202
+ domain: [interval.a, interval.b],
203
+ controls: [p0, [(p0[0] + p2[0]) / 2, (p0[1] + p2[1]) / 2], p2],
204
+ weight: 1,
205
+ maximumSampleErrorMm: maximum,
206
+ };
207
+ }
208
+
209
+ // Endpoint tangents must meet forward from the start and backward from
210
+ // the end, otherwise positive-weight coefficients cannot follow the arc.
211
+ const startScale: number = C.cross(chord, last) / determinant;
212
+ const endScale: number = C.cross(chord, first) / determinant;
213
+ if (startScale <= 0 || endScale >= 0) return straight();
214
+ const p1: C.Point = [p0[0] + startScale * first[0], p0[1] + startScale * first[1]];
215
+ if (!p1.every(Number.isFinite)) return straight();
216
+ const side: C.Point = minus(p1, p0);
217
+ const area: number = C.cross(side, chord);
218
+ if (area === 0 || !Number.isFinite(area)) return straight();
219
+ const samples: Radial[] = [];
220
+ let previousU: number = 0;
221
+ for (let i: number = 1; i < 32; ++i) {
222
+ const offset: C.Point = minus(points[i]!, p0);
223
+ const b1: number = C.cross(offset, chord) / area;
224
+ const b2: number = C.cross(side, offset) / area;
225
+ const b0: number = 1 - b1 - b2;
226
+ if (b0 <= 0 || b1 <= 0 || b2 <= 0) return straight();
227
+ const root0: number = Math.sqrt(b0);
228
+ const root2: number = Math.sqrt(b2);
229
+ const u: number = root2 / (root0 + root2);
230
+ if (u <= previousU) return straight();
231
+ previousU = u;
232
+ const base: C.Point = [
233
+ (b0 * p0[0] + b2 * p2[0]) / (b0 + b2),
234
+ (b0 * p0[1] + b2 * p2[1]) / (b0 + b2),
235
+ ];
236
+ const idealWeight: number = b1 / (2 * root0 * root2);
237
+ if (!(idealWeight > 0) || !Number.isFinite(idealWeight)) return straight();
238
+ samples.push({ u, apex: b1, scale: C.distance(p1, base), idealWeight });
239
+ }
240
+
241
+ /** Signed distance along a fixed apex ray is monotone in positive weight. */
242
+ const residual: (sample: Radial, weight: number) => number = (
243
+ sample: Radial,
244
+ weight: number,
245
+ ): number => {
246
+ const middle: number = 2 * weight * sample.u * (1 - sample.u);
247
+ const denominator: number = (1 - sample.u) ** 2 + middle + sample.u ** 2;
248
+ return (middle / denominator - sample.apex) * sample.scale;
249
+ };
250
+ const fitting: Radial[] = samples.filter((_sample: Radial, i: number): boolean => i % 2 === 1);
251
+ let low: number = Math.min(...fitting.map((sample: Radial): number => Math.log(sample.idealWeight)));
252
+ let high: number = Math.max(...fitting.map((sample: Radial): number => Math.log(sample.idealWeight)));
253
+ for (let iteration: number = 0; iteration < 48 && low < high; ++iteration) {
254
+ const middle: number = (low + high) / 2;
255
+ const weight: number = Math.exp(middle);
256
+ let minimum: number = 0;
257
+ let maximum: number = 0;
258
+ for (const sample of fitting) {
259
+ const error: number = residual(sample, weight);
260
+ minimum = Math.min(minimum, error);
261
+ maximum = Math.max(maximum, error);
262
+ }
263
+ if (minimum + maximum > 0) high = middle;
264
+ else low = middle;
265
+ }
266
+ const weight: number = Math.exp((low + high) / 2);
267
+ const maximum: number = samples.reduce(
268
+ (error: number, sample: Radial): number => Math.max(error, Math.abs(residual(sample, weight))),
269
+ 0,
270
+ );
271
+ if (!Number.isFinite(maximum) || !Number.isFinite(weight) || weight <= 0) return straight();
272
+ return {
273
+ domain: [interval.a, interval.b],
274
+ controls: [p0, p1, p2],
275
+ weight,
276
+ maximumSampleErrorMm: maximum,
277
+ };
278
+ };
279
+
280
+ const boundaries: number[] = [curve.domain[0], ...new Set(curve.breaks), curve.domain[1]].sort((a: number, b: number): number => a - b);
281
+ if (boundaries.length - 1 > options.maximumArcs) {
282
+ diagnostics.push("The supplied source intervals already exceed maximumArcs.");
283
+ return result("refused");
284
+ }
285
+ const pending: Interval[] = [];
286
+ for (let i: number = boundaries.length - 2; i >= 0; --i)
287
+ pending.push({ a: boundaries[i]!, b: boundaries[i + 1]!, depth: 0 });
288
+ let leaves: number = pending.length;
289
+ try {
290
+ while (pending.length !== 0) {
291
+ const interval: Interval = pending.pop()!;
292
+ const arc: C.IArc | undefined = fit(interval);
293
+ if (arc !== undefined && arc.maximumSampleErrorMm <= options.toleranceMm) {
294
+ arcs.push(arc);
295
+ continue;
296
+ }
297
+ const midpoint: number = (interval.a + interval.b) / 2;
298
+ const reason: string = arc === undefined ? "inadmissible or nonregular tangent triangle" : "held-out radial discrepancy exceeds tolerance";
299
+ if (interval.depth >= options.maximumDepth || leaves >= options.maximumArcs || midpoint === interval.a || midpoint === interval.b) {
300
+ ++unresolved;
301
+ reasons.set(reason, (reasons.get(reason) ?? 0) + 1);
302
+ continue;
303
+ }
304
+ ++leaves;
305
+ pending.push({ a: midpoint, b: interval.b, depth: interval.depth + 1 });
306
+ pending.push({ a: interval.a, b: midpoint, depth: interval.depth + 1 });
307
+ }
308
+ } catch (error) {
309
+ diagnostics.push(error instanceof Error ? error.message : String(error));
310
+ return result("refused");
311
+ }
312
+ for (const [reason, count] of reasons)
313
+ diagnostics.push(`${count} unresolved source interval(s): ${reason}; subdivision budget exhausted.`);
314
+ return result(unresolved === 0 ? "complete" : arcs.length === 0 ? "refused" : "partial");
315
+ }