diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-18 18:44:21 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-18 18:44:21 +0100 |
| commit | eff75e6d15eaebccec814b37504a6eeff53fb9e8 (patch) | |
| tree | 6b57a7336bd40be09f58d3ef4a9fcf6e3aedfc05 /tools/mq_editor/utils.js | |
| parent | 9b1439582853c24d240d1e62290f33809097bbe8 (diff) | |
feat(mq_editor): switch curve interpolation to Lagrange through all control points
Replace cubic Bezier with Lagrange interpolation so P1/P2 are actual
points on the curve. Eval uses stored t1/t2 as arbitrary knot positions.
fitBezier keeps least-squares but with Lagrange basis at u=1/3,2/3.
Remove endpoint companion drag (no longer tangent handles).
TODO: support arbitrary number of inner control points.
handoff(Claude): Lagrange interpolation replaces Bezier in mq_editor curve eval/fit.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'tools/mq_editor/utils.js')
| -rw-r--r-- | tools/mq_editor/utils.js | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/tools/mq_editor/utils.js b/tools/mq_editor/utils.js index 96d807c..2c6b2f5 100644 --- a/tools/mq_editor/utils.js +++ b/tools/mq_editor/utils.js @@ -1,29 +1,48 @@ // Shared utilities for mq_editor -// Evaluate cubic bezier curve at time t (robust: handles dt<=0) +// Evaluate interpolating curve at time t via Lagrange interpolation. +// The curve passes through all 4 control points at their stored time positions. +// Knot positions: u_k = (t_k - t0) / (t3 - t0), k=0..3. +// TODO: support arbitrary number of inner control points function evalBezier(curve, t) { const dt = curve.t3 - curve.t0; if (dt <= 0) return curve.v0; let u = (t - curve.t0) / dt; u = Math.max(0, Math.min(1, u)); - const u1 = 1.0 - u; - return u1*u1*u1 * curve.v0 + - 3*u1*u1*u * curve.v1 + - 3*u1*u*u * curve.v2 + - u*u*u * curve.v3; + const u1 = (curve.t1 - curve.t0) / dt; + const u2 = (curve.t2 - curve.t0) / dt; + const d0 = (-u1) * (-u2) * (-1); + const d1 = u1 * (u1 - u2) * (u1 - 1); + const d2 = u2 * (u2 - u1) * (u2 - 1); + const d3 = (1 - u1) * (1 - u2); + if (Math.abs(d0) < 1e-9 || Math.abs(d1) < 1e-9 || Math.abs(d2) < 1e-9 || Math.abs(d3) < 1e-9) + return curve.v0 + (curve.v3 - curve.v0) * u; + const l0 = (u - u1) * (u - u2) * (u - 1) / d0; + const l1 = u * (u - u2) * (u - 1) / d1; + const l2 = u * (u - u1) * (u - 1) / d2; + const l3 = u * (u - u1) * (u - u2) / d3; + return l0 * curve.v0 + l1 * curve.v1 + l2 * curve.v2 + l3 * curve.v3; } -// Evaluate amplitude component of unified bezier curve at time t +// Evaluate amplitude component of interpolating curve at time t function evalBezierAmp(curve, t) { const dt = curve.t3 - curve.t0; if (dt <= 0) return curve.a0; let u = (t - curve.t0) / dt; u = Math.max(0, Math.min(1, u)); - const u1 = 1.0 - u; - return u1*u1*u1 * curve.a0 + - 3*u1*u1*u * curve.a1 + - 3*u1*u*u * curve.a2 + - u*u*u * curve.a3; + const u1 = (curve.t1 - curve.t0) / dt; + const u2 = (curve.t2 - curve.t0) / dt; + const d0 = (-u1) * (-u2) * (-1); + const d1 = u1 * (u1 - u2) * (u1 - 1); + const d2 = u2 * (u2 - u1) * (u2 - 1); + const d3 = (1 - u1) * (1 - u2); + if (Math.abs(d0) < 1e-9 || Math.abs(d1) < 1e-9 || Math.abs(d2) < 1e-9 || Math.abs(d3) < 1e-9) + return curve.a0 + (curve.a3 - curve.a0) * u; + const l0 = (u - u1) * (u - u2) * (u - 1) / d0; + const l1 = u * (u - u2) * (u - 1) / d1; + const l2 = u * (u - u1) * (u - 1) / d2; + const l3 = u * (u - u1) * (u - u2) / d3; + return l0 * curve.a0 + l1 * curve.a1 + l2 * curve.a2 + l3 * curve.a3; } // Get canvas-relative {x, y} from a mouse event |
