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