00001
00002
00003
00004
00005
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
00016
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};
00026 enum {PA, PB, PC, PD};
00027 enum {RED, GREEN, BLUE};
00028
00029
00030
00031
00032
00033
00034
00035 class vec2
00036 {
00037 protected:
00038
00039 double n[2];
00040
00041 public:
00042
00043
00044
00045 vec2();
00046 vec2(const double x, const double y);
00047 vec2(const double d);
00048 vec2(const vec2& v);
00049 vec2(const vec3& v);
00050 vec2(const vec3& v, int dropAxis);
00051
00052
00053
00054 vec2& operator = ( const vec2& v );
00055 vec2& operator += ( const vec2& v );
00056 vec2& operator -= ( const vec2& v );
00057 vec2& operator *= ( const double d );
00058 vec2& operator /= ( const double d );
00059 double& operator [] ( int i);
00060 const double& operator[](int i) const;
00061
00062
00063
00064 double length();
00065 double length2();
00066 vec2& normalize();
00067 vec2& apply(V_FCT_PTR fct);
00068
00069
00070
00071 friend vec2 operator - (const vec2& v);
00072 friend vec2 operator + (const vec2& a, const vec2& b);
00073 friend vec2 operator - (const vec2& a, const vec2& b);
00074 friend vec2 operator * (const vec2& a, const double d);
00075 friend vec2 operator * (const double d, const vec2& a);
00076 friend vec2 operator * (const mat3& a, const vec2& v);
00077 friend vec2 operator * (const vec2& v, mat3& a);
00078 friend double operator * (const vec2& a, const vec2& b);
00079 friend vec2 operator / (const vec2& a, const double d);
00080 friend vec3 operator ^ (const vec2& a, const vec2& b);
00081 friend int operator == (const vec2& a, const vec2& b);
00082 friend int operator != (const vec2& a, const vec2& b);
00083 friend std::ostream& operator << (std::ostream& s, vec2& v);
00084 friend std::istream& operator >> (std::istream& s, vec2& v);
00085 friend void swap(vec2& a, vec2& b);
00086 friend vec2 min(const vec2& a, const vec2& b);
00087 friend vec2 max(const vec2& a, const vec2& b);
00088 friend vec2 prod(const vec2& a, const vec2& b);
00089
00090
00091
00092 friend class vec3;
00093 };
00094
00095
00096
00097
00098
00099
00100
00101 class vec3
00102 {
00103 protected:
00104
00105 double n[3];
00106
00107 public:
00108
00109
00110
00111 vec3();
00112 vec3(const double x, const double y, const double z);
00113 vec3(const double d);
00114 vec3(const vec3& v);
00115 vec3(const vec2& v);
00116 vec3(const vec2& v, double d);
00117 vec3(const vec4& v);
00118 vec3(const vec4& v, int dropAxis);
00119
00120
00121
00122 vec3& operator = ( const vec3& v );
00123 vec3& operator += ( const vec3& v );
00124 vec3& operator -= ( const vec3& v );
00125 vec3& operator *= ( const double d );
00126 vec3& operator /= ( const double d );
00127 double& operator [] ( int i);
00128 const double& operator[](int i) const;
00129
00130
00131
00132 double length();
00133 double length2();
00134 vec3& normalize();
00135 vec3& apply(V_FCT_PTR fct);
00136
00137
00138
00139 friend vec3 operator - (const vec3& v);
00140 friend vec3 operator + (const vec3& a, const vec3& b);
00141 friend vec3 operator - (const vec3& a, const vec3& b);
00142 friend vec3 operator * (const vec3& a, const double d);
00143 friend vec3 operator * (const double d, const vec3& a);
00144 friend vec3 operator * (const mat4& a, const vec3& v);
00145 friend vec3 operator * (const vec3& v, const mat4& a);
00146 friend double operator * (const vec3& a, const vec3& b);
00147 friend vec3 operator / (const vec3& a, const double d);
00148 friend vec3 operator ^ (const vec3& a, const vec3& b);
00149 friend int operator == (const vec3& a, const vec3& b);
00150 friend int operator != (const vec3& a, const vec3& b);
00151 friend std::ostream& operator << (std::ostream& s, vec3& v);
00152 friend std::istream& operator >> (std::istream& s, vec3& v);
00153 friend void swap(vec3& a, vec3& b);
00154 friend vec3 min(const vec3& a, const vec3& b);
00155 friend vec3 max(const vec3& a, const vec3& b);
00156 friend vec3 prod(const vec3& a, const vec3& b);
00157
00158
00159
00160 friend class vec2;
00161 friend class vec4;
00162 friend class mat3;
00163 friend vec2 operator * (const mat3& a, const vec2& v);
00164 friend mat3 operator * (const mat3& a, const mat3& b);
00165 };
00166
00167
00168
00169
00170
00171
00172
00173 class vec4
00174 {
00175 protected:
00176
00177 double n[4];
00178
00179 public:
00180
00181
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);
00187 vec4(const vec3& v);
00188 vec4(const vec3& v, const double d);
00189
00190
00191
00192 vec4& operator = ( const vec4& v );
00193 vec4& operator += ( const vec4& v );
00194 vec4& operator -= ( const vec4& v );
00195 vec4& operator *= ( const double d );
00196 vec4& operator /= ( const double d );
00197 double& operator [] ( int i);
00198 const double& operator [] ( int i) const;
00199
00200
00201
00202 double length();
00203 double length2();
00204 vec4& normalize();
00205 vec4& apply(V_FCT_PTR fct);
00206
00207
00208
00209 friend vec4 operator - (const vec4& v);
00210 friend vec4 operator + (const vec4& a, const vec4& b);
00211 friend vec4 operator - (const vec4& a, const vec4& b);
00212 friend vec4 operator * (const vec4& a, const double d);
00213 friend vec4 operator * (const double d, const vec4& a);
00214 friend vec4 operator * (const mat4& a, const vec4& v);
00215 friend vec4 operator * (const vec4& v, const mat4& a);
00216 friend double operator * (const vec4& a, const vec4& b);
00217 friend vec4 operator / (const vec4& a, const double d);
00218 friend int operator == (const vec4& a, const vec4& b);
00219 friend int operator != (const vec4& a, const vec4& b);
00220 friend std::ostream& operator << (std::ostream& s, vec4& v);
00221 friend std::istream& operator >> (std::istream& s, vec4& v);
00222 friend void swap(vec4& a, vec4& b);
00223 friend vec4 min(const vec4& a, const vec4& b);
00224 friend vec4 max(const vec4& a, const vec4& b);
00225 friend vec4 prod(const vec4& a, const vec4& b);
00226
00227
00228
00229 friend class vec3;
00230 friend class mat4;
00231 friend vec3 operator * (const mat4& a, const vec3& v);
00232 friend mat4 operator * (const mat4& a, const mat4& b);
00233 };
00234
00235
00236
00237
00238
00239
00240
00241 class mat3
00242 {
00243 protected:
00244
00245 vec3 v[3];
00246
00247 public:
00248
00249
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
00257
00258 mat3& operator = ( const mat3& m );
00259 mat3& operator += ( const mat3& m );
00260 mat3& operator -= ( const mat3& m );
00261 mat3& operator *= ( const double d );
00262 mat3& operator /= ( const double d );
00263 vec3& operator [] ( int i);
00264 const vec3& operator [] ( int i) const;
00265
00266
00267
00268 mat3 transpose() const;
00269 mat3 inverse();
00270 mat3& apply(V_FCT_PTR fct);
00271
00272
00273
00274 friend mat3 operator - (const mat3& a);
00275 friend mat3 operator + (const mat3& a, const mat3& b);
00276 friend mat3 operator - (const mat3& a, const mat3& b);
00277 friend mat3 operator * (const mat3& a, const mat3& b);
00278 friend mat3 operator * (const mat3& a, const double d);
00279 friend mat3 operator * (const double d, const mat3& a);
00280 friend mat3 operator / (const mat3& a, const double d);
00281 friend int operator == (const mat3& a, const mat3& b);
00282 friend int operator != (const mat3& a, const mat3& b);
00283 friend std::ostream& operator << (std::ostream& s, mat3& m);
00284 friend std::istream& operator >> (std::istream& s, mat3& m);
00285 friend void swap(mat3& a, mat3& b);
00286
00287
00288
00289 friend vec3 operator * (const mat3& a, const vec3& v);
00290 friend vec2 operator * (const mat3& a, const vec2& v);
00291 };
00292
00293
00294
00295
00296
00297
00298
00299 class mat4
00300 {
00301 protected:
00302
00303 vec4 v[4];
00304
00305 public:
00306
00307
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
00315
00316 mat4& operator = ( const mat4& m );
00317 mat4& operator += ( const mat4& m );
00318 mat4& operator -= ( const mat4& m );
00319 mat4& operator *= ( const double d );
00320 mat4& operator /= ( const double d );
00321 vec4& operator [] ( int i);
00322 const vec4& operator [] ( int i) const;
00323
00324
00325
00326 mat4 transpose() const;
00327 mat4 inverse();
00328 mat4& apply(V_FCT_PTR fct);
00329
00330
00331
00332 friend mat4 operator - (const mat4& a);
00333 friend mat4 operator + (const mat4& a, const mat4& b);
00334 friend mat4 operator - (const mat4& a, const mat4& b);
00335 friend mat4 operator * (const mat4& a, const mat4& b);
00336 friend mat4 operator * (const mat4& a, const double d);
00337 friend mat4 operator * (const double d, const mat4& a);
00338 friend mat4 operator / (const mat4& a, const double d);
00339 friend int operator == (const mat4& a, const mat4& b);
00340 friend int operator != (const mat4& a, const mat4& b);
00341 friend std::ostream& operator << (std::ostream& s, mat4& m);
00342 friend std::istream& operator >> (std::istream& s, mat4& m);
00343 friend void swap(mat4& a, mat4& b);
00344
00345
00346
00347 friend vec4 operator * (const mat4& a, const vec4& v);
00348 friend vec3 operator * (const mat4& a, const vec3& v);
00349 };
00350
00351
00352
00353
00354
00355
00356
00357 mat3 identity2D();
00358 mat3 translation2D(const vec2& v);
00359 mat3 rotation2D(const vec2& Center, const double angleDeg);
00360 mat3 scaling2D(const vec2& scaleVector);
00361 mat4 identity3D();
00362 mat4 translation3D(const vec3& v);
00363 mat4 rotation3D(const vec3& Axis, const double angleDeg);
00364 mat4 scaling3D(const vec3& scaleVector);
00365 mat4 perspective3D(const double d);
00366
00367 #endif