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 dVector dVector::reflect(dVector const &rhs) const
00155 {
00156     float vdn=dot(rhs)*2.0f;
00157     return (*this)-rhs*vdn;
00158 }
00159 
00160 float dVector::dist(dVector const &rhs) const
00161 {
00163     return sqrt((rhs.x-x)*(rhs.x-x)+
00164                 (rhs.y-y)*(rhs.y-y)+
00165                 (rhs.z-z)*(rhs.z-z));
00166 }
00167 
00168 float dVector::distsq(dVector const &rhs) const
00169 {
00170     return (rhs.x-x)*(rhs.x-x)+
00171            (rhs.y-y)*(rhs.y-y)+
00172            (rhs.z-z)*(rhs.z-z);
00173 }
00174 
00175 void dVector::get_euler(float &rx, float &ry, float &rz) const
00176 {
00177     if (z==0) rx=0;
00178     else rx=atan(y/z)*RAD_CONV;
00179     if (x==0) ry=0;
00180     else ry=atan(z/x)*RAD_CONV;
00181     if (y==0) rz=0;
00182     else rz=atan(x/y)*RAD_CONV;
00183 }
00184 
00185 float dVector::mag()
00186 {
00187     return dist(dVector(0,0,0));
00188 }
00189 
00190 dVector Fluxus::operator-(dVector rhs)
00191 {
00192     return dVector(-rhs.x,-rhs.y,-rhs.z);
00193 }
00194 
00195 ostream &Fluxus::operator<<(ostream &os, dVector const &om)
00196 {
00197     os<<om.x<<" "<<om.y<<" "<<om.z<<" "<<om.w<<" ";
00198     return os;
00199 }
00200 
00201 istream &Fluxus::operator>>(istream &is, dVector &om)
00202 {
00203     is>>om.x>>om.y>>om.z>>om.w;
00204     return is;
00205 }
00206 
00207 void dVector::get_rot(float m[16],dVector up)
00208 {
00209     dVector a,b,c;
00210     a.x=this->x; a.y=this->y; a.z=this->z;
00211     a.normalise();
00212     if (a==up) a.x+=0.01;
00213     b=a.cross(up);
00214     b.normalise();
00215     c=b.cross(a);
00216     c.normalise();
00217     
00218     for (int n=0; n<16; n++)
00219     {
00220         m[n]=0;
00221     }
00222 
00223     m[15]=1;
00224 
00225     m[0]=a.x; m[1]=a.y; m[2]=a.z;   
00226     m[4]=b.x; m[5]=b.y; m[6]=b.z;   
00227     m[8]=c.x; m[9]=c.y; m[10]=c.z;
00228 }
00229 
00230 bool dVector::feq(const dVector &other, float epsilon)
00231 {
00232     return (fabs(x-other.x)<epsilon && fabs(y-other.y)<epsilon && fabs(z-other.z)<epsilon);
00233 }
00234 
00236 
00237 dColour &dColour::operator=(dColour const &rhs)
00238 {
00239     r=rhs.r; g=rhs.g; b=rhs.b; a=rhs.a;
00240     return *this;
00241 }
00242 
00243 dColour dColour::operator+(dColour const &rhs) const
00244 {
00245     dColour t;
00246     t.r=r+rhs.r; t.g=g+rhs.g; t.b=b+rhs.b; t.a=a+rhs.a;
00247     return t;
00248 }
00249 
00250 dColour dColour::operator-(dColour const &rhs) const
00251 {
00252     dColour t;
00253     t.r=r-rhs.r; t.g=g-rhs.g; t.b=b-rhs.b; t.a=a-rhs.a;
00254     return t;
00255 }
00256 
00257 dColour dColour::operator*(dColour const &rhs) const
00258 {
00259     dColour t;
00260     t.r=r*rhs.r; t.g=g*rhs.g; t.b=b*rhs.b; t.a=a*rhs.a;
00261     return t;
00262 }
00263 
00264 dColour dColour::operator/(dColour const &rhs) const
00265 {
00266     dColour t;
00267     t.r=r/rhs.r; t.g=g/rhs.g; t.b=b/rhs.b; t.a=a/rhs.a;
00268     return t;
00269 }
00270 
00271 dColour dColour::operator+(float rhs) const
00272 {
00273     dColour t;
00274     t.r=r+rhs; t.g=g+rhs; t.b=b+rhs; t.a=a+rhs;
00275     return t;
00276 }
00277 
00278 dColour dColour::operator-(float rhs) const
00279 {
00280     dColour t;
00281     t.r=r-rhs; t.g=g-rhs; t.b=b-rhs; t.a=a-rhs;
00282     return t;
00283 }
00284 
00285 dColour dColour::operator*(float rhs) const
00286 {
00287     dColour t;
00288     t.r=r*rhs; t.g=g*rhs; t.b=b*rhs; t.a=a*rhs;
00289     return t;
00290 }
00291 
00292 dColour dColour::operator/(float rhs) const
00293 {
00294     dColour t;
00295     t.r=r/rhs; t.g=g/rhs; t.b=b/rhs; t.a=a/rhs;
00296     return t;
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-=(dColour const &rhs)
00306 {
00307     r-=rhs.r; g-=rhs.g; b-=rhs.b; a-=rhs.a;
00308     return *this;
00309 }
00310 
00311 dColour &dColour::operator*=(float rhs)
00312 {
00313     r*=rhs; g*=rhs; b*=rhs; a*=rhs;
00314     return *this;
00315 }
00316 
00317 dColour &dColour::operator/=(float rhs)
00318 {
00319     if (rhs) {r/=rhs; g/=rhs; b/=rhs; a/=rhs;}
00320     return *this;
00321 }
00322 
00323 ostream &Fluxus::operator<<(ostream &os, dColour const &om)
00324 {
00325     os<<"r="<<om.r<<" g="<<om.g<<" b="<<om.b<<" a="<<om.a<<" ";
00326     return os;
00327 }
00328 
00330 
00331 dVertex const &dVertex::operator=(dVertex const &rhs)
00332 {
00333     point=rhs.point;
00334     normal=rhs.normal;
00335     col=rhs.col;
00336     s=rhs.s;
00337     t=rhs.t;
00338     return rhs;
00339 }
00340 
00341 ostream &Fluxus::operator<<(ostream &os, dVertex const &v)
00342 {
00343     os<<"Vertex : p="<<v.point<<" n="<<v.normal<<v.col<<" "<<v.s<<" "<<v.t<<endl;
00344     return os;
00345 }
00346 
00348 
00349 dMatrix::dMatrix(float m00, float m10, float m20, float m30, 
00350             float m01, float m11, float m21, float m31, 
00351             float m02, float m12, float m22, float m32, 
00352             float m03, float m13, float m23, float m33)
00353 {
00354     m[0][0]=m00; m[1][0]=m10; m[2][0]=m20; m[3][0]=m30;
00355     m[0][1]=m01; m[1][1]=m11; m[2][1]=m21; m[3][1]=m31;
00356     m[0][2]=m02; m[1][2]=m12; m[2][2]=m22; m[3][2]=m32;
00357     m[0][3]=m03; m[1][3]=m13; m[2][3]=m23; m[3][3]=m33;
00358 }
00359 
00360 void dMatrix::init()
00361 {
00362     zero();
00363     m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
00364 }
00365 
00366 void dMatrix::zero()
00367 {
00368     memset(m,0,sizeof(float)*16);
00369 }
00370 
00371 const dMatrix &dMatrix::operator=(dMatrix const &rhs)
00372 {
00373     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];
00374     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];
00375     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];
00376     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];
00377     return rhs;
00378 }
00379 
00380 dMatrix dMatrix::operator+(dMatrix const &rhs) const
00381 {
00382     dMatrix t;
00383     for (int i=0; i<4; i++)
00384     {
00385         for (int j=0; j<4; j++)
00386         {
00387             t.m[i][j]=m[i][j]+rhs.m[i][j];
00388         }
00389     }
00390     return t;
00391 }
00392 
00393 dMatrix dMatrix::operator-(dMatrix const &rhs) const
00394 {
00395     dMatrix t;
00396     for (int i=0; i<4; i++)
00397     {
00398         for (int j=0; j<4; j++)
00399         {
00400             t.m[i][j]=m[i][j]-rhs.m[i][j];
00401         }
00402     }
00403     return t;
00404 }
00405 
00406 dMatrix dMatrix::operator*(dMatrix const &rhs) const
00407 {
00408     //dMatrix t;
00409     //for (int i=0; i<4; i++)
00410     //    for (int j=0; j<4; j++)
00411     //        t.m[i][j]=m[i][0]*rhs.m[0][j]+
00412     //                  m[i][1]*rhs.m[1][j]+
00413     //                  m[i][2]*rhs.m[2][j]+
00414     //                  m[i][3]*rhs.m[3][j];
00415 
00416     dMatrix t;
00417     /*for (int i=0; i<4; i++)
00418         for (int j=0; j<4; j++)
00419     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];
00420     */
00421 
00422     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];
00423     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];
00424     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];
00425     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];
00426 
00427     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];
00428     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];
00429     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];
00430     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];
00431 
00432     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];
00433     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];
00434     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];
00435     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];
00436 
00437     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];
00438     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];
00439     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];
00440     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];
00441 
00442     return t;
00443 }
00444 
00445 dMatrix dMatrix::operator/(dMatrix const &rhs) const
00446 {
00447     dMatrix t;
00448     for (int i=0; i<4; i++)
00449     {
00450         for (int j=0; j<4; j++)
00451         {
00452             t.m[i][j]=m[i][0]/rhs.m[0][j]+
00453                       m[i][1]/rhs.m[1][j]+
00454                       m[i][2]/rhs.m[2][j]+
00455                       m[i][3]/rhs.m[3][j];
00456         }
00457     }
00458     return t;
00459 }
00460 
00461 dMatrix dMatrix::operator+(float rhs) const
00462 {
00463     dMatrix t;
00464     for (int i=0; i<4; i++)
00465     {
00466         for (int j=0; j<4; j++)
00467         {
00468             t.m[i][j]=m[i][j]+rhs;
00469         }
00470     }
00471     return t;
00472 }
00473 
00474 dMatrix dMatrix::operator-(float rhs) const
00475 {
00476     dMatrix t;
00477     for (int i=0; i<4; i++)
00478     {
00479         for (int j=0; j<4; j++)
00480         {
00481             t.m[i][j]=m[i][j]-rhs;
00482         }
00483     }
00484     return t;
00485 }
00486 
00487 dMatrix dMatrix::operator*(float rhs) const
00488 {
00489     dMatrix t;
00490     for (int i=0; i<4; i++)
00491     {
00492         for (int j=0; j<4; j++)
00493         {
00494             t.m[i][j]=m[i][j]*rhs;
00495         }
00496     }
00497     return t;
00498 }
00499 
00500 dMatrix dMatrix::operator/(float rhs) const
00501 {
00502     dMatrix t;
00503     for (int i=0; i<4; i++)
00504     {
00505         for (int j=0; j<4; j++)
00506         {
00507             t.m[i][j]=m[i][j]/rhs;
00508         }
00509     }
00510     return t;
00511 }
00512 
00513 dMatrix &dMatrix::operator+=(dMatrix const &rhs)
00514 {
00515     for (int i=0; i<4; i++)
00516     {
00517         for (int j=0; j<4; j++)
00518         {
00519             m[i][j]+=rhs.m[i][j];
00520         }
00521     }
00522     return *this;
00523 }
00524 
00525 dMatrix &dMatrix::operator-=(dMatrix const &rhs)
00526 {
00527     for (int i=0; i<4; i++)
00528     {
00529         for (int j=0; j<4; j++)
00530         {
00531             m[i][j]-=rhs.m[i][j];
00532         }
00533     }
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::operator/=(dMatrix const &rhs)
00544 {
00545     *this=*this/rhs;
00546     return *this;
00547 }
00548 
00549 dMatrix &dMatrix::translate(float x, float y, float z)
00550 {
00551     dMatrix t;
00552     t.m[3][0]=x;
00553     t.m[3][1]=y;
00554     t.m[3][2]=z;
00555     *this=*this*t;
00556     return *this;
00557 
00558 }
00559 
00560 dMatrix &dMatrix::translate(dVector &tr)
00561 {
00562     dMatrix t;
00563     t.m[3][0]=tr.x;
00564     t.m[3][1]=tr.y;
00565     t.m[3][2]=tr.z;
00566     *this=*this*t;
00567     return *this;
00568 
00569 }
00570 
00571 void dMatrix::settranslate(dVector &tr)
00572 {
00573     m[3][0]=tr.x;
00574     m[3][1]=tr.y;
00575     m[3][2]=tr.z;
00576 }
00577 
00578 dVector dMatrix::gettranslate() const
00579 {
00580     return dVector(m[3][0],m[3][1],m[3][2]);
00581 }
00582 
00583 //#define USE_FAST_SINCOS
00584 
00585 dMatrix &dMatrix::rotxyz(float x,float y,float z)
00586 {
00587     dMatrix t;
00588     if (x)
00589     {
00590         x*=0.017453292;
00591         
00592         #ifdef USE_FAST_SINCOS
00593         float sx,cx;
00594         dSinCos(x,sx,cx);
00595         #else
00596         float sx=sin(x);
00597         float cx=cos(x);
00598         #endif
00599         
00600         t.m[1][1]=cx;
00601         t.m[2][1]=-sx;
00602         t.m[1][2]=sx;
00603         t.m[2][2]=cx;
00604         *this=*this*t;
00605     }
00606     
00607     if (y)
00608     {
00609         y*=0.017453292;
00610         
00611         #ifdef USE_FAST_SINCOS
00612         float sy,cy;
00613         dSinCos(y,sy,cy);
00614         #else
00615         float sy=sin(y);
00616         float cy=cos(y);
00617         #endif
00618         
00619         t.init();
00620         t.m[0][0]=cy;
00621         t.m[2][0]=-sy;
00622         t.m[0][2]=sy;
00623         t.m[2][2]=cy;
00624         *this=*this*t;
00625     }
00626 
00627     if (z)
00628     {
00629         z*=0.017453292;
00630         
00631         #ifdef USE_FAST_SINCOS
00632         float sz,cz;
00633         dSinCos(z,sz,cz);
00634         #else
00635         float sz=sin(z);
00636         float cz=cos(z);
00637         #endif
00638         
00639         t.init();
00640         t.m[0][0]=cz;
00641         t.m[1][0]=-sz;
00642         t.m[0][1]=sz;
00643         t.m[1][1]=cz;
00644         *this=*this*t;
00645     }
00646 
00647     return *this;
00648 }
00649 
00650 dMatrix &dMatrix::rotx(float a)
00651 {
00652     a*=0.017453292;
00653     dMatrix t;
00654 
00655     t.m[1][1]=cos(a);
00656     t.m[2][1]=-sin(a);
00657     t.m[1][2]=sin(a);
00658     t.m[2][2]=cos(a);
00659 
00660     *this=*this*t;
00661     return *this;
00662 }
00663 
00664 dMatrix &dMatrix::roty(float a)
00665 {
00666     a*=0.017453292;
00667     dMatrix t;
00668 
00669     t.m[0][0]=cos(a);
00670     t.m[2][0]=-sin(a);
00671     t.m[0][2]=sin(a);
00672     t.m[2][2]=cos(a);
00673 
00674     *this=*this*t;
00675     return *this;
00676 }
00677 
00678 dMatrix &dMatrix::rotz(float a)
00679 {
00680     a*=0.017453292;
00681     dMatrix t;
00682 
00683     t.m[0][0]=cos(a);
00684     t.m[1][0]=-sin(a);
00685     t.m[0][1]=sin(a);
00686     t.m[1][1]=cos(a);
00687 
00688     *this=*this*t;
00689     return *this;
00690 }
00691 
00692 dMatrix &dMatrix::scale(float x, float y, float z)
00693 {
00694     dMatrix t;
00695 
00696     t.m[0][0]=x;
00697     t.m[1][1]=y;
00698     t.m[2][2]=z;
00699 
00700     *this=*this*t;
00701     return *this;
00702 }
00703 
00704 dVector dMatrix::transform(dVector const &p) const
00705 {
00706     dVector t;
00707     t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0] + p.w*m[3][0];
00708     t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1] + p.w*m[3][1];
00709     t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2] + p.w*m[3][2];
00710     t.w=p.x*m[0][3] + p.y*m[1][3] + p.z*m[2][3] + p.w*m[3][3];
00711     return t;
00712 }
00713 
00714 dVector dMatrix::transform_persp(dVector const &p) const
00715 {
00716     dVector t;
00717     t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0] + p.w*m[3][0];
00718     t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1] + p.w*m[3][1];
00719     t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2] + p.w*m[3][2];
00720     t.w=p.x*m[0][3] + p.y*m[1][3] + p.z*m[2][3] + p.w*m[3][3];
00721     t.homog();
00722     return t;
00723 }
00724 
00725 dVertex dMatrix::transform(dVertex const &p) const
00726 {
00727     dVertex t=p;
00728     t.point=transform(p.point);
00729     t.normal=transform_no_trans(p.normal);
00730     return t;
00731 }
00732 
00733 dVector dMatrix::transform_no_trans(dVector const &p) const
00734 {
00735     dVector t;
00736     t.x=p.x*m[0][0] + p.y*m[1][0] + p.z*m[2][0];
00737     t.y=p.x*m[0][1] + p.y*m[1][1] + p.z*m[2][1];
00738     t.z=p.x*m[0][2] + p.y*m[1][2] + p.z*m[2][2];
00739     t.w=p.w;
00740     return t;
00741 }
00742 
00743 /*void dMatrix::load_glmatrix(float glm[16])
00744 {
00745     glm[0]= m[0][0]; glm[1]= m[1][0]; glm[2]= m[2][0]; glm[3]= m[3][0];
00746     glm[4]= m[0][1]; glm[5]= m[1][1]; glm[6]= m[2][1]; glm[7]= m[3][1];
00747     glm[8]= m[0][2]; glm[9]= m[1][2]; glm[10]=m[2][2]; glm[11]=m[3][2];
00748     glm[12]=m[0][3]; glm[13]=m[1][3]; glm[14]=m[2][3]; glm[15]=m[3][3];
00749 }*/
00750 
00751 void dMatrix::load_glmatrix(float glm[16])
00752 {
00753     glm[0]= m[0][0]; glm[4]= m[1][0]; glm[8]= m[2][0]; glm[12]= m[3][0];
00754     glm[1]= m[0][1]; glm[5]= m[1][1]; glm[9]= m[2][1]; glm[13]= m[3][1];
00755     glm[2]= m[0][2]; glm[6]= m[1][2]; glm[10]=m[2][2]; glm[14]=m[3][2];
00756     glm[3]= m[0][3]; glm[7]= m[1][3]; glm[11]=m[2][3]; glm[15]=m[3][3];
00757 }
00758 
00759 void dMatrix::load_dMatrix(float glm[16])
00760 {
00761     m[0][0]=glm[0]; m[1][0]=glm[4]; m[2][0]=glm[8]; m[3][0]=glm[12];
00762     m[0][1]=glm[1]; m[1][1]=glm[5]; m[2][1]=glm[9]; m[3][1]=glm[13];
00763     m[0][2]=glm[2]; m[1][2]=glm[6]; m[2][2]=glm[10]; m[3][2]=glm[14];
00764     m[0][3]=glm[3]; m[1][3]=glm[7]; m[2][3]=glm[11]; m[3][3]=glm[15];
00765 }
00766 
00767 void dMatrix::transpose()
00768 {
00769     dMatrix t;
00770     for (int i=0; i<4; i++)
00771     {
00772         for (int j=0; j<4; j++)
00773         {
00774             t.m[i][j]=m[j][i];
00775         }
00776     }
00777     *this=t;
00778 }
00779 
00780 dMatrix dMatrix::inverse() const
00781 {
00782     dMatrix temp;
00783     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];
00784     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];
00785     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];
00786     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];
00787     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];
00788     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];
00789     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];
00790     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];
00791     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];
00792     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];
00793     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];
00794     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];
00795     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];
00796     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];
00797     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];
00798     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];
00799    float scale=1/temp.determinant();
00800    temp.scale(scale,scale,scale);
00801    return temp;
00802 }
00803 
00804 float dMatrix::determinant()  const
00805 {
00806    return 
00807    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]+
00808    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]+
00809    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]+
00810    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]+
00811    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]+
00812    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];
00813 }
00814 
00815 void dMatrix::remove_scale()
00816 {
00817     dVector xvec = get_hori_i().normalise();
00818     dVector yvec = get_hori_j().normalise();
00819     dVector zvec = get_hori_k().normalise();
00820     
00821     m[0][0]=xvec.x; m[1][0]=xvec.y; m[2][0]=xvec.z;
00822     m[0][1]=yvec.x; m[1][1]=yvec.y; m[2][1]=yvec.z;
00823     m[0][2]=zvec.x; m[1][2]=zvec.y; m[2][2]=zvec.z;
00824 }
00825 
00826 void dMatrix::extract_euler(float &x, float &y, float &z) const
00827 {
00828     dMatrix t=*this;
00829     t.remove_scale();
00830     if (t.m[2][2]==0) x=0;
00831     else x = atan(t.m[1][2]/t.m[2][2])*RAD_CONV;
00832     y = asin(-t.m[0][2])*RAD_CONV;
00833     if (t.m[0][0]==0) z=0;
00834     else z=atan(t.m[0][1]/t.m[0][0])*RAD_CONV;
00835 
00836 
00837     /*dVector xvec = get_hori_i().normalise();
00838     dVector yvec = get_hori_j().normalise();
00839     dVector zvec = get_hori_k().normalise();
00840     float d1,d2;
00841     xvec.get_euler(x,d1,d2);
00842     Trace::Stream<<x<<" "<<d1<<" "<<d2<<endl;
00843     yvec.get_euler(d1,y,d2);
00844     Trace::Stream<<d1<<" "<<y<<" "<<d2<<endl;
00845     zvec.get_euler(d1,d2,z);
00846     Trace::Stream<<d1<<" "<<d2<<" "<<z<<endl;*/
00847 }
00848     
00849 void dMatrix::aim(dVector v, dVector up)
00850 {
00851     v.normalise();
00852     dVector l=v.cross(up);
00853     dVector u=v.cross(l);
00854     l.normalise();
00855     u.normalise();
00856     
00857     m[0][0]=v.x; m[0][1]=v.y; m[0][2]=v.z;
00858     m[1][0]=l.x; m[1][1]=l.y; m[1][2]=l.z;
00859     m[2][0]=u.x; m[2][1]=u.y; m[2][2]=u.z;  
00860 }
00861 
00862 void dMatrix::blend(dMatrix other, float amount)
00863 {
00864     for (int j=0; j<4; j++)
00865     {
00866         for (int i=0; i<4; i++)
00867         {
00868             m[i][j]=(1-amount)*m[i][j]+amount*other.m[i][j];
00869         }
00870     }
00871 }
00872 
00873 ostream &Fluxus::operator<<(ostream &os, dMatrix const &om)
00874 {
00875     for (int j=0; j<4; j++)
00876     {
00877         for (int i=0; i<4; i++)
00878         {
00879             os<<om.m[i][j]<<" ";
00880         }
00881         os<<endl;
00882     }
00883 
00884     return os;
00885 }
00886 
00887 /*
00888 void dAxis::aimx(dVector a, dVector up)
00889 {
00890     if (up.mag()!=1) up.normalise();
00891     if (a.mag()!=1) a.normalise();
00892     i=a;
00893     if (i==up) up.x+=0.000001;
00894     j=i.cross(up);
00895     k=i.cross(j);
00896 }
00897 */
00898     
00899 void dBoundingBox::expand(dVector v)
00900 {
00901     if (m_Empty)
00902     {   
00903         min=v;
00904         max=v;
00905         m_Empty=false;
00906     }
00907     
00908     if (v.x<min.x) min.x=v.x;
00909     if (v.y<min.y) min.y=v.y;
00910     if (v.z<min.z) min.z=v.z;
00911     
00912     if (v.x>=max.x) max.x=v.x;
00913     if (v.y>=max.y) max.y=v.y;
00914     if (v.z>=max.z) max.z=v.z;
00915 }
00916 
00917 void dBoundingBox::expand(dBoundingBox v)
00918 {
00919     expand(v.min);
00920     expand(dVector(v.max.x,v.min.y,v.min.z));
00921     expand(dVector(v.min.x,v.max.y,v.min.z));
00922     expand(dVector(v.max.x,v.max.y,v.min.z));
00923     expand(dVector(v.min.x,v.min.y,v.max.z));
00924     expand(dVector(v.max.x,v.min.y,v.max.z));
00925     expand(dVector(v.min.x,v.max.y,v.max.z));
00926     expand(v.max);
00927 }
00928 
00929 void dBoundingBox::expandby(float a)
00930 {
00931     max.x+=a; max.y+=a; max.z+=a;
00932     min.x-=a; min.y-=a; min.z-=a; 
00933 }
00934 
00935 bool dBoundingBox::inside(dVector p) const
00936 { 
00937     return (p.x>min.x && p.x<max.x &&
00938             p.y>min.y && p.y<max.y &&
00939             p.z>min.z && p.z<max.z);
00940 }
00941     
00942 // conversions
00943 dMatrix dQuat::toMatrix() const
00944 {
00945     float Nq = x*x + y*y + z*z + w*w;
00946     float s = (Nq > 0.f) ? (2.0f / Nq) : 0.f;
00947     float xs = x*s, ys = y*s, zs = z*s;
00948     float wx = w*xs, wy = w*ys, wz = w*zs;
00949     float xx = x*xs, xy = x*ys, xz = x*zs;
00950     float yy = y*ys, yz = y*zs, zz = z*zs;
00951     return dMatrix(1.0f - (yy + zz),
00952                xy + wz,
00953                xz - wy,
00954                0,
00955                xy - wz,          
00956                1.0f - (xx + zz),
00957                yz + wx,
00958                0,
00959                xz + wy,          
00960                yz - wx,          
00961                1.0f - (xx + yy),
00962                0, 0, 0, 0, 1.0f);
00963 
00964 }
00965 
00966 // operations
00967 dQuat dQuat::conjugate() const
00968 {
00969     return dQuat(-x,-y,-z,w);
00970 }
00971 
00972 // make multiply look like multiply
00973 dQuat dQuat::operator* (const dQuat&qR) const
00974 {
00975     dQuat qq;
00976     qq.w = w*qR.w - x*qR.x - y*qR.y - z*qR.z;
00977     qq.x = w*qR.x + x*qR.w + y*qR.z - z*qR.y;
00978     qq.y = w*qR.y + y*qR.w + z*qR.x - x*qR.z;
00979     qq.z = w*qR.z + z*qR.w + x*qR.y - y*qR.x;
00980     return (qq);
00981 }
00982 
00983 void dQuat::renorm() 
00984 {
00985     float Nq = 1.f / (float) (x*x + y*y + z*z + w*w);
00986     x *= Nq;
00987     y *= Nq;
00988     z *= Nq;
00989     w *= Nq;
00990 }
00991 
00992 void dQuat::setaxisangle(dVector axis, float angle)
00993 { 
00994     angle*=0.017453292;
00995     w = cos(angle/2);
00996     axis.normalise();
00997     axis *= sin(angle/2);
00998     x=axis.x;
00999     y=axis.y;
01000     z=axis.z;
01001 }
01002 
01003 float dGeometry::pointlinedist(const dVector &p, const dVector &start, const dVector &end)
01004 {
01005     float linemag;
01006     dVector intersection;
01007  
01008     float len = end.dist(start);
01009  
01010     float t = ((p.x-start.x)*(end.x-start.x) +
01011                (p.y-start.y)*(end.y-start.y) +
01012                (p.z-start.z)*(end.z-start.z)) / (len*len);
01013  
01014     if (t<0.0f) // off the end
01015     {
01016         return p.dist(start);
01017     }
01018     if (t>1.0f) // off the end
01019     {
01020         return p.dist(end);
01021     }
01022     
01023     intersection.x = start.x+t*(end.x-start.x);
01024     intersection.y = start.y+t*(end.y-start.y);
01025     intersection.z = start.z+t*(end.z-start.z);
01026  
01027     return p.dist(intersection);
01028 }
01029 

Generated on Mon Feb 11 06:54:25 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1