map

xod/math/map

Projects a number from one range to another preserving ratio
map
@/map
Projects a number from one range to another preserving ratio
Xnumber
Value to map
Sminnumber
Source range lower bound
Smaxnumber
Source range upper bound
Tminnumber
Target range lower bound
Tmaxnumber
Tartger range upper bound
map
OUT
X
Smin
Smax
Tmin
Tmax
OUTnumber
Mapped value

Previously known as xod/core/map-range.

C++ implementation

#pragma XOD dirtieness disable

node {
    void evaluate(Context ctx) {
        auto x = getValue<input_X>(ctx);
        auto sMin = getValue<input_Smin>(ctx);
        auto sMax = getValue<input_Smax>(ctx);
        auto tMin = getValue<input_Tmin>(ctx);
        auto tMax = getValue<input_Tmax>(ctx);
        auto k = (x - sMin) / (sMax - sMin);
        auto xm = isnan(x) ? x : tMin + k * (tMax - tMin);
        emitValue<output_OUT>(ctx, xm);
    }
}

Tabular tests

XSminSmaxTminTmaxOUT
001-1010-10
0.501-10100
2501010250
-1001010-100
0.5010InfInf
0.501-InfInfNaN
Inf01010Inf
-Inf01010-Inf
NaN01010NaN