map-copy

hio/nwt-gus-hi/map-copy

Projects a number from one range to another preserving ratio
map-copy
@/map-copy
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-copy
OUT
X
Smin
Smax
Tmin
Tmax
OUTnumber
Mapped value

Previously known as xod/core/map-range.

To use the node in your project you should have the hio/nwt-gus-hi library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

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