Skip to content

Commit

Permalink
fix(shader-ast-stdlib): update generics for clamp01(), clamp11() and …
Browse files Browse the repository at this point in the history
…various fitXX() fns
  • Loading branch information
postspectacular committed Mar 5, 2024
1 parent f6d6e33 commit 41d2882
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
14 changes: 7 additions & 7 deletions packages/shader-ast-stdlib/src/math/clamp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type {
FloatTerm,
PrimTerm,
Prim,
Term,
TermType,
Vec2Term,
Vec3Term,
Vec4Term,
Expand All @@ -13,14 +12,15 @@ import { clamp } from "@thi.ng/shader-ast/builtin/math";

const __clamp =
(min: number, max: number) =>
<T extends PrimTerm>(x: T): Term<TermType<T>> =>
<T extends Prim>(x: Term<T>): Term<T> =>
x.type === F
? clamp(<FloatTerm>x, float(min), float(max))
? <any>clamp(<FloatTerm>x, float(min), float(max))
: x.type === V2
? clamp(<Vec2Term>x, vec2(min), vec2(max))
? <any>clamp(<Vec2Term>x, vec2(min), vec2(max))
: x.type === V3
? clamp(<Vec3Term>x, vec3(min), vec3(max))
: clamp(<Vec4Term>x, vec4(min), vec4(max));
? <any>clamp(<Vec3Term>x, vec3(min), vec3(max))
: <any>clamp(<Vec4Term>x, vec4(min), vec4(max));

/**
* Inline function, expands to equivalent of `clamp(x, 0, 1)`.
*
Expand Down
48 changes: 22 additions & 26 deletions packages/shader-ast-stdlib/src/math/fit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PrimTerm, Term, TermType } from "@thi.ng/shader-ast";
import type { Prim, PrimTerm, Term, TermType } from "@thi.ng/shader-ast";
import { F } from "@thi.ng/shader-ast/api/types";
import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
Expand Down Expand Up @@ -27,13 +27,9 @@ export const fitNorm1 = defn(F, "fitNorm1", [F, F, F], (x, a, b) => [
* @param x
* @param a
* @param b
* @returns
*/
export const fitNorm = <T extends PrimTerm>(
x: T,
a: T,
b: T
): Term<TermType<T>> => div(sub(x, a), sub(b, a));
export const fitNorm = <T extends Prim>(x: Term<T>, a: Term<T>, b: Term<T>) =>
div(sub(x, a), sub(b, a));

/**
* Fits value `x` from closed interval [a,b] to closed interval [c,d]. No
Expand All @@ -45,13 +41,13 @@ export const fitNorm = <T extends PrimTerm>(
* @param c -
* @param d -
*/
export const fit = <T extends PrimTerm>(
x: T,
a: T,
b: T,
c: T,
d: T
): Term<TermType<T>> => mix(c, d, fitNorm(x, a, b));
export const fit = <T extends Prim>(
x: Term<T>,
a: Term<T>,
b: Term<T>,
c: Term<T>,
d: Term<T>
) => mix(c, d, fitNorm(x, a, b));

/**
* Same as {@link fit}, but first clamps `x` to closed [a,b] interval.
Expand All @@ -62,13 +58,13 @@ export const fit = <T extends PrimTerm>(
* @param c -
* @param d -
*/
export const fitClamped = <T extends PrimTerm>(
x: T,
a: T,
b: T,
c: T,
d: T
): Term<TermType<T>> => mix(c, d, clamp01(div(sub(x, a), sub(b, a))));
export const fitClamped = <T extends Prim>(
x: Term<T>,
a: Term<T>,
b: Term<T>,
c: Term<T>,
d: Term<T>
) => mix(c, d, clamp01(div(sub(x, a), sub(b, a))));

/**
* Inline function. Fits value `a` in [0..1] interval to new interval
Expand All @@ -78,11 +74,11 @@ export const fitClamped = <T extends PrimTerm>(
* @param b -
* @param c -
*/
export const fit01 = <T extends PrimTerm>(
a: T,
b: T,
c: T
): Term<TermType<T>> => mix(b, c, a);
export const fit01 = <T extends Prim>(
a: Term<T>,
b: Term<T>,
c: Term<T>
): Term<T> => mix(b, c, a);

/**
* Inline function. Fits value `x` in [-1..+1] interval to [0..1]
Expand Down

0 comments on commit 41d2882

Please sign in to comment.