Skip to content

Commit

Permalink
Move regular math functions to math scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Oct 11, 2024
1 parent 9b65d8b commit 1815c4b
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 137 deletions.
4 changes: 2 additions & 2 deletions packages/audio/include/audio/AudioUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace l::audio {
~FilterRWA() = default;

bool SnapAt(T value = static_cast<T>(0), T proximity = static_cast<T>(0.00001)) {
if (l::math::functions::abs(mValue - value) < proximity) {
if (l::math::abs(mValue - value) < proximity) {
mValue = value;
return true;
}
Expand All @@ -67,7 +67,7 @@ namespace l::audio {
}

FilterRWA<T>& SetRWAUpdateRate(T fetchesPerUpdate = static_cast<T>(1.0)) {
mRWAUpdateRate = l::math::functions::max2(fetchesPerUpdate, static_cast<T>(1.0));
mRWAUpdateRate = l::math::max2(fetchesPerUpdate, static_cast<T>(1.0));
return *this;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/audio/source/common/AudioUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ namespace l::audio {
const int32_t gNoNote = -500;

float GetFrequencyFromNote(float note) {
return 440.0f * l::math::functions::pow(2.0f, (note - 49.0f) / 12.0f);
return 440.0f * l::math::pow(2.0f, (note - 49.0f) / 12.0f);
}

double GetPhaseModifier(double note, double modifier) {
double limit = 1.0 / l::math::functions::max2(note / 25.0, 1.0);
double limit = 1.0 / l::math::max2(note / 25.0, 1.0);
return 800.0 * modifier * modifier * limit;
}

Expand All @@ -52,15 +52,15 @@ namespace l::audio {
}

float GetRWAFactorFromMSSkewed(float ms, float limit, float rwaUpdateRate, float sampleRate) {
float msRoot = l::math::functions::sqrt(ms);
float msRoot = l::math::sqrt(ms);
int32_t steps = GetAudioTicksFromMS(msRoot, sampleRate / rwaUpdateRate);
float factor = l::math::tween::GetRWAFactor(steps, limit);
factor *= factor;
return factor;
}

float BatchUpdate(float updateSamples, float samplesLeft, int32_t start, int32_t end, std::function<float()> update, std::function<void(int32_t, int32_t, bool)> process) {
updateSamples = l::math::functions::max2(updateSamples, 1.0f);
updateSamples = l::math::max2(updateSamples, 1.0f);
float startNum = static_cast<float>(start);
while (startNum < static_cast<float>(end)) {
bool updated = false;
Expand All @@ -78,7 +78,7 @@ namespace l::audio {
// * batch max has been reached
// * end has been reached

float samples = l::math::functions::min2(static_cast<float>(end) - startNum, samplesLeft);
float samples = l::math::min2(static_cast<float>(end) - startNum, samplesLeft);
if (process != nullptr) {
process(static_cast<int32_t>(startNum), static_cast<int32_t>(startNum + samples), updated);
}
Expand Down
88 changes: 46 additions & 42 deletions packages/math/include/math/MathFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "math/MathConstants.h"

namespace l::math::functions {
namespace l::math {

template<class T>
auto abs(T val) {
Expand All @@ -35,23 +35,6 @@ namespace l::math::functions {
}
}

template<class T>
auto equal(T a, T b, T accuracy = static_cast<T>(0.0001)) {
return functions::abs(a - b) < accuracy;
}

template <class T>
auto samesign(T a, T b) {
return a * b >= static_cast<T>(0.0);
}

template<class T>
void swap(T& val1, T& val2) {
T tmp = std::move(val1);
val1 = std::move(val2);
val2 = std::move(tmp);
}

template<class T>
auto min2(T val1, T val2) {
return val1 < val2 ? val1 : val2;
Expand All @@ -68,7 +51,7 @@ namespace l::math::functions {
}

template<class T>
requires std::is_floating_point_v<T>
requires std::is_floating_point_v<T>
auto floor(T val) {
if constexpr (std::is_floating_point_v<T>) {
if constexpr (sizeof(T) == 4) {
Expand Down Expand Up @@ -224,6 +207,36 @@ namespace l::math::functions {
}
}

// Sinc curve
// A phase shifted sinc curve can be useful if it starts at zeroand ends at zero, for some bouncing behaviors(suggested by Hubert - Jan).Give k different integer values to tweak the amount of bounces.It peaks at 1.0, but that take negative values, which can make it unusable in some applications.
template<class T>
auto sinc(T x, T k)
{
const T a = math::constants::PI * (k * x - static_cast<T>(1.0));
return math::sin(a) / a;
}

}

namespace l::math::functions {

template<class T>
auto equal(T a, T b, T accuracy = static_cast<T>(0.0001)) {
return math::abs(a - b) < accuracy;
}

template <class T>
auto samesign(T a, T b) {
return a * b >= static_cast<T>(0.0);
}

template<class T>
void swap(T& val1, T& val2) {
T tmp = std::move(val1);
val1 = std::move(val2);
val2 = std::move(tmp);
}

// source https://iquilezles.org/articles/functions/

// Almost Identity(I)
Expand All @@ -244,7 +257,7 @@ namespace l::math::functions {
template<class T>
auto almostIdentity2(T x, T n)
{
return functions::sqrt(x * x + n);
return math::sqrt(x * x + n);
}

// Almost Unit Identity
Expand All @@ -271,41 +284,41 @@ namespace l::math::functions {
auto expImpulse(T x, T k)
{
const T h = k * x;
return h * functions::exp(static_cast<T>(1.0) - h);
return h * math::exp(static_cast<T>(1.0) - h);
}

// Polynomial Impulse
// Another impulse function that doesn't use exponentials can be designed by using polynomials. Use k to control falloff of the function. For example, a quadratic can be used, which peaks at x = sqrt(1/k).
template<class T>
auto quaImpulse(T k, T x)
{
return static_cast<T>(1.0) * functions::sqrt(k) * x / (static_cast<T>(1.0) + k * x * x);
return static_cast<T>(1.0) * math::sqrt(k) * x / (static_cast<T>(1.0) + k * x * x);
}

// You can easily generalize it to other powers to get different falloff shapes, where n is the degree of the polynomial :
template<class T>
auto polyImpulse(T k, T n, T x)
{
return (n / (n - static_cast<T>(1.0))) * functions::pow((n - static_cast<T>(1.0)) * k, static_cast<T>(1.0) / n) * x / (static_cast<T>(1.0) + k * functions::pow(x, n));
return (n / (n - static_cast<T>(1.0))) * math::pow((n - static_cast<T>(1.0)) * k, static_cast<T>(1.0) / n) * x / (static_cast<T>(1.0) + k * math::pow(x, n));
}

// Sustained Impulse
// Similar to the previous, but it allows for control on the width of attack(through the parameter "k") and the release(parameter "f") independently.Also, the impulse releases at a value of 1 instead of 0.
template<class T>
auto expSustainedImpulse(T x, T f, T k)
{
auto s = functions::max2(x - f, static_cast<T>(0.0));
auto s = math::max2(x - f, static_cast<T>(0.0));
auto a = x * x / (f * f);
auto b = static_cast<T>(1.0) + (static_cast<T>(2.0) / f) * s * functions::exp(-k * s);
return functions::min2(a, b);
auto b = static_cast<T>(1.0) + (static_cast<T>(2.0) / f) * s * math::exp(-k * s);
return math::min2(a, b);
}

// Cubic Pulse
// Chances are you found yourself doing smoothstep(c - w, c, x) - smoothstep(c, c + w, x) very often.I do, for example when I need to isolate some features in a signal.For those cases, this cubicPulse() below is my new friendand will be yours too soon.Bonus - you can also use it as a performant replacement for a gaussian.
template<class T>
auto cubicPulse(T c, T w, T x)
{
x = functions::abs(x - c);
x = math::abs(x - c);
if (x > w) return static_cast<T>(0.0);
x /= w;
return static_cast<T>(1.0) - x * x * (static_cast<T>(3.0) - static_cast<T>(2.0) * x);
Expand All @@ -316,15 +329,15 @@ namespace l::math::functions {
template<class T>
auto expStep(T x, T k, T n)
{
return functions::exp(-k * functions::pow(x, n));
return math::exp(-k * math::pow(x, n));
}

// Gain
// Remapping the unit interval into the unit interval by expanding the sides and compressing the center, and keeping 1 / 2 mapped to 1 / 2, that can be done with the gain() function.This was a common function in RSL tutorials(the Renderman Shading Language).k = 1 is the identity curve, k < 1 produces the classic gain() shape, and k>1 produces "s" shaped curces.The curves are symmetric(and inverse) for k = a and k = 1 / a.
template<class T>
auto gain(T x, T k)
{
const T a = static_cast<T>(0.5) * functions::pow(static_cast<T>(2.0) * ((x < static_cast<T>(0.5)) ? x : static_cast<T>(1.0) - x), k);
const T a = static_cast<T>(0.5) * math::pow(static_cast<T>(2.0) * ((x < static_cast<T>(0.5)) ? x : static_cast<T>(1.0) - x), k);
return (x < static_cast<T>(0.5)) ? a : static_cast<T>(1.0) - a;
}

Expand All @@ -333,30 +346,21 @@ namespace l::math::functions {
template<class T>
auto parabola(T x, T k)
{
return functions::pow(4.0 * x * (static_cast<T>(1.0) - x), k);
return math::pow(4.0 * x * (static_cast<T>(1.0) - x), k);
}

// Power curve
// This is a generalziation of the Parabola() above.It also maps the 0..1 interval into 0..1 by keeping the corners mapped to 0. But in this generalziation you can control the shape one either side of the curve, which comes handy when creating leaves, eyes, and many other interesting shapes.
template<class T>
auto pcurve(T x, T a, T b)
{
const T k = functions::pow(a + b, a + b) / (functions::pow(a, a) * functions::pow(b, b));
return k * functions::pow(x, a) * functions::pow(1.0 - x, b);
}

// Sinc curve
// A phase shifted sinc curve can be useful if it starts at zeroand ends at zero, for some bouncing behaviors(suggested by Hubert - Jan).Give k different integer values to tweak the amount of bounces.It peaks at 1.0, but that take negative values, which can make it unusable in some applications.
template<class T>
auto sinc(T x, T k)
{
const T a = math::constants::PI * (k * x - static_cast<T>(1.0));
return functions::sin(a) / a;
const T k = math::pow(a + b, a + b) / (math::pow(a, a) * math::pow(b, b));
return k * math::pow(x, a) * math::pow(1.0 - x, b);
}

template<class T>
auto sigmoid(T x, T k) {
return static_cast<T>(static_cast<T>(1.0) / (static_cast<T>(1.0) + functions::exp(-x * k)));
return static_cast<T>(static_cast<T>(1.0) / (static_cast<T>(1.0) + math::exp(-x * k)));
}

template<class T>
Expand Down
8 changes: 4 additions & 4 deletions packages/math/source/common/MathTween.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace l::math::tween {
if (steps < 2) {
return 1.0f;
}
return 1.0f - l::math::functions::pow(l::math::constants::E_f, l::math::functions::log(limit) / static_cast<float>(steps-1));
return 1.0f - l::math::pow(l::math::constants::E_f, l::math::log(limit) / static_cast<float>(steps-1));
}

void RecentWeightedAverage::Reset(float value) {
Expand Down Expand Up @@ -58,15 +58,15 @@ namespace l::math::tween {
}

void DynamicTween::SetTweenLength(int32_t tweenCount) {
mUpdateCount = l::math::functions::max2(tweenCount, 4);
mUpdateCount = l::math::max2(tweenCount, 4);
}

void DynamicTween::SetTarget(float target, int32_t tweenCount) {
mTargetValuePrev = mValue;
mTargetValue = target;
mCounter = 0;
if (tweenCount >= 4) {
mUpdateCount = l::math::functions::max2(tweenCount, 4);
mUpdateCount = l::math::max2(tweenCount, 4);
}
}

Expand All @@ -78,7 +78,7 @@ namespace l::math::tween {
mTarget = mTargetValue;
if (mCounter < mUpdateCount) {
float t = (mCounter + 0.5f) / static_cast<float>(mUpdateCount - 0.5f);
t = l::math::functions::clamp(t, 0.0f, 1.0f);
t = l::math::clamp(t, 0.0f, 1.0f);
mTarget = mTargetValuePrev + l::math::smooth::smoothPolyh3(t) * (mTargetValue - mTargetValuePrev);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/math/tests/common/MathTweenTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ TEST(MathTween, DynamicTweenLongBatch) {

TEST(MathTween, Math) {
float t = 1.0f;
float v = l::math::functions::min2(0.0f, t);
float v = l::math::min2(0.0f, t);
TEST_FUZZY(0.0f, v, 0.0001f, "");
return 0;
}
2 changes: 1 addition & 1 deletion packages/nodegraph/include/nodegraph/core/NodeGraphData.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace l::nodegraph {
void Reset(NodeDataIterator&& iterator) {
mIterator = std::move(iterator);
mRwa.Value() = *mIterator;
mRwa.SetConvergenceInTicks(l::math::functions::max2(4.0f, iterator.GetStepsPerIncrement()), 0.35f);
mRwa.SetConvergenceInTicks(l::math::max2(4.0f, iterator.GetStepsPerIncrement()), 0.35f);
}
protected:
NodeDataIterator mIterator;
Expand Down
Loading

0 comments on commit 1815c4b

Please sign in to comment.