Range Mapping

by .

You might know the Range Mapper node in Xpresso. A very handy tool to map one value range to another, optionally applying a spline curve. Here is how you can do the same in C++.

inline Real MapRange(Real value, Real min_input, Real max_input, Real min_output, Real max_output, SplineData *curve = NULL)
{
  Real inrange = max_input - min_input;
  if (CompareFloatTolerant(inrange, RCO 0.0)) value = RCO 0.0;  // Prevent DivByZero error
  else value = (value - min_input) / inrange;             // Map input range to [0.0 ... 1.0]

  if (curve) value = curve->GetPoint(value).y;            // Apply spline curve

  return  min_output + (max_output - min_output) * value; // Map to output range and return result
}
Advertisements