Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

algebra3.h

Go to the documentation of this file.
00001 /*
00002 
00003   algebra3.cpp, algebra3.h -  C++ Vector and Matrix Algebra routines
00004 
00005   GLUI User Interface Toolkit (LGPL)
00006   Copyright (c) 1998 Paul Rademacher
00007 
00008   WWW:    http://sourceforge.net/projects/glui/
00009   Forums: http://sourceforge.net/forum/?group_id=92496
00010 
00011   This library is free software; you can redistribute it and/or
00012   modify it under the terms of the GNU Lesser General Public
00013   License as published by the Free Software Foundation; either
00014   version 2.1 of the License, or (at your option) any later version.
00015 
00016   This library is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019   Lesser General Public License for more details.
00020 
00021   You should have received a copy of the GNU Lesser General Public
00022   License along with this library; if not, write to the Free Software
00023   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025 */
00026 
00027 /**************************************************************************
00028     
00029   There are three vector classes and two matrix classes: vec2, vec3,
00030   vec4, mat3, and mat4.
00031 
00032   All the standard arithmetic operations are defined, with '*'
00033   for dot product of two vectors and multiplication of two matrices,
00034   and '^' for cross product of two vectors.
00035 
00036   Additional functions include length(), normalize(), homogenize for
00037   vectors, and print(), set(), apply() for all classes.
00038 
00039   There is a function transpose() for matrices, but note that it 
00040   does not actually change the matrix, 
00041 
00042   When multiplied with a matrix, a vector is treated as a row vector
00043   if it precedes the matrix (v*M), and as a column vector if it
00044   follows the matrix (M*v).
00045 
00046   Matrices are stored in row-major form.
00047 
00048   A vector of one dimension (2d, 3d, or 4d) can be cast to a vector
00049   of a higher or lower dimension.  If casting to a higher dimension,
00050   the new component is set by default to 1.0, unless a value is
00051   specified:
00052      vec3 a(1.0, 2.0, 3.0 );
00053      vec4 b( a, 4.0 );       // now b == {1.0, 2.0, 3.0, 4.0};
00054   When casting to a lower dimension, the vector is homogenized in
00055   the lower dimension.  E.g., if a 4d {X,Y,Z,W} is cast to 3d, the
00056   resulting vector is {X/W, Y/W, Z/W}.  It is up to the user to 
00057   insure the fourth component is not zero before casting.
00058 
00059   There are also the following function for building matrices:
00060      identity2D(), translation2D(), rotation2D(),
00061      scaling2D(),  identity3D(),    translation3D(),
00062      rotation3D(), rotation3Drad(),  scaling3D(),
00063      perspective3D()
00064 
00065   NOTE: When compiling for Windows, include this file first, to avoid
00066         certain name conflicts
00067  
00068   ---------------------------------------------------------------------
00069   
00070   Author: Jean-Francois DOUEg                   
00071   Revised: Paul Rademacher                                      
00072   Version 3.2 - Feb 1998
00073   Revised: Nigel Stewart (GLUI Code Cleaning)
00074   
00075 **************************************************************************/
00076 
00077 #ifndef GLUI_ALGEBRA3_H
00078 #define GLUI_ALGEBRA3_H
00079 
00080 #include <cmath>
00081 #include <cstdio>
00082 #include <cstdlib>
00083 
00084 // this line defines a new type: pointer to a function which returns a
00085 // float and takes as argument a float
00086 typedef float (*V_FCT_PTR)(float);
00087 
00088 class vec2;
00089 class vec3;
00090 class vec4;
00091 class mat3;
00092 class mat4;
00093 
00094 #ifndef M_PI
00095 #define M_PI 3.141592654
00096 #endif
00097 
00098 enum {VX, VY, VZ, VW};           // axes
00099 enum {PA, PB, PC, PD};           // planes
00100 enum {RED, GREEN, BLUE, ALPHA};  // colors
00101 enum {KA, KD, KS, ES};           // phong coefficients
00102 
00103 /****************************************************************
00104  *                                                              *
00105  *              2D Vector                                       *
00106  *                                                              *
00107  ****************************************************************/
00108 
00109 class vec2
00110 {
00111   friend class vec3;
00112 
00113 protected:
00114 
00115   float n[2];
00116 
00117 public:
00118 
00119   // Constructors
00120 
00121   vec2();
00122   vec2(float x, float y);
00123   vec2(const vec2 &v);                   // copy constructor
00124   vec2(const vec3 &v);                   // cast v3 to v2
00125   vec2(const vec3 &v, int dropAxis);     // cast v3 to v2
00126 
00127   // Assignment operators
00128 
00129   vec2  &operator  = (const vec2 &v);    // assignment of a vec2
00130   vec2  &operator += (const vec2 &v);    // incrementation by a vec2
00131   vec2  &operator -= (const vec2 &v);    // decrementation by a vec2
00132   vec2  &operator *= (float d);    // multiplication by a constant
00133   vec2  &operator /= (float d);    // division by a constant
00134 
00135   // special functions
00136 
00137   float  length()  const;                   // length of a vec2
00138   float  length2() const;                   // squared length of a vec2
00139   vec2  &normalize();                       // normalize a vec2
00140   vec2  &apply(V_FCT_PTR fct);              // apply a func. to each component
00141   void   set(float x, float y);             // set vector
00142 
00143   float &operator [] (int i);         // indexing
00144   const float &operator [] (int i) const;   // indexing
00145 
00146   // friends
00147 
00148   friend vec2  operator -  (const vec2 &v);                   // -v1
00149   friend vec2  operator +  (const vec2 &a, const vec2 &b);    // v1 + v2
00150   friend vec2  operator -  (const vec2 &a, const vec2 &b);    // v1 - v2
00151   friend vec2  operator *  (const vec2 &a, float d);          // v1 * 3.0
00152   friend vec2  operator *  (float d, const vec2 &a);          // 3.0 * v1
00153   friend vec2  operator *  (const mat3 &a, const vec2 &v);    // M . v
00154   friend vec2  operator *  (const vec2 &v, const mat3 &a);    // v . M
00155   friend float operator *  (const vec2 &a, const vec2 &b);    // dot product
00156   friend vec2  operator /  (const vec2 &a, float d);          // v1 / 3.0
00157   friend vec3  operator ^  (const vec2 &a, const vec2 &b);    // cross product
00158   friend int   operator == (const vec2 &a, const vec2 &b);    // v1 == v2 ?
00159   friend int   operator != (const vec2 &a, const vec2 &b);    // v1 != v2 ?
00160   //friend ostream& operator << (ostream& s, vec2& v);        // output to stream
00161   //friend istream& operator >> (istream& s, vec2& v);        // input from strm.
00162   friend void swap(vec2 &a, vec2 &b);                         // swap v1 & v2
00163   friend vec2 min_vec(const vec2 &a, const vec2 &b);          // min(v1, v2)
00164   friend vec2 max_vec(const vec2 &a, const vec2 &b);          // max(v1, v2)
00165   friend vec2 prod   (const vec2 &a, const vec2 &b);          // term by term *
00166 };
00167 
00168 /****************************************************************
00169  *                                                              *
00170  *               3D Vector                                      *
00171  *                                                              *
00172  ****************************************************************/
00173 
00174 class vec3
00175 {
00176   friend class vec2;
00177   friend class vec4;
00178   friend class mat3;
00179 
00180 protected:
00181 
00182   float n[3];
00183 
00184 public:
00185 
00186   // Constructors
00187 
00188   vec3();
00189   vec3(float x, float y, float z);
00190   vec3(const vec3 &v);               // copy constructor
00191   vec3(const vec2 &v);               // cast v2 to v3
00192   vec3(const vec2 &v, float d);      // cast v2 to v3
00193   vec3(const vec4 &v);               // cast v4 to v3
00194   vec3(const vec4 &v, int dropAxis); // cast v4 to v3
00195 
00196   // Assignment operators
00197 
00198   vec3  &operator  = (const vec3 &v);      // assignment of a vec3
00199   vec3  &operator += (const vec3 &v);      // incrementation by a vec3
00200   vec3  &operator -= (const vec3 &v);      // decrementation by a vec3
00201   vec3  &operator *= (float d);      // multiplication by a constant
00202   vec3  &operator /= (float d);      // division by a constant
00203 
00204   // special functions
00205 
00206   float  length()  const;                     // length of a vec3
00207   float  length2() const;                     // squared length of a vec3
00208   vec3&  normalize();                         // normalize a vec3
00209   vec3&  homogenize();                        // homogenize (div by Z)
00210   vec3&  apply(V_FCT_PTR fct);                // apply a func. to each component
00211   void   set(float x, float y, float z);      // set vector
00212 
00213   void   print(FILE *file, const char *name) const; // print vector to a file
00214 
00215 
00216   float &operator [] (int i);       // indexing
00217   const float &operator [] (int i) const; // indexing
00218 
00219   // friends
00220 
00221   friend vec3  operator -  (const vec3 &v);                 // -v1
00222   friend vec3  operator +  (const vec3 &a, const vec3 &b);  // v1 + v2
00223   friend vec3  operator -  (const vec3 &a, const vec3 &b);  // v1 - v2
00224   friend vec3  operator *  (const vec3 &a, float d);        // v1 * 3.0
00225   friend vec3  operator *  (float d, const vec3 &a);        // 3.0 * v1
00226   friend vec3  operator *  (const mat4 &a, const vec3 &v);  // M . v
00227   friend vec3  operator *  (const vec3 &v, const mat4 &a);  // v . M
00228   friend float operator *  (const vec3 &a, const vec3 &b);  // dot product
00229   friend vec3  operator /  (const vec3 &a, float d);  // v1 / 3.0
00230   friend vec3  operator ^  (const vec3 &a, const vec3 &b);  // cross product
00231   friend int   operator == (const vec3 &a, const vec3 &b);  // v1 == v2 ?
00232   friend int   operator != (const vec3 &a, const vec3 &b);  // v1 != v2 ?
00233   //friend ostream& operator << (ostream& s, vec3& v);      // output to stream
00234   //friend istream& operator >> (istream& s, vec3& v);      // input from strm.
00235   friend void swap(vec3 &a, vec3 &b);                       // swap v1 & v2
00236   friend vec3 min_vec(const vec3 &a, const vec3 &b);        // min(v1, v2)
00237   friend vec3 max_vec(const vec3 &a, const vec3 &b);        // max(v1, v2)
00238   friend vec3 prod(const vec3 &a, const vec3 &b);           // term by term *
00239 
00240   // necessary friend declarations
00241 
00242   friend vec2 operator * (const mat3 &a, const vec2 &v);    // linear transform
00243   friend vec3 operator * (const mat3 &a, const vec3 &v);    // linear transform
00244   friend mat3 operator * (const mat3 &a, const mat3 &b);    // matrix 3 product
00245 };
00246 
00247 /****************************************************************
00248  *                                                              *
00249  *              4D Vector                                       *
00250  *                                                              *
00251  ****************************************************************/
00252 
00253 class vec4
00254 {
00255   friend class vec3;
00256   friend class mat4;
00257 
00258 protected:
00259 
00260   float n[4];
00261 
00262 public:
00263 
00264   // Constructors
00265 
00266   vec4();
00267   vec4(float x, float y, float z, float w);
00268   vec4(const vec4 &v);             // copy constructor
00269   vec4(const vec3 &v);             // cast vec3 to vec4
00270   vec4(const vec3 &v, float d);    // cast vec3 to vec4
00271 
00272   // Assignment operators
00273 
00274   vec4  &operator  = (const vec4 &v);    // assignment of a vec4
00275   vec4  &operator += (const vec4 &v);    // incrementation by a vec4
00276   vec4  &operator -= (const vec4 &v);    // decrementation by a vec4
00277   vec4  &operator *= (float d);    // multiplication by a constant
00278   vec4  &operator /= (float d);    // division by a constant
00279 
00280   // special functions
00281 
00282   float  length()  const;                     // length of a vec4
00283   float  length2() const;                     // squared length of a vec4
00284   vec4  &normalize();                         // normalize a vec4
00285   vec4  &apply(V_FCT_PTR fct);                // apply a func. to each component
00286   vec4  &homogenize();
00287 
00288   void   print(FILE *file, const char *name) const; // print vector to a file
00289 
00290   void   set(float x, float y, float z, float a);                        
00291 
00292   float &operator [] (int i);             // indexing
00293   const float &operator [] (int i) const; // indexing
00294 
00295   // friends
00296 
00297   friend vec4  operator -  (const vec4 &v);                  // -v1
00298   friend vec4  operator +  (const vec4 &a, const vec4 &b);   // v1 + v2
00299   friend vec4  operator -  (const vec4 &a, const vec4 &b);   // v1 - v2
00300   friend vec4  operator *  (const vec4 &a, float d);         // v1 * 3.0
00301   friend vec4  operator *  (float d, const vec4 &a);         // 3.0 * v1
00302   friend vec4  operator *  (const mat4 &a, const vec4 &v);   // M . v
00303   friend vec4  operator *  (const vec4 &v, const mat4 &a);   // v . M
00304   friend float operator *  (const vec4 &a, const vec4 &b);   // dot product
00305   friend vec4  operator /  (const vec4 &a, float d);   // v1 / 3.0
00306   friend int   operator == (const vec4 &a, const vec4 &b);   // v1 == v2 ?
00307   friend int   operator != (const vec4 &a, const vec4 &b);   // v1 != v2 ?
00308   //friend ostream& operator << (ostream& s, vec4& v);       // output to stream
00309   //friend istream& operator >> (istream& s, vec4& v);       // input from strm.
00310   friend void swap(vec4 &a, vec4 &b);                        // swap v1 & v2
00311   friend vec4 min_vec(const vec4 &a, const vec4 &b);         // min(v1, v2)
00312   friend vec4 max_vec(const vec4 &a, const vec4 &b);         // max(v1, v2)
00313   friend vec4 prod   (const vec4 &a, const vec4 &b);         // term by term *
00314 
00315   // necessary friend declarations
00316 
00317   friend vec3 operator * (const mat4 &a, const vec3 &v);     // linear transform
00318   friend mat4 operator * (const mat4 &a, const mat4 &b);     // matrix 4 product
00319 };
00320 
00321 /****************************************************************
00322  *                                                              *
00323  *             3x3 Matrix                                       *
00324  *                                                              *
00325  ****************************************************************/
00326 
00327 class mat3
00328 {
00329 protected:
00330 
00331   vec3 v[3];
00332 
00333 public:
00334 
00335   // Constructors
00336 
00337   mat3();
00338   mat3(const vec3 &v0, const vec3 &v1, const vec3 &v2);
00339   mat3(const mat3 &m);
00340 
00341   // Assignment operators
00342 
00343   mat3 &operator  = (const mat3  &m);        // assignment of a mat3
00344   mat3 &operator += (const mat3  &m);        // incrementation by a mat3
00345   mat3 &operator -= (const mat3  &m);        // decrementation by a mat3
00346   mat3 &operator *= (float  d);        // multiplication by a constant
00347   mat3 &operator /= (float  d);        // division by a constant
00348 
00349   // special functions
00350 
00351   mat3  transpose() const;                    // transpose
00352   mat3  inverse() const;                      // inverse
00353   mat3 &apply(V_FCT_PTR fct);                 // apply a func. to each element
00354 
00355   void  print(FILE *file, const char *name ) const; // print matrix to a file
00356 
00357   void  set(const vec3 &v0, const vec3 &v1, const vec3 &v2);
00358 
00359   vec3 &operator [] (int i);       // indexing
00360   const vec3 &operator [] (int i) const; // indexing
00361 
00362   // friends
00363 
00364   friend mat3 operator -  (const mat3 &a);                     // -m1
00365   friend mat3 operator +  (const mat3 &a, const mat3 &b);      // m1 + m2
00366   friend mat3 operator -  (const mat3 &a, const mat3 &b);      // m1 - m2
00367   friend mat3 operator *  (const mat3 &a, const mat3 &b);      // m1 * m2
00368   friend mat3 operator *  (const mat3 &a, float d);            // m1 * 3.0
00369   friend mat3 operator *  (float d, const mat3 &a);            // 3.0 * m1
00370   friend mat3 operator /  (const mat3 &a, float d);            // m1 / 3.0
00371   friend int  operator == (const mat3 &a, const mat3 &b);      // m1 == m2 ?
00372   friend int  operator != (const mat3 &a, const mat3 &b);      // m1 != m2 ?
00373   //friend ostream& operator << (ostream& s, mat3& m);         // output to stream
00374   //friend istream& operator >> (istream& s, mat3& m);         // input from strm.
00375   friend void swap(mat3 &a, mat3 &b);                          // swap m1 & m2
00376 
00377   // necessary friend declarations
00378 
00379   friend vec3 operator * (const mat3 &a, const vec3 &v);     // linear transform
00380   friend vec2 operator * (const mat3 &a, const vec2 &v);     // linear transform
00381 };
00382 
00383 /****************************************************************
00384  *                                                              *
00385  *             4x4 Matrix                                       *
00386  *                                                              *
00387  ****************************************************************/
00388 
00389 class mat4
00390 {
00391 protected:
00392 
00393   vec4 v[4];
00394 
00395 public:
00396 
00397   // Constructors
00398 
00399   mat4();
00400   mat4(const vec4 &v0, const vec4 &v1, const vec4 &v2, const vec4 &v3);
00401   mat4(const mat4 &m);
00402   mat4(float a00, float a01, float a02, float a03,
00403        float a10, float a11, float a12, float a13,
00404        float a20, float a21, float a22, float a23,
00405        float a30, float a31, float a32, float a33 );
00406 
00407 
00408   // Assignment operators
00409 
00410   mat4 &operator  = (const mat4 &m);        // assignment of a mat4
00411   mat4 &operator += (const mat4 &m);        // incrementation by a mat4
00412   mat4 &operator -= (const mat4 &m);        // decrementation by a mat4
00413   mat4 &operator *= (float d);        // multiplication by a constant
00414   mat4 &operator /= (float d);        // division by a constant
00415 
00416   // special functions
00417 
00418   mat4  transpose() const;                   // transpose
00419   mat4  inverse() const;                     // inverse
00420   mat4 &apply(V_FCT_PTR fct);                // apply a func. to each element
00421 
00422   void  print(FILE *file, const char *name) const; // print matrix to a file
00423     
00424   vec4 &operator [] (int i);       // indexing
00425   const vec4 &operator [] (int i) const; // indexing
00426 
00427   void  swap_rows(int i, int j); // swap rows i and j
00428   void  swap_cols(int i, int j); // swap cols i and j
00429 
00430   // friends
00431 
00432   friend mat4 operator -  (const mat4 &a);                     // -m1
00433   friend mat4 operator +  (const mat4 &a, const mat4 &b);      // m1 + m2
00434   friend mat4 operator -  (const mat4 &a, const mat4 &b);      // m1 - m2
00435   friend mat4 operator *  (const mat4 &a, const mat4 &b);      // m1 * m2
00436   friend mat4 operator *  (const mat4 &a, float d);            // m1 * 4.0
00437   friend mat4 operator *  (float d, const mat4 &a);            // 4.0 * m1
00438   friend mat4 operator /  (const mat4 &a, float d);            // m1 / 3.0
00439   friend int  operator == (const mat4 &a, const mat4 &b);      // m1 == m2 ?
00440   friend int  operator != (const mat4 &a, const mat4 &b);      // m1 != m2 ?
00441   //friend ostream& operator << (ostream& s, mat4& m);         // output to stream
00442   //friend istream& operator >> (istream& s, mat4& m);         // input from strm.
00443   friend void swap(mat4 &a, mat4 &b);                          // swap m1 & m2
00444 
00445   // necessary friend declarations
00446 
00447   friend vec4 operator * (const mat4 &a, const vec4 &v);      // linear transform
00448   //friend vec4 operator * (const vec4& v, const mat4& a);    // linear transform
00449   friend vec3 operator * (const mat4 &a, const vec3 &v);      // linear transform
00450   friend vec3 operator * (const vec3 &v, const mat4 &a);      // linear transform
00451 };
00452 
00453 /****************************************************************
00454  *                                                              *
00455  *         2D functions and 3D functions                        *
00456  *                                                              *
00457  ****************************************************************/
00458 
00459 mat3 identity2D   ();                                   // identity 2D
00460 mat3 translation2D(const vec2 &v);                      // translation 2D
00461 mat3 rotation2D   (const vec2 &Center, float angleDeg); // rotation 2D
00462 mat3 scaling2D    (const vec2 &scaleVector);            // scaling 2D
00463 mat4 identity3D   ();                                   // identity 3D
00464 mat4 translation3D(const vec3 &v);                      // translation 3D
00465 mat4 rotation3D   (const vec3 &Axis, float angleDeg);   // rotation 3D
00466 mat4 rotation3Drad(const vec3 &Axis, float angleRad);   // rotation 3D
00467 mat4 scaling3D    (const vec3 &scaleVector);            // scaling 3D
00468 mat4 perspective3D(float d);                            // perspective 3D
00469 
00470 vec3 operator * (const vec3 &v, const mat3 &a);
00471 vec2 operator * (const vec2 &v, const mat3 &a);
00472 vec3 operator * (const vec3 &v, const mat4 &a);
00473 vec4 operator * (const vec4 &v, const mat4 &a);
00474 
00475 #endif

Generated on Fri Sep 15 17:51:56 2006 for GLUI by doxygen 1.3.6