mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2024-12-29 16:29:40 +00:00
Added v_lookup() for vector lookups.
This commit is contained in:
parent
57d906bb85
commit
3fca34babd
1 changed files with 25 additions and 0 deletions
25
vectors.scad
25
vectors.scad
|
@ -107,6 +107,31 @@ function v_ceil(v) =
|
||||||
[for (x=v) ceil(x)];
|
[for (x=v) ceil(x)];
|
||||||
|
|
||||||
|
|
||||||
|
// Function: v_lookup()
|
||||||
|
// Description:
|
||||||
|
// Works just like the built-in function [`lookup()`](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#lookup), except that it can also interpolate between vector result values of the same length.
|
||||||
|
// Arguments:
|
||||||
|
// x = The scalar value to look up.
|
||||||
|
// v = A list of [KEY,VAL] pairs. KEYs are scalars. VALs should either all be scalar, or all be vectors of the same length.
|
||||||
|
// Example:
|
||||||
|
// x = v_lookup(4.5, [[4, [3,4,5]], [5, [5,6,7]]]); // Returns: [4,5,6]
|
||||||
|
function v_lookup(x, v) =
|
||||||
|
is_num(v[0][1])? lookup(x,v) :
|
||||||
|
let(
|
||||||
|
i = lookup(x, [for (i=idx(v)) [v[i].x,i]]),
|
||||||
|
vlo = v[floor(i)],
|
||||||
|
vhi = v[ceil(i)],
|
||||||
|
lo = vlo[1],
|
||||||
|
hi = vhi[1]
|
||||||
|
)
|
||||||
|
assert(is_vector(lo) && is_vector(hi),
|
||||||
|
"Result values must all be numbers, or all be vectors.")
|
||||||
|
assert(len(lo) == len(hi), "Vector result values must be the same length")
|
||||||
|
vlo.x == vhi.x? vlo[1] :
|
||||||
|
let( u = (x - vlo.x) / (vhi.x - vlo.x) )
|
||||||
|
lerp(lo,hi,u);
|
||||||
|
|
||||||
|
|
||||||
// Function: unit()
|
// Function: unit()
|
||||||
// Usage:
|
// Usage:
|
||||||
// unit(v, [error]);
|
// unit(v, [error]);
|
||||||
|
|
Loading…
Reference in a new issue