00001 #ifndef __VECMATH_TUPLE3_HPP
00002 #define __VECMATH_TUPLE3_HPP
00003
00004 #include <stdio.h>
00005 #include <iostream>
00006
00007 #ifndef __LANG_MATH_HPP
00008 #include <vecmath/Math.h>
00009 #endif
00010
00011 #include <vecmath/String.h>
00012
00013
00014 #define DEBUGTUPLE3(d)
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 template <class Type> class VECMATH_EXPORT _Tuple3 {
00032 public:
00033 Type x, y, z;
00034
00035 _Tuple3<Type>() {}
00036
00037 explicit _Tuple3<Type>(Type x, Type y = 0, Type z = 0) : x(x), y(y), z(z) {
00038 DEBUGTUPLE3(std::cout << "_Tuple3 explicit constructor x,y,z " << *this << endl);
00039 }
00040
00041 _Tuple3<Type>(const _Tuple3<byte>& t) {
00042 set(t);
00043 DEBUGTUPLE3(cout << "_Tuple3 constructor &byte " << *this << endl);
00044 }
00045
00046 _Tuple3<Type>(const _Tuple3<double>& t) {
00047 set(t);
00048 DEBUGTUPLE3(cout << "_Tuple3 constructor &double " << *this << endl);
00049 }
00050
00051 _Tuple3<Type>(const _Tuple3<float>& t) {
00052 set(t);
00053 DEBUGTUPLE3(cout << "_Tuple3 constructor &float " << *this << endl);
00054 }
00055
00056 _Tuple3<Type>(const _Tuple3<int>& t) {
00057 set(t);
00058 DEBUGTUPLE3(cout << "_Tuple3 constructor &float " << *this << endl);
00059 }
00060
00061 _Tuple3<Type>(const Type t[3]) {
00062 set(t);
00063 DEBUGTUPLE3(cout << "_Tuple3 constructor v[3] " << *this << endl);
00064 }
00065
00066 ~_Tuple3<Type>() {
00067 DEBUGTUPLE3(cout << "_Tuple3 destructor " << *this << endl);
00068 }
00069
00070 void add(const _Tuple3<Type>& t) {
00071 x += t.x;
00072 y += t.y;
00073 z += t.z;
00074 }
00075
00076 void add(const _Tuple3<Type>& t, const _Tuple3<Type>& t1) {
00077 x = t.x + t1.x;
00078 y = t.y + t1.y;
00079 z = t.z + t1.z;
00080 }
00081
00082 static Type* add(Type t[3], const Type t1[3], const Type t2[3]) {
00083 t[0] = t1[0] + t2[0];
00084 t[1] = t1[1] + t2[1];
00085 t[2] = t1[2] + t2[2];
00086 return t;
00087 }
00088
00089 void absolute() {
00090 absolute(*this);
00091 }
00092
00093 void absolute(const _Tuple3<Type>& t) {
00094 DEBUGTUPLE3(cout << "_Tuple3 absolute(t) " << *this << " = ");
00095 set(Math::abs(t.x), Math::abs(t.y), Math::abs(t.z));
00096 DEBUGTUPLE3(cout << *this << endl);
00097 }
00098
00099 void clamp(Type min, Type max) {
00100 DEBUGTUPLE3(cout << "_Tuple3 clamp(" << min << ", " << max << ") = " << *this << endl);
00101 clamp(min, max, *this);
00102 }
00103
00104 void clampMin(Type min) {
00105 DEBUGTUPLE3(cout << "_Tuple3 clampMin(" << min << ") = " << *this << endl);
00106 clampMin(min, *this);
00107 }
00108
00109 void clampMax(Type max) {
00110 DEBUGTUPLE3(cout << "_Tuple3 clampMax(" << max << ") = " << *this << endl);
00111 clampMax(max, *this);
00112 }
00113
00114 void clamp(Type min, Type max, const _Tuple3<Type> &t);
00115 void clampMin(Type min, const _Tuple3<Type> &t);
00116 void clampMax(Type min, const _Tuple3<Type> &t);
00117
00118 bool equals(const _Tuple3<Type> &t) const {
00119 DEBUGTUPLE3(cout << "_Tuple3 epsilon() " << *this << endl);
00120 return (x == t.x && y == t.y && z == t.z);
00121 }
00122
00123 bool epsilonEquals(const _Tuple3<Type> &t, Type epsilon) const {
00124 DEBUGTUPLE3(cout << "_Tuple3 epsilonEquals() " << *this << endl);
00125
00126 return (Math::epsilonEquals(x, t.x, epsilon)
00127 && Math::epsilonEquals(y, t.y, epsilon)
00128 && Math::epsilonEquals(z, t.z, epsilon));
00129 }
00130
00131 void get(byte t[3]) const {
00132 t[0] = byte(x);
00133 t[1] = byte(y);
00134 t[2] = byte(z);
00135 }
00136
00137 void get(double t[3]) const {
00138 t[0] = x;
00139 t[1] = y;
00140 t[2] = z;
00141 }
00142
00143 void get(float t[3]) const {
00144 t[0] = x;
00145 t[1] = y;
00146 t[2] = z;
00147 }
00148
00149 void get(int t[3]) const {
00150 t[0] = int(x);
00151 t[1] = int(y);
00152 t[2] = int(z);
00153 }
00154
00155 void get(_Tuple3<Type> &t) const {
00156 t.x = x;
00157 t.y = y;
00158 t.z = z;
00159 }
00160
00161 void interpolate(_Tuple3<Type> &t, double alpha) {
00162 interpolate(*this, t, alpha);
00163 }
00164
00165 void interpolate(_Tuple3<Type> &t1, const _Tuple3<Type> &t2, double alpha);
00166
00167 void negate() {
00168 negate(*this);
00169 }
00170
00171 void negate(const _Tuple3<Type> &t) {
00172 set(-t.x, -t.y, -t.z);
00173 }
00174
00175 static void negate(Type t[3], Type t1[3]) {
00176 t[0] = -t1[0];
00177 t[1] = -t1[1];
00178 t[2] = -t1[2];
00179 }
00180
00181
00182 Type normSquared() const {
00183 return Type(Math::sqr(x) + Math::sqr(y) + Math::sqr(z));
00184 }
00185
00186 static Type normSquared(const Type t[3]) {
00187 fprintf(stderr,"norm: %f\n",(float)Math::sqr(t[0]) + Math::sqr(t[1]) + Math::sqr(t[2]));
00188 return Type(Math::sqr(t[0]) + Math::sqr(t[1]) + Math::sqr(t[2]));
00189 }
00190
00191
00192 Type norm() const {
00193 return Type(sqrt(normSquared()));
00194 }
00195
00196
00197 static Type norm(const Type t[3]) {
00198 return Type(sqrt(normSquared(t)));
00199 }
00200
00201 void scale(Type s) {
00202 x *= s;
00203 y *= s;
00204 z *= s;
00205 }
00206
00207 static Type* scale(Type t[3], Type s) {
00208 t[0] *= s;
00209 t[1] *= s;
00210 t[2] *= s;
00211
00212 return t;
00213 }
00214
00215 void scaleAdd(Type s, const _Tuple3<Type> &t) {
00216 scaleAdd(s, *this, t);
00217 }
00218
00219 void scaleAdd(Type s, const _Tuple3<Type> &t1, const _Tuple3<Type> &t2) {
00220 x = s * t1.x + t2.x;
00221 y = s * t1.y + t2.y;
00222 z = s * t1.z + t2.z;
00223 }
00224
00225 void scaleAdd(Type s, const float t1[3], const float t2[3]) {
00226 x = Type(s * t1[0] + t2[0]);
00227 y = Type(s * t1[1] + t2[1]);
00228 z = Type(s * t1[2] + t2[2]);
00229 }
00230
00231 static Type* scaleAdd(Type t[3], Type s, const Type t1[3], const Type t2[3]) {
00232 t[0] = s * t1[0] + t2[0];
00233 t[1] = s * t1[1] + t2[1];
00234 t[2] = s * t1[2] + t2[2];
00235 return t;
00236 }
00237
00238 void set(Type xx = 0, Type yy = 0, Type zz = 0) {
00239 x = xx;
00240 y = yy;
00241 z = zz;
00242 }
00243
00244 void set(Type t[3]) {
00245 x = t[0];
00246 y = t[1];
00247 z = t[2];
00248 }
00249
00250 static Type* set(Type t[3], Type xx, Type yy, Type zz) {
00251 t[0] = xx;
00252 t[1] = yy;
00253 t[2] = zz;
00254
00255 return t;
00256 }
00257
00258 void set(const _Tuple3<byte>& t) {
00259 set(Type(t.x), Type(t.y), Type(t.z));
00260 }
00261
00262 void set(const _Tuple3<double>& t) {
00263 set(Type(t.x), Type(t.y), Type(t.z));
00264 }
00265
00266 void set(const _Tuple3<float>& t) {
00267 set(Type(t.x), Type(t.y), Type(t.z));
00268 }
00269
00270 void set(const _Tuple3<int>& t) {
00271 set(Type(t.x), Type(t.y), Type(t.z));
00272 }
00273
00274 void sub(const _Tuple3<Type>& t) {
00275 sub(*this, t);
00276 }
00277
00278 static Type* sub(Type t[3], const Type t1[3]) {
00279 return sub(t, t, t1);
00280 }
00281
00282 void sub(const _Tuple3<Type>& t1, const _Tuple3<Type>& t2) {
00283 x = t1.x - t2.x;
00284 y = t1.y - t2.y;
00285 z = t1.z - t2.z;
00286 }
00287
00288 static Type* sub(Type t[3], const Type t1[3], const Type t2[3]) {
00289 t[0] = t1[0] - t2[0];
00290 t[1] = t1[1] - t2[1];
00291 t[2] = t1[2] - t2[2];
00292
00293 return t;
00294 }
00295
00296 #if 0
00297 String &toString(String &s);
00298 #endif
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 _Tuple3<Type>& operator +=(const _Tuple3<Type>& t) {
00315 add(t);
00316 DEBUGTUPLE3(cout << "_Tuple3 operator+= _Tuple3 " << *this << endl);
00317 return *this;
00318 }
00319
00320 _Tuple3<Type>& operator -=(const _Tuple3<Type>& t) {
00321 sub(t);
00322 DEBUGTUPLE3(cout << "_Tuple3 operator-= _Tuple3 " << *this << endl);
00323 return *this;
00324 }
00325
00326 _Tuple3<Type>& operator *=(Type c) {
00327 scale(c);
00328 DEBUGTUPLE3(cout << "_Tuple3 operator*= _Tuple3 " << *this << endl);
00329 return *this;
00330 }
00331
00332 _Tuple3<Type>& operator /=(Type c) {
00333
00334 x /= c; y /= c; z /= c;
00335 DEBUGTUPLE3(cout << "_Tuple3 operator/= _Tuple3 " << *this << endl);
00336 return *this;
00337 }
00338
00339 _Tuple3<Type> operator +(const _Tuple3<Type>& t) const {
00340 DEBUGTUPLE3(cout << "_Tuple3 operator+ _Tuple3 " << *this << endl);
00341 return _Tuple3<Type>(*this) += t;
00342 }
00343
00344 _Tuple3<Type> operator -(const _Tuple3<Type>& t) const {
00345 DEBUGTUPLE3(cout << "_Tuple3 operator- _Tuple3 " << *this << endl);
00346 return _Tuple3<Type>(*this) -= t;
00347 }
00348
00349 _Tuple3<Type> operator *(Type c) const {
00350 DEBUGTUPLE3(cout << "_Tuple3 operator* " << *this << " * " << c << endl);
00351 return _Tuple3<Type>(*this) *= c;
00352 }
00353
00354 _Tuple3<Type> operator /(Type c) const {
00355 DEBUGTUPLE3(cout << "_Tuple3 operator/ " << *this << " / " << c << endl);
00356 return _Tuple3<Type>(*this) /= c;
00357 }
00358
00359
00360
00361
00362
00363
00364 bool operator ==(const _Tuple3<Type>& t) const {
00365 DEBUGTUPLE3(cout << "_Tuple3 operator== _Tuple3 " << *this << endl);
00366 return (epsilonEquals(t, (Type) Math::EPSILON));
00367 }
00368
00369 bool operator !=(const _Tuple3<Type>& t) const {
00370 DEBUGTUPLE3(cout << "_Tuple3 operator!= _Tuple3 " << *this << endl);
00371 return (!(*this == t));
00372 }
00373
00374 Type& operator [](int i) const {
00375 return (Type&) (operator const Type *())[i];
00376 }
00377
00378 operator const Type * () const {
00379 return &x;
00380 }
00381
00382
00383
00384
00385 };
00386
00387 #endif