Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

algebra3.h

00001 /****************************************************************
00002 *                                                               *
00003 * C++ Vector and Matrix Algebra routines                        *
00004 * Author: Jean-Francois DOUE                                    *
00005 * Version 3.1 --- October 1993                                  *
00006 *                                                               *
00007 ****************************************************************/
00008 
00009 #ifndef _util_misc_algebra_h
00010 #define _util_misc_algebra_h
00011 
00012 #include <iostream>
00013 #include <stdlib.h>
00014 
00015 // this line defines a new type: pointer to a function which returns a
00016 // double and takes as argument a double
00017 typedef double (*V_FCT_PTR)(double);
00018 
00019 class vec2;
00020 class vec3;
00021 class vec4;
00022 class mat3;
00023 class mat4;
00024 
00025 enum {VX, VY, VZ, VW};              // axes
00026 enum {PA, PB, PC, PD};              // planes
00027 enum {RED, GREEN, BLUE};            // colors
00028 
00029 /****************************************************************
00030 *                                                               *
00031 *                           2D Vector                           *
00032 *                                                               *
00033 ****************************************************************/
00034 
00035 class vec2
00036 {
00037 protected:
00038 
00039  double n[2];
00040 
00041 public:
00042 
00043 // Constructors
00044 
00045 vec2();
00046 vec2(const double x, const double y);
00047 vec2(const double d);
00048 vec2(const vec2& v);                    // copy constructor
00049 vec2(const vec3& v);                    // cast v3 to v2
00050 vec2(const vec3& v, int dropAxis);      // cast v3 to v2
00051 
00052 // Assignment operators
00053 
00054 vec2& operator  = ( const vec2& v );    // assignment of a vec2
00055 vec2& operator += ( const vec2& v );    // incrementation by a vec2
00056 vec2& operator -= ( const vec2& v );    // decrementation by a vec2
00057 vec2& operator *= ( const double d );   // multiplication by a constant
00058 vec2& operator /= ( const double d );   // division by a constant
00059 double& operator [] ( int i);           // indexing
00060 const double& operator[](int i) const;  // indexing
00061 
00062 // special functions
00063 
00064 double length();                        // length of a vec2
00065 double length2();                       // squared length of a vec2
00066 vec2& normalize();                      // normalize a vec2
00067 vec2& apply(V_FCT_PTR fct);             // apply a func. to each component
00068 
00069 // friends
00070 
00071 friend vec2 operator - (const vec2& v);                     // -v1
00072 friend vec2 operator + (const vec2& a, const vec2& b);      // v1 + v2
00073 friend vec2 operator - (const vec2& a, const vec2& b);      // v1 - v2
00074 friend vec2 operator * (const vec2& a, const double d);     // v1 * 3.0
00075 friend vec2 operator * (const double d, const vec2& a);     // 3.0 * v1
00076 friend vec2 operator * (const mat3& a, const vec2& v);      // M . v
00077 friend vec2 operator * (const vec2& v, mat3& a);            // v . M
00078 friend double operator * (const vec2& a, const vec2& b);    // dot product
00079 friend vec2 operator / (const vec2& a, const double d);     // v1 / 3.0
00080 friend vec3 operator ^ (const vec2& a, const vec2& b);      // cross product
00081 friend int operator == (const vec2& a, const vec2& b);      // v1 == v2 ?
00082 friend int operator != (const vec2& a, const vec2& b);      // v1 != v2 ?
00083 friend std::ostream& operator << (std::ostream& s, vec2& v);// output to stream
00084 friend std::istream& operator >> (std::istream& s, vec2& v);// input from strm.
00085 friend void swap(vec2& a, vec2& b);                         // swap v1 & v2
00086 friend vec2 min(const vec2& a, const vec2& b);              // min(v1, v2)
00087 friend vec2 max(const vec2& a, const vec2& b);              // max(v1, v2)
00088 friend vec2 prod(const vec2& a, const vec2& b);             // term by term *
00089 
00090 // necessary friend declarations
00091 
00092 friend class vec3;
00093 };
00094 
00095 /****************************************************************
00096 *                                                               *
00097 *                           3D Vector                           *
00098 *                                                               *
00099 ****************************************************************/
00100 
00101 class vec3
00102 {
00103 protected:
00104 
00105  double n[3];
00106 
00107 public:
00108 
00109 // Constructors
00110 
00111 vec3();
00112 vec3(const double x, const double y, const double z);
00113 vec3(const double d);
00114 vec3(const vec3& v);                        // copy constructor
00115 vec3(const vec2& v);                        // cast v2 to v3
00116 vec3(const vec2& v, double d);              // cast v2 to v3
00117 vec3(const vec4& v);                        // cast v4 to v3
00118 vec3(const vec4& v, int dropAxis);          // cast v4 to v3
00119 
00120 // Assignment operators
00121 
00122 vec3& operator  = ( const vec3& v );        // assignment of a vec3
00123 vec3& operator += ( const vec3& v );        // incrementation by a vec3
00124 vec3& operator -= ( const vec3& v );        // decrementation by a vec3
00125 vec3& operator *= ( const double d );       // multiplication by a constant
00126 vec3& operator /= ( const double d );       // division by a constant
00127 double& operator [] ( int i);               // indexing
00128 const double& operator[](int i) const;      // indexing
00129 
00130 // special functions
00131 
00132 double length();                            // length of a vec3
00133 double length2();                           // squared length of a vec3
00134 vec3& normalize();                          // normalize a vec3
00135 vec3& apply(V_FCT_PTR fct);                 // apply a func. to each component
00136 
00137 // friends
00138 
00139 friend vec3 operator - (const vec3& v);                     // -v1
00140 friend vec3 operator + (const vec3& a, const vec3& b);      // v1 + v2
00141 friend vec3 operator - (const vec3& a, const vec3& b);      // v1 - v2
00142 friend vec3 operator * (const vec3& a, const double d);     // v1 * 3.0
00143 friend vec3 operator * (const double d, const vec3& a);     // 3.0 * v1
00144 friend vec3 operator * (const mat4& a, const vec3& v);      // M . v
00145 friend vec3 operator * (const vec3& v, const mat4& a);      // v . M
00146 friend double operator * (const vec3& a, const vec3& b);    // dot product
00147 friend vec3 operator / (const vec3& a, const double d);     // v1 / 3.0
00148 friend vec3 operator ^ (const vec3& a, const vec3& b);      // cross product
00149 friend int operator == (const vec3& a, const vec3& b);      // v1 == v2 ?
00150 friend int operator != (const vec3& a, const vec3& b);      // v1 != v2 ?
00151 friend std::ostream& operator << (std::ostream& s, vec3& v);// output to stream
00152 friend std::istream& operator >> (std::istream& s, vec3& v);// input from strm.
00153 friend void swap(vec3& a, vec3& b);                         // swap v1 & v2
00154 friend vec3 min(const vec3& a, const vec3& b);              // min(v1, v2)
00155 friend vec3 max(const vec3& a, const vec3& b);              // max(v1, v2)
00156 friend vec3 prod(const vec3& a, const vec3& b);             // term by term *
00157 
00158 // necessary friend declarations
00159 
00160 friend class vec2;
00161 friend class vec4;
00162 friend class mat3;
00163 friend vec2 operator * (const mat3& a, const vec2& v);      // linear transform
00164 friend mat3 operator * (const mat3& a, const mat3& b);              // matrix 3 product
00165 };
00166 
00167 /****************************************************************
00168 *                                                               *
00169 *                           4D Vector                           *
00170 *                                                               *
00171 ****************************************************************/
00172 
00173 class vec4
00174 {
00175 protected:
00176 
00177  double n[4];
00178 
00179 public:
00180 
00181 // Constructors
00182 
00183 vec4();
00184 vec4(const double x, const double y, const double z, const double w);
00185 vec4(const double d);
00186 vec4(const vec4& v);                        // copy constructor
00187 vec4(const vec3& v);                        // cast vec3 to vec4
00188 vec4(const vec3& v, const double d);        // cast vec3 to vec4
00189 
00190 // Assignment operators
00191 
00192 vec4& operator  = ( const vec4& v );        // assignment of a vec4
00193 vec4& operator += ( const vec4& v );        // incrementation by a vec4
00194 vec4& operator -= ( const vec4& v );        // decrementation by a vec4
00195 vec4& operator *= ( const double d );       // multiplication by a constant
00196 vec4& operator /= ( const double d );       // division by a constant
00197 double& operator [] ( int i);               // indexing
00198 const double& operator [] ( int i) const;                   // indexing
00199 
00200 // special functions
00201 
00202 double length();                            // length of a vec4
00203 double length2();                           // squared length of a vec4
00204 vec4& normalize();                          // normalize a vec4
00205 vec4& apply(V_FCT_PTR fct);                 // apply a func. to each component
00206 
00207 // friends
00208 
00209 friend vec4 operator - (const vec4& v);                     // -v1
00210 friend vec4 operator + (const vec4& a, const vec4& b);      // v1 + v2
00211 friend vec4 operator - (const vec4& a, const vec4& b);      // v1 - v2
00212 friend vec4 operator * (const vec4& a, const double d);     // v1 * 3.0
00213 friend vec4 operator * (const double d, const vec4& a);     // 3.0 * v1
00214 friend vec4 operator * (const mat4& a, const vec4& v);      // M . v
00215 friend vec4 operator * (const vec4& v, const mat4& a);      // v . M
00216 friend double operator * (const vec4& a, const vec4& b);    // dot product
00217 friend vec4 operator / (const vec4& a, const double d);     // v1 / 3.0
00218 friend int operator == (const vec4& a, const vec4& b);      // v1 == v2 ?
00219 friend int operator != (const vec4& a, const vec4& b);      // v1 != v2 ?
00220 friend std::ostream& operator << (std::ostream& s, vec4& v);// output to stream
00221 friend std::istream& operator >> (std::istream& s, vec4& v);// input from strm.
00222 friend void swap(vec4& a, vec4& b);                         // swap v1 & v2
00223 friend vec4 min(const vec4& a, const vec4& b);              // min(v1, v2)
00224 friend vec4 max(const vec4& a, const vec4& b);              // max(v1, v2)
00225 friend vec4 prod(const vec4& a, const vec4& b);             // term by term *
00226 
00227 // necessary friend declarations
00228 
00229 friend class vec3;
00230 friend class mat4;
00231 friend vec3 operator * (const mat4& a, const vec3& v);      // linear transform
00232 friend mat4 operator * (const mat4& a, const mat4& b);              // matrix 4 product
00233 };
00234 
00235 /****************************************************************
00236 *                                                               *
00237 *                          3x3 Matrix                           *
00238 *                                                               *
00239 ****************************************************************/
00240 
00241 class mat3
00242 {
00243 protected:
00244 
00245  vec3 v[3];
00246 
00247 public:
00248 
00249 // Constructors
00250 
00251 mat3();
00252 mat3(const vec3& v0, const vec3& v1, const vec3& v2);
00253 mat3(const double d);
00254 mat3(const mat3& m);
00255 
00256 // Assignment operators
00257 
00258 mat3& operator  = ( const mat3& m );        // assignment of a mat3
00259 mat3& operator += ( const mat3& m );        // incrementation by a mat3
00260 mat3& operator -= ( const mat3& m );        // decrementation by a mat3
00261 mat3& operator *= ( const double d );       // multiplication by a constant
00262 mat3& operator /= ( const double d );       // division by a constant
00263 vec3& operator [] ( int i);                 // indexing
00264 const vec3& operator [] ( int i) const;             // indexing
00265 
00266 // special functions
00267 
00268 mat3 transpose() const;                     // transpose
00269 mat3 inverse();                             // inverse
00270 mat3& apply(V_FCT_PTR fct);                 // apply a func. to each element
00271 
00272 // friends
00273 
00274 friend mat3 operator - (const mat3& a);                     // -m1
00275 friend mat3 operator + (const mat3& a, const mat3& b);      // m1 + m2
00276 friend mat3 operator - (const mat3& a, const mat3& b);      // m1 - m2
00277 friend mat3 operator * (const mat3& a, const mat3& b);              // m1 * m2
00278 friend mat3 operator * (const mat3& a, const double d);     // m1 * 3.0
00279 friend mat3 operator * (const double d, const mat3& a);     // 3.0 * m1
00280 friend mat3 operator / (const mat3& a, const double d);     // m1 / 3.0
00281 friend int operator == (const mat3& a, const mat3& b);      // m1 == m2 ?
00282 friend int operator != (const mat3& a, const mat3& b);      // m1 != m2 ?
00283 friend std::ostream& operator << (std::ostream& s, mat3& m);// output to stream
00284 friend std::istream& operator >> (std::istream& s, mat3& m);// input from strm.
00285 friend void swap(mat3& a, mat3& b);                         // swap m1 & m2
00286 
00287 // necessary friend declarations
00288 
00289 friend vec3 operator * (const mat3& a, const vec3& v);      // linear transform
00290 friend vec2 operator * (const mat3& a, const vec2& v);      // linear transform
00291 };
00292 
00293 /****************************************************************
00294 *                                                               *
00295 *                          4x4 Matrix                           *
00296 *                                                               *
00297 ****************************************************************/
00298 
00299 class mat4
00300 {
00301 protected:
00302 
00303  vec4 v[4];
00304 
00305 public:
00306 
00307 // Constructors
00308 
00309 mat4();
00310 mat4(const vec4& v0, const vec4& v1, const vec4& v2, const vec4& v3);
00311 mat4(const double d);
00312 mat4(const mat4& m);
00313 
00314 // Assignment operators
00315 
00316 mat4& operator  = ( const mat4& m );        // assignment of a mat4
00317 mat4& operator += ( const mat4& m );        // incrementation by a mat4
00318 mat4& operator -= ( const mat4& m );        // decrementation by a mat4
00319 mat4& operator *= ( const double d );       // multiplication by a constant
00320 mat4& operator /= ( const double d );       // division by a constant
00321 vec4& operator [] ( int i);                 // indexing
00322 const vec4& operator [] ( int i) const;             // indexing
00323 
00324 // special functions
00325 
00326 mat4 transpose() const;                     // transpose
00327 mat4 inverse();                             // inverse
00328 mat4& apply(V_FCT_PTR fct);                 // apply a func. to each element
00329 
00330 // friends
00331 
00332 friend mat4 operator - (const mat4& a);                     // -m1
00333 friend mat4 operator + (const mat4& a, const mat4& b);      // m1 + m2
00334 friend mat4 operator - (const mat4& a, const mat4& b);      // m1 - m2
00335 friend mat4 operator * (const mat4& a, const mat4& b);              // m1 * m2
00336 friend mat4 operator * (const mat4& a, const double d);     // m1 * 4.0
00337 friend mat4 operator * (const double d, const mat4& a);     // 4.0 * m1
00338 friend mat4 operator / (const mat4& a, const double d);     // m1 / 3.0
00339 friend int operator == (const mat4& a, const mat4& b);      // m1 == m2 ?
00340 friend int operator != (const mat4& a, const mat4& b);      // m1 != m2 ?
00341 friend std::ostream& operator << (std::ostream& s, mat4& m);// output to stream
00342 friend std::istream& operator >> (std::istream& s, mat4& m);// input from strm.
00343 friend void swap(mat4& a, mat4& b);                         // swap m1 & m2
00344 
00345 // necessary friend declarations
00346 
00347 friend vec4 operator * (const mat4& a, const vec4& v);      // linear transform
00348 friend vec3 operator * (const mat4& a, const vec3& v);      // linear transform
00349 };
00350 
00351 /****************************************************************
00352 *                                                               *
00353 *              2D functions and 3D functions                    *
00354 *                                                               *
00355 ****************************************************************/
00356 
00357 mat3 identity2D();                                          // identity 2D
00358 mat3 translation2D(const vec2& v);                          // translation 2D
00359 mat3 rotation2D(const vec2& Center, const double angleDeg); // rotation 2D
00360 mat3 scaling2D(const vec2& scaleVector);                    // scaling 2D
00361 mat4 identity3D();                                          // identity 3D
00362 mat4 translation3D(const vec3& v);                          // translation 3D
00363 mat4 rotation3D(const vec3& Axis, const double angleDeg);   // rotation 3D
00364 mat4 scaling3D(const vec3& scaleVector);                    // scaling 3D
00365 mat4 perspective3D(const double d);                         // perspective 3D
00366 
00367 #endif

Generated at Thu Oct 4 18:08:42 2001 for MPQC 2.0.0 using the documentation package Doxygen 1.2.5.