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