3#include <Foundation/Basics.h>
4#include <Foundation/Math/Angle.h>
5#include <Foundation/Math/Constants.h>
6#include <Foundation/Math/Declarations.h>
16 template <
typename Type>
17 constexpr static bool IsNaN(Type value)
23 template <
typename Type>
24 constexpr static bool IsFinite(Type value)
53 [[nodiscard]]
float Exp(
float f);
56 [[nodiscard]]
float Ln(
float f);
59 [[nodiscard]]
float Log2(
float f);
62 [[nodiscard]] plUInt32
Log2i(plUInt32 uiVal);
65 [[nodiscard]]
float Log10(
float f);
68 [[nodiscard]]
float Log(
float fBase,
float f);
71 [[nodiscard]]
float Pow2(
float f);
74 [[nodiscard]]
float Pow(
float fBase,
float fExp);
77 [[nodiscard]]
constexpr plInt32 Pow2(plInt32 i);
80 [[nodiscard]] plInt32 Pow(plInt32 iBase, plInt32 iExp);
84 [[nodiscard]]
constexpr T
Square(T f);
87 [[nodiscard]]
float Sqrt(
float f);
90 [[nodiscard]]
double Sqrt(
double f);
93 [[nodiscard]]
float Root(
float f,
float fNthRoot);
97 [[nodiscard]]
constexpr T
Sign(T f);
100 template <
typename T>
101 [[nodiscard]]
constexpr T
Abs(T f);
104 template <
typename T>
105 [[nodiscard]]
constexpr T
Min(T f1, T f2);
108 template <
typename T,
typename... ARGS>
109 [[nodiscard]]
constexpr T
Min(T f1, T f2, ARGS... f);
112 template <
typename T>
113 [[nodiscard]]
constexpr T
Max(T f1, T f2);
116 template <
typename T,
typename... ARGS>
117 [[nodiscard]]
constexpr T
Min(T f1, T f2, ARGS... f);
120 template <
typename T>
121 [[nodiscard]]
constexpr T
Clamp(T value, T min_val, T max_val);
124 template <
typename T>
125 [[nodiscard]]
constexpr T
Saturate(T value);
128 [[nodiscard]]
float Floor(
float f);
131 [[nodiscard]]
float Ceil(
float f);
134 [[nodiscard]]
float RoundDown(
float f,
float fMultiple);
137 [[nodiscard]]
double RoundDown(
double f,
double fMultiple);
140 [[nodiscard]]
float RoundUp(
float f,
float fMultiple);
143 [[nodiscard]]
double RoundUp(
double f,
double fMultiple);
146 template <
typename Type>
147 [[nodiscard]] Type
Trunc(Type f);
152 [[nodiscard]]
constexpr plInt32
FloatToInt(
float value);
156#if PL_DISABLED(PL_PLATFORM_ARCH_X86) || (_MSC_VER <= 1916)
160 [[nodiscard]]
constexpr plInt64
FloatToInt(
double value);
166 [[nodiscard]]
float Round(
float f);
171 [[nodiscard]]
double Round(
double f);
180 template <
typename Type>
181 [[nodiscard]] Type
Fraction(Type f);
184 [[nodiscard]]
float Mod(
float value,
float fDiv);
187 [[nodiscard]]
double Mod(
double f,
double fDiv);
190 template <
typename Type>
191 [[nodiscard]]
constexpr Type
Invert(Type f);
194 [[nodiscard]]
constexpr plInt32
RoundUp(plInt32 value, plUInt16 uiMultiple);
197 [[nodiscard]]
constexpr plInt32
RoundDown(plInt32 value, plUInt16 uiMultiple);
200 [[nodiscard]]
constexpr plUInt32
RoundUp(plUInt32 value, plUInt16 uiMultiple);
203 [[nodiscard]]
constexpr plUInt32
RoundDown(plUInt32 value, plUInt16 uiMultiple);
206 [[nodiscard]]
constexpr bool IsOdd(plInt32 i);
209 [[nodiscard]]
constexpr bool IsEven(plInt32 i);
214 [[nodiscard]] plUInt32
FirstBitLow(plUInt32 uiBitmask);
219 [[nodiscard]] plUInt32
FirstBitLow(plUInt64 uiBitmask);
224 [[nodiscard]] plUInt32
FirstBitHigh(plUInt32 uiBitmask);
229 [[nodiscard]] plUInt32
FirstBitHigh(plUInt64 uiBitmask);
254 [[nodiscard]] plUInt32
CountBits(plUInt32 value);
257 [[nodiscard]] plUInt32
CountBits(plUInt64 value);
262 template <
typename Type>
263 [[nodiscard]] Type
Bitmask_LowN(plUInt32 uiNumBitsToSet);
268 template <
typename Type>
272 template <
typename T>
273 void Swap(T& ref_f1, T& ref_f2);
276 template <
typename T>
277 [[nodiscard]] T
Lerp(T f1, T f2,
float fFactor);
280 template <
typename T>
281 [[nodiscard]] T
Lerp(T f1, T f2,
double fFactor);
284 template <
typename T>
285 [[nodiscard]]
constexpr float Unlerp(T fMin, T fMax, T fValue);
288 template <
typename T>
289 [[nodiscard]]
constexpr T
Step(T value, T edge);
292 template <
typename Type>
293 [[nodiscard]] Type
SmoothStep(Type value, Type edge1, Type edge2);
296 template <
typename Type>
297 [[nodiscard]] Type
SmootherStep(Type value, Type edge1, Type edge2);
300 [[nodiscard]] PL_FOUNDATION_DLL
bool IsPowerOf(plInt32 value, plInt32 iBase);
303 [[nodiscard]]
constexpr bool IsPowerOf2(plInt32 value);
306 [[nodiscard]]
constexpr bool IsPowerOf2(plUInt32 value);
309 [[nodiscard]]
constexpr bool IsPowerOf2(plUInt64 value);
318 [[nodiscard]] PL_FOUNDATION_DLL plUInt32
PowerOfTwo_Ceil(plUInt32 value);
321 [[nodiscard]] PL_FOUNDATION_DLL plUInt64
PowerOfTwo_Ceil(plUInt64 value);
327 template <
typename Type>
328 [[nodiscard]]
constexpr bool IsEqual(Type lhs, Type rhs, Type fEpsilon);
331 template <
typename T>
332 [[nodiscard]]
constexpr bool IsInRange(T value, T minVal, T maxVal);
335 template <
typename Type>
336 [[nodiscard]]
bool IsZero(Type f, Type fEpsilon);
364 template <
typename T,
typename T2>
365 [[nodiscard]] T
EvaluateBezierCurve(T2 t,
const T& startPoint,
const T& controlPoint1,
const T& controlPoint2,
const T& endPoint);
368 PL_FOUNDATION_DLL
plResult TryMultiply32(plUInt32& out_uiResult, plUInt32 a, plUInt32 b, plUInt32 c = 1, plUInt32 d = 1);
371 [[nodiscard]] PL_FOUNDATION_DLL plUInt32
SafeMultiply32(plUInt32 a, plUInt32 b, plUInt32 c = 1, plUInt32 d = 1);
374 PL_FOUNDATION_DLL
plResult TryMultiply64(plUInt64& out_uiResult, plUInt64 a, plUInt64 b, plUInt64 c = 1, plUInt64 d = 1);
377 [[nodiscard]] PL_FOUNDATION_DLL plUInt64
SafeMultiply64(plUInt64 a, plUInt64 b, plUInt64 c = 1, plUInt64 d = 1);
386 [[nodiscard]] PL_FOUNDATION_DLL
float ReplaceNaN(
float fValue,
float fFallback);
389 [[nodiscard]] PL_FOUNDATION_DLL
double ReplaceNaN(
double fValue,
double fFallback);
393#include <Foundation/Math/Implementation/MathDouble_inl.h>
394#include <Foundation/Math/Implementation/MathFixedPoint_inl.h>
395#include <Foundation/Math/Implementation/MathFloat_inl.h>
396#include <Foundation/Math/Implementation/MathInt32_inl.h>
397#include <Foundation/Math/Implementation/Math_inl.h>
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
This namespace provides common math-functionality as functions.
Definition Constants.h:6
PL_FOUNDATION_DLL plUInt64 SafeMultiply64(plUInt64 a, plUInt64 b, plUInt64 c=1, plUInt64 d=1)
returns a * b. If an overflow happens, the program is terminated.
Definition Math.cpp:200
PL_FOUNDATION_DLL float ReplaceNaN(float fValue, float fFallback)
If 'value' is not-a-number (NaN) 'fallback' is returned, otherwise 'value' is passed through unmodifi...
Definition Math.cpp:247
PL_FOUNDATION_DLL plResult TryMultiply32(plUInt32 &out_uiResult, plUInt32 a, plUInt32 b, plUInt32 c=1, plUInt32 d=1)
out_Result = a * b. If an overflow happens, PL_FAILURE is returned.
Definition Math.cpp:105
plInt8 ColorFloatToSignedByte(float value)
Converts a color value from float [-1;1] range to signed byte [-127;127] range, with proper rounding.
Definition Math_inl.h:368
double RoundDown(double f, double fMultiple)
Returns a multiple of fMultiple that is smaller than f.
Definition MathDouble_inl.h:40
constexpr plInt32 FloatToInt(float value)
Casts the float to an integer, removes the fractional part.
Definition Math_inl.h:462
PL_ALWAYS_INLINE plAngle ASin(float f)
Returns the arcus sinus of f.
Definition MathFloat_inl.h:77
PL_FORCE_INLINE constexpr float Unlerp(T fMin, T fMax, T fValue)
Returns the interpolation factor such that Lerp(fMin, fMax, factor) == fValue.
Definition Math_inl.h:248
T EvaluateBezierCurve(T2 t, const T &startPoint, const T &controlPoint1, const T &controlPoint2, const T &endPoint)
Evaluates the cubic spline defined by four control points at time t and returns the interpolated resu...
Definition Math_inl.h:443
PL_ALWAYS_INLINE plAngle ATan(float f)
Returns the arcus tangent of f.
Definition MathFloat_inl.h:87
PL_FOUNDATION_DLL plUInt32 GreatestCommonDivisor(plUInt32 a, plUInt32 b)
Returns the greatest common divisor.
Definition Math.cpp:79
constexpr PL_ALWAYS_INLINE T Square(T f)
Returns f * f.
Definition Math_inl.h:8
PL_FORCE_INLINE T Lerp(T f1, T f2, float fFactor)
Returns the linear interpolation of f1 and f2. factor is a value between 0 and 1.
Definition Math_inl.h:230
constexpr PL_ALWAYS_INLINE T Min(T f1, T f2)
Returns the smaller value, f1 or f2.
Definition Math_inl.h:27
PL_ALWAYS_INLINE plAngle ACos(float f)
Returns the arcus cosinus of f.
Definition MathFloat_inl.h:82
PL_ALWAYS_INLINE double RoundToMultiple(double f, double fMultiple)
Rounds f to the closest value of multiple.
Definition MathDouble_inl.h:54
PL_ALWAYS_INLINE double Round(double f)
Rounds f to the next integer.
Definition MathDouble_inl.h:35
PL_ALWAYS_INLINE float Sin(plAngle a)
***** Trigonometric Functions *****
Definition MathFloat_inl.h:62
PL_FOUNDATION_DLL bool IsPowerOf(plInt32 value, plInt32 iBase)
Returns true, if there exists some x with base^x == value.
Definition Math.cpp:16
PL_ALWAYS_INLINE plAngle ATan2(float y, float x)
Returns the atan2 of x and y.
Definition MathFloat_inl.h:92
PL_ALWAYS_INLINE plUInt32 FirstBitHigh(plUInt32 value)
Returns the index of the most significant bit set.
Definition Math_inl.h:119
PL_FOUNDATION_DLL plResult TryMultiply64(plUInt64 &out_uiResult, plUInt64 a, plUInt64 b, plUInt64 c=1, plUInt64 d=1)
out_Result = a * b. If an overflow happens, PL_FAILURE is returned.
Definition Math.cpp:144
PL_ALWAYS_INLINE Type Fraction(Type f)
Returns the fraction-part of f.
Definition Math_inl.h:305
constexpr Type Invert(Type f)
Returns 1 / f.
Definition Math_inl.h:63
constexpr PL_ALWAYS_INLINE T Clamp(T value, T min_val, T max_val)
Clamps "value" to the range [min; max]. Returns "value", if it is inside the range already.
Definition Math_inl.h:51
double RoundUp(double f, double fMultiple)
Returns a multiple of fMultiple that is larger than f.
Definition MathDouble_inl.h:47
plUInt8 ColorFloatToByte(float value)
Converts a color value from float [0;1] range to unsigned byte [0;255] range, with proper rounding.
Definition Math_inl.h:340
constexpr PL_ALWAYS_INLINE bool IsOdd(plInt32 i)
Returns true, if i is an odd number.
Definition MathInt32_inl.h:29
constexpr PL_ALWAYS_INLINE bool IsEven(plInt32 i)
Returns true, if i is an even number.
Definition MathInt32_inl.h:35
PL_ALWAYS_INLINE plUInt32 FirstBitLow(plUInt32 value)
Returns the index of the least significant bit set.
Definition Math_inl.h:70
constexpr float ColorSignedByteToFloat(plInt8 value)
Converts a color value from signed byte [-128;127] range to float [-1;1] range, with proper rounding.
Definition Math_inl.h:428
PL_FOUNDATION_DLL size_t SafeConvertToSizeT(plUInt64 uiValue)
Checks whether the given 64bit value actually fits into size_t, If it doesn't the program is terminat...
plInt16 ColorFloatToSignedShort(float value)
Converts a color value from float [-1;1] range to signed short [-32767;32767] range,...
Definition Math_inl.h:391
constexpr bool IsInRange(T value, T minVal, T maxVal)
Checks whether the value of the first parameter lies between the value of the second and third.
Definition Math_inl.h:282
PL_ALWAYS_INLINE double Mod(double f, double fDiv)
Returns "value mod div" for doubles. This also works with negative numbers, both for value and for di...
Definition MathDouble_inl.h:104
plUInt16 ColorFloatToShort(float value)
Converts a color value from float [0;1] range to unsigned short [0;65535] range, with proper rounding...
Definition Math_inl.h:354
constexpr float ColorByteToFloat(plUInt8 value)
Converts a color value from unsigned byte [0;255] range to float [0;1] range, with proper rounding.
Definition Math_inl.h:414
constexpr float ColorSignedShortToFloat(plInt16 value)
Converts a color value from signed short [-32768;32767] range to float [0;1] range,...
Definition Math_inl.h:435
Type SmootherStep(Type x, Type edge1, Type edge2)
Returns 0, if value is <= edge1, 1 if value >= edge2 and the second order hermite interpolation in be...
Definition Math_inl.h:326
PL_FOUNDATION_DLL plUInt32 PowerOfTwo_Floor(plUInt32 value)
Returns the next power-of-two that is <= value.
Definition Math.cpp:32
PL_ALWAYS_INLINE float Tan(plAngle a)
Takes an angle, returns its tangent.
Definition MathFloat_inl.h:72
PL_ALWAYS_INLINE plUInt32 CountBits(plUInt32 value)
Returns the number of bits set.
Definition Math_inl.h:186
PL_ALWAYS_INLINE Type Bitmask_LowN(plUInt32 uiNumBitsToSet)
Creates a bitmask in which the low N bits are set. For example for N=5, this would be '0000 ....
Definition Math_inl.h:212
PL_ALWAYS_INLINE plUInt32 CountLeadingZeros(plUInt32 uiBitmask)
Definition Math_inl.h:180
PL_ALWAYS_INLINE double Sqrt(double f)
Returns the square root of f.
Definition MathDouble_inl.h:99
constexpr PL_ALWAYS_INLINE T Sign(T f)
Returns the sign of f (i.e: -1, 1 or 0)
Definition Math_inl.h:14
bool IsZero(Type f, Type fEpsilon)
Checks whether the given number is close to zero.
Definition Math_inl.h:288
constexpr bool IsEqual(Type lhs, Type rhs, Type fEpsilon)
Checks, whether fValue is in the range [fDesired - fMaxImprecision; fDesired + fMaxImprecision].
Definition Math_inl.h:276
PL_ALWAYS_INLINE Type Trunc(Type f)
Returns the integer-part of f (removes the fraction).
Definition Math_inl.h:296
PL_ALWAYS_INLINE void Swap(T &ref_f1, T &ref_f2)
Swaps the values in the two variables f1 and f2.
Definition Math_inl.h:224
constexpr PL_ALWAYS_INLINE T Abs(T f)
Returns the absolute value of f.
Definition Math_inl.h:21
plResult TryConvertToSizeT(size_t &out_uiResult, plUInt64 uiValue)
Checks whether the given 64bit value actually fits into size_t, If it doesn't PL_FAILURE is returned.
Definition Math_inl.h:474
PL_ALWAYS_INLINE float Cos(plAngle a)
Takes an angle, returns its cosine.
Definition MathFloat_inl.h:67
constexpr PL_ALWAYS_INLINE T Saturate(T value)
Clamps "value" to the range [0; 1]. Returns "value", if it is inside the range already.
Definition Math_inl.h:57
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39
PL_ALWAYS_INLINE plUInt32 Log2i(plUInt32 uiVal)
Returns the integral logarithm to the base 2, that comes closest to the given integer.
Definition MathInt32_inl.h:41
PL_ALWAYS_INLINE plUInt32 CountTrailingZeros(plUInt32 uiBitmask)
Definition Math_inl.h:167
PL_FOUNDATION_DLL plUInt32 PowerOfTwo_Ceil(plUInt32 value)
Returns the next power-of-two that is >= value.
Definition Math.cpp:53
Type SmoothStep(Type x, Type edge1, Type edge2)
Returns 0, if value is <= edge1, 1 if value >= edge2 and the hermite interpolation in between.
Definition Math_inl.h:311
constexpr PL_FORCE_INLINE bool IsPowerOf2(plInt32 value)
Returns true, if there exists some x with 2^x == value.
Definition Math_inl.h:260
PL_ALWAYS_INLINE Type Bitmask_HighN(plUInt32 uiNumBitsToSet)
Creates a bitmask in which the high N bits are set. For example for N=5, this would be '1111 1000 ....
Definition Math_inl.h:218
constexpr float ColorShortToFloat(plUInt16 value)
Converts a color value from unsigned short [0;65535] range to float [0;1] range, with proper rounding...
Definition Math_inl.h:421
PL_FOUNDATION_DLL plUInt32 SafeMultiply32(plUInt32 a, plUInt32 b, plUInt32 c=1, plUInt32 d=1)
returns a * b. If an overflow happens, the program is terminated.
Definition Math.cpp:132
constexpr PL_FORCE_INLINE T Step(T value, T edge)
Returns 0, if value < edge, and 1, if value >= edge.
Definition Math_inl.h:255
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54