MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin_Math.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef MARVIN_MATH_H
12#define MARVIN_MATH_H
15#include <cmath>
16#include <algorithm>
17#include <span>
18#include <complex>
19#include <cassert>
20#include <numbers>
21
22namespace marvin::math {
23
31 template <FloatType T>
32 [[nodiscard]] T lerp(T start, T end, T ratio) noexcept {
33 const auto interpolated = start + (end - start) * ratio;
34 return interpolated;
35 }
36
37
45 template <FloatType T>
46 [[nodiscard]] T remap(T x, T newMin, T newMax) {
47 // (v_n . (mx - mn)) + mn = v
48 const auto rescaled = (x * (newMax - newMin)) + newMin;
49 return rescaled;
50 }
51
62 template <FloatType T>
63 [[nodiscard]] T remap(T x, T srcMin, T srcMax, T newMin, T newMax) {
64 // (v - mn) / (mx - mn) == v_n
65 const auto normalised = (x - srcMin) / (srcMax - srcMin);
66 return remap<T>(normalised, newMin, newMax);
67 }
68
76 template <FloatType T>
77 [[nodiscard]] T remap(T x, marvin::utils::Range<T> srcRange, marvin::utils::Range<T> newRange) {
78 return remap<T>(x, srcRange.min, srcRange.max, newRange.min, newRange.max);
79 }
80
87 template <FloatType T>
88 [[nodiscard]] T remap(T x, marvin::utils::Range<T> newRange) {
89 return remap<T>(x, newRange.min, newRange.max);
90 }
91
101 template <FloatType T>
102 [[nodiscard]] std::span<std::complex<T>> interleavedToComplexView(std::span<T> data) {
103 assert(data.size() % 2 == 0);
104 std::span<std::complex<T>> complexView{ reinterpret_cast<std::complex<T>*>(data.data()), data.size() / 2 };
105 return complexView;
106 }
107
116 template <FloatType T>
117 [[nodiscard]] std::span<T> complexViewToInterleaved(std::span<std::complex<T>> data) {
118 std::span<T> interleavedView{ reinterpret_cast<T*>(data.data()), data.size() * 2 };
119 return interleavedView;
120 }
121 template <FloatType T>
122 [[nodiscard]] T sinc(T x) noexcept {
123 static constexpr auto epsilon{ static_cast<T>(1e-6) };
124 if (std::abs(x) < epsilon) {
125 return static_cast<T>(1.0);
126 }
127 const auto xPi = x * std::numbers::pi_v<T>;
128 return std::sin(xPi) / xPi;
129 }
130
131} // namespace marvin::math
132
133#endif
Math helper functions and classes.
Definition marvin_Math.h:22
std::span< std::complex< T > > interleavedToComplexView(std::span< T > data)
Definition marvin_Math.h:102
std::span< T > complexViewToInterleaved(std::span< std::complex< T > > data)
Definition marvin_Math.h:117
T lerp(T start, T end, T ratio) noexcept
Definition marvin_Math.h:32
T remap(T x, T newMin, T newMax)
Definition marvin_Math.h:46
T sinc(T x) noexcept
Definition marvin_Math.h:122
POD type that represents a range of values, for classes requiring a min and a max.
Definition marvin_Range.h:21
T max
Definition marvin_Range.h:23
T min
Definition marvin_Range.h:22