/** * Pure geometry kernel for the pose-aware garment fit stage (`../poseAwareFit.ts`). No glTF, no I/O — * everything here is unit-tested in isolation (`math.spec.ts`), because every earlier attempt at this * pass was undone by a linear-algebra or search bug that produced plausible-looking, wrong numbers. * * CONVENTIONS — read before touching anything here: * * - 4x4 matrices are glTF column-major flat arrays (`m[col * 4 + row]`), exactly as `../../mat4.ts`. * - 3x3 matrices in this file are ROW-MAJOR flat arrays `[a,b,c, d,e,f, g,h,i]` meaning * `[[a,b,c],[d,e,f],[g,h,i]]`, applied to a column vector as a plain matrix-vector product. * `linearPart` is the ONLY place that crosses from one convention to the other, and the round-trip * tests in `math.spec.ts` pin it: `linearPart(M) · v` must equal `transformDir(M, v)` for any M, and * `transformPoint(M, r + inv(linearPart(M)) · d) - transformPoint(M, r)` must equal `d` exactly. * The garment-refit scratch tool (`run/open-alpha-qa/garment-refit/tools/pose-aware-pushout-v2.mjs`) * once had an inverse whose cofactor layout disagreed with its apply step — right magnitude, right * general direction, systematically wrong — and only a round-trip identity test caught it. */ export type V3 = [number, number, number]; /** Upper-left 3x3 of a column-major 4x4, returned ROW-major (see file header). */ export declare function linearPart(m4: ArrayLike): number[]; /** Row-major 3x3 times column vector. */ export declare function applyLinear3(m: ArrayLike, v: readonly number[]): V3; /** Transpose of a row-major 3x3 times column vector (`Mᵀ · v`) — used to pull a posed-space normal back * into a rest-space constraint gradient: if posed displacement = L · Δrest, then n · (L Δ) = (Lᵀ n) · Δ. */ export declare function applyLinear3Transposed(m: ArrayLike, v: readonly number[]): V3; /** Inverse of a row-major 3x3 via the adjugate, or null when (near-)singular. Adjugate entry (r,c) is the * cofactor of (c,r); written out in the same row-major layout as the input. */ export declare function invertLinear3(m: ArrayLike): number[] | null; export declare const dot: (a: readonly number[], b: readonly number[]) => number; export declare const len: (a: readonly number[]) => number; export interface ClosestPoint { point: V3; /** Barycentric weights of `point` for corners a, b, c (sum 1, each in [0,1]). */ bary: V3; } /** * Closest point on triangle (a,b,c) to p — Ericson, Real-Time Collision Detection §5.1.5 — returning the * barycentric weights of the region it lands in directly (no separate area-ratio re-derivation, which is * ill-conditioned for slivers). Identical case analysis to `pokethrough-check.mjs`'s `closestPointOnTriangle` * so the fit targets exactly the point the checker measures from. */ export declare function closestPointOnTriangle(p: readonly number[], a: readonly number[], b: readonly number[], c: readonly number[]): ClosestPoint; /** * Allocation-free core of `closestPointOnTriangle` for the hot loop: the triangle is 9 numbers of `T` starting * at `o`; writes the closest point to `out[0..2]` and its barycentrics to `out[3..5]`; returns the distance * (as `Math.hypot`, like the checker). (The public wrapper above calls this, so the unit tests exercise exactly this code.) */ export declare function closestPointFlat(px: number, py: number, pz: number, T: Float64Array, o: number, out: Float64Array): number; /** * A point q = Σ λᵢ vᵢ on a triangle must move by `amount` along a direction; how much must each corner vᵢ * move along that same direction? Any δ with Σ λᵢ δᵢ = amount works — this returns the MINIMUM-NORM one, * δᵢ = amount · λᵢ / Σ λⱼ². At a corner (λ = 1,0,0) the whole push lands on that corner; at the centroid * every corner moves the full amount (the face translates rigidly); in between it is weighted toward the * nearer corners. It hits `amount` EXACTLY in one step, where the scratch tool's `δᵢ = amount · λᵢ` * under-shot by a factor Σλ² (as little as 1/3 at the centroid) and had to rely on iteration to converge. */ export declare function minNormCornerPush(bary: readonly number[], amount: number): V3; /** * One requirement on a garment vertex's REST-space displacement Δ: `ĝ · Δ ≥ r` with ĝ a unit vector. Every * (pose, body point) that needs a garment vertex to move contributes one; a vertex that must clear the body * in two poses whose skinning differs gets two differently-oriented half-spaces. */ export interface HalfSpace { g: V3; r: number; } /** Collapses near-parallel requirements (cos > `mergeCos`) onto the stronger one and keeps at most `cap` — * a dense body surface emits many near-identical constraints per garment vertex; solving them all buys * nothing and costs a lot. */ export declare function addHalfSpace(list: HalfSpace[], g: readonly number[], r: number, mergeCos?: number, cap?: number): void; /** * Moves `x` (in place) into the intersection of the half-spaces by cyclic orthogonal projection (Kaczmarz / * POCS). Starting from 0 it lands on (or next to) the minimum-norm feasible point for the few, mostly * well-separated constraints a garment vertex carries. Returns the largest residual violation left after * `sweeps` — non-zero only when requirements genuinely conflict (two poses asking for opposite pushes). */ export declare function projectOntoHalfSpaces(x: V3, list: readonly HalfSpace[], sweeps?: number): number; export interface NearestHit { /** Index of the winning triangle (its position in the flat triangle array). */ index: number; distance: number; point: V3; bary: V3; } /** * Uniform-grid nearest-triangle index that returns the TRUE nearest triangle, not the first one it trips * over. Two bugs of exactly this class were already found in this project's tooling (`pokethrough-check.mjs` * and the scratch push-out both stopped widening their search at the first non-empty ring, returning a * farther triangle with a different normal and therefore a wrong signed distance), so the termination rule * here is the provable one: * * After scanning every cell within Chebyshev radius r of the query's cell, any triangle not yet seen lies * entirely outside that (2r+1)³ block, so its distance to p is at least r·cell (p sits inside the centre * cell). Stop as soon as the best distance found is ≤ r·cell — nothing unseen can beat it. * * Rings are scanned as shells (only the cells new at radius r), so no cell is visited twice. EXACT ties are * common, not exotic — a body vertex whose nearest garment point is a garment vertex is equidistant from * every triangle sharing it, and which one "wins" decides the normal the signed distance is taken along — * so ties are broken the way the checker's own scan breaks them (see `scanFirst`), keeping the gate's * verdict identical to the checker's rather than merely close. `maxDistance` * bounds the work: once r·cell > maxDistance nothing within range can remain, and null is returned when the * nearest triangle is farther than that. Brute-force parity is asserted in `math.spec.ts`. */ export declare class TriangleGrid { readonly cell: number; private readonly tris; /** Per-triangle axis-aligned bounds (min xyz, max xyz) — a cheap exact lower bound on its distance. */ private readonly boxes; private readonly grid; /** Query stamp per triangle, so a triangle bucketed in several cells is tested once per query. */ private readonly stamp; private query; private readonly scratch; readonly count: number; /** Bounds of every triangle (min xyz, max xyz) — lets a query far from the whole surface return at once. */ private readonly bounds; /** `tris`: 9 numbers per triangle (a.xyz, b.xyz, c.xyz). */ constructor(tris: Float64Array, cell: number); corner(t: number, k: number): V3; /** Triangle indices whose bucket cells overlap the axis-aligned box [min, max] (a superset of the * triangles that actually intersect it — callers run an exact test on each). */ candidatesInBox(min: readonly number[], max: readonly number[]): number[]; /** How many triangles the segment [p, p + d·maxLength] crosses (touches closer than `minDistance` ignored) * — the number of garment sheets stacked over a body point along its normal. */ countHitsAlong(p: readonly number[], d: readonly number[], maxLength: number, minDistance?: number): number; /** Distance along the unit direction `d` from `p` to the first triangle the segment [p, p + d·maxLength] * crosses (ignoring touches closer than `minDistance`), or null when it crosses none. */ firstHitAlong(p: readonly number[], d: readonly number[], maxLength: number, minDistance?: number): number | null; nearest(p: readonly number[], maxDistance: number): NearestHit | null; } /** * Segment p0→p1 against triangle (a,b,c), Möller–Trumbore. Returns the segment parameter t ∈ [0,1] of the * crossing, or null. Used to detect a pushed garment vertex passing THROUGH another layer of the same * garment (a lining shoved through its shell), which no body-vs-garment measurement can see. */ export declare function segmentHitsTriangle(p0: readonly number[], p1: readonly number[], a: readonly number[], b: readonly number[], c: readonly number[]): number | null; /** As `segmentHitsTriangle`, also returning the barycentric weights (for a, b, c) of the crossing point. */ export declare function segmentTriangleHit(p0: readonly number[], p1: readonly number[], a: readonly number[], b: readonly number[], c: readonly number[]): { t: number; bary: V3; } | null;