Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Beta/Math/Vector/NotDone/jMatrix.hpp

Go to the documentation of this file.
00001 /*
00002 The class jMatrix is a 4x4 matrix, and can deal with some of the operations that can be performed on a matrix. The class was designed with the intention of it's being used for transforming points in 3d space. Any object can be represented by enough points, therefore, It can be used to transform any 3d object. Lower dimentions should work also, but if it called often, will probably have a heavy cost.
00003 
00004 All functions that are specific to 3d transformation of points are held in a Seperate class, jMatrixStack.
00005 
00006 FIXME:
00007   check over jMatrixStack, something may be fishy... not intuitive
00008 
00009   make matrix class designed for 2d space
00010   make stack for 2d matrix
00011   come up with better names
00012   seperate the stacks for the matrices... -easy, just make a new file
00013   possibly make variable sized matrix, probably only usefull to math
00014 */
00015 
00016 #ifndef __jmatrix_h_
00017 #define __jmatrix_h_
00018 
00019 #include <vector>
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include <joe/vect3f.h>
00023 
00024 using namespace std;
00025 
00026 const double pi =  acos(-1);
00027 
00028 //////////////////////////////////////////////////////////////
00029 //*********************************************************///
00030 //////////////////////////////////////////////////////////////
00031 
00032 class jMatrix
00033 {
00034  public:
00035   // constructors
00036   jMatrix() {}
00037   jMatrix(const jMatrix& obj);
00038   ~jMatrix() {}
00039   
00040   // variables
00041   float m[4][4];
00042   
00043   // functions
00044   void enter(  float m00, float m01, float m02, float m03,
00045       float m10, float m11, float m12, float m13,
00046       float m20, float m21, float m22, float m23,
00047       float m30, float m31, float m32, float m33);
00048   
00049   void identity();
00050   void clear();
00051   void printSelf();
00052   
00053   void add(jMatrix& add);
00054   void subtract(jMatrix& sub);
00055   void mult(jMatrix mult);
00056   void negative();
00057   
00058   void tform(vect3f& point);
00059   void tform(vect3f* point);
00060   
00061   // operators
00062   jMatrix operator+(jMatrix&);
00063   jMatrix operator-(jMatrix&);
00064   jMatrix operator-();
00065   jMatrix operator*(jMatrix&);
00066   
00067   void operator+=(jMatrix&);
00068   void operator-=(jMatrix&);
00069   void operator*=(jMatrix&);
00070 };
00071 
00072 
00073 jMatrix::jMatrix(const jMatrix& obj)
00074 {
00075   for (int i = 0; i < 4; i++){
00076     for (int j = 0; j < 4; j++){
00077       m[i][j] = obj.m[i][j];
00078     }
00079   }
00080 }
00081 
00082 //---------------------------------------------------------//
00083 
00084 void jMatrix::enter(float m00, float m01, float m02, float m03,
00085           float m10, float m11, float m12, float m13,
00086           float m20, float m21, float m22, float m23,
00087           float m30, float m31, float m32, float m33)
00088 {
00089   m[0][0] = m00;
00090   m[0][1] = m01;
00091   m[0][2] = m02;
00092   m[0][3] = m03;
00093   
00094   m[1][0] = m10;
00095   m[1][1] = m11;
00096   m[1][2] = m12;
00097   m[1][3] = m13;
00098   
00099   m[2][0] = m20;
00100   m[2][1] = m21;
00101   m[2][2] = m22;
00102   m[2][3] = m23;
00103   
00104   m[3][0] = m30;
00105   m[3][1] = m31;
00106   m[3][2] = m32;
00107   m[3][3] = m33;
00108 }
00109 
00110 //---------------------------------------------------------//
00111 
00112 void jMatrix::identity()
00113 {
00114   int i, j;
00115   for (i = 0; i < 4; i++){
00116     for (j = 0; j < 4; j++){
00117       if (i == j)
00118    m[i][j] = 1;
00119       else
00120    m[i][j] = 0;
00121     }
00122   }
00123 }
00124 
00125 //---------------------------------------------------------//
00126 
00127 void jMatrix::clear()
00128 {
00129   int i, j;
00130   for (i = 0; i < 4; i++){
00131     for (j = 0; j < 4; j++)
00132       m[i][j] = 0;
00133   }
00134 }
00135 
00136 //---------------------------------------------------------//
00137 
00138 void jMatrix::printSelf()
00139 {
00140   printf("|  %-+7.2f %-+7.2f %-+7.2f %-+7.2f|\n", m[0][0], m[0][1], m[0][2], m[0][3]);
00141   printf("|  %-+7.2f %-+7.2f %-+7.2f %-+7.2f|\n", m[1][0], m[1][1], m[1][2], m[1][3]);
00142   printf("|  %-+7.2f %-+7.2f %-+7.2f %-+7.2f|\n", m[2][0], m[2][1], m[2][2], m[2][3]);
00143   printf("|  %-+7.2f %-+7.2f %-+7.2f %-+7.2f|\n\n", m[3][0], m[3][1], m[3][2], m[3][3]);
00144 }
00145 
00146 //---------------------------------------------------------//
00147 
00148 void jMatrix::add(jMatrix& add)
00149 {
00150   int i, j;
00151   for (i = 0; i < 4; i++){
00152     for (j = 0; j < 4; j++){
00153       m[i][j] += add.m[i][j];
00154     }
00155   }
00156 }
00157 
00158 //---------------------------------------------------------//
00159 
00160 void jMatrix::subtract(jMatrix& sub)
00161 {
00162   int i, j;
00163   for (i = 0; i < 4; i++){
00164     for (j = 0; j < 4; j++){
00165       m[i][j] -= sub.m[i][j];
00166     }
00167   }
00168 }
00169 
00170 //---------------------------------------------------------//
00171 
00172 void jMatrix::mult(jMatrix mult)
00173 {
00174   jMatrix tmp(*this);
00175   clear();
00176   
00177   int i, j, k;
00178   for (i = 0; i < 4; i++){
00179     for (j = 0; j < 4; j++){
00180       
00181       for (k = 0; k < 4; k++)
00182    m[i][j] += tmp.m[i][k] * mult.m[k][j];
00183       
00184     }
00185   }
00186 }
00187 
00188 //---------------------------------------------------------//
00189 
00190 void jMatrix::negative()
00191 {
00192   int i, j;
00193   for (i = 0; i < 4; i++){
00194     for (j = 0; j < 4; j++){
00195       m[i][j] = -m[i][j];
00196     }
00197   }
00198 }
00199 
00200 //---------------------------------------------------------//
00201 
00202 void jMatrix::tform(vect3f& point)
00203 {
00204   float w;
00205   vect3f tmp(point);
00206   
00207   w = m[3][0] * tmp[0] + m[3][1] * tmp[1] + m[3][2] * tmp[2] + m[3][3];
00208   
00209   for (int i = 0; i < 3; i++)
00210     point[i] = (m[i][0] * tmp[0] + m[i][1] * tmp[1] + m[i][2] * tmp[2] + m[i][3]) / w;
00211   
00212 }
00213 
00214 //---------------------------------------------------------//
00215 
00216 void jMatrix::tform(vect3f* point)
00217 {
00218   float w;
00219   vect3f tmp(*point);
00220   
00221   w = m[3][0] * tmp[0] + m[3][1] * tmp[1] + m[3][2] * tmp[2] + m[3][3];
00222   
00223   for (int i = 0; i < 3; i++)
00224     point->v[i] = (m[i][0] * tmp[0] + m[i][1] * tmp[1] + m[i][2] * tmp[2] + m[i][3]) / w;
00225   
00226 }
00227 
00228 //---------------------------------------------------------//
00229 
00230 jMatrix jMatrix::operator+(jMatrix& obj)
00231 {
00232   jMatrix tmp(*this);
00233   tmp.add(obj);
00234   return (tmp);
00235 }
00236 
00237 //---------------------------------------------------------//
00238 
00239 jMatrix jMatrix::operator-(jMatrix& obj)
00240 {
00241   jMatrix tmp(*this);
00242   tmp.subtract(obj);
00243   return (tmp);
00244 }
00245 
00246 //---------------------------------------------------------//
00247 
00248 jMatrix jMatrix::operator-()
00249 {
00250   jMatrix tmp(*this);
00251   tmp.negative();
00252   return (tmp);
00253 }
00254 
00255 //---------------------------------------------------------//
00256 
00257 jMatrix jMatrix::operator*(jMatrix& obj)
00258 {
00259   jMatrix tmp(*this);
00260   tmp.mult(obj);
00261   return (tmp);
00262 }
00263 
00264 //---------------------------------------------------------//
00265 
00266 void jMatrix::operator+=(jMatrix& obj)
00267 {
00268   add(obj);
00269 }
00270 
00271 //---------------------------------------------------------//
00272 
00273 void jMatrix::operator-=(jMatrix& obj)
00274 {
00275   subtract(obj);
00276 }
00277 
00278 //---------------------------------------------------------//
00279 
00280 void jMatrix::operator*=(jMatrix& obj)
00281 {
00282   mult(obj);
00283 }
00284 
00285 
00286 //////////////////////////////////////////////////////////////
00287 //*********************************************************///
00288 //////////////////////////////////////////////////////////////
00289 
00290 class jMatrixStack
00291 {
00292  protected:
00293   vector<jMatrix> stk;
00294   vector<jMatrix>::iterator iter;
00295   jMatrix mult;
00296   
00297  public:
00298   
00299   // constructors
00300   jMatrixStack();
00301   
00302   // push/pop
00303   void push();
00304   void pop();
00305   
00306   // fairly direct handling
00307   jMatrix getTop() { return *iter; }
00308   void setTop(jMatrix obj) { *iter = obj; }
00309   
00310   // less direct handling
00311   void tform(vect3f& point);
00312   void tform(vect3f* point);
00313   
00314   void trans(float x, float y, float z);
00315   
00316   void rotxrad(float angle);
00317   void rotxdeg(float angle);
00318   
00319   void rotyrad(float angle);
00320   void rotydeg(float angle);
00321   
00322   void rotzrad(float angle);
00323   void rotzdeg(float angle);
00324   
00325   void rotallrad(float x, float y, float z);
00326   void rotalldeg(float x, float y, float z);
00327   
00328   void scale(float x, float y, float z);
00329   
00330 } mstack;
00331 
00332 
00333 jMatrixStack::jMatrixStack()
00334 {
00335   jMatrix ident;
00336   ident.identity();
00337   
00338   stk.push_back(ident);
00339   iter = stk.begin();
00340 }
00341 
00342 //---------------------------------------------------------//
00343 
00344 void jMatrixStack::push()
00345 {
00346   mult = *iter;
00347   stk.push_back(mult);
00348   iter = stk.end() - 1;
00349 }
00350 
00351 //---------------------------------------------------------//
00352 
00353 void jMatrixStack::pop()
00354 {
00355   if (stk.size() > 1){
00356     stk.pop_back();
00357     iter = stk.end() - 1;
00358   }
00359   else
00360     (*iter).identity();
00361 }
00362 
00363 //---------------------------------------------------------//
00364 
00365 void jMatrixStack::tform(vect3f& point)
00366 {
00367   (*iter).tform(point);
00368 }
00369 
00370 //---------------------------------------------------------//
00371 
00372 void jMatrixStack::tform(vect3f* point)
00373 {
00374   (*iter).tform(point);
00375 }
00376 
00377 //---------------------------------------------------------//
00378 
00379 void jMatrixStack::trans(float x, float y, float z)
00380 {
00381   mult.identity();
00382   
00383   mult.m[0][3] = x;
00384   mult.m[1][3] = y;
00385   mult.m[2][3] = z;
00386   
00387   (*iter).mult(mult);
00388 }
00389 
00390 //---------------------------------------------------------//
00391 
00392 void jMatrixStack::rotxrad(float angle)
00393 {
00394   mult.identity();
00395   
00396   mult.m[1][1] = cos(angle);
00397   mult.m[2][2] = cos(angle);
00398   mult.m[1][2] = -sin(angle);
00399   mult.m[2][1] = sin(angle);
00400   
00401   (*iter).mult(mult);
00402 }
00403 
00404 //---------------------------------------------------------//
00405 
00406 void jMatrixStack::rotxdeg(float angle)
00407 {
00408   mult.identity();
00409   
00410   angle *= pi / 180;
00411   
00412   mult.m[1][1] = cos(angle);
00413   mult.m[2][2] = cos(angle);
00414   mult.m[1][2] = -sin(angle);
00415   mult.m[2][1] = sin(angle);
00416   
00417   (*iter).mult(mult);
00418 }
00419 
00420 //---------------------------------------------------------//
00421 
00422 void jMatrixStack::rotyrad(float angle)
00423 {
00424   mult.identity();
00425   
00426   mult.m[0][0] = cos(angle);
00427   mult.m[2][2] = cos(angle);
00428   mult.m[0][2] = sin(angle);
00429   mult.m[2][0] = -sin(angle);
00430   
00431   (*iter).mult(mult);
00432 }
00433 
00434 //---------------------------------------------------------//
00435 
00436 void jMatrixStack::rotydeg(float angle)
00437 {
00438   mult.identity();
00439   
00440   angle *= pi / 180;
00441   
00442   mult.m[0][0] = cos(angle);
00443   mult.m[2][2] = cos(angle);
00444   mult.m[0][2] = sin(angle);
00445   mult.m[2][0] = -sin(angle);
00446   
00447   (*iter).mult(mult);
00448 }
00449 
00450 
00451 //---------------------------------------------------------//
00452 
00453 void jMatrixStack::rotzrad(float angle)
00454 {
00455   mult.identity();
00456   
00457   mult.m[0][0] = cos(angle);
00458   mult.m[1][1] = cos(angle);
00459   mult.m[0][1] = -sin(angle);
00460   mult.m[1][0] = sin(angle);
00461   
00462   (*iter).mult(mult);
00463 }
00464 
00465 //---------------------------------------------------------//
00466 
00467 void jMatrixStack::rotzdeg(float angle)
00468 {
00469   mult.identity();
00470   
00471   angle *= pi / 180;
00472   
00473   mult.m[0][0] = cos(angle);
00474   mult.m[1][1] = cos(angle);
00475   mult.m[0][1] = -sin(angle);
00476   mult.m[1][0] = sin(angle);
00477   
00478   (*iter).mult(mult);
00479 }
00480 
00481 //---------------------------------------------------------//
00482 
00483 void jMatrixStack::rotallrad(float x, float y, float z)
00484 {
00485   if (x != 0)
00486     rotxrad(x);
00487   if (y != 0)
00488     rotyrad(y);
00489   if (z != 0)
00490     rotzrad(z);
00491 }
00492 
00493 //---------------------------------------------------------//
00494 
00495 void jMatrixStack::rotalldeg(float x, float y, float z)
00496 {
00497   if (x != 0)
00498     rotxdeg(x);
00499   if (y != 0)
00500     rotydeg(y);
00501   if (z != 0)
00502     rotzdeg(z);
00503 }
00504 
00505 //---------------------------------------------------------//
00506 
00507 void jMatrixStack::scale(float x, float y, float z)
00508 {
00509   mult.identity();
00510   
00511   mult.m[0][0] = x;
00512   mult.m[1][1] = y;
00513   mult.m[2][2] = z;
00514   
00515   (*iter).mult(mult);
00516 }
00517 
00518 
00519 //////////////////////////////////////////////////////////////
00520 //*********************************************************///
00521 //////////////////////////////////////////////////////////////
00522 
00523 #endif // __jmatrix_h_

Generated on Sun Oct 14 18:46:15 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001