sculpto
math_common.h
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file common.h
3 * \brief Math common functions, types implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 19 June 2022
7 *********************************************************************/
8
9 #pragma once
10
11#include <math.h>
12
13namespace scl
14{
16 using i8 = int8_t;
17 using u8 = uint8_t;
18 using i16 = int16_t;
19 using u16 = uint16_t;
20 using i32 = int32_t;
21 using u32 = uint32_t;
22 using i64 = int64_t;
23 using u64 = uint64_t;
24
25 namespace math
26 {
28 const float E = 2.7182818284590f; // e
29 const float LOG2E = 1.4426950408889f; // log2(e)
30 const float LOG10E = 0.4342944819032f; // log10(e)
31 const float LN2 = 0.6931471805599f; // ln(2)
32 const float LN10 = 2.3025850929940f; // ln(10)
33 const float PI = 3.1415926535897f; // pi
34 const float PI_2 = 1.5707963267948f; // pi/2
35 const float PI_4 = 0.7853981633974f; // pi/4
36 const float REV_1_PI = 0.3183098861837f; // 1/pi
37 const float REV_2_PI = 0.6366197723675f; // 2/pi
38 const float REV_2_SQRTPI = 1.1283791670955f; // 2/sqrt(pi)
39 const float SQRT2 = 1.4142135623730f; // sqrt(2)
40 const float SQRT1_2 = 0.7071067811865f; // 1/sqrt(2)
41
42 /*!*
43 * Getting smallest number function.
44 *
45 * \param Num1, Num2 - numbers to smallest one be chosen from.
46 * \return smallest number from pair.
47 */
48 template <typename T>
49 T Min(T Num1, T Num2)
50 {
51 return Num1 < Num2 ? Num1 : Num2;
52 }
53
54 /*!*
55 * Getting biggest number function.
56 *
57 * \param Num1, Num2 - numbers to biggest one be chosen from.
58 * \return smallest number from pair.
59 */
60 template <typename T>
61 T Max(T Num1, T Num2)
62 {
63 return Num1 > Num2 ? Num1 : Num2;
64 }
65
66 /*!*
67 * Number clamping function.
68 * If number is higher then upper bound value returned.
69 * If number is lower then lower bound value returned.
70 *
71 * \param Num - number to be clamped.
72 * \param Min - lower clipping limit.
73 * \param Max - upper clipping limit.
74 * \return clamped value.
75 */
76 template <typename T>
77 T Clamp(T Num, T Min = 0, T Max = 1)
78 {
79 return Num < Min ? Min : Num > Max ? Max : Num;
80 }
81
82 /*!*
83 * Linear interpolation between two points function.
84 *
85 * \param Start - first point value.
86 * \param End - second point value.
87 * \param Current - interpolation value [0;1].
88 * \return value.
89 */
90 template <typename T>
91 T Lerp(T Start, T End, T Current)
92 {
93 return Start + (End - Start) * Current;
94 }
95
96 /*!*
97 * Getting random number function.
98 *
99 * \param None.
100 * \return random number in range [0;1].
101 */
102 template <typename T>
104 {
105 return static_cast<T>(rand()) / RAND_MAX;
106 }
107
108 /*!*
109 * Getting randow number function.
110 *
111 * \param min - lower bound of random number.
112 * \param Max - upper bound of random number.
113 * \return random number in range [Min; Max].
114 */
115 template <typename T>
116 T Rnd(T Min = 0, T Max = 1)
117 {
118 return Min + Rnd0<T>() * (Max - Min + 1);
119 }
120 }
121}
Mathematical support include module.
const float PI_4
Definition: math_common.h:35
const float PI
Definition: math_common.h:33
const float LN2
Definition: math_common.h:31
T Max(T Num1, T Num2)
Definition: math_common.h:61
const float LOG10E
Definition: math_common.h:30
const float LOG2E
Definition: math_common.h:29
T Clamp(T Num, T Min=0, T Max=1)
Definition: math_common.h:77
const float SQRT1_2
Definition: math_common.h:40
const float REV_2_SQRTPI
Definition: math_common.h:38
const float SQRT2
Definition: math_common.h:39
T Min(T Num1, T Num2)
Definition: math_common.h:49
const float LN10
Definition: math_common.h:32
const float REV_1_PI
Definition: math_common.h:36
const float E
Definition: math_common.h:28
const float PI_2
Definition: math_common.h:34
T Lerp(T Start, T End, T Current)
Definition: math_common.h:91
T Rnd(T Min=0, T Max=1)
Definition: math_common.h:116
const float REV_2_PI
Definition: math_common.h:37
Definition: base.h:33
int32_t i32
Definition: math_common.h:20
uint32_t u32
Definition: math_common.h:21
int64_t i64
Definition: math_common.h:22
int8_t i8
Definition: math_common.h:16
uint16_t u16
Definition: math_common.h:19
int16_t i16
Definition: math_common.h:18
uint64_t u64
Definition: math_common.h:23
uint8_t u8
Definition: math_common.h:17