@automovie/recovery 0.1.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.
- package/lib/curves/AutoMovieRecoveryCompactApproximation.d.ts +125 -0
- package/lib/curves/AutoMovieRecoveryCompactApproximation.js +62 -0
- package/lib/curves/AutoMovieRecoveryCompactApproximation.js.map +1 -0
- package/lib/curves/AutoMovieRecoveryConicApproximation.d.ts +218 -0
- package/lib/curves/AutoMovieRecoveryConicApproximation.js +113 -0
- package/lib/curves/AutoMovieRecoveryConicApproximation.js.map +1 -0
- package/lib/curves/approximateAutoMovieRecoveryCompactCurves.d.ts +30 -0
- package/lib/curves/approximateAutoMovieRecoveryCompactCurves.js +303 -0
- package/lib/curves/approximateAutoMovieRecoveryCompactCurves.js.map +1 -0
- package/lib/curves/approximateAutoMovieRecoveryConicsAhn.d.ts +39 -0
- package/lib/curves/approximateAutoMovieRecoveryConicsAhn.js +289 -0
- package/lib/curves/approximateAutoMovieRecoveryConicsAhn.js.map +1 -0
- package/lib/curves/approximateAutoMovieRecoveryConicsGhosh.d.ts +32 -0
- package/lib/curves/approximateAutoMovieRecoveryConicsGhosh.js +350 -0
- package/lib/curves/approximateAutoMovieRecoveryConicsGhosh.js.map +1 -0
- package/lib/curves/findAutoMovieRecoveryCubicCriticalParameters.d.ts +29 -0
- package/lib/curves/findAutoMovieRecoveryCubicCriticalParameters.js +77 -0
- package/lib/curves/findAutoMovieRecoveryCubicCriticalParameters.js.map +1 -0
- package/lib/curves/index.d.ts +5 -0
- package/lib/curves/index.js +5 -0
- package/lib/curves/index.js.map +1 -1
- package/lib/internal/GhoshConicApproximationBalance.d.ts +25 -0
- package/lib/internal/GhoshConicApproximationBalance.js +177 -0
- package/lib/internal/GhoshConicApproximationBalance.js.map +1 -0
- package/lib/internal/GhoshConicApproximationDisplacement.d.ts +23 -0
- package/lib/internal/GhoshConicApproximationDisplacement.js +77 -0
- package/lib/internal/GhoshConicApproximationDisplacement.js.map +1 -0
- package/lib/internal/index.d.ts +2 -0
- package/lib/internal/index.js +2 -0
- package/lib/internal/index.js.map +1 -1
- package/package.json +6 -6
- package/src/curves/AutoMovieRecoveryCompactApproximation.ts +149 -0
- package/src/curves/AutoMovieRecoveryConicApproximation.ts +282 -0
- package/src/curves/approximateAutoMovieRecoveryCompactCurves.ts +285 -0
- package/src/curves/approximateAutoMovieRecoveryConicsAhn.ts +315 -0
- package/src/curves/approximateAutoMovieRecoveryConicsGhosh.ts +444 -0
- package/src/curves/findAutoMovieRecoveryCubicCriticalParameters.ts +76 -0
- package/src/curves/index.ts +5 -0
- package/src/internal/GhoshConicApproximationBalance.ts +184 -0
- package/src/internal/GhoshConicApproximationDisplacement.ts +83 -0
- package/src/internal/index.ts +2 -0
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { GhoshConicApproximationBalance as balance } from "../internal/GhoshConicApproximationBalance";
|
|
2
|
+
import { GhoshConicApproximationDisplacement as displacement } from "../internal/GhoshConicApproximationDisplacement";
|
|
3
|
+
import { AutoMovieRecoveryConicApproximation as C } from "./AutoMovieRecoveryConicApproximation";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Numerically reproduce the endpoint-bitangent equioscillation construction of
|
|
7
|
+
* Ghosh, Petitjean and Vegter, Approximation by Conic Splines (2007), sections
|
|
8
|
+
* 3.2–3.4 and 6, DOI 10.1007/s11786-007-0004-8.
|
|
9
|
+
*
|
|
10
|
+
* Source breaks are hard boundaries: piecewise Hermite input does not acquire
|
|
11
|
+
* the paper's smoothness merely by supplying derivatives. On each smooth chart
|
|
12
|
+
* we sample nonzero curvature and monotone affine curvature, construct the
|
|
13
|
+
* endpoint triple-contact limits of the positive-weight bitangent family, and
|
|
14
|
+
* bisect its weight until refined positive and negative normal extrema balance.
|
|
15
|
+
* Located extrema are exchanged into the fitting nodes; independent held-out
|
|
16
|
+
* nodes then validate the selected coefficient rather than driving its fit.
|
|
17
|
+
* Endpoint bisection then selects an interval meeting the requested tolerance.
|
|
18
|
+
* Unlike the paper's symbolic implementation, this experiment uses finite
|
|
19
|
+
* admission/extremum samples, so neither Hausdorff certification nor global
|
|
20
|
+
* minimum arc count is claimed. Collinear charts are an explicit line extension.
|
|
21
|
+
* A zero-curvature chart endpoint uses the compactified infinite-weight limit
|
|
22
|
+
* only to bracket finite positive weights. This is an explicit numerical
|
|
23
|
+
* boundary extension, not a proof that the paper's theorem admits that endpoint.
|
|
24
|
+
* Source callback failures refuse the whole call and discard all coefficients;
|
|
25
|
+
* sampled mathematical admission failures may leave explicitly partial coverage.
|
|
26
|
+
*
|
|
27
|
+
* @evidence contracts/development/common.md#meaningful-documentation States provenance, source-parameter units, preserved breaks, numerical admission, sampled errors, line extension and explicit refusal/partial coverage.
|
|
28
|
+
* @evidence contracts/development/implementation.md#principled-implementation Sections 3.2–3.4 and 6 supply bitangent normal-displacement equioscillation and endpoint bisection; proposition 2.3 supplies affine curvature. Finite samples replace symbolic extrema and cannot certify the continuous premises.
|
|
29
|
+
* @evidence contracts/development/implementation.md#clear-and-simple-structure Local helpers own source queries, differential admission, tangent-triangle algebra, displacement and the two nested bisections; no Human-specific data enters recovery.
|
|
30
|
+
* @evidence contracts/development/implementation.md#prohibited-implementation-shortcuts No source samples become output coefficients, no input smoothing or fixture branch is used, and unsupported intervals remain omitted with diagnostics.
|
|
31
|
+
* @evidence contracts/development/implementation.md#efficient-algorithms Chart count, accepted arcs and caller depth bound interval visits. Each candidate alternates 32-step weight bisection with at most depth extremum exchanges; the balance helper bounds its witness/golden-search cost, and 64 independent nodes validate the result. A separate visit budget bounds unsuccessful subdivision.
|
|
32
|
+
* @evidence contracts/development/implementation.md#reuse-equivalent-work Each candidate caches its source points, normals and barycentric coordinates across coefficient bisection; accepted coefficients and measured error are reused when the endpoint search advances.
|
|
33
|
+
* @evidence contracts/development/implementation.md#bound-retention-and-release-resources The synchronous pure call retains only its bounded interval stack, result arcs and one candidate's samples; no callback, handle or source array survives return.
|
|
34
|
+
*/
|
|
35
|
+
export function approximateAutoMovieRecoveryConicsGhosh(
|
|
36
|
+
curve: C.ICurve,
|
|
37
|
+
options: C.IOptions,
|
|
38
|
+
): C.IResult {
|
|
39
|
+
/** A source point and its Euclidean unit normal, in the caller's frame. */
|
|
40
|
+
interface Sample {
|
|
41
|
+
point: C.Point;
|
|
42
|
+
normal: C.Point;
|
|
43
|
+
}
|
|
44
|
+
/** Source and normal expressed in the endpoint tangent triangle. */
|
|
45
|
+
interface Coordinate {
|
|
46
|
+
value: readonly number[];
|
|
47
|
+
normal: readonly number[];
|
|
48
|
+
}
|
|
49
|
+
/** A pending chart never crosses a caller-supplied source break. */
|
|
50
|
+
interface Interval {
|
|
51
|
+
a: number;
|
|
52
|
+
b: number;
|
|
53
|
+
depth: number;
|
|
54
|
+
}
|
|
55
|
+
const errors: string[] = C.validate(curve, options);
|
|
56
|
+
const diagnostics: Set<string> = new Set(errors);
|
|
57
|
+
const arcs: C.IArc[] = [];
|
|
58
|
+
let queries: number = 0;
|
|
59
|
+
let omitted: number = 0;
|
|
60
|
+
let visits: number = 0;
|
|
61
|
+
let sourceFailure: string | undefined;
|
|
62
|
+
const roundoff: number = 128 * Number.EPSILON;
|
|
63
|
+
|
|
64
|
+
/** Root-rounding residual admission changes the decision, never the source jet. */
|
|
65
|
+
function zeroCurvature(
|
|
66
|
+
t: number,
|
|
67
|
+
v: C.Point,
|
|
68
|
+
a: C.Point,
|
|
69
|
+
j: C.Point,
|
|
70
|
+
): boolean {
|
|
71
|
+
const width: number = curve.domain[1] - curve.domain[0];
|
|
72
|
+
const parameterScale: number = Math.max(width, Math.abs(t));
|
|
73
|
+
const scale: number =
|
|
74
|
+
Math.hypot(...v) * (Math.hypot(...a) + parameterScale * Math.hypot(...j));
|
|
75
|
+
return Math.abs(C.cross(v, a)) <= roundoff * scale;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Count every source callback and reject nonfinite data at its first use. */
|
|
79
|
+
function point(
|
|
80
|
+
t: number,
|
|
81
|
+
order: 0 | 1 | 2 | 3 | 4 = 0,
|
|
82
|
+
side?: "left" | "right",
|
|
83
|
+
): C.Point {
|
|
84
|
+
++queries;
|
|
85
|
+
try {
|
|
86
|
+
const p: C.Point =
|
|
87
|
+
order === 0 ? curve.evaluate(t) : curve.derivative(t, order, side);
|
|
88
|
+
if (p.length !== 2 || !Number.isFinite(p[0]) || !Number.isFinite(p[1]))
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Nonfinite source ${order === 0 ? "point" : "derivative"} at parameter ${t}.`,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Callbacks may reuse one mutable scratch tuple between successive queries.
|
|
94
|
+
return [p[0], p[1]];
|
|
95
|
+
} catch (error) {
|
|
96
|
+
sourceFailure = error instanceof Error ? error.message : String(error);
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Form a local vector without retaining or modifying the source tuple. */
|
|
101
|
+
function subtract(a: C.Point, b: C.Point): C.Point {
|
|
102
|
+
return [a[0] - b[0], a[1] - b[1]];
|
|
103
|
+
}
|
|
104
|
+
/** Keep admission separate from coefficient approximation and tolerance. */
|
|
105
|
+
function finite(p: C.Point): boolean {
|
|
106
|
+
return Number.isFinite(p[0]) && Number.isFinite(p[1]);
|
|
107
|
+
}
|
|
108
|
+
/** Sample proposition 2.3; this is evidence of admission, not a proof of it. */
|
|
109
|
+
function admit(a: number, b: number): "regular" | "line" | string {
|
|
110
|
+
let sign: number = 0;
|
|
111
|
+
let trend: number = 0;
|
|
112
|
+
let previous: number | undefined;
|
|
113
|
+
let previousUncertainty: number = 0;
|
|
114
|
+
let zero: number = 0;
|
|
115
|
+
let boundary: number = 0;
|
|
116
|
+
for (let i: number = 0; i <= 8; ++i) {
|
|
117
|
+
const t: number = a + ((b - a) * i) / 8;
|
|
118
|
+
const side: "left" | "right" | undefined =
|
|
119
|
+
i === 0 ? "right" : i === 8 ? "left" : undefined;
|
|
120
|
+
const v: C.Point = point(t, 1, side);
|
|
121
|
+
const acceleration: C.Point = point(t, 2, side);
|
|
122
|
+
const jerk: C.Point = point(t, 3, side);
|
|
123
|
+
const fourth: C.Point = point(t, 4, side);
|
|
124
|
+
if (
|
|
125
|
+
![v, acceleration, jerk, fourth].every(finite) ||
|
|
126
|
+
Math.hypot(...v) === 0
|
|
127
|
+
)
|
|
128
|
+
return "Nonfinite or nonregular source jet.";
|
|
129
|
+
const determinant: number = C.cross(v, acceleration);
|
|
130
|
+
const determinantScale: number =
|
|
131
|
+
Math.hypot(...v) * Math.hypot(...acceleration);
|
|
132
|
+
if ((i === 0 || i === 8) && zeroCurvature(t, v, acceleration, jerk)) {
|
|
133
|
+
++boundary;
|
|
134
|
+
++zero;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (Math.abs(determinant) <= roundoff * determinantScale) {
|
|
138
|
+
++zero;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (sign !== 0 && Math.sign(determinant) !== sign)
|
|
142
|
+
return "Inflection prevents a nonvanishing-curvature chart.";
|
|
143
|
+
sign = Math.sign(determinant);
|
|
144
|
+
|
|
145
|
+
// phi=det(alpha',alpha'')^(1/3); differentiate phi analytically.
|
|
146
|
+
// Combining proposition 2.3 avoids subtraction of two phi derivatives.
|
|
147
|
+
const first: number = C.cross(v, jerk);
|
|
148
|
+
const second: number = C.cross(acceleration, jerk) + C.cross(v, fourth);
|
|
149
|
+
const phi: number = Math.cbrt(determinant);
|
|
150
|
+
const positiveTerm: number =
|
|
151
|
+
(C.cross(acceleration, jerk) + second / 3) / phi ** 5;
|
|
152
|
+
const negativeTerm: number = (5 * first * first) / (9 * phi ** 8);
|
|
153
|
+
const curvature: number = positiveTerm - negativeTerm;
|
|
154
|
+
const uncertainty: number =
|
|
155
|
+
roundoff * (Math.abs(positiveTerm) + Math.abs(negativeTerm));
|
|
156
|
+
if (!Number.isFinite(curvature)) return "Affine curvature overflow.";
|
|
157
|
+
if (previous !== undefined) {
|
|
158
|
+
const difference: number = curvature - previous;
|
|
159
|
+
const threshold: number =
|
|
160
|
+
128 *
|
|
161
|
+
Math.sqrt(Number.EPSILON) *
|
|
162
|
+
Math.max(
|
|
163
|
+
Number.MIN_VALUE,
|
|
164
|
+
Math.abs(curvature),
|
|
165
|
+
Math.abs(previous),
|
|
166
|
+
) +
|
|
167
|
+
uncertainty +
|
|
168
|
+
previousUncertainty;
|
|
169
|
+
if (Math.abs(difference) > threshold) {
|
|
170
|
+
if (trend !== 0 && Math.sign(difference) !== trend)
|
|
171
|
+
return "Affine curvature is not sample-monotone.";
|
|
172
|
+
trend = Math.sign(difference);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
previous = curvature;
|
|
176
|
+
previousUncertainty = uncertainty;
|
|
177
|
+
}
|
|
178
|
+
if (zero === 9) return "line";
|
|
179
|
+
if (zero !== boundary)
|
|
180
|
+
return "Interior zero curvature prevents a regular chart.";
|
|
181
|
+
if (boundary !== 0)
|
|
182
|
+
diagnostics.add(
|
|
183
|
+
"Numerical boundary extension: a zero-curvature chart endpoint uses the infinite-weight family limit for bracketing only; the paper's nonzero-curvature theorem is not asserted there.",
|
|
184
|
+
);
|
|
185
|
+
return "regular";
|
|
186
|
+
}
|
|
187
|
+
/** Exact straight input is a declared extension beyond the paper's premise. */
|
|
188
|
+
function line(a: number, b: number): C.IArc | undefined {
|
|
189
|
+
const p: C.Point = point(a);
|
|
190
|
+
const q: C.Point = point(b);
|
|
191
|
+
const chord: C.Point = subtract(q, p);
|
|
192
|
+
const length: number = Math.hypot(...chord);
|
|
193
|
+
if (!finite(p) || !finite(q) || length === 0) return undefined;
|
|
194
|
+
for (let i: number = 1; i < 16; ++i) {
|
|
195
|
+
const v: C.Point = subtract(point(a + ((b - a) * i) / 16), p);
|
|
196
|
+
const projection: number =
|
|
197
|
+
(v[0] * chord[0] + v[1] * chord[1]) / length ** 2;
|
|
198
|
+
if (
|
|
199
|
+
!finite(v) ||
|
|
200
|
+
Math.abs(C.cross(chord, v)) / length > roundoff * length ||
|
|
201
|
+
projection < 0 ||
|
|
202
|
+
projection > 1
|
|
203
|
+
)
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
diagnostics.add(
|
|
207
|
+
"Exactly collinear sampled charts use a line extension, outside the paper's nonzero-curvature theorem.",
|
|
208
|
+
);
|
|
209
|
+
return {
|
|
210
|
+
domain: [a, b],
|
|
211
|
+
controls: [p, [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2], q],
|
|
212
|
+
weight: 1,
|
|
213
|
+
maximumSampleErrorMm: 0,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** Fit the one-parameter bitangent family, never free least-squares controls. */
|
|
217
|
+
function fit(a: number, b: number): C.IArc | undefined {
|
|
218
|
+
const p: C.Point = point(a);
|
|
219
|
+
const q: C.Point = point(b);
|
|
220
|
+
const v: C.Point = point(a, 1, "right");
|
|
221
|
+
const z: C.Point = point(b, 1, "left");
|
|
222
|
+
const determinant: number = C.cross(v, z);
|
|
223
|
+
if (
|
|
224
|
+
![p, q, v, z].every(finite) ||
|
|
225
|
+
Math.abs(determinant) <= roundoff * Math.hypot(...v) * Math.hypot(...z)
|
|
226
|
+
)
|
|
227
|
+
return undefined;
|
|
228
|
+
const factor: number = C.cross(subtract(q, p), z) / determinant;
|
|
229
|
+
const corner: C.Point = [p[0] + factor * v[0], p[1] + factor * v[1]];
|
|
230
|
+
const endDirection: C.Point = subtract(q, corner);
|
|
231
|
+
if (factor <= 0 || endDirection[0] * z[0] + endDirection[1] * z[1] <= 0)
|
|
232
|
+
return undefined;
|
|
233
|
+
const x: C.Point = subtract(corner, p);
|
|
234
|
+
const y: C.Point = subtract(q, p);
|
|
235
|
+
const area: number = C.cross(x, y);
|
|
236
|
+
if (!finite(corner) || area === 0) return undefined;
|
|
237
|
+
|
|
238
|
+
/** Affine barycentric coordinates are linear, including along a normal. */
|
|
239
|
+
function coordinates(sample: Sample): Coordinate {
|
|
240
|
+
const delta: C.Point = subtract(sample.point, p);
|
|
241
|
+
const l1: number = C.cross(delta, y) / area;
|
|
242
|
+
const l2: number = C.cross(x, delta) / area;
|
|
243
|
+
const n1: number = C.cross(sample.normal, y) / area;
|
|
244
|
+
const n2: number = C.cross(x, sample.normal) / area;
|
|
245
|
+
return { value: [1 - l1 - l2, l1, l2], normal: [-n1 - n2, n1, n2] };
|
|
246
|
+
}
|
|
247
|
+
/** Source evaluation is cached once across the coefficient bisections. */
|
|
248
|
+
function sample(t: number): Coordinate | undefined {
|
|
249
|
+
const position: C.Point = point(t);
|
|
250
|
+
const tangent: C.Point = point(t, 1);
|
|
251
|
+
const speed: number = Math.hypot(...tangent);
|
|
252
|
+
if (!finite(position) || !finite(tangent) || speed === 0)
|
|
253
|
+
return undefined;
|
|
254
|
+
return coordinates({
|
|
255
|
+
point: position,
|
|
256
|
+
normal: [-tangent[1] / speed, tangent[0] / speed],
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
// Section 3.1 defines endpoint limits by triple contact. For rational
|
|
260
|
+
// Q=(A,B,E), Q'(0)=2w(B-A), so kappa(0)=|det(B-A,E-A)| /
|
|
261
|
+
// (2w²|B-A|³). Matching each source endpoint curvature gives the exact
|
|
262
|
+
// boundary weights of C_u, without truncating its interior contact range.
|
|
263
|
+
const accelerationA: C.Point = point(a, 2, "right");
|
|
264
|
+
const accelerationB: C.Point = point(b, 2, "left");
|
|
265
|
+
const ka: number = zeroCurvature(a, v, accelerationA, point(a, 3, "right"))
|
|
266
|
+
? 0
|
|
267
|
+
: Math.abs(C.cross(v, accelerationA)) / Math.hypot(...v) ** 3;
|
|
268
|
+
const kb: number = zeroCurvature(b, z, accelerationB, point(b, 3, "left"))
|
|
269
|
+
? 0
|
|
270
|
+
: Math.abs(C.cross(z, accelerationB)) / Math.hypot(...z) ** 3;
|
|
271
|
+
const wa: number = Math.sqrt(
|
|
272
|
+
Math.abs(area) / (2 * ka * Math.hypot(...x) ** 3),
|
|
273
|
+
);
|
|
274
|
+
const wb: number = Math.sqrt(
|
|
275
|
+
Math.abs(area) / (2 * kb * Math.hypot(...endDirection) ** 3),
|
|
276
|
+
);
|
|
277
|
+
if (Number.isNaN(wa) || Number.isNaN(wb) || wa <= 0 || wb <= 0)
|
|
278
|
+
return undefined;
|
|
279
|
+
const left: number = Math.min(
|
|
280
|
+
wa === Infinity ? 1 : wa / (1 + wa),
|
|
281
|
+
wb === Infinity ? 1 : wb / (1 + wb),
|
|
282
|
+
);
|
|
283
|
+
const right: number = Math.max(
|
|
284
|
+
wa === Infinity ? 1 : wa / (1 + wa),
|
|
285
|
+
wb === Infinity ? 1 : wb / (1 + wb),
|
|
286
|
+
);
|
|
287
|
+
if (left === 1) return undefined;
|
|
288
|
+
// Source-coordinate subtraction and barycentric arithmetic have a finite
|
|
289
|
+
// roundoff floor. It controls extremum-search convergence, never tolerance.
|
|
290
|
+
const uncertainty: number =
|
|
291
|
+
roundoff *
|
|
292
|
+
Math.max(
|
|
293
|
+
C.distance(p, q),
|
|
294
|
+
Math.abs(p[0]),
|
|
295
|
+
Math.abs(p[1]),
|
|
296
|
+
Math.abs(q[0]),
|
|
297
|
+
Math.abs(q[1]),
|
|
298
|
+
);
|
|
299
|
+
const refined: readonly [number, number] | undefined = balance(
|
|
300
|
+
sample,
|
|
301
|
+
[a, b],
|
|
302
|
+
[left, right],
|
|
303
|
+
options.maximumDepth,
|
|
304
|
+
uncertainty,
|
|
305
|
+
);
|
|
306
|
+
if (refined === undefined) return undefined;
|
|
307
|
+
const rho: number = refined[0];
|
|
308
|
+
const weight: number = rho / (1 - rho);
|
|
309
|
+
if (!Number.isFinite(weight) || weight <= 0) return undefined;
|
|
310
|
+
let maximum: number = refined[1];
|
|
311
|
+
|
|
312
|
+
// These half-shifted nodes never participate in fitting the weight.
|
|
313
|
+
for (let i: number = 0; i < 64; ++i) {
|
|
314
|
+
const coordinate: Coordinate | undefined = sample(
|
|
315
|
+
a + ((b - a) * (i + 0.5)) / 64,
|
|
316
|
+
);
|
|
317
|
+
const d: number | undefined =
|
|
318
|
+
coordinate === undefined ? undefined : displacement(coordinate, rho);
|
|
319
|
+
if (d === undefined) return undefined;
|
|
320
|
+
maximum = Math.max(maximum, Math.abs(d));
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
domain: [a, b],
|
|
324
|
+
controls: [p, corner, q],
|
|
325
|
+
weight,
|
|
326
|
+
maximumSampleErrorMm: maximum,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
/** Construct an immutable report, with omissions distinct from fit precision. */
|
|
330
|
+
function result(): C.IResult {
|
|
331
|
+
return {
|
|
332
|
+
method:
|
|
333
|
+
"ghosh-petitjean-vegter-2007-numerical-equioscillation-boundary-extension",
|
|
334
|
+
status:
|
|
335
|
+
omitted === 0 && errors.length === 0
|
|
336
|
+
? "complete"
|
|
337
|
+
: arcs.length === 0
|
|
338
|
+
? "refused"
|
|
339
|
+
: "partial",
|
|
340
|
+
arcs,
|
|
341
|
+
diagnostics: [...diagnostics],
|
|
342
|
+
maximumSampleErrorMm: arcs.reduce(
|
|
343
|
+
(value: number, arc: C.IArc): number =>
|
|
344
|
+
Math.max(value, arc.maximumSampleErrorMm),
|
|
345
|
+
0,
|
|
346
|
+
),
|
|
347
|
+
certified: false,
|
|
348
|
+
queries,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
if (errors.length !== 0) return result();
|
|
352
|
+
diagnostics.add(
|
|
353
|
+
"Numerical adaptation: sampled regularity, affine-curvature monotonicity and normal extrema do not certify the paper's smoothness/Hausdorff hypotheses or optimal arc count.",
|
|
354
|
+
);
|
|
355
|
+
const breaks: number[] = [
|
|
356
|
+
curve.domain[0],
|
|
357
|
+
...new Set(curve.breaks),
|
|
358
|
+
curve.domain[1],
|
|
359
|
+
].sort((a: number, b: number): number => a - b);
|
|
360
|
+
const pending: Interval[] = [];
|
|
361
|
+
for (let i: number = breaks.length - 2; i >= 0; --i)
|
|
362
|
+
pending.push({ a: breaks[i]!, b: breaks[i + 1]!, depth: 0 });
|
|
363
|
+
const visitBudget: number =
|
|
364
|
+
(breaks.length + options.maximumArcs) * (options.maximumDepth + 1) * 2;
|
|
365
|
+
try {
|
|
366
|
+
while (pending.length !== 0) {
|
|
367
|
+
if (
|
|
368
|
+
arcs.length + omitted >= options.maximumArcs ||
|
|
369
|
+
++visits > visitBudget
|
|
370
|
+
) {
|
|
371
|
+
omitted += pending.length;
|
|
372
|
+
diagnostics.add(
|
|
373
|
+
"Arc or admission-work budget exhausted; remaining source domains are omitted.",
|
|
374
|
+
);
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
const interval: Interval = pending.pop()!;
|
|
378
|
+
const { a, b, depth } = interval;
|
|
379
|
+
const admission: string = admit(a, b);
|
|
380
|
+
let candidate: C.IArc | undefined =
|
|
381
|
+
admission === "regular"
|
|
382
|
+
? fit(a, b)
|
|
383
|
+
: admission === "line"
|
|
384
|
+
? line(a, b)
|
|
385
|
+
: undefined;
|
|
386
|
+
if (
|
|
387
|
+
candidate !== undefined &&
|
|
388
|
+
candidate.maximumSampleErrorMm <= options.toleranceMm
|
|
389
|
+
) {
|
|
390
|
+
arcs.push(candidate);
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Proposition 3.4 permits endpoint bisection on an admitted affine spiral.
|
|
395
|
+
// A failed numerical candidate is only an upper search bound, never a fit.
|
|
396
|
+
if (admission === "regular" && candidate !== undefined) {
|
|
397
|
+
let left: number = a;
|
|
398
|
+
let right: number = b;
|
|
399
|
+
let accepted: C.IArc | undefined;
|
|
400
|
+
for (
|
|
401
|
+
let iteration: number = 0;
|
|
402
|
+
iteration < options.maximumDepth - depth;
|
|
403
|
+
++iteration
|
|
404
|
+
) {
|
|
405
|
+
const middle: number = (left + right) / 2;
|
|
406
|
+
candidate = fit(a, middle);
|
|
407
|
+
if (
|
|
408
|
+
candidate !== undefined &&
|
|
409
|
+
candidate.maximumSampleErrorMm <= options.toleranceMm
|
|
410
|
+
) {
|
|
411
|
+
left = middle;
|
|
412
|
+
accepted = candidate;
|
|
413
|
+
} else right = middle;
|
|
414
|
+
}
|
|
415
|
+
if (accepted !== undefined) {
|
|
416
|
+
arcs.push(accepted);
|
|
417
|
+
pending.push({ a: accepted.domain[1], b, depth: depth + 1 });
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const middle: number = (a + b) / 2;
|
|
422
|
+
if (depth >= options.maximumDepth || middle === a || middle === b) {
|
|
423
|
+
++omitted;
|
|
424
|
+
diagnostics.add(
|
|
425
|
+
admission === "regular" || admission === "line"
|
|
426
|
+
? "Unresolved bitangent triangle, normal intersection or tolerance at subdivision limit."
|
|
427
|
+
: admission,
|
|
428
|
+
);
|
|
429
|
+
} else {
|
|
430
|
+
pending.push(
|
|
431
|
+
{ a: middle, b, depth: depth + 1 },
|
|
432
|
+
{ a, b: middle, depth: depth + 1 },
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
} catch (error) {
|
|
437
|
+
if (sourceFailure === undefined) throw error;
|
|
438
|
+
errors.push(sourceFailure);
|
|
439
|
+
diagnostics.add(`Source callback failure: ${sourceFailure}`);
|
|
440
|
+
arcs.length = 0;
|
|
441
|
+
++omitted;
|
|
442
|
+
}
|
|
443
|
+
return result();
|
|
444
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { rootsAutoMovieViewPolynomial } from "@automovie/engine";
|
|
2
|
+
|
|
3
|
+
import type { AutoMovieRecoveryConicApproximation as C } from "./AutoMovieRecoveryConicApproximation";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Locate isolated cubic regularity and affine-curvature candidates analytically.
|
|
7
|
+
*
|
|
8
|
+
* For p(t)=a*t^3+b*t^2+c*t+d, D=cross(p',p'') is quadratic,
|
|
9
|
+
* D=D0+e0*t+f*t^2/2 with e0=6*cross(c,a), f=12*cross(b,a).
|
|
10
|
+
* Away from D=0, affine curvature has a stationary value only where e=D'=0
|
|
11
|
+
* or 4*e^2-9*f*D=0. The existing engine polynomial-root owner evaluates these
|
|
12
|
+
* source-coefficient polynomials; it does not fit observations or a point grid.
|
|
13
|
+
* Roots of the two derivative components also identify stationary candidates.
|
|
14
|
+
*
|
|
15
|
+
* Input is a finite planar Bernstein control quadruple, in any fixed uniform
|
|
16
|
+
* spatial unit. Translation and uniform edge normalization condition only the
|
|
17
|
+
* root calculation: the supplied controls and source evaluator stay unchanged.
|
|
18
|
+
* Results are numerical candidates in the open normalized interval, not exact
|
|
19
|
+
* real roots or certified isolating intervals. Exactly identical values merge;
|
|
20
|
+
* nearby distinct values remain separate. A constant polynomial has no isolated
|
|
21
|
+
* roots. Zero-curvature values include inflections, repeated roots and stationary
|
|
22
|
+
* points and do not themselves establish source regularity.
|
|
23
|
+
*
|
|
24
|
+
* @evidence contracts/development/common.md#meaningful-documentation States the derivative polynomials, normalized parameter, source preservation, conditioning, degenerate cases and numerical proof limits.
|
|
25
|
+
* @evidence contracts/development/implementation.md#principled-implementation Cubic differentiation gives the exact bounded-degree critical equations in real arithmetic; binary64 root candidates reuse the engine's normalized monotone-partition owner and stationary candidates require both velocity components to vanish within Horner roundoff.
|
|
26
|
+
* @evidence contracts/development/implementation.md#clear-and-simple-structure This pure coefficient helper owns one derivation; the shared namespace exposes it to Ahn, Ghosh and their source-chart adapter without artifact I/O.
|
|
27
|
+
* @evidence contracts/development/implementation.md#prohibited-implementation-shortcuts No Human coordinates, finite-difference offsets, epsilon root merging, endpoint displacement or altered source jets are used. Nonfinite controls or unrepresentable edge arithmetic throw RangeError.
|
|
28
|
+
* @evidence contracts/development/implementation.md#efficient-algorithms A fixed four-control source yields polynomial degrees at most two; the engine solver has bounded derivative recursion and 60 bisections, giving constant work and storage per cubic.
|
|
29
|
+
* @evidence contracts/development/implementation.md#reuse-equivalent-work One difference/normalization pass supplies the velocity, curvature and affine critical polynomials; callers may reuse the returned candidates for both algorithms.
|
|
30
|
+
* @evidence contracts/development/implementation.md#bound-retention-and-release-resources All coefficients and root arrays are call-local; only scalar parameters and diagnostics transfer to the caller, with no retained source or external resource.
|
|
31
|
+
*/
|
|
32
|
+
export function findAutoMovieRecoveryCubicCriticalParameters(
|
|
33
|
+
controls: readonly [C.Point, C.Point, C.Point, C.Point],
|
|
34
|
+
): C.ICubicCriticalParameters {
|
|
35
|
+
if (controls.length !== 4)
|
|
36
|
+
throw new RangeError("Cubic critical parameters require four finite planar Bernstein controls.");
|
|
37
|
+
for (let i: number = 0; i < 4; ++i) {
|
|
38
|
+
const p: C.Point | undefined = controls[i];
|
|
39
|
+
if (p === undefined || !Array.isArray(p) || p.length !== 2 || !Number.isFinite(p[0]) || !Number.isFinite(p[1]))
|
|
40
|
+
throw new RangeError("Cubic critical parameters require four finite planar Bernstein controls.");
|
|
41
|
+
}
|
|
42
|
+
const raw: C.Point[] = controls.slice(1).map((p: C.Point, i: number): C.Point => [p[0] - controls[i]![0], p[1] - controls[i]![1]]);
|
|
43
|
+
const scale: number = Math.max(...raw.flatMap((p: C.Point): number[] => [Math.abs(p[0]), Math.abs(p[1])]));
|
|
44
|
+
if (!Number.isFinite(scale)) throw new RangeError("Source control differences exceed finite binary64 arithmetic.");
|
|
45
|
+
if (scale === 0) return { breaks: [], zeroCurvature: [], affineCritical: [], stationary: [], collinear: true,
|
|
46
|
+
diagnostics: ["Constant cubic: every parameter is stationary; no isolated critical parameters exist."] };
|
|
47
|
+
const edges: C.Point[] = raw.map((p: C.Point): C.Point => [p[0] / scale, p[1] / scale]);
|
|
48
|
+
const a: C.Point = [edges[2]![0] - 2 * edges[1]![0] + edges[0]![0], edges[2]![1] - 2 * edges[1]![1] + edges[0]![1]];
|
|
49
|
+
const b: C.Point = [3 * (edges[1]![0] - edges[0]![0]), 3 * (edges[1]![1] - edges[0]![1])];
|
|
50
|
+
const c: C.Point = [3 * edges[0]![0], 3 * edges[0]![1]];
|
|
51
|
+
const determinant: (x: C.Point, y: C.Point) => number = (x: C.Point, y: C.Point): number => x[0] * y[1] - x[1] * y[0];
|
|
52
|
+
const d0: number = 2 * determinant(c, b);
|
|
53
|
+
const e0: number = 6 * determinant(c, a);
|
|
54
|
+
const f: number = 12 * determinant(b, a);
|
|
55
|
+
const collinear: boolean = d0 === 0 && e0 === 0 && f === 0;
|
|
56
|
+
const zeroCurvature: number[] = collinear ? [] : rootsAutoMovieViewPolynomial([d0, e0, f / 2]);
|
|
57
|
+
const affineCritical: number[] = collinear ? [] : [...new Set([
|
|
58
|
+
...rootsAutoMovieViewPolynomial([e0, f]),
|
|
59
|
+
...rootsAutoMovieViewPolynomial([4 * e0 * e0 - 9 * f * d0, -f * e0, -f * f / 2]),
|
|
60
|
+
])].sort((x: number, y: number): number => x - y);
|
|
61
|
+
|
|
62
|
+
// A common velocity root is a regularity event even when D is identically
|
|
63
|
+
// zero or touches zero without changing sign. Both components must agree.
|
|
64
|
+
const vx: number[] = [c[0], 2 * b[0], 3 * a[0]];
|
|
65
|
+
const vy: number[] = [c[1], 2 * b[1], 3 * a[1]];
|
|
66
|
+
const primary: number[] = vx.some((v: number): boolean => v !== 0) ? vx : vy;
|
|
67
|
+
const residual: (coefficients: number[], t: number) => number = (coefficients: number[], t: number): number => coefficients[0]! + t * (coefficients[1]! + t * coefficients[2]!);
|
|
68
|
+
const allowance: (coefficients: number[], t: number) => number = (coefficients: number[], t: number): number => 64 * Number.EPSILON * (Math.abs(coefficients[0]!) + Math.abs(t * coefficients[1]!) + Math.abs(t * t * coefficients[2]!));
|
|
69
|
+
const stationary: number[] = rootsAutoMovieViewPolynomial(primary).filter((t: number): boolean =>
|
|
70
|
+
Math.abs(residual(vx, t)) <= allowance(vx, t) && Math.abs(residual(vy, t)) <= allowance(vy, t));
|
|
71
|
+
const diagnostics: string[] = ["Critical parameters are binary64 polynomial-root candidates, not certified real-root enclosures; close multiple roots may remain unresolved."];
|
|
72
|
+
if (collinear) diagnostics.push("Curvature numerator is identically zero: regular pieces are collinear and have no isolated curvature roots.");
|
|
73
|
+
if (!collinear && e0 === 0 && f === 0) diagnostics.push("Affine curvature is constant on regular intervals; no isolated affine-curvature extrema exist.");
|
|
74
|
+
return { breaks: [...new Set([...zeroCurvature, ...affineCritical, ...stationary])].sort((x: number, y: number): number => x - y),
|
|
75
|
+
zeroCurvature, affineCritical, stationary, collinear, diagnostics };
|
|
76
|
+
}
|
package/src/curves/index.ts
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
1
|
export * from "./convertAutoMovieRecoveryCurves";
|
|
2
|
+
export * from "./AutoMovieRecoveryCompactApproximation";
|
|
3
|
+
export * from "./AutoMovieRecoveryConicApproximation";
|
|
4
|
+
export * from "./approximateAutoMovieRecoveryCompactCurves";
|
|
5
|
+
export * from "./approximateAutoMovieRecoveryConicsAhn";
|
|
6
|
+
export * from "./approximateAutoMovieRecoveryConicsGhosh";
|