00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 void dVector::get_rot(float m[16],dVector up)
00057 {
00058 dVector a,b,c;
00059 a.x=this->x; a.y=this->y; a.z=this->z;
00060 a.normalise();
00061 if (a==up) a.x+=0.01;
00062 b=a.cross(up);
00063 b.normalise();
00064 c=b.cross(a);
00065 c.normalise();
00066
00067 for (int n=0; n<16; n++)
00068 {
00069 m[n]=0;
00070 }
00071
00072 m[15]=1;
00073
00074 m[0]=a.x; m[1]=a.y; m[2]=a.z;
00075 m[4]=b.x; m[5]=b.y; m[6]=b.z;
00076 m[8]=c.x; m[9]=c.y; m[10]=c.z;
00077 }
00078
00079 dVector Fluxus::operator-(dVector rhs)
00080 {
00081 return dVector(-rhs.x,-rhs.y,-rhs.z);
00082 }
00083
00084 ostream &Fluxus::operator<<(ostream &os, dVector const &om)
00085 {
00086 os<<om.x<<" "<<om.y<<" "<<om.z<<" "<<om.w<<" ";
00087 return os;
00088 }
00089
00090 istream &Fluxus::operator>>(istream &is, dVector &om)
00091 {
00092 is>>om.x>>om.y>>om.z>>om.w;
00093 return is;
00094 }
00095
00097
00098 dColour::dColour(float x, float y, float z, float w, COLOUR_MODE mode)
00099 {
00100 if (mode == MODE_RGB)
00101 {
00102 r = x; g = y; b = z; a = w;
00103 }
00104 else
00105 {
00106 HSVtoRGB(x, y, z, arr());
00107 a = w;
00108 }
00109 }
00110
00111 dColour::dColour(float *xyzw, COLOUR_MODE mode)
00112 {
00113 if (mode == MODE_RGB)
00114 {
00115 r = xyzw[0]; g = xyzw[1]; b = xyzw[2]; a = xyzw[3];
00116 }
00117 else
00118 {
00119 HSVtoRGB(xyzw[0], xyzw[1], xyzw[2], arr());
00120 a = xyzw[3];
00121 }
00122 }
00123
00124 void dColour::RGBtoHSV(float r, float g, float b, float *hsv)
00125 {
00126 float h, s, v;
00127
00128 float rgbmax = (r > g) ?
00129 ((b > r) ? b : r) :
00130 ((b > g) ? b : g);
00131 float rgbmin = (r < g) ?
00132 ((b < r) ? b : r) :
00133 ((b < g) ? b : g);
00134 float delta = rgbmax - rgbmin;
00135
00136 v = rgbmax;
00137
00138 if (rgbmax == 0)
00139 {
00140 hsv[0] = hsv[1] = hsv[2] = 0;
00141 return;
00142 }
00143
00144 s = delta / rgbmax;
00145
00146 if (s == 0)
00147 h = 0;
00148 else
00149 if (r == rgbmax)
00150 h = (g - b) / delta;
00151 else
00152 if (g == rgbmax)
00153 h = 2 + (b - r) / delta;
00154 else
00155 h = 4 + (r - g) / delta;
00156
00157 if (h < 0)
00158 h += 6;
00159
00160 h /= 6;
00161
00162 hsv[0] = h;
00163 hsv[1] = s;
00164 hsv[2] = v;
00165 }
00166
00167 void dColour::HSVtoRGB(float h, float s, float v, float *rgb)
00168 {
00169 if (h >= 1) h = 0;
00170 if (s > 1) s = 1;
00171 if (v > 1) v = 1;
00172 if (h < 0) h = 0;
00173 if (s < 0) s = 0;
00174 if (v < 0) v = 0;
00175
00176 float h6 = h * 6;
00177 int i = floor(h6);
00178 float f = h6 - i;
00179
00180 float p = v * (1 - s);
00181 float q = v * (1 - f * s);
00182 float t = v * (1 - (1 - f) * s);
00183
00184 switch (i)
00185 {
00186 case 0:
00187 rgb[0] = v; rgb[1] = t; rgb[2] = p;
00188 break;
00189 case 1:
00190 rgb[0] = q; rgb[1] = v; rgb[2] = p;
00191 break;
00192 case 2:
00193 rgb[0] = p; rgb[1] = v; rgb[2] = t;
00194 break;
00195 case 3:
00196 rgb[0] = p; rgb[1] = q; rgb[2] = v;
00197 break;
00198 case 4:
00199 rgb[0] = t; rgb[1] = p; rgb[2] = v;
00200 break;
00201 case 5:
00202 rgb[0] = v; rgb[1] = p; rgb[2] = q;
00203 break;
00204 }
00205 }
00206
00207 ostream &Fluxus::operator<<(ostream &os, dColour const &om)
00208 {
00209 os<<"r="<<om.r<<" g="<<om.g<<" b="<<om.b<<" a="<<om.a<<" ";
00210 return os;
00211 }
00212
00214
00215 dVertex const &dVertex::operator=(dVertex const &rhs)
00216 {
00217 point=rhs.point;
00218 normal=rhs.normal;
00219 col=rhs.col;
00220 s=rhs.s;
00221 t=rhs.t;
00222 return rhs;
00223 }
00224
00225 ostream &Fluxus::operator<<(ostream &os, dVertex const &v)
00226 {
00227 os<<"Vertex : p="<<v.point<<" n="<<v.normal<<v.col<<" "<<v.s<<" "<<v.t<<endl;
00228 return os;
00229 }
00230
00232
00233 ostream &Fluxus::operator<<(ostream &os, dMatrix const &om)
00234 {
00235 for (int j=0; j<4; j++)
00236 {
00237 for (int i=0; i<4; i++)
00238 {
00239 os<<om.m[i][j]<<" ";
00240 }
00241 os<<endl;
00242 }
00243
00244 return os;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 void dBoundingBox::expand(dVector v)
00260 {
00261 if (m_Empty)
00262 {
00263 min=v;
00264 max=v;
00265 m_Empty=false;
00266 }
00267
00268 if (v.x<min.x) min.x=v.x;
00269 if (v.y<min.y) min.y=v.y;
00270 if (v.z<min.z) min.z=v.z;
00271
00272 if (v.x>=max.x) max.x=v.x;
00273 if (v.y>=max.y) max.y=v.y;
00274 if (v.z>=max.z) max.z=v.z;
00275 }
00276
00277 void dBoundingBox::expand(dBoundingBox v)
00278 {
00279 expand(v.min);
00280 expand(dVector(v.max.x,v.min.y,v.min.z));
00281 expand(dVector(v.min.x,v.max.y,v.min.z));
00282 expand(dVector(v.max.x,v.max.y,v.min.z));
00283 expand(dVector(v.min.x,v.min.y,v.max.z));
00284 expand(dVector(v.max.x,v.min.y,v.max.z));
00285 expand(dVector(v.min.x,v.max.y,v.max.z));
00286 expand(v.max);
00287 }
00288
00289 void dBoundingBox::expandby(float a)
00290 {
00291 max.x+=a; max.y+=a; max.z+=a;
00292 min.x-=a; min.y-=a; min.z-=a;
00293 }
00294
00295 bool dBoundingBox::inside(dVector p) const
00296 {
00297 return (p.x>min.x && p.x<max.x &&
00298 p.y>min.y && p.y<max.y &&
00299 p.z>min.z && p.z<max.z);
00300 }
00301
00302
00303 dMatrix dQuat::toMatrix() const
00304 {
00305 float Nq = x*x + y*y + z*z + w*w;
00306 float s = (Nq > 0.f) ? (2.0f / Nq) : 0.f;
00307 float xs = x*s, ys = y*s, zs = z*s;
00308 float wx = w*xs, wy = w*ys, wz = w*zs;
00309 float xx = x*xs, xy = x*ys, xz = x*zs;
00310 float yy = y*ys, yz = y*zs, zz = z*zs;
00311 return dMatrix(1.0f - (yy + zz),
00312 xy + wz,
00313 xz - wy,
00314 0,
00315 xy - wz,
00316 1.0f - (xx + zz),
00317 yz + wx,
00318 0,
00319 xz + wy,
00320 yz - wx,
00321 1.0f - (xx + yy),
00322 0, 0, 0, 0, 1.0f);
00323
00324 }
00325
00326
00327 dQuat dQuat::conjugate() const
00328 {
00329 return dQuat(-x,-y,-z,w);
00330 }
00331
00332
00333 dQuat dQuat::operator* (const dQuat&qR) const
00334 {
00335 dQuat qq;
00336 qq.w = w*qR.w - x*qR.x - y*qR.y - z*qR.z;
00337 qq.x = w*qR.x + x*qR.w + y*qR.z - z*qR.y;
00338 qq.y = w*qR.y + y*qR.w + z*qR.x - x*qR.z;
00339 qq.z = w*qR.z + z*qR.w + x*qR.y - y*qR.x;
00340 return (qq);
00341 }
00342
00343 void dQuat::renorm()
00344 {
00345 float Nq = 1.f / (float) (x*x + y*y + z*z + w*w);
00346 x *= Nq;
00347 y *= Nq;
00348 z *= Nq;
00349 w *= Nq;
00350 }
00351
00352 void dQuat::setaxisangle(dVector axis, float angle)
00353 {
00354 angle*=0.017453292;
00355 w = cos(angle/2);
00356 axis.normalise();
00357 axis *= sin(angle/2);
00358 x=axis.x;
00359 y=axis.y;
00360 z=axis.z;
00361 }
00362
00363