首页 > 代码库 > 矩阵操作类

矩阵操作类

  1 #ifndef __MAPLE_MATRIX_H__  
  2 #define __MAPLE_MATRIX_H__ 
  3 
  4 #include "maple.h"  
  5 
  6 typedef struct MATRIX{ 
  7     union{  
  8         float m[4][4];
  9         struct{
 10             float _11,_12,_13,_14,  
 11             _21,_22,_23,_24,  
 12             _31,_32,_33,_34,
 13             _41,_42,_43,_44; 
 14 
 15         };
 16         struct{
 17             float a,b,__13,__14,  
 18             c,d,__23,__24,  
 19             __31,__32,__33,__34,
 20             tx,ty,__43,__44;  
 21         };
 22     };  
 23 #ifdef __cplusplus
 24     MAPLE_EXT MATRIX(); 
 25     MAPLE_EXT MATRIX(const float & _11,const float & _12,const float & _13,const float & _14,
 26         const float & _21,const float & _22,const float & _23,const float & _24,  
 27         const float & _31,const float & _32,const float & _33,const float & _34,  
 28         const float & _41,const float & _42,const float & _43,const float & _44);
 29     MAPLE_EXT MATRIX(const MATRIX & matrix);  
 30     MAPLE_EXT float operator () (const unsigned & row,const unsigned & col) const;  
 31     MAPLE_EXT float & operator () (const unsigned & row,const unsigned & col);  
 32     MAPLE_EXT MATRIX operator + (const MATRIX & matrix) const;  
 33     MAPLE_EXT MATRIX operator - (const MATRIX & matrix) const;  
 34     MAPLE_EXT MATRIX operator * (const float n) const;  
 35     MAPLE_EXT MATRIX operator * (const MATRIX & matrix) const;  
 36     MAPLE_EXT void operator = (const MATRIX & matrix);  
 37     MAPLE_EXT void operator += (const MATRIX & matrix);  
 38     MAPLE_EXT void operator -= (const MATRIX & matrix);  
 39     MAPLE_EXT void operator *= (const float n);  
 40     MAPLE_EXT void operator *= (const MATRIX & matrix);  
 41     MAPLE_EXT bool operator == (const MATRIX & matrix) const;  
 42     MAPLE_EXT bool operator != (const MATRIX & matrix) const;    
 43     MAPLE_EXT void concat(const MATRIX &m);
 44     MAPLE_EXT void translate(const float x,const float y,const float z);
 45     MAPLE_EXT void scale(const float sx,const float sy,const float sz); 
 46     MAPLE_EXT void rotate(const float angle);
 47     MAPLE_EXT void identity(); 
 48     MAPLE_EXT void transport(); 
 49     MAPLE_EXT ~MATRIX(); 
 50 #endif //__cplusplus  
 51 }MATRIX;  
 52 
 53 MAPLE_EXT_C void  MatrixIdentity(MATRIX * _p_result);  
 54 MAPLE_EXT_C void MatrixAdd(MATRIX * _p_result,  const MATRIX * _p_m1,  const MATRIX * _p_m2);  
 55 MAPLE_EXT_C void MatrixSubtract(MATRIX * _p_result,const MATRIX * _p_m1, const MATRIX * _p_m2);  
 56 MAPLE_EXT_C void MatrixMultiply(MATRIX * _p_result, const MATRIX * _p_m1, const MATRIX * _p_m2);
 57 MAPLE_EXT_C void MatrixMultiplyEx(void * _p_result,  const void * _p_m1,    const void * _p_m2);
 58 MAPLE_EXT_C void MatrixMultiplyWithN(MATRIX * _p_result, const MATRIX * _p_m1, const float n);  
 59 MAPLE_EXT_C void MatrixTranslate(MATRIX * _p_result, const float x,const float y,const float z);  
 60 MAPLE_EXT_C void MatrixRotateX(MATRIX * _p_result, const float angle);  
 61 MAPLE_EXT_C void MatrixRotateY(MATRIX * _p_result, const float angle); 
 62 MAPLE_EXT_C void MatrixRotateZ(MATRIX * _p_result, const float angle);  
 63 MAPLE_EXT_C void MatrixScaling(MATRIX * _p_result, const float sx,const float sy,const float sz); 
 64 MAPLE_EXT_C void MatrixTransport(MATRIX * _p_result);
 65 
 66 #endif //__MAPLE_MATRIX_H__ 
 67 
 68 
 69 //matrix.cpp
 70 
 71 
 72 #include "matrix.h"
 73 #include <memory.h>
 74 #include <math.h>
 75 #include <string.h>
 76 
 77 MAPLE_EXT_C void MatrixIdentity(MATRIX * _p_result)
 78 {
 79     memset((void*)_p_result->m,0,sizeof(float)*16);
 80     for(int i=0;i<4;i++)
 81         _p_result->m[i][i]=1;
 82 }
 83 
 84 MAPLE_EXT_C void MatrixAdd(MATRIX * _p_result,  const MATRIX * _p_m1,  const MATRIX * _p_m2)
 85 {
 86     for(int j=0;j<4;j++)
 87         for(int i=0;i<4;i++)
 88             _p_result->m[j][i]=(_p_m1->m[j][i]+_p_m2->m[j][i]);
 89 }
 90 
 91 MAPLE_EXT_C void MatrixSubtract(MATRIX * _p_result,  const MATRIX * _p_m1,  const MATRIX * _p_m2)
 92 {
 93     for(int j=0;j<4;j++)
 94         for(int i=0;i<4;i++)
 95             _p_result->m[j][i]=(_p_m1->m[j][i]-_p_m2->m[j][i]);
 96 }
 97 
 98 MAPLE_EXT_C void MatrixMultiply(MATRIX * _p_result,  const MATRIX * _p_m1,    const MATRIX * _p_m2)
 99 {
100     _p_result->m[0][0]=(_p_m1->m[0][0]*_p_m2->m[0][0])+
101         (_p_m1->m[0][1]*_p_m2->m[1][0])+
102         (_p_m1->m[0][2]*_p_m2->m[2][0])+
103         (_p_m1->m[0][3]*_p_m2->m[3][0]);
104     _p_result->m[0][1]=(_p_m1->m[0][0]*_p_m2->m[0][1])+
105         (_p_m1->m[0][1]*_p_m2->m[1][1])+
106         (_p_m1->m[0][2]*_p_m2->m[2][1])+
107         (_p_m1->m[0][3]*_p_m2->m[3][1]);
108     _p_result->m[0][2]=(_p_m1->m[0][0]*_p_m2->m[0][2])+
109         (_p_m1->m[0][1]*_p_m2->m[1][2])+
110         (_p_m1->m[0][2]*_p_m2->m[2][2])+
111         (_p_m1->m[0][3]*_p_m2->m[3][2]);
112     _p_result->m[0][3]=(_p_m1->m[0][0]*_p_m2->m[0][3])+
113         (_p_m1->m[0][1]*_p_m2->m[1][3])+
114         (_p_m1->m[0][2]*_p_m2->m[2][3])+
115         (_p_m1->m[0][3]*_p_m2->m[3][3]);
116     _p_result->m[1][0]=(_p_m1->m[1][0]*_p_m2->m[0][0])+
117         (_p_m1->m[1][1]*_p_m2->m[1][0])+
118         (_p_m1->m[1][2]*_p_m2->m[2][0])+
119         (_p_m1->m[1][3]*_p_m2->m[3][0]);
120     _p_result->m[1][1]=(_p_m1->m[1][0]*_p_m2->m[0][1])+
121         (_p_m1->m[1][1]*_p_m2->m[1][1])+
122         (_p_m1->m[1][2]*_p_m2->m[2][1])+
123         (_p_m1->m[1][3]*_p_m2->m[3][1]);
124     _p_result->m[1][2]=(_p_m1->m[1][0]*_p_m2->m[0][2])+
125         (_p_m1->m[1][1]*_p_m2->m[1][2])+
126         (_p_m1->m[1][2]*_p_m2->m[2][2])+
127         (_p_m1->m[1][3]*_p_m2->m[3][2]);
128     _p_result->m[1][3]=(_p_m1->m[1][0]*_p_m2->m[0][3])+
129         (_p_m1->m[1][1]*_p_m2->m[1][3])+
130         (_p_m1->m[1][2]*_p_m2->m[2][3])+
131         (_p_m1->m[1][3]*_p_m2->m[3][3]);
132     _p_result->m[2][0]=(_p_m1->m[2][0]*_p_m2->m[0][0])+
133         (_p_m1->m[2][1]*_p_m2->m[1][0])+
134         (_p_m1->m[2][2]*_p_m2->m[2][0])+
135         (_p_m1->m[2][3]*_p_m2->m[3][0]);
136     _p_result->m[2][1]=(_p_m1->m[2][0]*_p_m2->m[0][1])+
137         (_p_m1->m[2][1]*_p_m2->m[1][1])+
138         (_p_m1->m[2][2]*_p_m2->m[2][1])+
139         (_p_m1->m[2][3]*_p_m2->m[3][1]);
140     _p_result->m[2][2]=(_p_m1->m[2][0]*_p_m2->m[0][2])+
141         (_p_m1->m[2][1]*_p_m2->m[1][2])+
142         (_p_m1->m[2][2]*_p_m2->m[2][2])+
143         (_p_m1->m[2][3]*_p_m2->m[3][2]);
144     _p_result->m[2][3]=(_p_m1->m[2][0]*_p_m2->m[0][3])+
145         (_p_m1->m[2][1]*_p_m2->m[1][3])+
146         (_p_m1->m[2][2]*_p_m2->m[2][3])+
147         (_p_m1->m[2][3]*_p_m2->m[3][3]);
148     _p_result->m[3][0]=(_p_m1->m[3][0]*_p_m2->m[0][0])+
149         (_p_m1->m[3][1]*_p_m2->m[1][0])+
150         (_p_m1->m[3][2]*_p_m2->m[2][0])+
151         (_p_m1->m[3][3]*_p_m2->m[3][0]);
152     _p_result->m[3][1]=(_p_m1->m[3][0]*_p_m2->m[0][1])+
153         (_p_m1->m[3][1]*_p_m2->m[1][1])+
154         (_p_m1->m[3][2]*_p_m2->m[2][1])+
155         (_p_m1->m[3][3]*_p_m2->m[3][1]);
156     _p_result->m[3][2]=(_p_m1->m[3][0]*_p_m2->m[0][2])+
157         (_p_m1->m[3][1]*_p_m2->m[1][2])+
158         (_p_m1->m[3][2]*_p_m2->m[2][2])+
159         (_p_m1->m[3][3]*_p_m2->m[3][2]);
160     _p_result->m[3][3]=(_p_m1->m[3][0]*_p_m2->m[0][3])+
161         (_p_m1->m[3][1]*_p_m2->m[1][3])+
162         (_p_m1->m[3][2]*_p_m2->m[2][3])+
163         (_p_m1->m[3][3]*_p_m2->m[3][3]);
164 }
165 
166 MAPLE_EXT_C void MatrixMultiplyEx(void * _p_result,  const void * _p_m1,    const void * _p_m2)
167 {
168     MatrixMultiply((MATRIX*)_p_result,(MATRIX*)_p_m1,(MATRIX*)_p_m2 );
169 }
170 
171 MAPLE_EXT_C void MatrixMultiplyWithN(MATRIX * _p_result, const MATRIX * _p_m1,const float n)
172 {
173     for(int j=0;j<4;j++)
174         for(int i=0;i<4;i++)
175             _p_result->m[j][i]=_p_m1->m[j][i]*n;
176 }
177 
178 MAPLE_EXT_C void MatrixTranslate(MATRIX * _p_result, const float x,const float y,const float z)
179 {
180     MatrixIdentity(_p_result);
181     _p_result->m[3][0]=x;
182     _p_result->m[3][1]=y;
183     _p_result->m[3][2]=z;
184 }
185 
186 MAPLE_EXT_C void MatrixRotateX(MATRIX * _p_result,   const float angle)
187 {
188     MatrixIdentity(_p_result);
189     _p_result->m[1][1]=cos(angle);
190     _p_result->m[1][2]=sin(angle);
191     _p_result->m[2][1]=sin(angle)*(-1.0f);
192     _p_result->m[2][2]=cos(angle);
193 }
194 
195 MAPLE_EXT_C void MatrixRotateY(MATRIX * _p_result,  const float angle)
196 {
197     MatrixIdentity(_p_result);
198     _p_result->m[0][0]=cos(angle);
199     _p_result->m[0][2]=sin(angle)*(-1.0f);
200     _p_result->m[2][0]=sin(angle);
201     _p_result->m[2][2]=cos(angle);
202 }
203 
204 MAPLE_EXT_C void MatrixRotateZ(MATRIX * _p_result, const float angle)
205 {
206     MatrixIdentity(_p_result);
207     _p_result->m[0][0]=cos(angle);
208     _p_result->m[0][1]=sin(angle);
209     _p_result->m[1][0]=sin(angle)*(-1.0f);
210     _p_result->m[1][1]=cos(angle);
211 }
212 
213 MAPLE_EXT_C void MatrixScaling(MATRIX * _p_result, const float sx,const float sy,const float sz)
214 {
215     MatrixIdentity(_p_result);
216     _p_result->m[0][0]=sx;
217     _p_result->m[1][1]=sy;
218     _p_result->m[2][2]=sz;
219 }
220 
221 MAPLE_EXT_C void MatrixTransport(MATRIX * _p_result)
222 {
223     float temp;
224     for(int j=0;j<4;j++)
225         for(int i=0;i<=j;i++) {
226             temp = _p_result->m[j][i];
227              _p_result->m[j][i] = _p_result->m[i][j];
228              _p_result->m[i][j] = temp;
229         }
230 }
231 
232 MAPLE_EXT MATRIX::MATRIX(){
233     MatrixIdentity(this);
234 }
235 
236 MAPLE_EXT MATRIX::MATRIX(const float & _11,const float & _12,const float & _13,const float & _14,
237                          const float & _21,const float & _22,const float & _23,const float & _24,
238                          const float & _31,const float & _32,const float & _33,const float & _34,
239                          const float & _41,const float & _42,const float & _43,const float & _44)
240 {
241     m[0][0]=_11;m[0][1]=_12;m[0][2]=_13;m[0][3]=_14;
242     m[1][0]=_21;m[1][1]=_22;m[1][2]=_23;m[1][3]=_24;
243     m[2][0]=_31;m[2][1]=_32;m[2][2]=_33;m[2][3]=_34;
244     m[3][0]=_41;m[3][1]=_42;m[4][2]=_43;m[4][3]=_44;
245 }
246 
247 MAPLE_EXT MATRIX::MATRIX(const MATRIX & matrix)
248 {
249     for(int j=0;j<4;j++)
250         for(int i=0;i<4;i++)
251             m[j][i]=matrix.m[j][i];
252 }
253 
254 MAPLE_EXT float MATRIX::operator () (const unsigned & row,const unsigned & col) const
255 {
256     return m[row][col];
257 }
258 
259 MAPLE_EXT float & MATRIX::operator () (const unsigned & row,const unsigned & col)
260 {
261     return m[row][col];
262 }
263 
264 MAPLE_EXT MATRIX MATRIX::operator + (const MATRIX & matrix) const
265 {
266     MATRIX _m;
267     MatrixAdd(&_m,this,&matrix);
268     return _m;
269 }
270 
271 MAPLE_EXT MATRIX MATRIX::operator - (const MATRIX & matrix) const
272 {
273     MATRIX _m;
274     MatrixSubtract(&_m,this,&matrix);
275     return _m;
276 }
277 
278 MAPLE_EXT MATRIX MATRIX::operator * (const MATRIX & matrix) const
279 {
280     MATRIX _m;
281     MatrixMultiply(&_m,this,&matrix);
282     return _m;
283 }
284 
285 MAPLE_EXT MATRIX MATRIX::operator * (const float n) const
286 {
287     MATRIX _m;
288     MatrixMultiplyWithN(&_m,this,n);
289     return _m;
290 }
291 
292 MAPLE_EXT void MATRIX::operator = (const MATRIX & matrix)
293 {
294     for(int j=0;j<4;j++)
295         for(int i=0;i<4;i++)
296             m[j][i]=matrix.m[j][i];
297 }
298 
299 MAPLE_EXT void MATRIX::operator += (const MATRIX & matrix)
300 {
301     *this=*this+matrix;
302 }
303 
304 MAPLE_EXT void MATRIX::operator -= (const MATRIX & matrix){
305     *this=*this-matrix;
306 }
307 
308 MAPLE_EXT void MATRIX::operator *= (const MATRIX & matrix)
309 {
310     *this=*this*matrix;
311 }
312 
313 MAPLE_EXT void MATRIX::operator *= (const float n)
314 {
315     *this=*this*n;
316 }
317 
318 MAPLE_EXT bool MATRIX::operator != (const MATRIX & matrix) const
319 {
320     for(int j=0;j<4;j++){
321         for(int i=0;i<4;i++){
322             if(m[j][i]!=matrix.m[j][i])
323                 return true;
324         }
325     }
326     return false;
327 }
328 
329 MAPLE_EXT bool MATRIX::operator == (const MATRIX & matrix) const
330 {
331     for(int j=0;j<4;j++){
332         for(int i=0;i<4;i++){
333             if(m[j][i]!=matrix.m[j][i])
334                 return false;
335         }
336     }
337     return true;
338 }
339 
340 MAPLE_EXT void MATRIX::concat(const MATRIX &m)
341 {
342     MATRIX temp(*this);
343     MatrixMultiply(this,&temp,&m);
344     //MatrixMultiply(this,&m,&temp);
345 }
346 
347 MAPLE_EXT void MATRIX::translate(const float x,const float y,const float z)
348 {
349     MatrixTranslate(this, x, y, z);
350 }
351 MAPLE_EXT void MATRIX::scale(const float sx,const float sy,const float sz)
352 {
353     MatrixScaling(this, sx, sy, sz);
354 }
355 MAPLE_EXT void MATRIX::rotate(const float angle)
356 {
357     MatrixRotateZ(this, angle);
358 }
359 
360 MAPLE_EXT void MATRIX::identity()
361 {
362     MatrixIdentity(this);
363 }
364 MAPLE_EXT void MATRIX::transport()
365 {
366     MatrixTransport(this);
367 }
368 
369 MAPLE_EXT MATRIX::~MATRIX()
370 {
371 }