9 #ifndef CUBBYFLOW_MATH_UTILS_IMPL_H 10 #define CUBBYFLOW_MATH_UTILS_IMPL_H 22 return (std::abs(x - y) <= eps);
41 return (x * x < y * y) ? x : y;
47 return (x * x > y * y) ? x : y;
55 for (
size_t i = 1; i < n; i++)
68 for (
size_t i = 1; i < n; i++)
79 return (x < y) ? 0 : 1;
85 return (x > y) ? 0 : 1;
93 return (x < z) ? 0 : 2;
96 return (y < z) ? 1 : 2;
104 return (x > z) ? 0 : 2;
107 return (y > z) ? 1 : 2;
110 template <
typename T>
116 template <
typename T>
122 template <
typename T>
123 inline T
Clamp(T val, T low, T high)
138 template <
typename T>
141 return angleInDegrees * PI<T>() / 180;
144 template <
typename T>
147 return angleInRadians * 180 / PI<T>();
154 *i =
static_cast<ssize_t
>(s);
156 ssize_t offset = -iLow;
170 else if (*i > iHigh - 1)
177 *f =
static_cast<T
>(x - s);
183 template<
typename S,
typename T>
184 inline S
Lerp(
const S& value0,
const S& value1, T f)
186 return (1 - f) * value0 + f * value1;
189 template<
typename S,
typename T>
191 const S& f00,
const S& f10,
192 const S& f01,
const S& f11,
195 return Lerp(
Lerp(f00, f10, tx),
Lerp(f01, f11, tx), ty);
198 template<
typename S,
typename T>
200 const S& f000,
const S& f100,
201 const S& f010,
const S& f110,
202 const S& f001,
const S& f101,
203 const S& f011,
const S& f111,
207 BiLerp(f000, f100, f010, f110, tx, ty),
208 BiLerp(f001, f101, f011, f111, tx, ty),
212 template <
typename S,
typename T>
213 inline S
CatmullRom(
const S& f0,
const S& f1,
const S& f2,
const S& f3, T f)
215 S d1 = (f2 - f0) / 2;
216 S d2 = (f3 - f1) / 2;
219 S a3 = d1 + d2 - 2 * D1;
220 S a2 = 3 * D1 - 2 * d1 - d2;
224 return a3 *
Cubic(f) + a2 *
Square(f) + a1 * f + a0;
227 template <
typename T>
230 T d1 = (f2 - f0) / 2;
231 T d2 = (f3 - f1) / 2;
234 if (std::fabs(D1) < std::numeric_limits<double>::epsilon())
249 T a3 = d1 + d2 - 2 * D1;
250 T a2 = 3 * D1 - 2 * d1 - d2;
254 return a3 *
Cubic(f) + a2 *
Square(f) + a1 * f + a0;
T AbsMin(T x, T y)
Returns the absolute minimum value among the two inputs.
Definition: MathUtils-Impl.h:39
S Lerp(const S &value0, const S &value1, T f)
Computes linear interpolation.
Definition: MathUtils-Impl.h:184
T AbsMaxN(const T *x, size_t n)
Returns absolute maximum among n-elements.
Definition: MathUtils-Impl.h:64
size_t ArgMin2(T x, T y)
Definition: MathUtils-Impl.h:77
T Square(T x)
Returns the square of x.
Definition: MathUtils-Impl.h:111
size_t ArgMax3(T x, T y, T z)
Definition: MathUtils-Impl.h:100
size_t ArgMax2(T x, T y)
Definition: MathUtils-Impl.h:83
S CatmullRom(const S &f0, const S &f1, const S &f2, const S &f3, T f)
Computes Catmull-Rom interpolation.
Definition: MathUtils-Impl.h:213
T AbsMinN(const T *x, size_t n)
Returns absolute minimum among n-elements.
Definition: MathUtils-Impl.h:51
Definition: pybind11Utils.h:24
T Clamp(T val, T low, T high)
Returns the clamped value.
Definition: MathUtils-Impl.h:123
S BiLerp(const S &f00, const S &f10, const S &f01, const S &f11, T tx, T ty)
Computes bilinear interpolation.
Definition: MathUtils-Impl.h:190
T Cubic(T x)
Returns the cubic of x.
Definition: MathUtils-Impl.h:117
S TriLerp(const S &f000, const S &f100, const S &f010, const S &f110, const S &f001, const S &f101, const S &f011, const S &f111, T tx, T ty, T fz)
Computes trilinear interpolation.
Definition: MathUtils-Impl.h:199
bool Similar(T x, T y, T eps)
Returns true if x and y are similar.
Definition: MathUtils-Impl.h:20
T MonotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T f)
Computes monotonic Catmull-Rom interpolation.
Definition: MathUtils-Impl.h:228
T AbsMax(T x, T y)
Returns the absolute maximum value among the two inputs.
Definition: MathUtils-Impl.h:45
T Sign(T x)
Returns the sign of the value.
Definition: MathUtils-Impl.h:26
size_t ArgMin3(T x, T y, T z)
Definition: MathUtils-Impl.h:89
T DegreesToRadians(T angleInDegrees)
Converts degrees to radians.
Definition: MathUtils-Impl.h:139
T RadiansToDegrees(T angleInRadians)
Converts radians to degrees.
Definition: MathUtils-Impl.h:145
void GetBarycentric(T x, ssize_t iLow, ssize_t iHigh, ssize_t *i, T *f)
Gets the barycentric coordinate.
Definition: MathUtils-Impl.h:151