My Project: dynamics.mod/NewtonDynamics/packages/dMath/dMathDefines.h Source File

My Project

dMathDefines.h
Go to the documentation of this file.
1 /* Copyright (c) <2009> <Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely
10 */
11
12
13 #ifndef __dMathDefined__
14 #define __dMathDefined__
15
16 #include <math.h>
17 #include <float.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <assert.h>
22
23 #ifndef dFloat
24 #ifdef _NEWTON_USE_DOUBLE
25 typedef double dFloat;
26 #else
27 typedef float dFloat;
28 #endif
29 #endif
30
31 #ifndef dFloat64
32 typedef double dFloat64;
33 #endif
34
35
36 // transcendental functions
37 #define dAbs(x) dFloat (fabs (dFloat(x)))
38 #define dSqrt(x) dFloat (sqrt (dFloat(x)))
39 #define dFloor(x) dFloat (floor (dFloat(x)))
40 #define dCiel(x) dFloat (ceil (dFloat(x)))
41 #define dMod(x,y) dFloat (fmod (dFloat(x), dFloat(y)))
42 #define dPow(x,y) dFloat (pow (dFloat(x), dFloat(y)))
43
44 #define dSin(x) dFloat (sin (dFloat(x)))
45 #define dCos(x) dFloat (cos (dFloat(x)))
46 #define dTan(x) dFloat (tan (dFloat(x)))
47 #define dAsin(x) dFloat (asin (dFloat(x)))
48 #define dAcos(x) dFloat (acos (dFloat(x)))
49 #define dAtan2(x,y) dFloat (atan2 (dFloat(x), dFloat(y)))
50
51
52 #define D_MSC_VECTOR_ALIGMENT
53
54 enum dEulerAngleOrder
55 {
56 m_pitchYawRoll = (0 << 8) + (1 << 4) + (2 << 0),
57 m_rollYawpitch = (2 << 8) + (1 << 4) + (0 << 0),
58 };
59
60
61
62
63 #if ( defined (_MSC_VER) ) // || defined (_MINGW_32_VER) || defined (_MINGW_64_VER) )
64 #define dAssert(x) _ASSERTE(x)
65 #else
66 #define dAssert(x) assert(x)
67 #endif
68
69
70
71
72 template <class T>
73 T dSign (T A)
74 {
75 return (A >= T(0)) ? T(1) : T(-1);
76 }
77
78 template <class T>
79 void dSwap(T& A, T& B)
80 {
81 T tmp (A);
82 A = B;
83 B = tmp;
84 }
85
86 template <class T>
87 T dMax(T A, T B)
88 {
89 return (A > B) ? A : B;
90 }
91
92 template <class T>
93 T dMin(T A, T B)
94 {
95 return (A < B) ? A : B;
96 }
97
98 template <class T>
99 T dClamp(T val, T min, T max)
100 {
101 return dMax (min, dMin (max, val));
102 }
103
104 template <class T>
105 void dSort (T* const array, int elements, int (*compare) (const T* const A, const T* const B, void* const context), void* const context = NULL)
106 {
107 int stride = 8;
108 int stack[1024][2];
109
110 stack[0][0] = 0;
111 stack[0][1] = elements - 1;
112 int stackIndex = 1;
113 while (stackIndex) {
114 stackIndex --;
115 int lo = stack[stackIndex][0];
116 int hi = stack[stackIndex][1];
117 if ((hi - lo) > stride) {
118 int i = lo;
119 int j = hi;
120 T val (array[(lo + hi) >> 1]);
121 do {
122 while (compare (&array[i], &val, context) < 0) i ++;
123 while (compare (&array[j], &val, context) > 0) j --;
124
125 if (i <= j) {
126 dSwap(array[i], array[j]);
127 i++;
128 j--;
129 }
130 } while (i <= j);
131
132 if (i < hi) {
133 stack[stackIndex][0] = i;
134 stack[stackIndex][1] = hi;
135 stackIndex ++;
136 }
137 if (lo < j) {
138 stack[stackIndex][0] = lo;
139 stack[stackIndex][1] = j;
140 stackIndex ++;
141 }
142 dAssert (stackIndex < int (sizeof (stack) / (2 * sizeof (stack[0][0]))));
143 }
144 }
145
146 stride = stride * 2;
147 if (elements < stride) {
148 stride = elements;
149 }
150 for (int i = 1; i < stride; i ++) {
151 if (compare (&array[0], &array[i], context) > 0) {
152 dSwap(array[0], array[i]);
153 }
154 }
155
156 for (int i = 1; i < elements; i ++) {
157 int j = i;
158 T tmp (array[i]);
159 for (; compare (&array[j - 1], &tmp, context) > 0; j --) {
160 dAssert (j > 0);
161 array[j] = array[j - 1];
162 }
163 array[j] = tmp;
164 }
165
166 #ifdef _DEBUG
167 for (int i = 0; i < (elements - 1); i ++) {
168 dAssert (compare (&array[i], &array[i + 1], context) <= 0);
169 }
170 #endif
171 }
172
173
174
175 #ifdef _MSC_VER
176 #ifdef _DEBUG
177 #include <windows.h>
178 #include <stdarg.h>
179 inline void dExpandTraceMessage (const char* const fmt, ...)
180 {
181 va_list v_args;
182 char text[4096];
183
184 text[0] = 0;
185 va_start (v_args, fmt);
186 vsprintf(text, fmt, v_args);
187 va_end (v_args);
188
189 OutputDebugStringA (text);
190 }
191
192 #define dTrace(x) \
193 { \
194 dExpandTraceMessage x; \
195 }
196 #else
197 #define dTrace(x)
198 #endif
199 #else
200 #define dTrace(x)
201 #endif
202
203
204 #endif
205
dMax
T dMax(T A, T B)
Definition: dMathDefines.h:87
dAssert
#define dAssert(x)
Definition: dMathDefines.h:66
m_rollYawpitch
Definition: dMathDefines.h:57
dMin
T dMin(T A, T B)
Definition: dMathDefines.h:93
dSort
void dSort(T *const array, int elements, int(*compare)(const T *const A, const T *const B, void *const context), void *const context=NULL)
Definition: dMathDefines.h:105
dFloat
float dFloat
Definition: dMathDefines.h:27
dSwap
void dSwap(T &A, T &B)
Definition: dMathDefines.h:79
dEulerAngleOrder
dEulerAngleOrder
Definition: dMathDefines.h:54
dSign
T dSign(T A)
Definition: dMathDefines.h:73
dFloat64
double dFloat64
Definition: dMathDefines.h:32
m_pitchYawRoll
Definition: dMathDefines.h:56
dClamp
T dClamp(T val, T min, T max)
Definition: dMathDefines.h:99