Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
Frm_3DMath.h
Go to the documentation of this file.
1 // Frm_3DMath.h
3 // Declares vector, matrix, rectangles and defines special
4 // array elements
6 
7 #ifndef FRM_MATH_H
8 #define FRM_MATH_H
9 
10 #ifndef _MSC_VER
11 #include <mem.h>
12 #endif
13 
14 #include <math.h>
15 
16 #define MINFLOAT 1.17549435E-38F
17 #ifndef M_PI
18 #define M_PI 3.14159265358979323846
19 #endif
20 
21 namespace Frm {
22 
23 //Vector description/////////////////////////
24 template <typename T>
25 class vector {
26  public:
27  T x, y, z;
28  vector(void): x(0), y(0), z(0) {};
29  vector(T px, T py, T pz): x(px), y(py), z(pz) {};
30  vector(const vector<T> &pv):x (pv.x), y(pv.y), z(pv.z){};
31  vector(const T* &pv){x = pv[0]; y = pv[1]; z = pv[2];};
32 
34  {x = pv.x; y = pv.y; z = pv.z; return *this;};
35  inline void Set(T px, T py, T pz){x=px; y=py; z=pz;};
36  bool operator>(const vector<T> &pv){return (x+y)>(pv.x+pv.y);};
37 
38  T operator[](int index)const{if (index== 1) return y; if (index== 2)return z; return x;};
39  T& operator[](int index){if (index== 1) return y; if (index== 2)return z; return x;};
40 
41  vector<T> operator+(vector<T> pv)const {return vector<T>((T)(x + pv.x), (T)(y + pv.y), (T)(z + pv.z));};
42  vector<T> operator-(vector<T>& pv)const{return vector<T>((T)(x - pv.x), (T)(y - pv.y),(T)(z - pv.z));};
43  vector<T> operator*(T pT)const{return vector<T>((T)(pT * x), (T)(pT * y), (T)(pT * z));};
44 
45  void Normalize(void){vector<T> pv((T)(x*x),(T)(y*y), (T)(z*z));T fLength = (T)(1.0f/(float)(pv.x + pv.y + pv.z)); if (fLength < 1e-08) return; x = (T)(pv.x * fLength); y = (T)(pv.y * fLength); z = (T)(pv.z * fLength);};
46  T Dot(vector<T> pV)const{return (T)(x*pV.x + y*pV.y + z*pV.z);};
47  vector<T> Cross(vector<T> pV)const{return vector<T>((T)(y * pV.z - z * pV.y), (T)(z * pV.x - x * pV.z), (T)(x * pV.y - y * pV.x));};
48  vector<T> UnitCross(vector<T> pV)const{vector<T> pR((T)(y * pV.z - z * pV.y), (T)(z * pV.x - x * pV.z), (T)(x * pV.y - y * pV.x)); pR.Normalize(); return pR;};
49 };
50 
52 
53 template<typename T, int size>
54 struct Array {
55  T data[size];
56  Array(void){memset(data, 0, size * sizeof(T));};
57  Array(const Array<T, size> &pA){memcpy(data, pA.data, size * sizeof(T));};
58  Array(const T* pT){memcpy(data, pT, size * sizeof(T));};
59 
61  {memcpy(data, pA.data, size * sizeof(T)); return *this;};
63  Array<T, size> operator*(const T pScalar);
66  T operator[](int index)const{return data[index];};
67  T& operator[](int index){return data[index];};
68 };
69 
70 typedef Array<unsigned short, 3> Face;
71 typedef Array<float, 3> Vertex;
72 typedef Array<float, 2> TCoord;
73 typedef Array<float, 4> Color4;
74 typedef Array<float, 3> Color3;
75 
76 template<typename T>
77 Array<T, 4> Set4(T px, T py, T pz, T pw){T data[4]; data[0]=px; data[1]=py; data[2]=pz; data[3]=pw;return Array<T, 4>(data);};
78 
79 template<typename T>
80 Array<T, 3> Set3(T px, T py, T pz){T data[3]; data[0]=px; data[1]=py; data[2]=pz;return Array<T, 3>(data);};
81 
82 template<typename T>
83 Array<T, 2> Set2(T pu, T pv){T data[2]; data[0]=pu; data[1]=pv;return Array<T, 2>(data);};
84 
85 template <typename T>
86 class matrix {
87  public:
88  T data[16];
89  matrix(void){memset(data, 0, 16 * sizeof(T));};
90  matrix(const matrix<T> &pm){memcpy(data, pm.data, 16 * sizeof(T));};
91  matrix(const T* pT){memcpy(data, pT, 16 * sizeof(T));};
92  void Zero(void){memset(data, 0, 16 * sizeof(T));};
93  void Identity(void){memset(data, 0, 16 * sizeof(T)); data[0]=(T)1; data[5]=(T)1; data[10]=(T)1; data[15]=(T)1;};
94  matrix<T>& operator=(const matrix<T> &pm){memcpy(data, pm.data, 16 * sizeof(T));return *this;};
102 
103  T operator[](int index)const{return data[index];};
104  T& operator[](int index){return data[index];};
105 
106  T _fastcall Index(int pRow, int pCol)const{return data[(pRow<<2) + pCol];};
107  T operator()(int pRow, int pCol)const{return data[(pRow<<2) + pCol];};
108  T& operator()(int pRow, int pCol){return data[(pRow<<2) + pCol];};
109  vector<T> GetRow(int index){index <<= 2;return vector<T>(data[index++], data[index++], data[index]);};
111  void ScalingMatrix(const vector<T> &pvT);
112  void TranslationMatrix(const Array<T,3> &pAT);
113  void QuaternionMatrix(T &x, T &y, T &z, T &w);
114 
115  private:
116  int k, l, row, col;
117 };
118 
120 //typedef matrix<float> Matrix4F;
121 
122 template <typename T>
123 class quaternion {
124  public:
125  T data[4];
126  quaternion(void){memset(data, 0, 4 * sizeof(T));};
127  quaternion(const quaternion<T> &pq){memcpy(data, pq.data, 4 * sizeof(T));};
128  quaternion(const T* pT){memcpy(data, pT, 4 * sizeof(T));};
129  void Zero(void){memset(data, 0, 4 * sizeof(T));};
130  T operator[](int index)const{return data[index];};
131  T& operator[](int index){return data[index];};
132  quaternion<T>& operator=(const quaternion<T> &pq){memcpy(data, pq.data, 4 * sizeof(T));return *this;};
138  private:
139  float qACos(float pValue);
140 };
141 
143 
144 //Rectangle description///////////////////////
145 template <typename T>
146 class trect {
147  public:
148  T xs, ys, xe, ye;
149  trect(void): xs(0), ys(0), xe(0), ye(0){};
150  trect(const trect<T> &prect)
151  {xs = prect.xs; ys = prect.ys; xe = prect.xe; ye = prect.ye;};
152  trect<T>& operator=(const trect<T> &prect)
153  {xs = prect.xs; ys = prect.ys; xe = prect.xe; ye = prect.ye; return *this;};
155  {xs += pV.x; ys += pV.y; xe += pV.x; ye += pV.y; return *this;};
156 /* trect<T>& operator+=(vector<T> &pV)
157  {xs += pV[VX]; ys += pV[VY]; xe += pV[VX]; ye += pV[VY]; return *this;};/**/
158  trect(const T pxs, const T pys, const T pxe, const T pye)
159  {xs = pxs; ys = pys; xe = pxe; ye = pye;};
160  inline void Set(const T pxs, const T pys, const T pxe, const T pye)
161  {xs = pxs; ys = pys; xe = pxe; ye = pye;};
162  bool _fastcall InRect(const T &px, const T &py)
163  { if (px < xs) return false;
164  if (px > xe) return false;
165  if (py < ys) return false;
166  if (py > ye) return false;
167  return true;};
168  bool _fastcall InRect(const vector<T> &pv)
169  { if (pv.x < xs) return false;
170  if (pv.x > xe) return false;
171  if (pv.y < ys) return false;
172  if (pv.y > ye) return false;
173  return true;};
174 /* bool _fastcall InRect( vector<T> &pv)
175  { if (pv[VX] < xs) return false;
176  if (pv[VX] > xe) return false;
177  if (pv[VY] < ys) return false;
178  if (pv[VY] > ye) return false;
179  return true;};/**/
180  bool _fastcall InRect(const trect<T> &prect)
181  { if ((prect.xe <= xe) &&
182  (prect.ye <= ye) &&
183  (prect.xs >= xs) &&
184  (prect.ys >= ys))
185  return true;
186  return false; }
187  bool _fastcall Intersect(const trect<T> &prect)
188  { if (prect.xe < xs) return false;
189  if (prect.xs > xe) return false;
190  if (prect.ye < ys) return false;
191  if (prect.ys > ye) return false;
192  return true;};
193  bool _fastcall Union(const trect<T> &prect)
194  { if (!Intersect(prect)) return false;
195  if (xs < prect.xs) xs = prect.xs;
196  if (ys < prect.ys) ys = prect.ys;
197  if (xe > prect.xe) xe = prect.xe;
198  if (ye > prect.ye) ye = prect.ye;
199  return true;};
200 };
201 
203 
204 template<typename T, int size>
206 {
207  for (int i= 0; i< size; i++)
208  data[i] += pA.data[i];
209  return *this;
210 }
211 
212 template<typename T, int size>
214 {
215  T rdata[size];
216  memcpy(rdata, data, size * sizeof(T));
217  for (int i= 0; i< size; i++)
218  rdata[i]*=pScalar;
219  return Array<T,size>(rdata);
220 }
221 
222 template<typename T, int size>
224 {
225  T rdata[size];
226  for (int i= 0; i< size; i++)
227  rdata[i]=data[i] + pA.data[i];
228  return Array<T,size>(rdata);
229 };
230 
231 template<typename T, int size>
233 {
234  T rdata[size];
235  for (int i= 0; i< size; i++)
236  rdata[i]=data[i] - pA.data[i];
237  return Array<T,size>(rdata);
238 };
239 
240 template<typename T>
242 {
243  T Rdata[16];
244  for(k = 0; k < 16; k++)
245  Rdata[k] = data[k] + pm.data[k];
246  return matrix<T>(Rdata);
247 }
248 
249 template<typename T>
251 {
252  T Rdata[16];
253  for(k = 0; k < 16; k++)
254  Rdata[k] = data[k] - pm.data[k];
255  return matrix<T>(Rdata);
256 }
257 
258 template<typename T>
260 {
261  T Rdata[16];
262  for (row = 0; row < 16; row +=4)
263  for (col = 0; col < 4; col ++)
264  {
265  l = 0;
266  Rdata[row + col] = 0;
267  for (k = 0; k < 4; k++, l += 4)
268  Rdata[row + col] += data[row + k] * pm.data[l + col];
269  }
270  return matrix<T>(Rdata);
271 }
272 
273 template<typename T>
275 {
276  T Rdata[16];
277  for (row = 0; row < 16; row +=4)
278  for (col = 0; col < 4; col ++)
279  {
280  l = 0;
281  Rdata[row + col] = 0;
282  for (k = 0; k < 4; k++, l += 4)
283  Rdata[row + col] += data[row + k] * pm.data[l + col];
284  }
285  memcpy(data, Rdata, 16*sizeof(T));
286  return *this;
287 }
288 
289 template<typename T>
290 matrix<T> matrix<T>::operator*(T pT)
291 {
292  T Rdata[16];
293  memcpy(Rdata, data, 16 * sizeof(T));
294  for(k = 0; k < 16; k++)
295  Rdata[k] *= pT;
296  return matrix<T>(Rdata);
297 }
298 
299 template<typename T>
300 vector<T> matrix<T>::operator*(const vector<T>& pV)
301 {
302  T vdata[4], pvdata[4];
303  pvdata[0] = pV.x;
304  pvdata[1] = pV.y;
305  pvdata[2] = pV.z;
306  pvdata[3] = 0;
307  for (col = 0; col < 4; col++)
308  {
309  vdata[row] = 0;
310  k=0;
311  for (row = 0; row < 4; row++, k+=4)
312  vdata[col] += data[k + col]*pvdata[row];
313  }
314  return vector<T>(vdata);
315 }
316 
317 template<typename T>
318 Array<T, 3> matrix<T>::operator*(const Array<T, 3>& pV)
319 {
320  T vdata[4], pvdata[4];
321 
322  memcpy(pvdata, pV.data, 3*sizeof(T));
323  pvdata[3] = 1.0f;
324 
325  for (col = 0; col < 4; col++)
326  {
327  k = 0;
328  vdata[col] = 0;
329  for (row = 0; row < 4; row++, k+=4)
330  vdata[col] += data[k + col]*pvdata[row];
331  }
332 
333  return Array<T, 3>(vdata);
334 }
335 
336 template<typename T>
338 {
339  Identity();
340  data[0] = pvT.x;
341  data[5] = pvT.y;
342  data[10] = pvT.z;
343 }
344 
345 template<typename T>
347 {
348  Identity();
349  data[12] = pAT[0];
350  data[13] = pAT[1];
351  data[14] = pAT[2];
352 }
353 
354 template<typename T>
355 void matrix<T>::QuaternionMatrix(T &x, T &y, T &z, T &w)
356 {
357  T xx = x*x; T yy = y*y; T zz = z*z;
358  T xy = x*y; T xz = x*z; T yz = y*z;
359  T wx = w*x; T wy = w*y; T wz = w*z;
360 
361  data[0] = 1 - 2 * ( yy + zz );
362  data[1] = 2 * ( xy - wz );
363  data[2] = 2 * ( xz + wy );
364 
365  data[4] = 2 * ( xy + wz );
366  data[5] = 1 - 2 * ( xx + zz );
367  data[6] = 2 * ( yz - wx );
368 
369  data[8] = 2 * ( xz - wy );
370  data[9] = 2 * ( yz + wx );
371  data[10] = 1 - 2 * ( xx + yy );
372 
373  data[3] = data[7] = data[11] = 0.0f;
374  data[12] = data[13] = data[14] = 0.0f;
375  data[15] = 1.0f;
376 }
377 
378 /*************************************************
379 NEW- NEW- NEW- NEW- NEW- NEW- NEW- NEW- NEW- NEW*/
380 
381 template<typename T>
382 float quaternion<T>::qACos(float pValue)
383 {
384  if ( -1.0f < pValue )
385  {
386  if ( pValue < 1.0f )
387  return (float)acos(pValue);
388  else
389  return 0.0f;
390  }
391  else
392  return M_PI;
393 }
394 
395 template<typename T>
397 {
398  T rdata[4];
399  rdata[0] = pq.data[0] + data[0];
400  rdata[1] = pq.data[1] + data[1];
401  rdata[2] = pq.data[2] + data[2];
402  rdata[3] = pq.data[3] + data[3];
403  return quaternion<T>(rdata);
404 }
405 
406 template<typename T>
408 {
409  T rdata[4];
410  rdata[0] = pT * data[0];
411  rdata[1] = pT * data[1];
412  rdata[2] = pT * data[2];
413  rdata[3] = pT * data[3];
414  return quaternion<T>(rdata);
415 }
416 
417 template<typename T>
419 {
420  return ((data[0] * pq.data[0]) + (data[1] * pq.data[1]) + (data[2] * pq.data[2]) + (data[3] * pq.data[3]));
421 }
422 
423 template<typename T>
425 {
426  //We calculate the angle spread between both quaternions
427  T AngleCos = pq.Dot(*this);
428  T Angle = qACos(AngleCos); //see the function ACos above
429 
430  if (Angle < MINFLOAT)
431  return quaternion<T>(*this);
432  //We calculate the interpolated angle and deduce the resulting quaternion
433  T InvAngleSin = (T)(1.0f / sin(Angle));
434 
435  T Coeff0 = sin((1-pT) * Angle) * InvAngleSin;
436  T Coeff1 = sin(pT * Angle) * InvAngleSin;
437  return quaternion<T>((*this * Coeff0)+(pq * Coeff1));
438 }
439 
440 /*************************************************/
441 
442 };
443 #endif
Using X files without the sources and the makefile How to use you just create a debug directory e the sample3 directory must contain Sample3 Final Sample3 exe Sample3 Final Debug Sample3 Final Gfx OpenGL bmp Sample3 Final Gfx tiny_skin bmp Sample3 Final Gfx tiny_4anim x The source files have the DevCpp project file plus the makefile The demos use standard FreeGlut functions Technical without warranty Neither Paul Coppens nor GameDev net make any or either express or with respect to the their or fitness for a specific purpose neither Paul Coppens nor GameDev net shall have any liability to you or any other person or entity with respect to any or damage caused or alleged to have been caused directly or indirectly by the programs provided by Paul Coppens and GameDev net This but is not limited interruption of loss of data
Definition: Readme.txt:39
Definition: Frm_3DMath.h:79
void QuaternionMatrix(T &x, T &y, T &z, T &w)
Definition: Frm_3DMath.h:355
T & operator[](int index)
Definition: Frm_3DMath.h:104
vector< T > GetRow(int index)
Definition: Frm_3DMath.h:109
void Zero(void)
Definition: Frm_3DMath.h:92
matrix< T > operator*(matrix< T > &pm)
Definition: Frm_3DMath.h:218
matrix< T > & operator*=(matrix< T > &pm)
matrix(const T *pT)
Definition: Frm_3DMath.h:91
void Identity(void)
Definition: Frm_3DMath.h:93
matrix(const matrix< T > &pm)
Definition: Frm_3DMath.h:90
matrix< T > operator-(matrix< T > &pm)
Definition: Frm_3DMath.h:209
matrix< T > & operator*=(matrix< T > &pm)
Definition: Frm_3DMath.h:233
T data[16]
Definition: Frm_3DMath.h:81
matrix< T > operator+(matrix< T > pm)
Definition: Frm_3DMath.h:241
Array< T, 3 > operator*(const Array< T, 3 > &pV)
matrix< T > & operator=(const matrix< T > &pm)
Definition: Frm_3DMath.h:94
vector< T > GetColumn(int index)
Definition: Frm_3DMath.h:110
matrix< T > operator+(matrix< T > &pm)
Definition: Frm_3DMath.h:200
matrix< T > operator*(matrix< T > pm)
Definition: Frm_3DMath.h:259
T operator()(int pRow, int pCol) const
Definition: Frm_3DMath.h:107
T _fastcall Index(int pRow, int pCol) const
Definition: Frm_3DMath.h:106
matrix(void)
Definition: Frm_3DMath.h:89
matrix< T > operator-(matrix< T > pm)
Definition: Frm_3DMath.h:250
vector< T > operator*(const vector< T > &pV)
T operator[](int index) const
Definition: Frm_3DMath.h:103
T & operator()(int pRow, int pCol)
Definition: Frm_3DMath.h:108
void ScalingMatrix(const vector< T > &pvT)
Definition: Frm_3DMath.h:337
matrix< T > operator*(T pT)
void TranslationMatrix(const Array< T, 3 > &pAT)
Definition: Frm_3DMath.h:346
Definition: Frm_3DMath.h:123
void Zero(void)
Definition: Frm_3DMath.h:129
T data[4]
Definition: Frm_3DMath.h:125
quaternion< T > & operator=(const quaternion< T > &pq)
Definition: Frm_3DMath.h:132
quaternion< T > operator*(T pT)
Definition: Frm_3DMath.h:407
quaternion(const T *pT)
Definition: Frm_3DMath.h:128
quaternion< T > operator*(quaternion< T > pq)
T & operator[](int index)
Definition: Frm_3DMath.h:131
quaternion(const quaternion< T > &pq)
Definition: Frm_3DMath.h:127
quaternion< T > Slerp(T pT, quaternion< T > &pq)
Definition: Frm_3DMath.h:424
T Dot(quaternion< T > &pq)
Definition: Frm_3DMath.h:418
quaternion< T > operator+(quaternion< T > pq)
Definition: Frm_3DMath.h:396
quaternion(void)
Definition: Frm_3DMath.h:126
T operator[](int index) const
Definition: Frm_3DMath.h:130
Definition: Frm_3DMath.h:114
bool _fastcall Union(const trect< T > &prect)
Definition: Frm_3DMath.h:193
trect< T > & operator=(const trect< T > &prect)
Definition: Frm_3DMath.h:152
bool _fastcall InRect(const trect< T > &prect)
Definition: Frm_3DMath.h:180
trect(const T pxs, const T pys, const T pxe, const T pye)
Definition: Frm_3DMath.h:158
trect(void)
Definition: Frm_3DMath.h:149
T ys
Definition: Frm_3DMath.h:116
bool _fastcall InRect(const T &px, const T &py)
Definition: Frm_3DMath.h:162
bool _fastcall InRect(const vector< T > &pv)
Definition: Frm_3DMath.h:168
trect< T > & operator+=(const vector< T > &pV)
Definition: Frm_3DMath.h:154
trect(const trect< T > &prect)
Definition: Frm_3DMath.h:150
bool _fastcall Intersect(const trect< T > &prect)
Definition: Frm_3DMath.h:187
T xs
Definition: Frm_3DMath.h:116
T xe
Definition: Frm_3DMath.h:116
T ye
Definition: Frm_3DMath.h:116
void Set(const T pxs, const T pys, const T pxe, const T pye)
Definition: Frm_3DMath.h:160
Definition: Frm_3DMath.h:16
vector< T > & operator=(const vector< T > &pv)
Definition: Frm_3DMath.h:33
T y
Definition: Frm_3DMath.h:18
T & operator[](int index)
Definition: Frm_3DMath.h:39
vector< T > operator*(T pT) const
Definition: Frm_3DMath.h:43
vector< T > Cross(vector< T > pV) const
Definition: Frm_3DMath.h:47
vector< T > operator-(vector< T > &pv) const
Definition: Frm_3DMath.h:42
bool operator>(const vector< T > &pv)
Definition: Frm_3DMath.h:36
vector(T px, T py, T pz)
Definition: Frm_3DMath.h:29
void Set(T px, T py, T pz)
Definition: Frm_3DMath.h:35
vector< T > operator+(vector< T > pv) const
Definition: Frm_3DMath.h:41
T x
Definition: Frm_3DMath.h:18
vector(const T *&pv)
Definition: Frm_3DMath.h:31
vector(const vector< T > &pv)
Definition: Frm_3DMath.h:30
vector(void)
Definition: Frm_3DMath.h:28
T operator[](int index) const
Definition: Frm_3DMath.h:38
vector< T > UnitCross(vector< T > pV) const
Definition: Frm_3DMath.h:48
T Dot(vector< T > pV) const
Definition: Frm_3DMath.h:46
void Normalize(void)
Definition: Frm_3DMath.h:45
T z
Definition: Frm_3DMath.h:18
Definition: Frm.h:12
Array< T, 2 > Set2(T pu, T pv)
Definition: Frm_3DMath.h:42
Array< T, 3 > Set3(T px, T py, T pz)
Definition: Frm_3DMath.h:39
Array< float, 3 > Vertex
Definition: Frm_3DMath.h:30
Array< float, 4 > Color4
Definition: Frm_3DMath.h:32
Array< T, 4 > Set4(T px, T py, T pz, T pw)
Definition: Frm_3DMath.h:36
Array< float, 2 > TCoord
Definition: Frm_3DMath.h:31
matrix< float > Matrix
Definition: Frm_3DMath.h:119
Array< float, 3 > Color3
Definition: Frm_3DMath.h:33
quaternion< float > Quaternion
Definition: Frm_3DMath.h:142
Array< unsigned short, 3 > Face
Definition: Frm_3DMath.h:58
vector< float > Vector
Definition: Frm_3DMath.h:51
#define MINFLOAT
Definition: Frm_3DMath.h:16
#define M_PI
Definition: Frm_3DMath.h:18
uint16 index
Definition: sample3.cpp:29
Definition: Frm_3DMath.h:15
Array(const T *pT)
Definition: Frm_3DMath.h:58
Array< T, size > operator+(Array< T, size > &pA)
Definition: Frm_3DMath.h:65
Array(void)
Definition: Frm_3DMath.h:56
Array< T, size > & operator=(Array< T, size > pA)
Definition: Frm_3DMath.h:60
Array< T, size > operator*(const T pScalar)
Definition: Frm_3DMath.h:181
Array< T, size > operator+(Array< T, size > pA)
Definition: Frm_3DMath.h:223
Array< T, size > & operator+=(Array< T, size > pA)
Definition: Frm_3DMath.h:205
T data[size]
Definition: Frm_3DMath.h:16
Array< T, size > operator*(const T pScalar)
T & operator[](int index)
Definition: Frm_3DMath.h:67
T operator[](int index) const
Definition: Frm_3DMath.h:66
Array(const Array< T, size > &pA)
Definition: Frm_3DMath.h:57
Array< T, size > operator-(Array< T, size > pA)
Definition: Frm_3DMath.h:232
Array< T, size > & operator+=(Array< T, size > &pA)
Definition: Frm_3DMath.h:47