Main Page   Namespace List   Alphabetical List   Data Structures   File List   Data Fields   Globals  

algebra3.h

Go to the documentation of this file.
00001 
00002 //                          algebra3.h                                  //
00003 //                             -------------------                      //
00004 //    begin                : 1998                                       //
00005 //    copyright            : Jean-Francois DOUE                         //
00006 //    modified             : cbaudry (mainly removed global funcs)      //
00007 //    email                : cedric.baudry@tremplin-utc.fr              //
00009 
00011 //                                                                        //
00012 //   This program is free software; you can redistribute it and/or modify //
00013 //   it under the terms of the GNU General Public License as published by //
00014 //   the Free Software Foundation, version 2 of the License               //
00015 //                                                                        //
00017 
00018 /*
00019 
00020   algebra3.cpp, algebra3.h -  C++ Vector and Matrix Algebra routines           
00021 
00022   There are two vector classes and two matrix classes: vec3, vec4, mat3, and mat4.
00023 
00024   All the standard arithmetic operations are defined, with '*'
00025   for dot product of two vectors and multiplication of two matrices,
00026   and '^' for cross product of two vectors.
00027 
00028   Additional functions include length(), normalize(), homogenize for
00029   vectors, and print(), set(), apply() for all classes.
00030 
00031   There is a function transpose() for matrices, but note that it 
00032   does not actually change the matrix, 
00033 
00034   When multiplied with a matrix, a vector is treated as a row vector
00035   if it precedes the matrix (v*M), and as a column vector if it
00036   follows the matrix (M*v).
00037 
00038   Matrices are stored in row-major form.
00039 
00040   A vector of one dimension (3d, or 4d) can be cast to a vector
00041   of a higher or lower dimension.  If casting to a higher dimension,
00042   the new component is set by default to 1.0, unless a value is
00043   specified:
00044      vec3 a(1.0, 2.0, 3.0 );
00045      vec4 b( a, 4.0 );       // now b == {1.0, 2.0, 3.0, 4.0};
00046   When casting to a lower dimension, the vector is homogenized in
00047   the lower dimension.  E.g., if a 4d {X,Y,Z,W} is cast to 3d, the
00048   resulting vector is {X/W, Y/W, Z/W}.  It is up to the user to 
00049   insure the fourth component is not zero before casting.
00050 
00051   There are also the following function for building matrices:
00052      identity3D(), translation3D(),
00053      rotation3D(), rotation3Drad(),
00054      scaling3D(),  perspective3D()
00055 
00056   NOTE: When compiling for Windows, include this file first, to avoid
00057         certain name conflicts
00058  
00059 */
00060 
00061 #pragma once
00062 
00063 #include <math.h>
00064 #include <stdio.h>
00065 #include <stdlib.h>
00066 #include "macros.h"
00067 
00068 #ifdef VEC_ERROR_FATAL
00069 #ifndef VEC_ERROR
00070 #define VEC_ERROR(E) { printf( "VERROR %s\n", E ); exit(1); }
00071 #endif
00072 #else
00073 #ifndef VEC_ERROR
00074 #define VEC_ERROR(E) { printf( "VERROR %s\n", E ); }
00075 #endif
00076 #endif
00077 
00078 class vec3;
00079 class vec4;
00080 class mat3;
00081 class mat4;
00082 
00083 enum
00084 { VX, VY, VZ, VW };             
00085 enum
00086 { PA, PB, PC, PD };             
00087 enum
00088 { RED, GREEN, BLUE, ALPHA };    
00089 enum
00090 { KA, KD, KS, ES };             
00091 
00095 class vec3
00096 {
00097   protected:
00098     float n[3];
00099 
00100   public:
00101 
00102     // Constructors
00103 
00104       vec3 (void);
00105       vec3 (const float x, const float y, const float z);
00106       vec3 (const float d);
00107       vec3 (const vec3 & v);    
00108       vec3 (const vec4 & v);    
00109       vec3 (const vec4 & v, int dropAxis); 
00110 
00111     // Read accessors
00112 
00113     float x ();
00114     float y ();
00115     float z ();
00116     float *px ()
00117     {
00118         return n;
00119     };
00120     float *py ()
00121     {
00122         return n + 1;
00123     };
00124     float *pz ()
00125     {
00126         return n + 2;
00127     };
00128 
00129     // Assignment operators
00130 
00131     vec3 & operator  = (const vec3 & v); 
00132     vec3 & operator += (const vec3 & v); 
00133     vec3 & operator -= (const vec3 & v); 
00134     vec3 & operator *= (const float d); 
00135     vec3 & operator /= (const float d); 
00136     float &operator [] (int i); 
00137 
00138     // Special functions
00139 
00140     float length (void);        
00141     float length2 (void);       
00142     vec3 & normalize (void);    
00143     void set (float x, float y, float z); 
00144     void print (FILE * file, char *name); 
00145 
00146     // Unary and binary operators
00147 
00148     vec3 operator - ();         
00149     vec3 operator + (const vec3 & a); 
00150     vec3 operator - (const vec3 & b); 
00151     vec3 operator * (const float d); 
00152     vec3 operator * (mat4 & m); 
00153     float operator * (const vec3 & a); 
00154     vec3 operator / (const float d); 
00155     vec3 operator ^ (const vec3 & b); 
00156     int operator == (const vec3 & b); 
00157     int operator != (const vec3 & b); 
00158 
00159     // Static methods
00160 
00161     static void swap (vec3 & a, vec3 & b); 
00162     static vec3 min (const vec3 & a, const vec3 & b); 
00163     static vec3 max (const vec3 & a, const vec3 & b); 
00164     static vec3 prod (const vec3 & a, const vec3 & b); 
00165 
00166     // Friends
00167 
00168     friend class vec4;
00169     friend class mat3;
00170     friend class mat4;
00171 
00172 };
00173 
00178 class vec4
00179 {
00180   protected:
00181     float n[4];
00182 
00183   public:
00184 
00185     // Constructors
00186 
00187       vec4 (void);
00188       vec4 (const float x, const float y, const float z, const float w);
00189       vec4 (const float d);
00190       vec4 (const vec4 & v);    
00191       vec4 (const vec3 & v);    
00192       vec4 (const vec3 & v, const float d); 
00193 
00194     // Assignment operators
00195 
00196       vec4 & operator	= (const vec4 & v); 
00197       vec4 & operator += (const vec4 & v); 
00198       vec4 & operator -= (const vec4 & v); 
00199       vec4 & operator *= (const float d); 
00200       vec4 & operator /= (const float d); 
00201     float &operator [] (int i); 
00202 
00203     // Special functions
00204 
00205     float length (void);        
00206     float length2 (void);       
00207       vec4 & normalize (void);  
00208       vec4 & homogenize (void); 
00209     void print (FILE * file, char *name); 
00210     void set (float x, float y, float z, float a);
00211 
00212     // Unary and binary operators
00213 
00214     vec4 operator - ();         
00215     vec4 operator + (const vec4 & b); 
00216     vec4 operator - (const vec4 & b); 
00217     vec4 operator * (const float d); 
00218     vec4 operator * (mat4 & a); 
00219     float operator * (const vec4 & a); 
00220     vec4 operator / (const float d); 
00221     int operator == (const vec4 & a); 
00222     int operator != (const vec4 & a); 
00223 
00224     // Static methods
00225 
00226     static void swap (vec4 & a, vec4 & b); 
00227     static vec4 min (const vec4 & a, const vec4 & b); 
00228     static vec4 max (const vec4 & a, const vec4 & b); 
00229     static vec4 prod (const vec4 & a, const vec4 & b); 
00230 
00231     // Friends
00232 
00233     friend class vec3;
00234     friend class mat4;
00235 
00236 };
00237 
00242 class mat3
00243 {
00244   protected:
00245     vec3 v[3];
00246 
00247   public:
00248 
00249     // Constructors
00250 
00251     mat3 (void);
00252       mat3 (const vec3 & v0, const vec3 & v1, const vec3 & v2);
00253       mat3 (const float d);
00254       mat3 (const mat3 & m);
00255 
00256     // Assignment operators
00257 
00258       mat3 & operator	= (const mat3 & m); 
00259       mat3 & operator += (const mat3 & m); 
00260       mat3 & operator -= (const mat3 & m); 
00261       mat3 & operator *= (const float d); 
00262       mat3 & operator /= (const float d); 
00263       vec3 & operator [] (int i); 
00264 
00265     // special functions
00266 
00267     mat3 transpose (void);      
00268     mat3 inverse (void);        
00269     void print (FILE * file, char *name); 
00270     void set (const vec3 & v0, const vec3 & v1, const vec3 & v2);
00271 
00272     // Operators
00273 
00274     mat3 operator - ();         
00275     mat3 operator + (const mat3 & b); 
00276     mat3 operator - (const mat3 & b); 
00277     mat3 operator * (mat3 & b); 
00278     mat3 operator * (const float d); 
00279     mat3 operator / (const float d); 
00280     int operator == (const mat3 & b); 
00281     int operator != (const mat3 & b); 
00282     vec3 operator * (const vec3 & v); 
00283 
00284     // Static methods
00285 
00286     static void swap (mat3 & a, mat3 & b); 
00287     static mat3 identity (void);
00288 
00289 };
00290 
00295 class mat4
00296 {
00297   protected:
00298     vec4 v[4];
00299 
00300   public:
00301 
00302     // Constructors
00303 
00304     mat4 (void);
00305       mat4 (const vec4 & v0, const vec4 & v1, const vec4 & v2, const vec4 & v3);
00306       mat4 (const float d);
00307       mat4 (const mat4 & m);
00308       mat4 (const float a00, const float a01, const float a02, const float a03,
00309             const float a10, const float a11, const float a12, const float a13,
00310             const float a20, const float a21, const float a22, const float a23, const float a30, const float a31, const float a32, const float a33);
00311 
00312     // Assignment operators
00313 
00314       mat4 & operator	= (const mat4 & m); 
00315       mat4 & operator += (const mat4 & m); 
00316       mat4 & operator -= (const mat4 & m); 
00317       mat4 & operator *= (const float d); 
00318       mat4 & operator /= (const float d); 
00319       vec4 & operator [] (int i); 
00320 
00321     // Special functions
00322 
00323     mat4 transpose (void);      
00324     mat4 inverse (void);        
00325     void print (FILE * file, char *name); 
00326     void swap_rows (int i, int j); 
00327     void swap_cols (int i, int j); 
00328 
00329     // Operators
00330 
00331     mat4 operator - ();         
00332     mat4 operator + (const mat4 & b); 
00333     mat4 operator - (const mat4 & b); 
00334     mat4 operator * (mat4 & b); 
00335     mat4 operator * (const float d); 
00336     mat4 operator / (const float d); 
00337     int operator == (const mat4 & b); 
00338     int operator != (const mat4 & b); 
00339     vec4 operator * (const vec4 & v); 
00340     vec3 operator * (const vec3 & v); 
00341 
00342     // Static methods
00343 
00344     static void swap (mat4 & a, mat4 & b); 
00345 
00346     // Transformations
00347 
00348     static mat4 identity3D (void); 
00349     static mat4 translation3D (vec3 & v); 
00350     static mat4 rotation3D (vec3 & Axis, const float angleDeg); 
00351     static mat4 rotation3Drad (vec3 & Axis, const float angleRad); 
00352     static mat4 scaling3D (vec3 & scaleVector); 
00353     static mat4 perspective3D (const float d); 
00354 
00355 };

Generated on Wed Feb 18 16:32:50 2004 for POVAMA by doxygen1.2.18