xmscore  1.0
math.h
Go to the documentation of this file.
1 #pragma once
2 //------------------------------------------------------------------------------
7 //------------------------------------------------------------------------------
8 #define XMSCORE_MATH_H
9 
10 #include <cmath> // for std::abs
11 
12 namespace xms
13 {
14 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
19 template <class _T>
20 int Round(_T a)
21 {
22  return (((a) > 0.0) ? ((int)((a) + 0.5)) : (((a) < 0.0) ? ((int)((a)-0.5)) : 0));
23 }
24 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 template <class _T>
30 _T Miabs(_T a)
31 {
32  return (((a) >= 0) ? (a) : (-(a)));
33 }
34 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 template <class _T>
40 _T Mfabs(_T a)
41 {
42  return (((a) >= 0.) ? (a) : (-(a)));
43 }
44 //------------------------------------------------------------------------------
49 //------------------------------------------------------------------------------
50 template <class _T, class _U>
51 _T Mmax(_T a, _U b)
52 {
53  return (((a) >= (b)) ? (a) : (b));
54 }
55 //------------------------------------------------------------------------------
60 //------------------------------------------------------------------------------
61 template <class _T, class _U>
62 _T Mmin(_T a, _U b)
63 {
64  return (((a) >= (_T)(b)) ? (b) : (a));
65 }
66 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 template <class _T, class _U, class _V>
74 _T Mmax3(_T a, _U b, _V c)
75 {
76  return (((a) >= (b)) ? (((a) >= (c)) ? (a) : (c)) : (((b) >= (c)) ? (b) : (c)));
77 }
78 //------------------------------------------------------------------------------
84 //------------------------------------------------------------------------------
85 template <class _T, class _U, class _V>
86 _T Mmin3(_T a, _U b, _V c)
87 {
88  return (((a) <= (b)) ? (((a) <= (c)) ? (a) : (c)) : (((b) <= (c)) ? (b) : (c)));
89 }
90 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 template <typename _T>
96 inline _T sqr(const _T x)
97 {
98  return x * x;
99 }
100 //------------------------------------------------------------------------------
107 //------------------------------------------------------------------------------
108 template <class _T, class _U, class _V, class _W>
109 double Mdist(_T x1, _U y1, _V x2, _W y2)
110 {
111  return sqrt((double)(sqr(x1 - x2) + sqr(y1 - y2)));
112 }
113 //------------------------------------------------------------------------------
120 //------------------------------------------------------------------------------
121 template <class _T, class _U, class _V, class _W>
122 double MdistSq(_T x1, _U y1, _V x2, _W y2)
123 {
124  return (double)(sqr(x1 - x2) + sqr(y1 - y2));
125 }
126 //------------------------------------------------------------------------------
135 //------------------------------------------------------------------------------
136 template <typename X1, typename Y1, typename Z1, typename X2, typename Y2, typename Z2>
137 double Mdist(X1 x1, Y1 y1, Z1 z1, X2 x2, Y2 y2, Z2 z2)
138 {
139  return sqrt((double)(sqr(x1 - x2) + sqr(y1 - y2) + sqr(z1 - z2)));
140 }
141 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
149 template <typename _T>
150 double MagSquared(_T const x, _T const y, _T const z = 0, _T const w = 0)
151 {
152  return (double)(x * x + y * y + z * z + w * w);
153 }
154 //------------------------------------------------------------------------------
161 //------------------------------------------------------------------------------
162 template <typename _T>
163 double Mag(_T const x, _T const y, _T const z = 0, _T const w = 0)
164 {
165  return sqrt(MagSquared(x, y, z, w));
166 }
167 //------------------------------------------------------------------------------
173 //------------------------------------------------------------------------------
174 template <typename _T>
175 _T Clamp(_T a_in, _T a_min, _T a_max)
176 {
177  return Mmin(Mmax(a_in, a_min), a_max);
178 }
179 
180 // The following 3 macros ending with EPS should have an epsilon
181 // value passed to them. This should be something like FLT_EPSILON or
182 // DBL_EPS or 1e-6 etc. The epsilon value is multiplied by the
183 // sum of the two floats to compute a tolerance
184 
185 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
192 template <class _T, class _U, class _V>
193 bool EQ_EPS(_T A, _U B, _V epsilon)
194 {
195  return (std::abs((A) - (B)) <= std::abs(((A) + (B)) * (epsilon)));
196 }
197 //------------------------------------------------------------------------------
203 //------------------------------------------------------------------------------
204 template <class _T, class _U, class _V>
205 bool LT_EPS(_T A, _U B, _V epsilon)
206 {
207  return (((B) - (A)) > std::abs(((A) + (B)) * (epsilon)));
208 }
209 //------------------------------------------------------------------------------
215 //------------------------------------------------------------------------------
216 template <class _T, class _U, class _V>
217 bool GT_EPS(_T A, _U B, _V epsilon)
218 {
219  return (((A) - (B)) > std::abs(((A) + (B)) * (epsilon)));
220 }
221 //------------------------------------------------------------------------------
227 //------------------------------------------------------------------------------
228 template <class _T, class _U, class _V>
229 bool LTEQ_EPS(_T A, _U B, _V epsilon)
230 {
231  return (LT_EPS((A), (B), (epsilon)) || EQ_EPS((A), (B), (epsilon)));
232 }
233 //------------------------------------------------------------------------------
239 //------------------------------------------------------------------------------
240 template <class _T, class _U, class _V>
241 bool GTEQ_EPS(_T A, _U B, _V epsilon)
242 {
243  return (GT_EPS((A), (B), (epsilon)) || EQ_EPS((A), (B), (epsilon)));
244 }
245 
246 // The following 3 macros ending with TOL should have a tolerance
247 // passed to them. This tolerance should be something like
248 // g_triangletolerancexy, or a tolerance that has been computed
249 // from the range of the numbers involved. The numbers are compared
250 // against the tolerance
251 // Equal-with-tolerance EQ_TOL is needed, otherwise double values would not be
252 // equal when, for all practical purposes, they are equal. Given that a number
253 // is equal-with-tolerance to another number, then it cannot also be either
254 // less-than or greater-than that other number. Therefore, LT_TOL and GT_TOL
255 // give non-overlapping results rather than the result given by < and >.
256 
257 //------------------------------------------------------------------------------
263 //------------------------------------------------------------------------------
264 template <class _T, class _U, class _V>
265 bool EQ_TOL(const _T& A, const _U& B, const _V& tolerance)
266 {
267  return (std::abs((A) - (B)) <= (tolerance));
268 }
269 //------------------------------------------------------------------------------
275 //------------------------------------------------------------------------------
276 template <class _T, class _U, class _V>
277 bool LT_TOL(_T A, _U B, _V tolerance)
278 {
279  return (((B) - (A)) > (tolerance));
280 }
281 //------------------------------------------------------------------------------
287 //------------------------------------------------------------------------------
288 template <class _T, class _U, class _V>
289 bool GT_TOL(_T A, _U B, _V tolerance)
290 {
291  return (((A) - (B)) > (tolerance));
292 }
293 //------------------------------------------------------------------------------
299 //------------------------------------------------------------------------------
300 template <class _T, class _U, class _V>
301 bool LTEQ_TOL(_T A, _U B, _V tol)
302 {
303  return (LT_TOL((A), (B), (tol)) || EQ_TOL((A), (B), (tol)));
304 }
305 //------------------------------------------------------------------------------
311 //------------------------------------------------------------------------------
312 template <class _T, class _U, class _V>
313 bool GTEQ_TOL(_T A, _U B, _V tol)
314 {
315  return (GT_TOL((A), (B), (tol)) || EQ_TOL((A), (B), (tol)));
316 }
317 
318 } // namespace xms {
bool LT_EPS(_T A, _U B, _V epsilon)
Returns true if A < B equal within an epsilon (DBL EPS).
Definition: math.h:205
bool GT_EPS(_T A, _U B, _V epsilon)
Returns true if A > B equal within an epsilon (DBL EPS).
Definition: math.h:217
_T Miabs(_T a)
Integer absolute value.
Definition: math.h:30
double Mag(_T const x, _T const y, _T const z=0, _T const w=0)
Magnituded sqrt(x*x + y*y + z*z + w*w)
Definition: math.h:163
bool LTEQ_TOL(_T A, _U B, _V tol)
Returns true if A <= B equal within a tolerance.
Definition: math.h:301
_T Mfabs(_T a)
Float absolute value.
Definition: math.h:40
double MagSquared(_T const x, _T const y, _T const z=0, _T const w=0)
Magnituded squared (x*x + y*y + z*z + w*w)
Definition: math.h:150
int Round(_T a)
Rounds.
Definition: math.h:20
_T Mmax3(_T a, _U b, _V c)
Max of three values.
Definition: math.h:74
bool GTEQ_TOL(_T A, _U B, _V tol)
Returns true if A >= B equal within a tolerance.
Definition: math.h:313
double MdistSq(_T x1, _U y1, _V x2, _W y2)
XY distance squared.
Definition: math.h:122
_T sqr(const _T x)
Square.
Definition: math.h:96
_T Mmax(_T a, _U b)
Max of two values.
Definition: math.h:51
bool LTEQ_EPS(_T A, _U B, _V epsilon)
Returns true if A <= B equal within an epsilon (DBL EPS).
Definition: math.h:229
bool EQ_TOL(const _T &A, const _U &B, const _V &tolerance)
Returns true if A == B equal within a tolerance.
Definition: math.h:265
bool LT_TOL(_T A, _U B, _V tolerance)
Returns true if A < B equal within a tolerance.
Definition: math.h:277
double Mdist(_T x1, _U y1, _V x2, _W y2)
XY distance.
Definition: math.h:109
_T Mmin(_T a, _U b)
Min of two values.
Definition: math.h:62
_T Mmin3(_T a, _U b, _V c)
Min of three values.
Definition: math.h:86
bool GTEQ_EPS(_T A, _U B, _V epsilon)
Returns true if A >= B equal within an epsilon (DBL EPS).
Definition: math.h:241
bool GT_TOL(_T A, _U B, _V tolerance)
Returns true if A > B equal within a tolerance.
Definition: math.h:289
_T Clamp(_T a_in, _T a_min, _T a_max)
Returns a value between a_min and a_max.
Definition: math.h:175
bool EQ_EPS(_T A, _U B, _V epsilon)
Returns true if A == B within an epsilon (DBL EPS).
Definition: math.h:193