From e0eb1e28c5a4d1d304cf06363bce1e42f55e47f6 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 23 Oct 2023 17:03:35 -0700 Subject: [PATCH] feat: add Lagrange interpolation - added only as 3 buffer interpolation --- packages/maath/src/buffer.ts | 20 +++++++++++++++++++- packages/maath/src/misc.ts | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/maath/src/buffer.ts b/packages/maath/src/buffer.ts index b36d6e8..0fc6ea9 100644 --- a/packages/maath/src/buffer.ts +++ b/packages/maath/src/buffer.ts @@ -6,7 +6,7 @@ import { Quaternion, Vector3 } from "three"; import type { TypedArray, MyVector2, MyVector3 } from "./ctypes"; -import { lerp as _lerp } from "./misc"; +import { lerp as _lerp, lpi3 as _lpi3 } from "./misc"; import * as v2 from "./vector2"; import * as v3 from "./vector3"; @@ -84,6 +84,24 @@ export function lerp( } } +/** + * + * Interpolates using Lagrane Interpolation between three buffers, passed in as array into final + * + * @param buffers array of three buffers: bufferA, bufferB, bufferC + * @param final interpolation buffer (output) + * @param t time, reccomended + */ +export function lpi3( + buffers: [TypedArray, TypedArray, TypedArray], + final: TypedArray, + t: number +) { + for (let i = 0; i < buffers[0].length; i++) { + final[i] = _lpi3([buffers[0][i], buffers[1][i], buffers[2][i]], t); + } +} + // TODO add stride // TODO Fix types & vectors /** diff --git a/packages/maath/src/misc.ts b/packages/maath/src/misc.ts index 9d050f5..b973cc2 100644 --- a/packages/maath/src/misc.ts +++ b/packages/maath/src/misc.ts @@ -174,6 +174,22 @@ export function fade(t: number) { return t * t * t * (t * (t * 6 - 15) + 10); } +/** + * + * Returns the result of lagrange polynomial interpolation between A, B, and C by the time of t + * + * @param v array of three numbers to interpolate between + * @param t time + * @returns 3-point interpolation given pos of t + */ +export function lpi3(v: [number, number, number], t: number) { + return ( + 2 * (t - 1) * (t - 0.5) * v[0] - + 4 * (t - 1) * t * v[1] + + 2 * (t - 0.5) * t * v[2] + ); +} + /** * * Returns the result of linearly interpolating between input A and input B by input T.