Plasma Engine  2.0
Loading...
Searching...
No Matches
Math.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Math/Angle.h>
5#include <Foundation/Math/Constants.h>
6#include <Foundation/Math/Declarations.h>
7
8
13namespace plMath
14{
16 template <typename Type>
17 constexpr static bool IsNaN(Type value)
18 {
19 return false;
20 }
21
23 template <typename Type>
24 constexpr static bool IsFinite(Type value)
25 {
26 return true;
27 }
28
30
32 [[nodiscard]] float Sin(plAngle a); // [tested]
33
35 [[nodiscard]] float Cos(plAngle a); // [tested]
36
38 [[nodiscard]] float Tan(plAngle a); // [tested]
39
41 [[nodiscard]] plAngle ASin(float f); // [tested]
42
44 [[nodiscard]] plAngle ACos(float f); // [tested]
45
47 [[nodiscard]] plAngle ATan(float f); // [tested]
48
50 [[nodiscard]] plAngle ATan2(float y, float x); // [tested]
51
53 [[nodiscard]] float Exp(float f); // [tested]
54
56 [[nodiscard]] float Ln(float f); // [tested]
57
59 [[nodiscard]] float Log2(float f); // [tested]
60
62 [[nodiscard]] plUInt32 Log2i(plUInt32 uiVal); // [tested]
63
65 [[nodiscard]] float Log10(float f); // [tested]
66
68 [[nodiscard]] float Log(float fBase, float f); // [tested]
69
71 [[nodiscard]] float Pow2(float f); // [tested]
72
74 [[nodiscard]] float Pow(float fBase, float fExp); // [tested]
75
77 [[nodiscard]] constexpr plInt32 Pow2(plInt32 i); // [tested]
78
80 [[nodiscard]] plInt32 Pow(plInt32 iBase, plInt32 iExp); // [tested]
81
83 template <typename T>
84 [[nodiscard]] constexpr T Square(T f); // [tested]
85
87 [[nodiscard]] float Sqrt(float f); // [tested]
88
90 [[nodiscard]] double Sqrt(double f); // [tested]
91
93 [[nodiscard]] float Root(float f, float fNthRoot); // [tested]
94
96 template <typename T>
97 [[nodiscard]] constexpr T Sign(T f); // [tested]
98
100 template <typename T>
101 [[nodiscard]] constexpr T Abs(T f); // [tested]
102
104 template <typename T>
105 [[nodiscard]] constexpr T Min(T f1, T f2); // [tested]
106
108 template <typename T, typename... ARGS>
109 [[nodiscard]] constexpr T Min(T f1, T f2, ARGS... f); // [tested]
110
112 template <typename T>
113 [[nodiscard]] constexpr T Max(T f1, T f2); // [tested]
114
116 template <typename T, typename... ARGS>
117 [[nodiscard]] constexpr T Min(T f1, T f2, ARGS... f); // [tested]
118
120 template <typename T>
121 [[nodiscard]] constexpr T Clamp(T value, T min_val, T max_val); // [tested]
122
124 template <typename T>
125 [[nodiscard]] constexpr T Saturate(T value); // [tested]
126
128 [[nodiscard]] float Floor(float f); // [tested]
129
131 [[nodiscard]] float Ceil(float f); // [tested]
132
134 [[nodiscard]] float RoundDown(float f, float fMultiple); // [tested]
135
137 [[nodiscard]] double RoundDown(double f, double fMultiple); // [tested]
138
140 [[nodiscard]] float RoundUp(float f, float fMultiple); // [tested]
141
143 [[nodiscard]] double RoundUp(double f, double fMultiple); // [tested]
144
146 template <typename Type>
147 [[nodiscard]] Type Trunc(Type f); // [tested]
148
152 [[nodiscard]] constexpr plInt32 FloatToInt(float value);
153
154 // There is a compiler bug in VS 2019 targeting 32-bit that causes an internal compiler error when casting double to long long.
155 // FloatToInt(double) is not available on these version of the MSVC compiler.
156#if PL_DISABLED(PL_PLATFORM_ARCH_X86) || (_MSC_VER <= 1916)
160 [[nodiscard]] constexpr plInt64 FloatToInt(double value);
161#endif
162
166 [[nodiscard]] float Round(float f); // [tested]
167
171 [[nodiscard]] double Round(double f); // [tested]
172
174 [[nodiscard]] float RoundToMultiple(float f, float fMultiple);
175
177 [[nodiscard]] double RoundToMultiple(double f, double fMultiple);
178
180 template <typename Type>
181 [[nodiscard]] Type Fraction(Type f); // [tested]
182
184 [[nodiscard]] float Mod(float value, float fDiv); // [tested]
185
187 [[nodiscard]] double Mod(double f, double fDiv); // [tested]
188
190 template <typename Type>
191 [[nodiscard]] constexpr Type Invert(Type f); // [tested]
192
194 [[nodiscard]] constexpr plInt32 RoundUp(plInt32 value, plUInt16 uiMultiple); // [tested]
195
197 [[nodiscard]] constexpr plInt32 RoundDown(plInt32 value, plUInt16 uiMultiple); // [tested]
198
200 [[nodiscard]] constexpr plUInt32 RoundUp(plUInt32 value, plUInt16 uiMultiple); // [tested]
201
203 [[nodiscard]] constexpr plUInt32 RoundDown(plUInt32 value, plUInt16 uiMultiple); // [tested]
204
206 [[nodiscard]] constexpr bool IsOdd(plInt32 i); // [tested]
207
209 [[nodiscard]] constexpr bool IsEven(plInt32 i); // [tested]
210
214 [[nodiscard]] plUInt32 FirstBitLow(plUInt32 uiBitmask); // [tested]
215
219 [[nodiscard]] plUInt32 FirstBitLow(plUInt64 uiBitmask); // [tested]
220
224 [[nodiscard]] plUInt32 FirstBitHigh(plUInt32 uiBitmask); // [tested]
225
229 [[nodiscard]] plUInt32 FirstBitHigh(plUInt64 uiBitmask); // [tested]
230
238 [[nodiscard]] plUInt32 CountTrailingZeros(plUInt32 uiBitmask); // [tested]
239
241 [[nodiscard]] plUInt32 CountTrailingZeros(plUInt64 uiBitmask); // [tested]
242
251 [[nodiscard]] plUInt32 CountLeadingZeros(plUInt32 uiBitmask); // [tested]
252
254 [[nodiscard]] plUInt32 CountBits(plUInt32 value);
255
257 [[nodiscard]] plUInt32 CountBits(plUInt64 value);
258
262 template <typename Type>
263 [[nodiscard]] Type Bitmask_LowN(plUInt32 uiNumBitsToSet);
264
268 template <typename Type>
269 [[nodiscard]] Type Bitmask_HighN(plUInt32 uiNumBitsToSet);
270
272 template <typename T>
273 void Swap(T& ref_f1, T& ref_f2); // [tested]
274
276 template <typename T>
277 [[nodiscard]] T Lerp(T f1, T f2, float fFactor); // [tested]
278
280 template <typename T>
281 [[nodiscard]] T Lerp(T f1, T f2, double fFactor); // [tested]
282
284 template <typename T>
285 [[nodiscard]] constexpr float Unlerp(T fMin, T fMax, T fValue); // [tested]
286
288 template <typename T>
289 [[nodiscard]] constexpr T Step(T value, T edge); // [tested]
290
292 template <typename Type>
293 [[nodiscard]] Type SmoothStep(Type value, Type edge1, Type edge2); // [tested]
294
296 template <typename Type>
297 [[nodiscard]] Type SmootherStep(Type value, Type edge1, Type edge2); // [tested]
298
300 [[nodiscard]] PL_FOUNDATION_DLL bool IsPowerOf(plInt32 value, plInt32 iBase); // [tested]
301
303 [[nodiscard]] constexpr bool IsPowerOf2(plInt32 value); // [tested]
304
306 [[nodiscard]] constexpr bool IsPowerOf2(plUInt32 value); // [tested]
307
309 [[nodiscard]] constexpr bool IsPowerOf2(plUInt64 value); // [tested]
310
312 [[nodiscard]] PL_FOUNDATION_DLL plUInt32 PowerOfTwo_Floor(plUInt32 value); // [tested]
313
315 [[nodiscard]] PL_FOUNDATION_DLL plUInt64 PowerOfTwo_Floor(plUInt64 value); // [tested]
316
318 [[nodiscard]] PL_FOUNDATION_DLL plUInt32 PowerOfTwo_Ceil(plUInt32 value); // [tested]
319
321 [[nodiscard]] PL_FOUNDATION_DLL plUInt64 PowerOfTwo_Ceil(plUInt64 value); // [tested]
322
324 [[nodiscard]] PL_FOUNDATION_DLL plUInt32 GreatestCommonDivisor(plUInt32 a, plUInt32 b); // [tested]
325
327 template <typename Type>
328 [[nodiscard]] constexpr bool IsEqual(Type lhs, Type rhs, Type fEpsilon);
329
331 template <typename T>
332 [[nodiscard]] constexpr bool IsInRange(T value, T minVal, T maxVal); // [tested]
333
335 template <typename Type>
336 [[nodiscard]] bool IsZero(Type f, Type fEpsilon); // [tested]
337
339 [[nodiscard]] plUInt8 ColorFloatToByte(float value); // [tested]
340
342 [[nodiscard]] plUInt16 ColorFloatToShort(float value); // [tested]
343
345 [[nodiscard]] plInt8 ColorFloatToSignedByte(float value); // [tested]
346
348 [[nodiscard]] plInt16 ColorFloatToSignedShort(float value); // [tested]
349
351 [[nodiscard]] constexpr float ColorByteToFloat(plUInt8 value); // [tested]
352
354 [[nodiscard]] constexpr float ColorShortToFloat(plUInt16 value); // [tested]
355
357 [[nodiscard]] constexpr float ColorSignedByteToFloat(plInt8 value); // [tested]
358
360 [[nodiscard]] constexpr float ColorSignedShortToFloat(plInt16 value); // [tested]
361
364 template <typename T, typename T2>
365 [[nodiscard]] T EvaluateBezierCurve(T2 t, const T& startPoint, const T& controlPoint1, const T& controlPoint2, const T& endPoint);
366
368 PL_FOUNDATION_DLL plResult TryMultiply32(plUInt32& out_uiResult, plUInt32 a, plUInt32 b, plUInt32 c = 1, plUInt32 d = 1); // [tested]
369
371 [[nodiscard]] PL_FOUNDATION_DLL plUInt32 SafeMultiply32(plUInt32 a, plUInt32 b, plUInt32 c = 1, plUInt32 d = 1);
372
374 PL_FOUNDATION_DLL plResult TryMultiply64(plUInt64& out_uiResult, plUInt64 a, plUInt64 b, plUInt64 c = 1, plUInt64 d = 1); // [tested]
375
377 [[nodiscard]] PL_FOUNDATION_DLL plUInt64 SafeMultiply64(plUInt64 a, plUInt64 b, plUInt64 c = 1, plUInt64 d = 1);
378
380 plResult TryConvertToSizeT(size_t& out_uiResult, plUInt64 uiValue); // [tested]
381
383 [[nodiscard]] PL_FOUNDATION_DLL size_t SafeConvertToSizeT(plUInt64 uiValue);
384
386 [[nodiscard]] PL_FOUNDATION_DLL float ReplaceNaN(float fValue, float fFallback); // [tested]
387
389 [[nodiscard]] PL_FOUNDATION_DLL double ReplaceNaN(double fValue, double fFallback); // [tested]
390
391} // namespace plMath
392
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