maths: add smoothstep
This commit is contained in:
parent
b12d0b95fc
commit
8487f700f6
10
maths.cpp
10
maths.cpp
@ -19,6 +19,8 @@
|
||||
|
||||
#include "maths.hpp"
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
#include "float.hpp"
|
||||
|
||||
#include <cmath>
|
||||
@ -174,3 +176,11 @@ sign (double val) {
|
||||
|
||||
template int sign (int);
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Simple instantiations. Some functions aren't used internally to the library
|
||||
// so it's easier to instantiate early and check for broken code at library
|
||||
// build time.
|
||||
|
||||
template float smoothstep (float, float, float);
|
||||
template double smoothstep (double, double, double);
|
||||
|
13
maths.hpp
13
maths.hpp
@ -20,6 +20,8 @@
|
||||
#ifndef __MATHS_HPP
|
||||
#define __MATHS_HPP
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
@ -284,6 +286,17 @@ limit [[gnu::pure]] (const T val, const U hi, const V lo)
|
||||
}
|
||||
|
||||
|
||||
// clamped cubic hermite interpolation
|
||||
template <typename T>
|
||||
T
|
||||
smoothstep [[gnu::pure]] (T a, T b, T x)
|
||||
{
|
||||
CHECK_LE(a, b);
|
||||
x = limit ((x - a) / (b - a), T{0}, T{1});
|
||||
return x * x * (3 - 2 * x);
|
||||
}
|
||||
|
||||
|
||||
#include "maths.ipp"
|
||||
|
||||
#endif // __MATHS_HPP
|
||||
|
Loading…
Reference in New Issue
Block a user