00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <math.h>
00022 #include <iostream>
00023 #include <list>
00024 #include "Trace.h"
00025
00026 using namespace std;
00027
00028 #ifndef DADA
00029 #define DADA
00030
00031 namespace Fluxus
00032 {
00033
00034 static const float TWO_PI=3.141592654*2.0f;
00035 static const float DEG_CONV = 0.017453292;
00036 static const float RAD_CONV = 1/0.017453292;
00037
00038 inline void debug(char *s) {Trace::Stream<<"dada debug: "<<s<<endl;}
00039
00040 void InitDada();
00041 float RandFloat();
00042 float RandRange(float L, float H);
00043 void dSinCos(float a, float &s, float &c);
00044
00045 class dVector
00046 {
00047 public:
00048 float x,y,z,w;
00049 dVector() { x=y=z=0; w=1; }
00050 dVector(float X, float Y, float Z, float W=1) { x=X; y=Y; z=Z; w=W; }
00051 dVector(dVector const &c) { *this=c; }
00052
00053 float *arr() { return &x; }
00054 int operator==(dVector const &rhs) { return (x==rhs.x&&y==rhs.y&&z==rhs.z); }
00055
00056 inline dVector &operator=(dVector const &rhs)
00057 {
00058 x=rhs.x; y=rhs.y; z=rhs.z; w=rhs.w;
00059 return *this;
00060 }
00061
00062 inline dVector operator+(dVector const &rhs) const
00063 {
00064 dVector t;
00065 t.x=x+rhs.x; t.y=y+rhs.y; t.z=z+rhs.z;
00066 return t;
00067 }
00068
00069 inline dVector operator-(dVector const &rhs) const
00070 {
00071 dVector t;
00072 t.x=x-rhs.x; t.y=y-rhs.y; t.z=z-rhs.z;
00073 return t;
00074 }
00075
00076 inline dVector operator*(dVector const &rhs) const
00077 {
00078 dVector t;
00079 t.x=x*rhs.x; t.y=y*rhs.y; t.z=z*rhs.z;
00080 return t;
00081 }
00082
00083 inline dVector operator/(dVector const &rhs) const
00084 {
00085 dVector t;
00086 t.x=x/rhs.x; t.y=y/rhs.y; t.z=z/rhs.z;
00087 return t;
00088 }
00089
00090 inline dVector operator+(float rhs) const
00091 {
00092 dVector t;
00093 t.x=x+rhs; t.y=y+rhs; t.z=z+rhs;
00094 return t;
00095 }
00096
00097 inline dVector operator-(float rhs) const
00098 {
00099 dVector t;
00100 t.x=x-rhs; t.y=y-rhs; t.z=z-rhs;
00101 return t;
00102 }
00103
00104 inline dVector operator*(float rhs) const
00105 {
00106 dVector t;
00107 t.x=x*rhs; t.y=y*rhs; t.z=z*rhs;
00108 return t;
00109 }
00110
00111 inline dVector operator/(float rhs) const
00112 {
00113 dVector t;
00114 t.x=x/rhs; t.y=y/rhs; t.z=z/rhs;
00115 return t;
00116 }
00117
00118 inline dVector &operator+=(dVector const &rhs)
00119 {
00120 x+=rhs.x; y+=rhs.y; z+=rhs.z;
00121 return *this;
00122 }
00123
00124 inline dVector &operator-=(dVector const &rhs)
00125 {
00126 x-=rhs.x; y-=rhs.y; z-=rhs.z;
00127 return *this;
00128 }
00129
00130 inline dVector &operator*=(float rhs)
00131 {
00132 x*=rhs; y*=rhs; z*=rhs;
00133 return *this;
00134 }
00135
00136 inline dVector &operator/=(float rhs)
00137 {
00138 if (rhs) {x/=rhs; y/=rhs; z/=rhs;}
00139 return *this;
00140 }
00141
00142 inline float dot(dVector const &rhs) const
00143 {
00144 return x*rhs.x+y*rhs.y+z*rhs.z;
00145 }
00146
00147 inline dVector cross(dVector const &rhs) const
00148 {
00149 return dVector(y*rhs.z - z*rhs.y,
00150 z*rhs.x - x*rhs.z,
00151 x*rhs.y - y*rhs.x);
00152 }
00153
00154 inline dVector reflect(dVector const &rhs) const
00155 {
00156 float vdn=dot(rhs)*2.0f;
00157 return (*this)-rhs*vdn;
00158 }
00159
00160 inline float dist(dVector const &rhs) const
00161 {
00162 return sqrt((rhs.x-x)*(rhs.x-x)+
00163 (rhs.y-y)*(rhs.y-y)+
00164 (rhs.z-z)*(rhs.z-z));
00165 }
00166
00167 inline float distsq(dVector const &rhs) const
00168 {
00169 return (rhs.x-x)*(rhs.x-x)+
00170 (rhs.y-y)*(rhs.y-y)+
00171 (rhs.z-z)*(rhs.z-z);
00172 }
00173
00174 inline void get_euler(float &rx, float &ry, float &rz) const
00175 {
00176 if (z==0) rx=0;
00177 else rx=atan(y/z)*RAD_CONV;
00178 if (x==0) ry=0;
00179 else ry=atan(z/x)*RAD_CONV;
00180 if (y==0) rz=0;
00181 else rz=atan(x/y)*RAD_CONV;
00182 }
00183
00184 inline float mag()
00185 {
00186 return dist(dVector(0,0,0));
00187 }
00188
00189 inline bool feq(const dVector &other, float epsilon=0.001)
00190 {
00191 return (fabs(x-other.x)<epsilon && fabs(y-other.y)<epsilon && fabs(z-other.z)<epsilon);
00192 }
00193
00194 inline void homog() { if (w && w!=1.0) { x/=w; y/=w; z/=w; w=1; } }
00195 inline dVector &normalise() { *this/=mag(); return *this; }
00196 void get_rot(float m[16],dVector up);
00197 private:
00198 };
00199
00200 dVector operator-(dVector rhs);
00201 ostream &operator<<(ostream &os, dVector const &om);
00202 istream &operator>>(istream &is, dVector &om);
00203
00205
00206
00207 enum COLOUR_MODE
00208 {
00209 MODE_RGB = 0,
00210 MODE_HSV
00211 };
00212
00213 class dColour
00214 {
00215 public:
00216 float r,g,b,a;
00217 dColour() {r=g=b=0; a=1;}
00218 dColour(float x, float y, float z, float w=1, COLOUR_MODE mode=MODE_RGB);
00219 dColour(float *xyzw, COLOUR_MODE mode=MODE_RGB);
00220 dColour(float G, float A=1) {r=g=b=G; a=A;}
00221 dColour(dColour const &c) {*this=c;}
00222
00223 float *arr() { return &r; }
00224
00225 inline dColour &operator=(dColour const &rhs)
00226 {
00227 r=rhs.r; g=rhs.g; b=rhs.b; a=rhs.a;
00228 return *this;
00229 }
00230
00231 inline dColour operator+(dColour const &rhs) const
00232 {
00233 dColour t;
00234 t.r=r+rhs.r; t.g=g+rhs.g; t.b=b+rhs.b; t.a=a+rhs.a;
00235 return t;
00236 }
00237
00238 inline dColour operator-(dColour const &rhs) const
00239 {
00240 dColour t;
00241 t.r=r-rhs.r; t.g=g-rhs.g; t.b=b-rhs.b; t.a=a-rhs.a;
00242 return t;
00243 }
00244
00245 inline dColour operator*(dColour const &rhs) const
00246 {
00247 dColour t;
00248 t.r=r*rhs.r; t.g=g*rhs.g; t.b=b*rhs.b; t.a=a*rhs.a;
00249 return t;
00250 }
00251
00252 inline dColour operator/(dColour const &rhs) const
00253 {
00254 dColour t;
00255 t.r=r/rhs.r; t.g=g/rhs.g; t.b=b/rhs.b; t.a=a/rhs.a;
00256 return t;
00257 }
00258
00259 inline dColour operator+(float rhs) const
00260 {
00261 dColour t;
00262 t.r=r+rhs; t.g=g+rhs; t.b=b+rhs; t.a=a+rhs;
00263 return t;
00264 }
00265
00266 inline dColour operator-(float rhs) const
00267 {
00268 dColour t;
00269 t.r=r-rhs; t.g=g-rhs; t.b=b-rhs; t.a=a-rhs;
00270 return t;
00271 }
00272
00273 inline dColour operator*(float rhs) const
00274 {
00275 dColour t;
00276 t.r=r*rhs; t.g=g*rhs; t.b=b*rhs; t.a=a*rhs;
00277 return t;
00278 }
00279
00280 inline dColour operator/(float rhs) const
00281 {
00282 dColour t;
00283 t.r=r/rhs; t.g=g/rhs; t.b=b/rhs; t.a=a/rhs;
00284 return t;
00285 }
00286
00287 inline dColour &operator+=(dColour const &rhs)
00288 {
00289 r+=rhs.r; g+=rhs.g; b+=rhs.b; a+=rhs.a;
00290 return *this;
00291 }
00292
00293 inline dColour &operator-=(dColour const &rhs)
00294 {
00295 r-=rhs.r; g-=rhs.g; b-=rhs.b; a-=rhs.a;
00296 return *this;
00297 }
00298
00299 inline dColour &operator*=(float rhs)
00300 {
00301 r*=rhs; g*=rhs; b*=rhs; a*=rhs;
00302 return *this;
00303 }
00304
00305 inline dColour &operator/=(float rhs)
00306 {
00307 if (rhs) {r/=rhs; g/=rhs; b/=rhs; a/=rhs;}
00308 return *this;
00309 }
00310
00311 inline void clamp()
00312 {
00313 if (r<0) r=0; if (g<0) g=0; if (b<0) b=0; if (a<0) a=0;
00314 if (r>1) r=1; if (g>1) g=1; if (b>1) b=1; if (a>1) a=1;
00315 }
00316
00317 static void RGBtoHSV(float r, float g, float b, float *hsv);
00318 static void HSVtoRGB(float h, float s, float v, float *rgb);
00319
00320 private:
00321 };
00322
00323 ostream &operator<<(ostream &os, dColour const &om);
00324
00326
00327 class dVertex
00328 {
00329 public:
00330 dVertex() {}
00331 dVertex(dVector p, dVector n, float S=0, float T=0) {point=p; normal=n; s=S; t=T;}
00332 dVertex(dVector p, dVector n, dColour c, float S=0, float T=0) { point=p; normal=n; col=c; s=S; t=T;}
00333 dVertex(dVertex const &rhs) {*this=rhs;}
00334 dVertex const &operator=(dVertex const &rhs);
00335 void homog() {point.homog(); normal.homog();}
00336 friend ostream&operator<<(ostream &os, dVertex const &v);
00337
00338 dVector point;
00339 dVector normal;
00340 dColour col;
00341 float s,t;
00342 private:
00343 };
00344
00345 class dMatrix
00346 {
00347 public:
00348 dMatrix() { init(); }
00349 dMatrix(const dMatrix &other) { (*this)=other; }
00350
00351 dMatrix(float m00, float m10, float m20, float m30,
00352 float m01, float m11, float m21, float m31,
00353 float m02, float m12, float m22, float m32,
00354 float m03, float m13, float m23, float m33)
00355 {
00356 m[0][0]=m00; m[1][0]=m10; m[2][0]=m20; m[3][0]=m30;
00357 m[0][1]=m01; m[1][1]=m11; m[2][1]=m21; m[3][1]=m31;
00358 m[0][2]=m02; m[1][2]=m12; m[2][2]=m22; m[3][2]=m32;
00359 m[0][3]=m03; m[1][3]=m13; m[2][3]=m23; m[3][3]=m33;
00360 }
00361
00362 inline float *arr() { return &m[0][0]; }
00363
00364 inline void init()
00365 {
00366 zero();
00367 m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
00368 }
00369
00370 inline void zero()
00371 {
00372 memset(m,0,sizeof(float)*16);
00373 }
00374
00375 inline dVector get_hori_i() {return dVector(m[0][0],m[1][0],m[2][0]);}
00376 inline dVector get_hori_j() {return dVector(m[0][1],m[1][1],m[2][1]);}
00377 inline dVector get_hori_k() {return dVector(m[0][2],m[1][2],m[2][2]);}
00378 inline dVector get_vert_i() {return dVector(m[0][0],m[0][1],m[0][2]);}
00379 inline dVector get_vert_j() {return dVector(m[1][0],m[1][1],m[1][2]);}
00380 inline dVector get_vert_k() {return dVector(m[2][0],m[2][1],m[2][2]);}
00381
00382 inline const dMatrix &operator=(dMatrix const &rhs)
00383 {
00384 m[0][0]=rhs.m[0][0]; m[0][1]=rhs.m[0][1]; m[0][2]=rhs.m[0][2]; m[0][3]=rhs.m[0][3];
00385 m[1][0]=rhs.m[1][0]; m[1][1]=rhs.m[1][1]; m[1][2]=rhs.m[1][2]; m[1][3]=rhs.m[1][3];
00386 m[2][0]=rhs.m[2][0]; m[2][1]=rhs.m[2][1]; m[2][2]=rhs.m[2][2]; m[2][3]=rhs.m[2][3];
00387 m[3][0]=rhs.m[3][0]; m[3][1]=rhs.m[3][1]; m[3][2]=rhs.m[3][2]; m[3][3]=rhs.m[3][3];
00388 return rhs;
00389 }
00390
00391 inline dMatrix operator+(dMatrix const &rhs) const
00392 {
00393 dMatrix t;
00394 for (int i=0; i<4; i++)
00395 {
00396 for (int j=0; j<4; j++)
00397 {
00398 t.m[i][j]=m[i][j]+rhs.m[i][j];
00399 }
00400 }
00401 return t;
00402 }
00403
00404 inline dMatrix operator-(dMatrix const &rhs) const
00405 {
00406 dMatrix t;
00407 for (int i=0; i<4; i++)
00408 {
00409 for (int j=0; j<4; j++)
00410 {
00411 t.m[i][j]=m[i][j]-rhs.m[i][j];
00412 }
00413 }
00414 return t;
00415 }
00416
00417 inline dMatrix operator*(dMatrix const &rhs) const
00418 {
00419
00420
00421
00422
00423
00424
00425
00426
00427 dMatrix t;
00428
00429
00430
00431
00432
00433 t.m[0][0]=m[0][0]*rhs.m[0][0]+m[1][0]*rhs.m[0][1]+m[2][0]*rhs.m[0][2]+m[3][0]*rhs.m[0][3];
00434 t.m[0][1]=m[0][1]*rhs.m[0][0]+m[1][1]*rhs.m[0][1]+m[2][1]*rhs.m[0][2]+m[3][1]*rhs.m[0][3];
00435 t.m[0][2]=m[0][2]*rhs.m[0][0]+m[1][2]*rhs.m[0][1]+m[2][2]*rhs.m[0][2]+m[3][2]*rhs.m[0][3];
00436 t.m[0][3]=m[0][3]*rhs.m[0][0]+m[1][3]*rhs.m[0][1]+m[2][3]*rhs.m[0][2]+m[3][3]*rhs.m[0][3];
00437
00438 t.m[1][0]=m[0][0]*rhs.m[1][0]+m[1][0]*rhs.m[1][1]+m[2][0]*rhs.m[1][2]+m[3][0]*rhs.m[1][3];
00439 t.m[1][1]=m[0][1]*rhs.m[1][0]+m[1][1]*rhs.m[1][1]+m[2][1]*rhs.m[1][2]+m[3][1]*rhs.m[1][3];
00440 t.m[1][2]=m[0][2]*rhs.m[1][0]+m[1][2]*rhs.m[1][1]+m[2][2]*rhs.m[1][2]+m[3][2]*rhs.m[1][3];
00441 t.m[1][3]=m[0][3]*rhs.m[1][0]+m[1][3]*rhs.m[1][1]+m[2][3]*rhs.m[1][2]+m[3][3]*rhs.m[1][3];
00442
00443 t.m[2][0]=m[0][0]*rhs.m[2][0]+m[1][0]*rhs.m[2][1]+m[2][0]*rhs.m[2][2]+m[3][0]*rhs.m[2][3];
00444 t.m[2][1]=m[0][1]*rhs.m[2][0]+m[1][1]*rhs.m[2][1]+m[2][1]*rhs.m[2][2]+m[3][1]*rhs.m[2][3];
00445 t.m[2][2]=m[0][2]*rhs.m[2][0]+m[1][2]*rhs.m[2][1]+m[2][2]*rhs.m[2][2]+m[3][2]*rhs.m[2][3];
00446 t.m[2][3]=m[0][3]*rhs.m[2][0]+m[1][3]*rhs.m[2][1]+m[2][3]*rhs.m[2][2]+m[3][3]*rhs.m[2][3];
00447
00448 t.m[3][0]=m[0][0]*rhs.m[3][0]+m[1][0]*rhs.m[3][1]+m[2][0]*rhs.m[3][2]+m[3][0]*rhs.m[3][3];
00449 t.m[3][1]=m[0][1]*rhs.m[3][0]+m[1][1]*rhs.m[3][1]+m[2][1]*rhs.m[3][2]+m[3][1]*rhs.m[3][3];
00450 t.m[3][2]=m[0][2]*rhs.m[3][0]+m[1][2]*rhs.m[3][1]+m[2][2]*rhs.m[3][2]+m[3][2]*rhs.m[3][3];
00451 t.m[3][3]=m[0][3]*rhs.m[3][0]+m[1][3]*rhs.m[3][1]+m[2][3]*rhs.m[3][2]+m[3][3]*rhs.m[3][3];
00452
00453 return t;
00454 }
00455
00456 inline dMatrix operator/(dMatrix const &rhs) const
00457 {
00458 dMatrix t;
00459 for (int i=0; i<4; i++)
00460 {
00461 for (int j=0; j<4; j++)
00462 {
00463 t.m[i][j]=m[i][0]/rhs.m[0][j]+
00464 m[i][1]/rhs.m[1][j]+
00465 m[i][2]/rhs.m[2][j]+
00466 m[i][3]/rhs.m[3][j];
00467 }
00468 }
00469 return t;
00470 }
00471
00472 inline dMatrix operator+(float rhs) const
00473 {
00474 dMatrix t;
00475 for (int i=0; i<4; i++)
00476 {
00477 for (int j=0; j<4; j++)
00478 {
00479 t.m[i][j]=m[i][j]+rhs;
00480 }
00481 }
00482 return t;
00483 }
00484
00485 inline dMatrix operator-(float rhs) const
00486 {
00487 dMatrix t;
00488 for (int i=0; i<4; i++)
00489 {
00490 for (int j=0; j<4; j++)
00491 {
00492 t.m[i][j]=m[i][j]-rhs;
00493 }
00494 }
00495 return t;
00496 }
00497
00498 inline dMatrix operator*(float rhs) const
00499 {
00500 dMatrix t;
00501 for (int i=0; i<4; i++)
00502 {
00503 for (int j=0; j<4; j++)
00504 {
00505 t.m[i][j]=m[i][j]*rhs;
00506 }
00507 }
00508 return t;
00509 }
00510
00511 inline dMatrix operator/(float rhs) const
00512 {
00513 dMatrix t;
00514 for (int i=0; i<4; i++)
00515 {
00516 for (int j=0; j<4; j++)
00517 {
00518 t.m[i][j]=m[i][j]/rhs;
00519 }
00520 }
00521 return t;
00522 }
00523
00524 inline dMatrix &operator+=(dMatrix const &rhs)
00525 {
00526 for (int i=0; i<4; i++)
00527 {
00528 for (int j=0; j<4; j++)
00529 {
00530 m[i][j]+=rhs.m[i][j];
00531 }
00532 }
00533 return *this;
00534 }
00535
00536 inline dMatrix &operator-=(dMatrix const &rhs)
00537 {
00538 for (int i=0; i<4; i++)
00539 {
00540 for (int j=0; j<4; j++)
00541 {
00542 m[i][j]-=rhs.m[i][j];
00543 }
00544 }
00545 return *this;
00546 }
00547
00548 inline dMatrix &operator*=(dMatrix const &rhs)
00549 {
00550 *this=*this*rhs;
00551 return *this;
00552 }
00553
00554 inline dMatrix &operator/=(dMatrix const &rhs)
00555 {
00556 *this=*this/rhs;
00557 return *this;
00558 }
00559
00560 inline dMatrix &translate(float x, float y, float z)
00561 {
00562 dMatrix t;
00563 t.m[3][0]=x;
00564 t.m[3][1]=y;
00565 t.m[3][2]=z;
00566 *this=*this*t;
00567 return *this;
00568
00569 }
00570
00571 inline dMatrix &translate(dVector &tr)
00572 {
00573 dMatrix t;
00574 t.m[3][0]=tr.x;
00575 t.m[3][1]=tr.y;
00576 t.m[3][2]=tr.z;
00577 *this=*this*t;
00578 return *this;
00579
00580 }
00581
00582 inline void settranslate(dVector &tr)
00583 {
00584 m[3][0]=tr.x;
00585 m[3][1]=tr.y;
00586 m[3][2]=tr.z;
00587 }
00588
00589 inline dVector gettranslate() const
00590 {
00591 return dVector(m[3][0],m[3][1],m[3][2]);
00592 }
00593
00594
00595
00596 inline dMatrix &rotxyz(float x,float y,float z)
00597 {
00598 dMatrix t;
00599 if (x)
00600 {
00601 x*=0.017453292;
00602
00603 #ifdef USE_FAST_SINCOS
00604 float sx,cx;
00605 dSinCos(x,sx,cx);
00606 #else
00607 float sx=sin(x);
00608 float cx=cos(x);
00609 #endif
00610
00611 t.m[1][1]=cx;
00612 t.m[2][1]=-sx;
00613 t.m[1][2]=sx;
00614 t.m[2][2]=cx;
00615 *this=*this*t;
00616 }
00617
00618 if (y)
00619 {
00620 y*=0.017453292;
00621
00622 #ifdef USE_FAST_SINCOS
00623 float sy,cy;
00624 dSinCos(y,sy,cy);
00625 #else
00626 float sy=sin(y);
00627 float cy=cos(y);
00628 #endif
00629
00630 t.init();
00631 t.m[0][0]=cy;
00632 t.m[2][0]=-sy;
00633 t.m[0][2]=sy;
00634 t.m[2][2]=cy;
00635 *this=*this*t;
00636 }
00637
00638 if (z)
00639 {
00640 z*=0.017453292;
00641
00642 #ifdef USE_FAST_SINCOS
00643 float sz,cz;
00644 dSinCos(z,sz,cz);
00645 #else
00646 float sz=sin(z);
00647 float cz=cos(z);
00648 #endif
00649
00650 t.init();
00651 t.m[0][0]=cz;
00652 t.m[1][0]=-sz;
00653 t.m[0][1]=sz;
00654 t.m[1][1]=cz;
00655 *this=*this*t;
00656 }
00657
00658 return *this;
00659 }
00660
00661 inline dMatrix &rotx(float a)
00662 {
00663 a*=0.017453292;
00664 dMatrix t;
00665
00666 t.m[1][1]=cos(a);
00667 t.m[2][1]=-sin(a);
00668 t.m[1][2]=sin(a);
00669 t.m[2][2]=cos(a);
00670
00671 *this=*this*t;
00672 return *this;
00673 }
00674
00675 inline dMatrix &roty(float a)
00676 {
00677 a*=0.017453292;
00678 dMatrix t;
00679
00680 t.m[0][0]=cos(a);
00681 t.m[2][0]=-sin(a);
00682 t.m[0][2]=sin(a);
00683 t.m[2][2]=cos(a);
00684
00685 *this=*this*t;
00686 return *this;
00687 }
00688
00689 inline dMatrix &rotz(float a)
00690 {
00691 a*=0.017453292;
00692 dMatrix t;
00693
00694 t.m[0][0]=cos(a);
00695 t.m[1][0]=-sin(a);
00696 t.m[0][1]=sin(a);
00697 t.m[1][1]=cos(a);
00698
00699 *this=*this*t;
00700 return *this;
00701 }
00702
00703 inline dMatrix &scale(float x, float y, float z)
00704 {
00705 dMatrix t;
00706
00707 t.m[0][0]=x;
00708 t.m[1][1]=y;
00709 t.m[2][2]=z;
00710
00711 *this=*this*t;
00712 return *this;
00713 }
00714
00715 inline dVector transform(dVector const &p) const
00716 {
00717 dVector t;
00718 t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0] + p.w*m[3][0];
00719 t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1] + p.w*m[3][1];
00720 t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2] + p.w*m[3][2];
00721 t.w=p.x*m[0][3] + p.y*m[1][3] + p.z*m[2][3] + p.w*m[3][3];
00722 return t;
00723 }
00724
00725 inline dVector transform_persp(dVector const &p) const
00726 {
00727 dVector t;
00728 t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0] + p.w*m[3][0];
00729 t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1] + p.w*m[3][1];
00730 t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2] + p.w*m[3][2];
00731 t.w=p.x*m[0][3] + p.y*m[1][3] + p.z*m[2][3] + p.w*m[3][3];
00732 t.homog();
00733 return t;
00734 }
00735
00736 inline dVertex transform(dVertex const &p) const
00737 {
00738 dVertex t=p;
00739 t.point=transform(p.point);
00740 t.normal=transform_no_trans(p.normal);
00741 return t;
00742 }
00743
00744 inline dVector transform_no_trans(dVector const &p) const
00745 {
00746 dVector t;
00747 t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0];
00748 t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1];
00749 t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2];
00750 t.w=p.w;
00751 return t;
00752 }
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762 inline void load_glmatrix(float glm[16])
00763 {
00764 glm[0]= m[0][0]; glm[4]= m[1][0]; glm[8]= m[2][0]; glm[12]= m[3][0];
00765 glm[1]= m[0][1]; glm[5]= m[1][1]; glm[9]= m[2][1]; glm[13]= m[3][1];
00766 glm[2]= m[0][2]; glm[6]= m[1][2]; glm[10]=m[2][2]; glm[14]=m[3][2];
00767 glm[3]= m[0][3]; glm[7]= m[1][3]; glm[11]=m[2][3]; glm[15]=m[3][3];
00768 }
00769
00770 inline void load_dMatrix(float glm[16])
00771 {
00772 m[0][0]=glm[0]; m[1][0]=glm[4]; m[2][0]=glm[8]; m[3][0]=glm[12];
00773 m[0][1]=glm[1]; m[1][1]=glm[5]; m[2][1]=glm[9]; m[3][1]=glm[13];
00774 m[0][2]=glm[2]; m[1][2]=glm[6]; m[2][2]=glm[10]; m[3][2]=glm[14];
00775 m[0][3]=glm[3]; m[1][3]=glm[7]; m[2][3]=glm[11]; m[3][3]=glm[15];
00776 }
00777
00778 inline void transpose()
00779 {
00780 dMatrix t;
00781 for (int i=0; i<4; i++)
00782 {
00783 for (int j=0; j<4; j++)
00784 {
00785 t.m[i][j]=m[j][i];
00786 }
00787 }
00788 *this=t;
00789 }
00790
00791 inline dMatrix inverse() const
00792 {
00793 dMatrix temp;
00794 temp.m[0][0] = m[1][2]*m[2][3]*m[3][1] - m[1][3]*m[2][2]*m[3][1] + m[1][3]*m[2][1]*m[3][2] - m[1][1]*m[2][3]*m[3][2] - m[1][2]*m[2][1]*m[3][3] + m[1][1]*m[2][2]*m[3][3];
00795 temp.m[0][1] = m[0][3]*m[2][2]*m[3][1] - m[0][2]*m[2][3]*m[3][1] - m[0][3]*m[2][1]*m[3][2] + m[0][1]*m[2][3]*m[3][2] + m[0][2]*m[2][1]*m[3][3] - m[0][1]*m[2][2]*m[3][3];
00796 temp.m[0][2] = m[0][2]*m[1][3]*m[3][1] - m[0][3]*m[1][2]*m[3][1] + m[0][3]*m[1][1]*m[3][2] - m[0][1]*m[1][3]*m[3][2] - m[0][2]*m[1][1]*m[3][3] + m[0][1]*m[1][2]*m[3][3];
00797 temp.m[0][3] = m[0][3]*m[1][2]*m[2][1] - m[0][2]*m[1][3]*m[2][1] - m[0][3]*m[1][1]*m[2][2] + m[0][1]*m[1][3]*m[2][2] + m[0][2]*m[1][1]*m[2][3] - m[0][1]*m[1][2]*m[2][3];
00798 temp.m[1][0] = m[1][3]*m[2][2]*m[3][0] - m[1][2]*m[2][3]*m[3][0] - m[1][3]*m[2][0]*m[3][2] + m[1][0]*m[2][3]*m[3][2] + m[1][2]*m[2][0]*m[3][3] - m[1][0]*m[2][2]*m[3][3];
00799 temp.m[1][1] = m[0][2]*m[2][3]*m[3][0] - m[0][3]*m[2][2]*m[3][0] + m[0][3]*m[2][0]*m[3][2] - m[0][0]*m[2][3]*m[3][2] - m[0][2]*m[2][0]*m[3][3] + m[0][0]*m[2][2]*m[3][3];
00800 temp.m[1][2] = m[0][3]*m[1][2]*m[3][0] - m[0][2]*m[1][3]*m[3][0] - m[0][3]*m[1][0]*m[3][2] + m[0][0]*m[1][3]*m[3][2] + m[0][2]*m[1][0]*m[3][3] - m[0][0]*m[1][2]*m[3][3];
00801 temp.m[1][3] = m[0][2]*m[1][3]*m[2][0] - m[0][3]*m[1][2]*m[2][0] + m[0][3]*m[1][0]*m[2][2] - m[0][0]*m[1][3]*m[2][2] - m[0][2]*m[1][0]*m[2][3] + m[0][0]*m[1][2]*m[2][3];
00802 temp.m[2][0] = m[1][1]*m[2][3]*m[3][0] - m[1][3]*m[2][1]*m[3][0] + m[1][3]*m[2][0]*m[3][1] - m[1][0]*m[2][3]*m[3][1] - m[1][1]*m[2][0]*m[3][3] + m[1][0]*m[2][1]*m[3][3];
00803 temp.m[2][1] = m[0][3]*m[2][1]*m[3][0] - m[0][1]*m[2][3]*m[3][0] - m[0][3]*m[2][0]*m[3][1] + m[0][0]*m[2][3]*m[3][1] + m[0][1]*m[2][0]*m[3][3] - m[0][0]*m[2][1]*m[3][3];
00804 temp.m[2][2] = m[0][1]*m[1][3]*m[3][0] - m[0][3]*m[1][1]*m[3][0] + m[0][3]*m[1][0]*m[3][1] - m[0][0]*m[1][3]*m[3][1] - m[0][1]*m[1][0]*m[3][3] + m[0][0]*m[1][1]*m[3][3];
00805 temp.m[2][3] = m[0][3]*m[1][1]*m[2][0] - m[0][1]*m[1][3]*m[2][0] - m[0][3]*m[1][0]*m[2][1] + m[0][0]*m[1][3]*m[2][1] + m[0][1]*m[1][0]*m[2][3] - m[0][0]*m[1][1]*m[2][3];
00806 temp.m[3][0] = m[1][2]*m[2][1]*m[3][0] - m[1][1]*m[2][2]*m[3][0] - m[1][2]*m[2][0]*m[3][1] + m[1][0]*m[2][2]*m[3][1] + m[1][1]*m[2][0]*m[3][2] - m[1][0]*m[2][1]*m[3][2];
00807 temp.m[3][1] = m[0][1]*m[2][2]*m[3][0] - m[0][2]*m[2][1]*m[3][0] + m[0][2]*m[2][0]*m[3][1] - m[0][0]*m[2][2]*m[3][1] - m[0][1]*m[2][0]*m[3][2] + m[0][0]*m[2][1]*m[3][2];
00808 temp.m[3][2] = m[0][2]*m[1][1]*m[3][0] - m[0][1]*m[1][2]*m[3][0] - m[0][2]*m[1][0]*m[3][1] + m[0][0]*m[1][2]*m[3][1] + m[0][1]*m[1][0]*m[3][2] - m[0][0]*m[1][1]*m[3][2];
00809 temp.m[3][3] = m[0][1]*m[1][2]*m[2][0] - m[0][2]*m[1][1]*m[2][0] + m[0][2]*m[1][0]*m[2][1] - m[0][0]*m[1][2]*m[2][1] - m[0][1]*m[1][0]*m[2][2] + m[0][0]*m[1][1]*m[2][2];
00810 float scale=1/temp.determinant();
00811 temp.scale(scale,scale,scale);
00812 return temp;
00813 }
00814
00815 inline float determinant() const
00816 {
00817 return
00818 m[0][3] * m[1][2] * m[2][1] * m[3][0]-m[0][2] * m[1][3] * m[2][1] * m[3][0]-m[0][3] * m[1][1] * m[2][2] * m[3][0]+m[0][1] * m[1][3] * m[2][2] * m[3][0]+
00819 m[0][2] * m[1][1] * m[2][3] * m[3][0]-m[0][1] * m[1][2] * m[2][3] * m[3][0]-m[0][3] * m[1][2] * m[2][0] * m[3][1]+m[0][2] * m[1][3] * m[2][0] * m[3][1]+
00820 m[0][3] * m[1][0] * m[2][2] * m[3][1]-m[0][0] * m[1][3] * m[2][2] * m[3][1]-m[0][2] * m[1][0] * m[2][3] * m[3][1]+m[0][0] * m[1][2] * m[2][3] * m[3][1]+
00821 m[0][3] * m[1][1] * m[2][0] * m[3][2]-m[0][1] * m[1][3] * m[2][0] * m[3][2]-m[0][3] * m[1][0] * m[2][1] * m[3][2]+m[0][0] * m[1][3] * m[2][1] * m[3][2]+
00822 m[0][1] * m[1][0] * m[2][3] * m[3][2]-m[0][0] * m[1][1] * m[2][3] * m[3][2]-m[0][2] * m[1][1] * m[2][0] * m[3][3]+m[0][1] * m[1][2] * m[2][0] * m[3][3]+
00823 m[0][2] * m[1][0] * m[2][1] * m[3][3]-m[0][0] * m[1][2] * m[2][1] * m[3][3]-m[0][1] * m[1][0] * m[2][2] * m[3][3]+m[0][0] * m[1][1] * m[2][2] * m[3][3];
00824 }
00825
00826 inline void remove_scale()
00827 {
00828 dVector xvec = get_hori_i().normalise();
00829 dVector yvec = get_hori_j().normalise();
00830 dVector zvec = get_hori_k().normalise();
00831
00832 m[0][0]=xvec.x; m[1][0]=xvec.y; m[2][0]=xvec.z;
00833 m[0][1]=yvec.x; m[1][1]=yvec.y; m[2][1]=yvec.z;
00834 m[0][2]=zvec.x; m[1][2]=zvec.y; m[2][2]=zvec.z;
00835 }
00836
00837 inline void extract_euler(float &x, float &y, float &z) const
00838 {
00839 dMatrix t=*this;
00840 t.remove_scale();
00841 if (t.m[2][2]==0) x=0;
00842 else x = atan(t.m[1][2]/t.m[2][2])*RAD_CONV;
00843 y = asin(-t.m[0][2])*RAD_CONV;
00844 if (t.m[0][0]==0) z=0;
00845 else z=atan(t.m[0][1]/t.m[0][0])*RAD_CONV;
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858 }
00859
00860 inline void aim(dVector v, dVector up)
00861 {
00862 v.normalise();
00863 dVector l=v.cross(up);
00864 dVector u=v.cross(l);
00865 l.normalise();
00866 u.normalise();
00867
00868 m[0][0]=v.x; m[0][1]=v.y; m[0][2]=v.z;
00869 m[1][0]=l.x; m[1][1]=l.y; m[1][2]=l.z;
00870 m[2][0]=u.x; m[2][1]=u.y; m[2][2]=u.z;
00871 }
00872
00873 inline void blend(dMatrix other, float amount)
00874 {
00875 for (int j=0; j<4; j++)
00876 {
00877 for (int i=0; i<4; i++)
00878 {
00879 m[i][j]=(1-amount)*m[i][j]+amount*other.m[i][j];
00880 }
00881 }
00882 }
00883
00884 friend ostream &operator<<(ostream &os, dMatrix const &om);
00885
00886 float m[4][4];
00887 };
00888
00889 ostream &operator<<(ostream &os, dMatrix const &om);
00890
00891 class dBoundingBox
00892 {
00893 public:
00894 dBoundingBox() : m_Empty(true) {}
00895 dBoundingBox(const dVector &cmin, const dVector &cmax) : min(cmin), max(cmax) {}
00896 virtual ~dBoundingBox() {}
00897
00898 bool empty() { return m_Empty; }
00899 void expand(dVector v);
00900 void expand(dBoundingBox v);
00901 void expandby(float a);
00902 bool inside(dVector point) const;
00903
00904 dVector min;
00905 dVector max;
00906
00907 private:
00908 bool m_Empty;
00909 };
00910
00911 class dQuat
00912 {
00913 public:
00914 dQuat():x(0),y(0),z(0),w(1){}
00915 dQuat(float x, float y, float z, float w):x(x),y(y),z(z),w(w){}
00916 dQuat(const dQuat& q):x(q.x),y(q.y),z(q.z),w(q.w){}
00917
00918
00919 dMatrix toMatrix() const;
00920
00921
00922 dQuat conjugate() const;
00923 void setaxisangle(dVector axis, float angle);
00924
00925
00926 dQuat operator* (const dQuat&qR) const;
00927
00928 void renorm();
00929 float *arr() {return &x;}
00930
00931
00932 float x,y,z,w;
00933 };
00934
00936
00937 }
00938
00939 #endif // DADA