| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/common/mathutils.h`/ |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace Utilscxx . . $ Utils/::Mathcxx . . $ Utils/Math/ { |
| 11 | |
| 12 | /// Performs a linear normalization to a collection of values from an old range |
| 13 | /// to a new range. |
| 14 | /// |
| 15 | /// @note Only works with regular numeric types. |
| 16 | /// |
| 17 | /// @param values collection of values to normalize |
| 18 | /// @param oldRange old range of the values of the collection |
| 19 | /// @param newRange new range for the result of the normalization |
| 20 | template<typename T> |
| 21 | void linearNormcxx . . $ Utils/Math/linearNorm(985260a9f02479ec).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<T> &values, stdcxx . . $ std/::paircxx . . $ std/pair#<T, T> const &oldRange, |
| 22 | stdcxx . . $ std/::paircxx . . $ std/pair#<T, T> const &newRange) |
| 23 | { |
| 24 | stdcxx . . $ std/::transformcxx . . $ std/transform(cb3c40587c5a1d4f).(values.cbegincxx . . $ std/vector#cbegin(d1e1699016b9d7e6).(), values.cendcxx . . $ std/vector#cend(d1e1699016b9d7e6).(), values.begincxx . . $ std/vector#begin(55a6de06420d4903).(), [&](T value) { |
| 25 | return static_cast<T>( |
| 26 | (value - oldRange.firstcxx . . $ std/pair#first.) * |
| 27 | (static_cast<double>(newRange.secondcxx . . $ std/pair#second. - newRange.firstcxx . . $ std/pair#first.) / |
| 28 | (oldRange.secondcxx . . $ std/pair#second. - oldRange.firstcxx . . $ std/pair#first.)) + |
| 29 | newRange.firstcxx . . $ std/pair#first.); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | /// Performs a lineal interpolation between two points resolving the y value at |
| 34 | /// the supplied x value. |
| 35 | /// |
| 36 | /// @note Only works with regular numeric types. |
| 37 | /// |
| 38 | /// @param value x value to be evaluated |
| 39 | /// @param p1 first point |
| 40 | /// @param p2 second point |
| 41 | /// @return resolved y value |
| 42 | template<typename X, typename Y> |
| 43 | Y lerpXcxx . . $ Utils/Math/lerpX(41a02240c2bcefae).(X value, stdcxx . . $ std/::paircxx . . $ std/pair#<X, Y> const &p1, stdcxx . . $ std/::paircxx . . $ std/pair#<X, Y> const &p2) |
| 44 | { |
| 45 | return static_cast<X>( |
| 46 | (static_cast<double>(p2.secondcxx . . $ std/pair#second. - p1.secondcxx . . $ std/pair#second.) / (p2.firstcxx . . $ std/pair#first. - p1.firstcxx . . $ std/pair#first.)) * |
| 47 | (value - p1.firstcxx . . $ std/pair#first.) + |
| 48 | p1.secondcxx . . $ std/pair#second.); |
| 49 | } |
| 50 | |
| 51 | /// Performs a lineal interpolation between two points resolving the x value at |
| 52 | /// the supplied y value. |
| 53 | /// |
| 54 | /// @note Only works with regular numeric types. |
| 55 | /// |
| 56 | /// @param value y value to be evaluated |
| 57 | /// @param p1 first point |
| 58 | /// @param p2 second point |
| 59 | /// @return resolved x value |
| 60 | template<typename X, typename Y> |
| 61 | X lerpYcxx . . $ Utils/Math/lerpY(35236b01fa293033).(Y value, stdcxx . . $ std/::paircxx . . $ std/pair#<X, Y> const &p1, stdcxx . . $ std/::paircxx . . $ std/pair#<X, Y> const &p2) |
| 62 | { |
| 63 | return static_cast<Y>( |
| 64 | (static_cast<double>(p2.firstcxx . . $ std/pair#first. - p1.firstcxx . . $ std/pair#first.) / (p2.secondcxx . . $ std/pair#second. - p1.secondcxx . . $ std/pair#second.)) * |
| 65 | (value - p1.secondcxx . . $ std/pair#second.) + |
| 66 | p1.firstcxx . . $ std/pair#first.); |
| 67 | } |
| 68 | |
| 69 | } // namespace Utils::Math |