dada.cpp

Go to the documentation of this file.
00001 /*  Dada
00002  *  Copyright (C) 2005 David Griffiths <dave@pawfal.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */ 
00018 #include "dada.h"
00019 
00020 using namespace Fluxus;
00021 
00022 static const int SINCOS_TABLESIZE = 2048;
00023 static float SinTab[SINCOS_TABLESIZE];
00024 static float CosTab[SINCOS_TABLESIZE];
00025 static const float SINCOS_LOOKUP=SINCOS_TABLESIZE/(float)TWO_PI;
00026 
00028 
00029 float Fluxus::RandFloat()
00030 {
00031     return rand()%10000/10000.0f;
00032 }
00033 
00034 float Fluxus::RandRange(float L, float H)
00035 {
00036     return ((rand()%10000/10000.0f)*(H-L))+L;
00037 }
00038 
00039 void Fluxus::InitDada()
00040 {
00041     for (int n=0; n<SINCOS_TABLESIZE; n++)
00042     {
00043         float a=n*(TWO_PI/(float)SINCOS_TABLESIZE);
00044         SinTab[n]=sin(a);
00045         CosTab[n]=cos(a);
00046     }
00047 }
00048 
00049 void Fluxus::dSinCos(float a, float &s, float &c)
00050 {
00051     int Index=(int)rint(a*SINCOS_LOOKUP)&SINCOS_TABLESIZE-1;
00052     s=SinTab[Index];
00053     c=CosTab[Index];    
00054 }
00055 
00056 dVector &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 dVector 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; //t.w=w+rhs.w;
00066     return t;
00067 }
00068 
00069 dVector 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; //t.w=w-rhs.w;
00073     return t;
00074 }
00075 
00076 dVector 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; //t.w=w+rhs.w;
00080     return t;
00081 }
00082 
00083 dVector 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; //t.w=w-rhs.w;
00087     return t;
00088 }
00089 
00090 dVector dVector::operator+(float rhs) const
00091 {
00092     dVector t;
00093     t.x=x+rhs; t.y=y+rhs; t.z=z+rhs; //t.w=w*rhs;
00094     return t;
00095 }
00096 
00097 dVector dVector::operator-(float rhs) const
00098 {
00099     dVector t;
00100     t.x=x-rhs; t.y=y-rhs; t.z=z-rhs; //t.w=w/rhs;
00101     return t;
00102 }
00103 
00104 dVector dVector::operator*(float rhs) const
00105 {
00106     dVector t;
00107     t.x=x*rhs; t.y=y*rhs; t.z=z*rhs; //t.w=w*rhs;
00108     return t;
00109 }
00110 
00111 dVector dVector::operator/(float rhs) const
00112 {
00113     dVector t;
00114     t.x=x/rhs; t.y=y/rhs; t.z=z/rhs; //t.w=w/rhs;
00115     return t;
00116 }
00117 
00118 dVector &dVector::operator+=(dVector const &rhs)
00119 {
00120     x+=rhs.x; y+=rhs.y; z+=rhs.z; //w+=rhs.w;
00121     return *this;
00122 }
00123 
00124 dVector &dVector::operator-=(dVector const &rhs)
00125 {
00126     x-=rhs.x; y-=rhs.y; z-=rhs.z; //w-=rhs.w;
00127     return *this;
00128 }
00129 
00130 dVector &dVector::operator*=(float rhs)
00131 {
00132     x*=rhs; y*=rhs; z*=rhs; //w*=rhs;
00133     return *this;
00134 }
00135 
00136 dVector &dVector::operator/=(float rhs)
00137 {
00138     if (rhs) {x/=rhs; y/=rhs; z/=rhs;}// w/=rhs;}
00139     return *this;
00140 }
00141 
00142 float dVector::dot(dVector const &rhs) const
00143 {
00144     return x*rhs.x+y*rhs.y+z*rhs.z;
00145 }
00146 
00147 dVector 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 float dVector::dist(dVector const &rhs) const
00155 {
00157     return sqrt((rhs.x-x)*(rhs.x-x)+
00158                 (rhs.y-y)*(rhs.y-y)+
00159                 (rhs.z-z)*(rhs.z-z));
00160 }
00161 
00162 float dVector::distsq(dVector const &rhs) const
00163 {
00164     return (rhs.x-x)*(rhs.x-x)+
00165            (rhs.y-y)*(rhs.y-y)+
00166            (rhs.z-z)*(rhs.z-z);
00167 }
00168 
00169 void dVector::get_euler(float &rx, float &ry, float &rz) const
00170 {
00171     if (z==0) rx=0;
00172     else rx=atan(y/z)*RAD_CONV;
00173     if (x==0) ry=0;
00174     else ry=atan(z/x)*RAD_CONV;
00175     if (y==0) rz=0;
00176     else rz=atan(x/y)*RAD_CONV;
00177 }
00178 
00179 float dVector::mag()
00180 {
00181     return dist(dVector(0,0,0));
00182 }
00183 
00184 dVector Fluxus::operator-(dVector rhs)
00185 {
00186     return dVector(-rhs.x,-rhs.y,-rhs.z);
00187 }
00188 
00189 ostream &Fluxus::operator<<(ostream &os, dVector const &om)
00190 {
00191     os<<om.x<<" "<<om.y<<" "<<om.z<<" "<<om.w<<" ";
00192     return os;
00193 }
00194 
00195 istream &Fluxus::operator>>(istream &is, dVector &om)
00196 {
00197     is>>om.x>>om.y>>om.z>>om.w;
00198     return is;
00199 }
00200 
00201 void dVector::get_rot(float m[16],dVector up)
00202 {
00203     dVector a,b,c;
00204     a.x=this->x; a.y=this->y; a.z=this->z;
00205     a.normalise();
00206     if (a==up) a.x+=0.01;
00207     b=a.cross(up);
00208     b.normalise();
00209     c=b.cross(a);
00210     c.normalise();
00211     
00212     for (int n=0; n<16; n++)
00213     {
00214         m[n]=0;
00215     }
00216 
00217     m[15]=1;
00218 
00219     m[0]=a.x; m[1]=a.y; m[2]=a.z;   
00220     m[4]=b.x; m[5]=b.y; m[6]=b.z;   
00221     m[8]=c.x; m[9]=c.y; m[10]=c.z;
00222 }
00223 
00224 bool dVector::feq(const dVector &other, float epsilon)
00225 {
00226     return (fabs(x-other.x)<epsilon && fabs(y-other.y)<epsilon && fabs(z-other.z)<epsilon);
00227 }
00228 
00230 
00231 dColour &dColour::operator=(dColour const &rhs)
00232 {
00233     r=rhs.r; g=rhs.g; b=rhs.b; a=rhs.a;
00234     return *this;
00235 }
00236 
00237 dColour dColour::operator+(dColour const &rhs) const
00238 {
00239     dColour t;
00240     t.r=r+rhs.r; t.g=g+rhs.g; t.b=b+rhs.b; t.a=a+rhs.a;
00241     return t;
00242 }
00243 
00244 dColour dColour::operator-(dColour const &rhs) const
00245 {
00246     dColour t;
00247     t.r=r-rhs.r; t.g=g-rhs.g; t.b=b-rhs.b; t.a=a-rhs.a;
00248     return t;
00249 }
00250 
00251 dColour dColour::operator*(dColour const &rhs) const
00252 {
00253     dColour t;
00254     t.r=r*rhs.r; t.g=g*rhs.g; t.b=b*rhs.b; t.a=a*rhs.a;
00255     return t;
00256 }
00257 
00258 dColour dColour::operator/(dColour const &rhs) const
00259 {
00260     dColour t;
00261     t.r=r/rhs.r; t.g=g/rhs.g; t.b=b/rhs.b; t.a=a/rhs.a;
00262     return t;
00263 }
00264 
00265 dColour dColour::operator+(float rhs) const
00266 {
00267     dColour t;
00268     t.r=r+rhs; t.g=g+rhs; t.b=b+rhs; t.a=a+rhs;
00269     return t;
00270 }
00271 
00272 dColour dColour::operator-(float rhs) const
00273 {
00274     dColour t;
00275     t.r=r-rhs; t.g=g-rhs; t.b=b-rhs; t.a=a-rhs;
00276     return t;
00277 }
00278 
00279 dColour dColour::operator*(float rhs) const
00280 {
00281     dColour t;
00282     t.r=r*rhs; t.g=g*rhs; t.b=b*rhs; t.a=a*rhs;
00283     return t;
00284 }
00285 
00286 dColour dColour::operator/(float rhs) const
00287 {
00288     dColour t;
00289     t.r=r/rhs; t.g=g/rhs; t.b=b/rhs; t.a=a/rhs;
00290     return t;
00291 }
00292 
00293 dColour &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 dColour &dColour::operator-=(dColour const &rhs)
00300 {
00301     r-=rhs.r; g-=rhs.g; b-=rhs.b; a-=rhs.a;
00302     return *this;
00303 }
00304 
00305 dColour &dColour::operator*=(float rhs)
00306 {
00307     r*=rhs; g*=rhs; b*=rhs; a*=rhs;
00308     return *this;
00309 }
00310 
00311 dColour &dColour::operator/=(float rhs)
00312 {
00313     if (rhs) {r/=rhs; g/=rhs; b/=rhs; a/=rhs;}
00314     return *this;
00315 }
00316 
00317 ostream &Fluxus::operator<<(ostream &os, dColour const &om)
00318 {
00319     os<<"r="<<om.r<<" g="<<om.g<<" b="<<om.b<<" a="<<om.a<<" ";
00320     return os;
00321 }
00322 
00324 
00325 dVertex const &dVertex::operator=(dVertex const &rhs)
00326 {
00327     point=rhs.point;
00328     normal=rhs.normal;
00329     col=rhs.col;
00330     s=rhs.s;
00331     t=rhs.t;
00332     return rhs;
00333 }
00334 
00335 ostream &Fluxus::operator<<(ostream &os, dVertex const &v)
00336 {
00337     os<<"Vertex : p="<<v.point<<" n="<<v.normal<<v.col<<" "<<v.s<<" "<<v.t<<endl;
00338     return os;
00339 }
00340 
00342 
00343 dMatrix::dMatrix(float m00, float m10, float m20, float m30, 
00344             float m01, float m11, float m21, float m31, 
00345             float m02, float m12, float m22, float m32, 
00346             float m03, float m13, float m23, float m33)
00347 {
00348     m[0][0]=m00; m[1][0]=m10; m[2][0]=m20; m[3][0]=m30;
00349     m[0][1]=m01; m[1][1]=m11; m[2][1]=m21; m[3][1]=m31;
00350     m[0][2]=m02; m[1][2]=m12; m[2][2]=m22; m[3][2]=m32;
00351     m[0][3]=m03; m[1][3]=m13; m[2][3]=m23; m[3][3]=m33;
00352 }
00353 
00354 void dMatrix::init()
00355 {
00356     zero();
00357     m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
00358 }
00359 
00360 void dMatrix::zero()
00361 {
00362     memset(m,0,sizeof(float)*16);
00363 }
00364 
00365 const dMatrix &dMatrix::operator=(dMatrix const &rhs)
00366 {
00367     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];
00368     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];
00369     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];
00370     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];
00371     return rhs;
00372 }
00373 
00374 dMatrix dMatrix::operator+(dMatrix const &rhs) const
00375 {
00376     dMatrix t;
00377     for (int i=0; i<4; i++)
00378     {
00379         for (int j=0; j<4; j++)
00380         {
00381             t.m[i][j]=m[i][j]+rhs.m[i][j];
00382         }
00383     }
00384     return t;
00385 }
00386 
00387 dMatrix dMatrix::operator-(dMatrix const &rhs) const
00388 {
00389     dMatrix t;
00390     for (int i=0; i<4; i++)
00391     {
00392         for (int j=0; j<4; j++)
00393         {
00394             t.m[i][j]=m[i][j]-rhs.m[i][j];
00395         }
00396     }
00397     return t;
00398 }
00399 
00400 dMatrix dMatrix::operator*(dMatrix const &rhs) const
00401 {
00402     //dMatrix t;
00403     //for (int i=0; i<4; i++)
00404     //    for (int j=0; j<4; j++)
00405     //        t.m[i][j]=m[i][0]*rhs.m[0][j]+
00406     //                  m[i][1]*rhs.m[1][j]+
00407     //                  m[i][2]*rhs.m[2][j]+
00408     //                  m[i][3]*rhs.m[3][j];
00409 
00410     dMatrix t;
00411     /*for (int i=0; i<4; i++)
00412         for (int j=0; j<4; j++)
00413     t.m[i][j]=m[0][j]*rhs.m[i][0]+m[1][j]*rhs.m[i][1]+m[2][j]*rhs.m[i][2]+m[3][j]*rhs.m[i][3];
00414     */
00415 
00416     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];
00417     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];
00418     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];
00419     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];
00420 
00421     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];
00422     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];
00423     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];
00424     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];
00425 
00426     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];
00427     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];
00428     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];
00429     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];
00430 
00431     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];
00432     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];
00433     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];
00434     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];
00435 
00436     return t;
00437 }
00438 
00439 dMatrix dMatrix::operator/(dMatrix const &rhs) const
00440 {
00441     dMatrix t;
00442     for (int i=0; i<4; i++)
00443     {
00444         for (int j=0; j<4; j++)
00445         {
00446             t.m[i][j]=m[i][0]/rhs.m[0][j]+
00447                       m[i][1]/rhs.m[1][j]+
00448                       m[i][2]/rhs.m[2][j]+
00449                       m[i][3]/rhs.m[3][j];
00450         }
00451     }
00452     return t;
00453 }
00454 
00455 dMatrix dMatrix::operator+(float rhs) const
00456 {
00457     dMatrix t;
00458     for (int i=0; i<4; i++)
00459     {
00460         for (int j=0; j<4; j++)
00461         {
00462             t.m[i][j]=m[i][j]+rhs;
00463         }
00464     }
00465     return t;
00466 }
00467 
00468 dMatrix dMatrix::operator-(float rhs) const
00469 {
00470     dMatrix t;
00471     for (int i=0; i<4; i++)
00472     {
00473         for (int j=0; j<4; j++)
00474         {
00475             t.m[i][j]=m[i][j]-rhs;
00476         }
00477     }
00478     return t;
00479 }
00480 
00481 dMatrix dMatrix::operator*(float rhs) const
00482 {
00483     dMatrix t;
00484     for (int i=0; i<4; i++)
00485     {
00486         for (int j=0; j<4; j++)
00487         {
00488             t.m[i][j]=m[i][j]*rhs;
00489         }
00490     }
00491     return t;
00492 }
00493 
00494 dMatrix dMatrix::operator/(float rhs) const
00495 {
00496     dMatrix t;
00497     for (int i=0; i<4; i++)
00498     {
00499         for (int j=0; j<4; j++)
00500         {
00501             t.m[i][j]=m[i][j]/rhs;
00502         }
00503     }
00504     return t;
00505 }
00506 
00507 dMatrix &dMatrix::operator+=(dMatrix const &rhs)
00508 {
00509     for (int i=0; i<4; i++)
00510     {
00511         for (int j=0; j<4; j++)
00512         {
00513             m[i][j]+=rhs.m[i][j];
00514         }
00515     }
00516     return *this;
00517 }
00518 
00519 dMatrix &dMatrix::operator-=(dMatrix const &rhs)
00520 {
00521     for (int i=0; i<4; i++)
00522     {
00523         for (int j=0; j<4; j++)
00524         {
00525             m[i][j]-=rhs.m[i][j];
00526         }
00527     }
00528     return *this;
00529 }
00530 
00531 dMatrix &dMatrix::operator*=(dMatrix const &rhs)
00532 {
00533     *this=*this*rhs;
00534     return *this;
00535 }
00536 
00537 dMatrix &dMatrix::operator/=(dMatrix const &rhs)
00538 {
00539     *this=*this/rhs;
00540     return *this;
00541 }
00542 
00543 dMatrix &dMatrix::translate(float x, float y, float z)
00544 {
00545     dMatrix t;
00546     t.m[3][0]=x;
00547     t.m[3][1]=y;
00548     t.m[3][2]=z;
00549     *this=*this*t;
00550     return *this;
00551 
00552 }
00553 
00554 dMatrix &dMatrix::translate(dVector &tr)
00555 {
00556     dMatrix t;
00557     t.m[3][0]=tr.x;
00558     t.m[3][1]=tr.y;
00559     t.m[3][2]=tr.z;
00560     *this=*this*t;
00561     return *this;
00562 
00563 }
00564 
00565 void dMatrix::settranslate(dVector &tr)
00566 {
00567     m[3][0]=tr.x;
00568     m[3][1]=tr.y;
00569     m[3][2]=tr.z;
00570 }
00571 
00572 dVector dMatrix::gettranslate() const
00573 {
00574     return dVector(m[3][0],m[3][1],m[3][2]);
00575 }
00576 
00577 //#define USE_FAST_SINCOS
00578 
00579 dMatrix &dMatrix::rotxyz(float x,float y,float z)
00580 {
00581     dMatrix t;
00582     if (x)
00583     {
00584         x*=0.017453292;
00585         
00586         #ifdef USE_FAST_SINCOS
00587         float sx,cx;
00588         dSinCos(x,sx,cx);
00589         #else
00590         float sx=sin(x);
00591         float cx=cos(x);
00592         #endif
00593         
00594         t.m[1][1]=cx;
00595         t.m[2][1]=-sx;
00596         t.m[1][2]=sx;
00597         t.m[2][2]=cx;
00598         *this=*this*t;
00599     }
00600     
00601     if (y)
00602     {
00603         y*=0.017453292;
00604         
00605         #ifdef USE_FAST_SINCOS
00606         float sy,cy;
00607         dSinCos(y,sy,cy);
00608         #else
00609         float sy=sin(y);
00610         float cy=cos(y);
00611         #endif
00612         
00613         t.init();
00614         t.m[0][0]=cy;
00615         t.m[2][0]=-sy;
00616         t.m[0][2]=sy;
00617         t.m[2][2]=cy;
00618         *this=*this*t;
00619     }
00620 
00621     if (z)
00622     {
00623         z*=0.017453292;
00624         
00625         #ifdef USE_FAST_SINCOS
00626         float sz,cz;
00627         dSinCos(z,sz,cz);
00628         #else
00629         float sz=sin(z);
00630         float cz=cos(z);
00631         #endif
00632         
00633         t.init();
00634         t.m[0][0]=cz;
00635         t.m[1][0]=-sz;
00636         t.m[0][1]=sz;
00637         t.m[1][1]=cz;
00638         *this=*this*t;
00639     }
00640 
00641     return *this;
00642 }
00643 
00644 dMatrix &dMatrix::rotx(float a)
00645 {
00646     a*=0.017453292;
00647     dMatrix t;
00648 
00649     t.m[1][1]=cos(a);
00650     t.m[2][1]=-sin(a);
00651     t.m[1][2]=sin(a);
00652     t.m[2][2]=cos(a);
00653 
00654     *this=*this*t;
00655     return *this;
00656 }
00657 
00658 dMatrix &dMatrix::roty(float a)
00659 {
00660     a*=0.017453292;
00661     dMatrix t;
00662 
00663     t.m[0][0]=cos(a);
00664     t.m[2][0]=-sin(a);
00665     t.m[0][2]=sin(a);
00666     t.m[2][2]=cos(a);
00667 
00668     *this=*this*t;
00669     return *this;
00670 }
00671 
00672 dMatrix &dMatrix::rotz(float a)
00673 {
00674     a*=0.017453292;
00675     dMatrix t;
00676 
00677     t.m[0][0]=cos(a);
00678     t.m[1][0]=-sin(a);
00679     t.m[0][1]=sin(a);
00680     t.m[1][1]=cos(a);
00681 
00682     *this=*this*t;
00683     return *this;
00684 }
00685 
00686 dMatrix &dMatrix::scale(float x, float y, float z)
00687 {
00688     dMatrix t;
00689 
00690     t.m[0][0]=x;
00691     t.m[1][1]=y;
00692     t.m[2][2]=z;
00693 
00694     *this=*this*t;
00695     return *this;
00696 }
00697 
00698 dVector dMatrix::transform(dVector const &p) const
00699 {
00700     dVector t;
00701     t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0] + p.w*m[3][0];
00702     t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1] + p.w*m[3][1];
00703     t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2] + p.w*m[3][2];
00704     t.w=p.x*m[0][3] + p.y*m[1][3] + p.z*m[2][3] + p.w*m[3][3];
00705     return t;
00706 }
00707 
00708 dVector dMatrix::transform_persp(dVector const &p) const
00709 {
00710     dVector t;
00711     t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0] + p.w*m[3][0];
00712     t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1] + p.w*m[3][1];
00713     t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2] + p.w*m[3][2];
00714     t.w=p.x*m[0][3] + p.y*m[1][3] + p.z*m[2][3] + p.w*m[3][3];
00715     t.homog();
00716     return t;
00717 }
00718 
00719 dVertex dMatrix::transform(dVertex const &p) const
00720 {
00721     dVertex t=p;
00722     t.point=transform(p.point);
00723     t.normal=transform_no_trans(p.normal);
00724     return t;
00725 }
00726 
00727 dVector dMatrix::transform_no_trans(dVector const &p) const
00728 {
00729     dVector t;
00730     t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0];
00731     t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1];
00732     t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2];
00733     t.w=p.w;
00734     return t;
00735 }
00736 
00737 /*void dMatrix::load_glmatrix(float glm[16])
00738 {
00739     glm[0]= m[0][0]; glm[1]= m[1][0]; glm[2]= m[2][0]; glm[3]= m[3][0];
00740     glm[4]= m[0][1]; glm[5]= m[1][1]; glm[6]= m[2][1]; glm[7]= m[3][1];
00741     glm[8]= m[0][2]; glm[9]= m[1][2]; glm[10]=m[2][2]; glm[11]=m[3][2];
00742     glm[12]=m[0][3]; glm[13]=m[1][3]; glm[14]=m[2][3]; glm[15]=m[3][3];
00743 }*/
00744 
00745 void dMatrix::load_glmatrix(float glm[16])
00746 {
00747     glm[0]= m[0][0]; glm[4]= m[1][0]; glm[8]= m[2][0]; glm[12]= m[3][0];
00748     glm[1]= m[0][1]; glm[5]= m[1][1]; glm[9]= m[2][1]; glm[13]= m[3][1];
00749     glm[2]= m[0][2]; glm[6]= m[1][2]; glm[10]=m[2][2]; glm[14]=m[3][2];
00750     glm[3]= m[0][3]; glm[7]= m[1][3]; glm[11]=m[2][3]; glm[15]=m[3][3];
00751 }
00752 
00753 void dMatrix::load_dMatrix(float glm[16])
00754 {
00755     m[0][0]=glm[0]; m[1][0]=glm[4]; m[2][0]=glm[8]; m[3][0]=glm[12];
00756     m[0][1]=glm[1]; m[1][1]=glm[5]; m[2][1]=glm[9]; m[3][1]=glm[13];
00757     m[0][2]=glm[2]; m[1][2]=glm[6]; m[2][2]=glm[10]; m[3][2]=glm[14];
00758     m[0][3]=glm[3]; m[1][3]=glm[7]; m[2][3]=glm[11]; m[3][3]=glm[15];
00759 }
00760 
00761 void dMatrix::transpose()
00762 {
00763     dMatrix t;
00764     for (int i=0; i<4; i++)
00765     {
00766         for (int j=0; j<4; j++)
00767         {
00768             t.m[i][j]=m[j][i];
00769         }
00770     }
00771     *this=t;
00772 }
00773 
00774 dMatrix dMatrix::inverse() const
00775 {
00776     dMatrix temp;
00777     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];
00778     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];
00779     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];
00780     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];
00781     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];
00782     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];
00783     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];
00784     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];
00785     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];
00786     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];
00787     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];
00788     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];
00789     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];
00790     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];
00791     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];
00792     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];
00793    float scale=1/temp.determinant();
00794    temp.scale(scale,scale,scale);
00795    return temp;
00796 }
00797 
00798 float dMatrix::determinant()  const
00799 {
00800    return 
00801    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]+
00802    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]+
00803    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]+
00804    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]+
00805    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]+
00806    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];
00807 }
00808 
00809 void dMatrix::remove_scale()
00810 {
00811     dVector xvec = get_hori_i().normalise();
00812     dVector yvec = get_hori_j().normalise();
00813     dVector zvec = get_hori_k().normalise();
00814     
00815     m[0][0]=xvec.x; m[1][0]=xvec.y; m[2][0]=xvec.z;
00816     m[0][1]=yvec.x; m[1][1]=yvec.y; m[2][1]=yvec.z;
00817     m[0][2]=zvec.x; m[1][2]=zvec.y; m[2][2]=zvec.z;
00818 }
00819 
00820 void dMatrix::extract_euler(float &x, float &y, float &z) const
00821 {
00822     dMatrix t=*this;
00823     t.remove_scale();
00824     if (t.m[2][2]==0) x=0;
00825     else x = atan(t.m[1][2]/t.m[2][2])*RAD_CONV;
00826     y = asin(-t.m[0][2])*RAD_CONV;
00827     if (t.m[0][0]==0) z=0;
00828     else z=atan(t.m[0][1]/t.m[0][0])*RAD_CONV;
00829 
00830 
00831     /*dVector xvec = get_hori_i().normalise();
00832     dVector yvec = get_hori_j().normalise();
00833     dVector zvec = get_hori_k().normalise();
00834     float d1,d2;
00835     xvec.get_euler(x,d1,d2);
00836     Trace::Stream<<x<<" "<<d1<<" "<<d2<<endl;
00837     yvec.get_euler(d1,y,d2);
00838     Trace::Stream<<d1<<" "<<y<<" "<<d2<<endl;
00839     zvec.get_euler(d1,d2,z);
00840     Trace::Stream<<d1<<" "<<d2<<" "<<z<<endl;*/
00841 }
00842     
00843 void dMatrix::aim(dVector v, dVector up)
00844 {
00845     v.normalise();
00846     dVector l=v.cross(up);
00847     dVector u=v.cross(l);
00848     l.normalise();
00849     u.normalise();
00850     
00851     m[0][0]=v.x; m[0][1]=v.y; m[0][2]=v.z;
00852     m[1][0]=l.x; m[1][1]=l.y; m[1][2]=l.z;
00853     m[2][0]=u.x; m[2][1]=u.y; m[2][2]=u.z;  
00854 }
00855 
00856 void dMatrix::blend(dMatrix other, float amount)
00857 {
00858     for (int j=0; j<4; j++)
00859     {
00860         for (int i=0; i<4; i++)
00861         {
00862             m[i][j]=(1-amount)*m[i][j]+amount*other.m[i][j];
00863         }
00864     }
00865 }
00866 
00867 ostream &Fluxus::operator<<(ostream &os, dMatrix const &om)
00868 {
00869     for (int j=0; j<4; j++)
00870     {
00871         for (int i=0; i<4; i++)
00872         {
00873             os<<om.m[i][j]<<" ";
00874         }
00875         os<<endl;
00876     }
00877 
00878     return os;
00879 }
00880 
00881 /*
00882 void dAxis::aimx(dVector a, dVector up)
00883 {
00884     if (up.mag()!=1) up.normalise();
00885     if (a.mag()!=1) a.normalise();
00886     i=a;
00887     if (i==up) up.x+=0.000001;
00888     j=i.cross(up);
00889     k=i.cross(j);
00890 }
00891 */
00892     
00893 void dBoundingBox::expand(dVector v)
00894 {
00895     if (m_Empty)
00896     {   
00897         min=v;
00898         max=v;
00899         m_Empty=false;
00900     }
00901     
00902     if (v.x<min.x) min.x=v.x;
00903     if (v.y<min.y) min.y=v.y;
00904     if (v.z<min.z) min.z=v.z;
00905     
00906     if (v.x>=max.x) max.x=v.x;
00907     if (v.y>=max.y) max.y=v.y;
00908     if (v.z>=max.z) max.z=v.z;
00909 }
00910 
00911 void dBoundingBox::expand(dBoundingBox v)
00912 {
00913     expand(v.min);
00914     expand(dVector(v.max.x,v.min.y,v.min.z));
00915     expand(dVector(v.min.x,v.max.y,v.min.z));
00916     expand(dVector(v.max.x,v.max.y,v.min.z));
00917     expand(dVector(v.min.x,v.min.y,v.max.z));
00918     expand(dVector(v.max.x,v.min.y,v.max.z));
00919     expand(dVector(v.min.x,v.max.y,v.max.z));
00920     expand(v.max);
00921 }
00922 
00923 void dBoundingBox::expandby(float a)
00924 {
00925     max.x+=a; max.y+=a; max.z+=a;
00926     min.x-=a; min.y-=a; min.z-=a; 
00927 }
00928 
00929 bool dBoundingBox::inside(dVector p) const
00930 { 
00931     return (p.x>min.x && p.x<max.x &&
00932             p.y>min.y && p.y<max.y &&
00933             p.z>min.z && p.z<max.z);
00934 }
00935     
00936 // conversions
00937 dMatrix dQuat::toMatrix() const
00938 {
00939     float Nq = x*x + y*y + z*z + w*w;
00940     float s = (Nq > 0.f) ? (2.0f / Nq) : 0.f;
00941     float xs = x*s, ys = y*s, zs = z*s;
00942     float wx = w*xs, wy = w*ys, wz = w*zs;
00943     float xx = x*xs, xy = x*ys, xz = x*zs;
00944     float yy = y*ys, yz = y*zs, zz = z*zs;
00945     return dMatrix(1.0f - (yy + zz),
00946                xy + wz,
00947                xz - wy,
00948                0,
00949                xy - wz,          
00950                1.0f - (xx + zz),
00951                yz + wx,
00952                0,
00953                xz + wy,          
00954                yz - wx,          
00955                1.0f - (xx + yy),
00956                0, 0, 0, 0, 1.0f);
00957 
00958 }
00959 
00960 // operations
00961 dQuat dQuat::conjugate() const
00962 {
00963     return dQuat(-x,-y,-z,w);
00964 }
00965 
00966 // make multiply look like multiply
00967 dQuat dQuat::operator* (const dQuat&qR) const
00968 {
00969     dQuat qq;
00970     qq.w = w*qR.w - x*qR.x - y*qR.y - z*qR.z;
00971     qq.x = w*qR.x + x*qR.w + y*qR.z - z*qR.y;
00972     qq.y = w*qR.y + y*qR.w + z*qR.x - x*qR.z;
00973     qq.z = w*qR.z + z*qR.w + x*qR.y - y*qR.x;
00974     return (qq);
00975 }
00976 
00977 void dQuat::renorm() 
00978 {
00979     float Nq = 1.f / (float) (x*x + y*y + z*z + w*w);
00980     x *= Nq;
00981     y *= Nq;
00982     z *= Nq;
00983     w *= Nq;
00984 }
00985 
00986 void dQuat::setaxisangle(dVector axis, float angle)
00987 { 
00988     angle*=0.017453292;
00989     w = cos(angle/2);
00990     axis.normalise();
00991     axis *= sin(angle/2);
00992     x=axis.x;
00993     y=axis.y;
00994     z=axis.z;
00995 }
00996 
00997 float dGeometry::pointlinedist(const dVector &p, const dVector &start, const dVector &end)
00998 {
00999     float linemag;
01000     dVector intersection;
01001  
01002     float len = end.dist(start);
01003  
01004     float t = ((p.x-start.x)*(end.x-start.x) +
01005                (p.y-start.y)*(end.y-start.y) +
01006                (p.z-start.z)*(end.z-start.z)) / (len*len);
01007  
01008     if (t<0.0f) // off the end
01009     {
01010         return p.dist(start);
01011     }
01012     if (t>1.0f) // off the end
01013     {
01014         return p.dist(end);
01015     }
01016     
01017     intersection.x = start.x+t*(end.x-start.x);
01018     intersection.y = start.y+t*(end.y-start.y);
01019     intersection.z = start.z+t*(end.z-start.z);
01020  
01021     return p.dist(intersection);
01022 }
01023 

Generated on Tue Sep 4 23:22:18 2007 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1