WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Source
  4. Math.h
Mission NodeSDK 6.1C++math

Math.h

SDK Source File: Math.h

Source / SDK 6.1 / C++ / math

Math.h

Math.h
CPP
1/* Copyright Statement:
2 *
3 * (C) 2021-2024 Cubios Inc. All rights reserved.
4 */
5
6#ifndef WASMLIBS_CPP_MATH_H_
7#define WASMLIBS_CPP_MATH_H_
8
9#include <math.h>
10
11#ifndef LN_2
12#define LN_2 0.693147180559945f
13#endif
14
15#ifndef PI
16#define PI (3.141592653589f)
17#endif
18
19#ifndef PI_2
20#define PI_2 (6.28318530718f)
21#endif
22
23#ifndef TINY
24#define TINY (0.0000001)
25#endif
26
27#define n_max(a,b) (((a) > (b)) ? (a) : (b))
28#define n_min(a,b) (((a) < (b)) ? (a) : (b))
29#define n_abs(a) (((a)<0.0f) ? (-(a)) : (a))
30#define n_sgn(a) (((a)<0.0f) ? (-1) : (1))
31#define n_deg2rad(d) (((d)*PI)/180.0f)
32#define n_rad2deg(r) (((r)*180.0f)/PI)
33#define n_sin(x) (float(sin(x)))
34#define n_cos(x) (float(cos(x)))
35#define n_tan(x) (float(tan(x)))
36#define n_atan(x) (float(atan(x)))
37
38
39namespace Cubios
40{
41 namespace Math
42 {
43
44 //log2() function.
45static inline float log2(float f)
46{
47 return logf(f) / LN_2;
48}
49
50//Integer clamping.
51static inline int iclamp(int val, int minVal, int maxVal)
52{
53 if (val < minVal) return minVal;
54 else if (val > maxVal) return maxVal;
55 else return val;
56}
57
58 // acos with value clamping.
59static inline float acos(float x)
60{
61 if(x > 1.0f) x = 1.0f;
62 if(x < -1.0f) x = -1.0f;
63 return (float)acos(x);
64}
65
66//asin with value clamping.
67static inline float asin(float x)
68{
69 if(x > 1.0f) x = 1.0f;
70 if(x < -1.0f) x = -1.0f;
71 return (float)asin(x);
72}
73
74//Safe sqrt.
75static inline float sqrt(float x)
76{
77 if (x<0.0f) x=(float)0.0f;
78 return (float) sqrt(x);
79}
80
81//A fuzzy floating point equality check
82static inline bool fequal(float f0, float f1, float tol)
83{
84 float f = f0-f1;
85 if ((f>(-tol)) && (f<tol)) return true;
86 else return false;
87}
88
89//A fuzzy floating point less-then check.
90static inline bool fless(float f0, float f1, float tol)
91{
92 if ((f0-f1)<tol) return true;
93 else return false;
94}
95
96//A fuzzy floating point greater-then check.
97static inline bool fgreater(float f0, float f1, float tol)
98{
99 if ((f0-f1)>tol) return true;
100 else return false;
101}
102
103/**
104 fast float to int conversion (always truncates)
105 see http://www.stereopsis.com/FPU.html for a discussion.
106 NOTE: this works only on little endian machines.
107*/
108static inline long ftol(float val)
109{
110 double v = double(val) + (68719476736.0*1.5);
111 return ((long*)&v)[0] >> 16;
112}
113
114
115//Smooth a new value towards an old value using a change value.
116static inline float smooth(float newVal, float curVal, float maxChange)
117{
118 float diff = newVal - curVal;
119 if (fabs(diff) > maxChange)
120 {
121 if (diff > 0.0f)
122 {
123 curVal += maxChange;
124 if (curVal > newVal)
125 {
126 curVal = newVal;
127 }
128 }
129 else if (diff < 0.0f)
130 {
131 curVal -= maxChange;
132 if (curVal < newVal)
133 {
134 curVal = newVal;
135 }
136 }
137 }
138 else
139 {
140 curVal = newVal;
141 }
142 return curVal;
143}
144
145
146//Clamp a value against lower und upper boundary.
147static inline float clamp(float val, float lower, float upper)
148{
149 if (val < lower) return lower;
150 else if (val > upper) return upper;
151 else return val;
152}
153
154//Saturate a value (clamps between 0.0f and 1.0f)
155static inline float saturate(float val)
156{
157 if (val < 0.0f) return 0.0f;
158 else if (val > 1.0f) return 1.0f;
159 else return val;
160}
161
162static unsigned long seed = 0;
163
164//Return a pseudo random number between 0 and 1.
165static inline float rand2()
166{
167 return float(rand()) / float(RAND_MAX);
168}
169
170
171// Geteral randomizer
172static inline unsigned long rand()
173{
174 seed = (1664525L*seed + 1013904223L) & 0xffffffff;
175 return seed;
176}
177
178
179// Gets randomizer seed
180static inline unsigned long randGetSeed()
181{
182 return seed;
183}
184
185// Initializes randomizer's seed
186static inline void randSetSeed (unsigned long s)
187{
188 seed = s;
189}
190
191// Returns random int in range of [0,n]
192static inline int randInt (int n)
193{
194 double a = double(n) / 4294967296.0;
195 return (int) (double(rand()) * a);
196}
197
198// Returns random float in [0,1]
199static inline float randReal()
200{
201 return ((float) rand()) / ((float) 0xffffffff);
202}
203
204//Linearly interpolate between 2 values: ret = x + l * (y - x)
205static inline float lerp(float x, float y, float l)
206{
207 return x + l * (y - x);
208}
209
210 }
211
212}
213#endif /* WASMLIBS_CPP_MATH_H_ */
214
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Related nodes

Color.h
Source / SDK 6.1 / C++ / math
Transform.h
Source / SDK 6.1 / C++ / math
Vec2.h
Source / SDK 6.1 / C++ / math
Color.h
Source / SDK 6.2 / C++ / math
Previous Node
Color.h
Source / SDK 6.1 / C++ / math
Next Node
Transform.h
Source / SDK 6.1 / C++ / math