MathUtils.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_MATH_UTILS_HPP
12 #define CUBBYFLOW_MATH_UTILS_HPP
13 
14 #include <Core/Utils/Macros.hpp>
15 
16 #include <cstddef>
17 #include <limits>
18 #include <type_traits>
19 
20 namespace CubbyFlow
21 {
33 template <typename T>
34 std::enable_if_t<std::is_arithmetic<T>::value, bool> Similar(
35  T x, T y, T eps = std::numeric_limits<T>::epsilon());
36 
46 template <typename T>
47 std::enable_if_t<std::is_arithmetic<T>::value, T> Sign(T x);
48 
60 template <typename T>
61 std::enable_if_t<std::is_arithmetic<T>::value, T> Min3(T x, T y, T z);
62 
74 template <typename T>
75 std::enable_if_t<std::is_arithmetic<T>::value, T> Max3(T x, T y, T z);
76 
78 template <typename T>
79 std::enable_if_t<std::is_arithmetic<T>::value, T> MinN(const T* x, size_t n);
80 
82 template <typename T>
83 std::enable_if_t<std::is_arithmetic<T>::value, T> MaxN(const T* x, size_t n);
84 
95 template <typename T>
96 std::enable_if_t<std::is_arithmetic<T>::value, T> AbsMin(T x, T y);
97 
108 template <typename T>
109 std::enable_if_t<std::is_arithmetic<T>::value, T> AbsMax(T x, T y);
110 
112 template <typename T>
113 std::enable_if_t<std::is_arithmetic<T>::value, T> AbsMinN(const T* x, size_t n);
114 
116 template <typename T>
117 std::enable_if_t<std::is_arithmetic<T>::value, T> AbsMaxN(const T* x, size_t n);
118 
119 template <typename T>
120 std::enable_if_t<std::is_arithmetic<T>::value, size_t> ArgMin2(T x, T y);
121 
122 template <typename T>
123 std::enable_if_t<std::is_arithmetic<T>::value, size_t> ArgMax2(T x, T y);
124 
125 template <typename T>
126 std::enable_if_t<std::is_arithmetic<T>::value, size_t> ArgMin3(T x, T y, T z);
127 
128 template <typename T>
129 std::enable_if_t<std::is_arithmetic<T>::value, size_t> ArgMax3(T x, T y, T z);
130 
140 template <typename T>
141 std::enable_if_t<std::is_arithmetic<T>::value, T> Square(T x);
142 
152 template <typename T>
153 std::enable_if_t<std::is_arithmetic<T>::value, T> Cubic(T x);
154 
166 template <typename T>
167 std::enable_if_t<std::is_arithmetic<T>::value, T> Clamp(T val, T low, T high);
168 
178 template <typename T>
179 std::enable_if_t<std::is_arithmetic<T>::value, T> DegreesToRadians(
180  T angleInDegrees);
181 
191 template <typename T>
192 std::enable_if_t<std::is_arithmetic<T>::value, T> RadiansToDegrees(
193  T angleInRadians);
194 
221 template <typename T>
222 std::enable_if_t<std::is_arithmetic<T>::value> GetBarycentric(T x, size_t begin,
223  size_t end,
224  size_t& i, T& t);
225 
252 template <typename T>
253 std::enable_if_t<std::is_arithmetic<T>::value> GetBarycentric(T x,
254  ssize_t begin,
255  ssize_t end,
256  ssize_t& i, T& t);
257 
270 template <typename S, typename T>
271 std::enable_if_t<std::is_arithmetic<T>::value, S> Lerp(const S& f0, const S& f1,
272  T t);
273 
275 template <typename S, typename T>
276 std::enable_if_t<std::is_arithmetic<T>::value, S> BiLerp(
277  const S& f00, const S& f10, const S& f01, const S& f11, T tx, T ty);
278 
280 template <typename S, typename T>
281 std::enable_if_t<std::is_arithmetic<T>::value, S> TriLerp(
282  const S& f000, const S& f100, const S& f010, const S& f110, const S& f001,
283  const S& f101, const S& f011, const S& f111, T tx, T ty, T tz);
284 
286 template <typename S, typename T>
287 std::enable_if_t<std::is_arithmetic<T>::value, S> CatmullRom(const S& f0,
288  const S& f1,
289  const S& f2,
290  const S& f3, T t);
291 
293 template <typename T>
294 std::enable_if_t<std::is_arithmetic<T>::value, T> MonotonicCatmullRom(
295  const T& f0, const T& f1, const T& f2, const T& f3, T t);
296 } // namespace CubbyFlow
297 
299 
300 #endif
std::enable_if_t< std::is_arithmetic< T >::value, size_t > ArgMax2(T x, T y)
Definition: MathUtils-Impl.hpp:122
std::enable_if_t< std::is_arithmetic< T >::value, size_t > ArgMax3(T x, T y, T z)
Definition: MathUtils-Impl.hpp:141
std::enable_if_t< std::is_arithmetic< T >::value, S > Lerp(const S &f0, const S &f1, T t)
Computes linear interpolation.
Definition: MathUtils-Impl.hpp:295
std::enable_if_t< std::is_arithmetic< T >::value, T > Clamp(T val, T low, T high)
Returns the clamped value.
Definition: MathUtils-Impl.hpp:166
std::enable_if_t< std::is_arithmetic< T >::value, T > Square(T x)
Returns the square of x.
Definition: MathUtils-Impl.hpp:154
std::enable_if_t< std::is_arithmetic< T >::value > GetBarycentric(T x, size_t begin, size_t end, size_t &i, T &t)
Computes the barycentric coordinate.
Definition: MathUtils-Impl.hpp:196
std::enable_if_t< std::is_arithmetic< T >::value, size_t > ArgMin2(T x, T y)
Definition: MathUtils-Impl.hpp:116
std::enable_if_t< std::is_arithmetic< T >::value, T > Sign(T x)
Returns the sign of the value.
Definition: MathUtils-Impl.hpp:29
std::enable_if_t< std::is_arithmetic< T >::value, T > DegreesToRadians(T angleInDegrees)
Converts degrees to radians.
Definition: MathUtils-Impl.hpp:182
std::enable_if_t< std::is_arithmetic< T >::value, T > Max3(T x, T y, T z)
Returns the maximum value among three inputs.
Definition: MathUtils-Impl.hpp:46
std::enable_if_t< std::is_arithmetic< T >::value, bool > Similar(T x, T y, T eps)
Returns true if x and y are similar.
Definition: MathUtils-Impl.hpp:23
std::enable_if_t< std::is_arithmetic< T >::value, T > AbsMaxN(const T *x, size_t n)
Returns absolute maximum among n-elements.
Definition: MathUtils-Impl.hpp:103
std::enable_if_t< std::is_arithmetic< T >::value, T > AbsMinN(const T *x, size_t n)
Returns absolute minimum among n-elements.
Definition: MathUtils-Impl.hpp:90
std::enable_if_t< std::is_arithmetic< T >::value, S > BiLerp(const S &f00, const S &f10, const S &f01, const S &f11, T tx, T ty)
Computes bilinear interpolation.
Definition: MathUtils-Impl.hpp:302
Definition: pybind11Utils.hpp:20
std::enable_if_t< std::is_arithmetic< T >::value, T > Min3(T x, T y, T z)
Returns the minimum value among three inputs.
Definition: MathUtils-Impl.hpp:40
std::enable_if_t< std::is_arithmetic< T >::value, T > Cubic(T x)
Returns the cubic of x.
Definition: MathUtils-Impl.hpp:160
std::enable_if_t< std::is_arithmetic< T >::value, 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 tz)
Computes trilinear interpolation.
Definition: MathUtils-Impl.hpp:309
std::enable_if_t< std::is_arithmetic< T >::value, S > CatmullRom(const S &f0, const S &f1, const S &f2, const S &f3, T t)
Computes Catmull-Rom interpolation.
Definition: MathUtils-Impl.hpp:318
std::enable_if_t< std::is_arithmetic< T >::value, T > AbsMax(T x, T y)
Returns the absolute maximum value among the two inputs.
Definition: MathUtils-Impl.hpp:84
std::enable_if_t< std::is_arithmetic< T >::value, T > AbsMin(T x, T y)
Returns the absolute minimum value among the two inputs.
Definition: MathUtils-Impl.hpp:78
std::enable_if_t< std::is_arithmetic< T >::value, size_t > ArgMin3(T x, T y, T z)
Definition: MathUtils-Impl.hpp:128
std::enable_if_t< std::is_arithmetic< T >::value, T > MonotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T t)
Computes monotonic Catmull-Rom interpolation.
Definition: MathUtils-Impl.hpp:336
std::enable_if_t< std::is_arithmetic< T >::value, T > MaxN(const T *x, size_t n)
Returns maximum among n-elements.
Definition: MathUtils-Impl.hpp:65
std::enable_if_t< std::is_arithmetic< T >::value, T > RadiansToDegrees(T angleInRadians)
Converts radians to degrees.
Definition: MathUtils-Impl.hpp:189
std::enable_if_t< std::is_arithmetic< T >::value, T > MinN(const T *x, size_t n)
Returns minimum among n-elements.
Definition: MathUtils-Impl.hpp:52