00001 /* 00002 Copyright (C) 1997,1998,1999 00003 Kenji Hiranabe, Eiwa System Management, Inc. 00004 00005 This program is free software. 00006 Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), 00007 conforming to the Java(TM) 3D API specification by Sun Microsystems. 00008 00009 Permission to use, copy, modify, distribute and sell this software 00010 and its documentation for any purpose is hereby granted without fee, 00011 provided that the above copyright notice appear in all copies and 00012 that both that copyright notice and this permission notice appear 00013 in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. 00014 makes no representations about the suitability of this software for any 00015 purpose. It is provided "AS IS" with NO WARRANTY. 00016 00017 Copyright (C) 2004 00018 Rune Myrland, Skalden Studio AS 00019 SagaEngine adaptions. Kenji Hiranabe's license apply. 00020 */ 00021 #ifndef base_vecmath_Tuple4_hpp 00022 #define base_vecmath_Tuple4_hpp 00023 00024 #include "util_vecmath.hpp" 00025 #include "../error/Log.hpp" 00026 #include "../math/CoorT.hpp" 00027 00028 namespace se_core { 00029 00038 class _SeCoreExport Tuple4 { 00039 public: 00043 typedef coor_t value_type; 00047 typedef short size_type; 00051 enum { DIMENSION = 4 }; 00052 00056 coor_t x_; 00057 00061 coor_t y_; 00062 00066 coor_t z_; 00067 00071 coor_t w_; 00072 00080 Tuple4(coor_t xvalue, coor_t yvalue, coor_t zvalue, coor_t wvalue): 00081 x_(xvalue), y_(yvalue), z_(zvalue), w_(wvalue) { } 00082 00087 Tuple4(const coor_t t[]): x_(t[0]), y_(t[1]), z_(t[2]), w_(t[3]) { } 00088 00092 Tuple4() { } 00093 00101 void set(coor_t xvalue, coor_t yvalue, coor_t zvalue, coor_t wvalue) { 00102 x_ = xvalue; 00103 y_ = yvalue; 00104 z_ = zvalue; 00105 w_ = wvalue; 00106 } 00107 00112 void set(const coor_t t[]) { 00113 x_ = t[0]; 00114 y_ = t[1]; 00115 z_ = t[2]; 00116 w_ = t[3]; 00117 } 00118 00123 void set(const Tuple4& t1) { 00124 x_ = t1.x_; 00125 y_ = t1.y_; 00126 z_ = t1.z_; 00127 w_ = t1.w_; 00128 } 00129 00134 void get(coor_t t[]) const { 00135 t[0] = x_; 00136 t[1] = y_; 00137 t[2] = z_; 00138 t[3] = w_; 00139 } 00140 00145 void get(Tuple4* t) const { 00146 Assert(t); 00147 t->x_ = x_; 00148 t->y_ = y_; 00149 t->z_ = z_; 00150 t->w_ = w_; 00151 } 00152 00158 void add(const Tuple4& t1, const Tuple4& t2) { 00159 x_ = t1.x_ + t2.x_; 00160 y_ = t1.y_ + t2.y_; 00161 z_ = t1.z_ + t2.z_; 00162 w_ = t1.w_ + t2.w_; 00163 } 00164 00169 void add(const Tuple4& t1) { 00170 x_ += t1.x_; 00171 y_ += t1.y_; 00172 z_ += t1.z_; 00173 w_ += t1.w_; 00174 } 00175 00176 00182 void sub(const Tuple4& t1, const Tuple4& t2) { 00183 x_ = t1.x_ - t2.x_; 00184 y_ = t1.y_ - t2.y_; 00185 z_ = t1.z_ - t2.z_; 00186 w_ = t1.w_ - t2.w_; 00187 } 00188 00193 void sub(const Tuple4& t1) { 00194 x_ -= t1.x_; 00195 y_ -= t1.y_; 00196 z_ -= t1.z_; 00197 w_ -= t1.w_; 00198 } 00199 00204 void negate(const Tuple4& t1) { 00205 x_ = -t1.x_; 00206 y_ = -t1.y_; 00207 z_ = -t1.z_; 00208 w_ = -t1.w_; 00209 } 00210 00214 void negate() { 00215 x_ = -x_; 00216 y_ = -y_; 00217 z_ = -z_; 00218 w_ = -w_; 00219 } 00220 00221 00227 void scale(scale_t s, const Tuple4& t1) { 00228 x_ = CoorT::scale(s, t1.x_); 00229 y_ = CoorT::scale(s, t1.y_); 00230 z_ = CoorT::scale(s, t1.z_); 00231 w_ = CoorT::scale(s, t1.w_); 00232 } 00233 00238 void scale(scale_t s) { 00239 x_ = CoorT::scale(s, x_); 00240 y_ = CoorT::scale(s, y_); 00241 z_ = CoorT::scale(s, z_); 00242 w_ = CoorT::scale(s, w_); 00243 } 00244 00252 void scaleAdd(scale_t s, const Tuple4& t1, const Tuple4& t2) { 00253 x_ = CoorT::scale(s, t1.x_) + t2.x_; 00254 y_ = CoorT::scale(s, t1.y_) + t2.y_; 00255 z_ = CoorT::scale(s, t1.z_) + t2.z_; 00256 w_ = CoorT::scale(s, t1.w_) + t2.w_; 00257 } 00258 00265 void scaleAdd(scale_t s, const Tuple4& t1) { 00266 x_ = CoorT::scale(s, x_) + t1.x_; 00267 y_ = CoorT::scale(s, y_) + t1.y_; 00268 z_ = CoorT::scale(s, z_) + t1.z_; 00269 w_ = CoorT::scale(s, w_) + t1.w_; 00270 } 00271 00272 00278 bool equals(const Tuple4& t1) const { 00279 return x_ == t1.x_ && y_ == t1.y_ && z_ == t1.z_ && w_ == t1.w_; 00280 } 00281 00282 bool isNan() const; 00283 00291 bool epsilonEquals(const Tuple4& t1, coor_t epsilon) const; 00292 00300 void clamp(coor_t min, coor_t max, const Tuple4& t) { 00301 set(t); 00302 clamp(min, max); 00303 } 00304 00311 void clampMin(coor_t min, const Tuple4& t) { 00312 set(t); 00313 clampMin(min); 00314 } 00315 00322 void clampMax(coor_t max, const Tuple4& t) { 00323 set(t); 00324 clampMax(max); 00325 } 00326 00332 void absolute(const Tuple4& t) { 00333 set(t); 00334 absolute(); 00335 } 00336 00342 void clamp(coor_t min, coor_t max) { 00343 clampMin(min); 00344 clampMax(max); 00345 } 00346 00351 void clampMin(coor_t min) { 00352 if (x_ < min) 00353 x_ = min; 00354 if (y_ < min) 00355 y_ = min; 00356 if (z_ < min) 00357 z_ = min; 00358 if (w_ < min) 00359 w_ = min; 00360 } 00361 00366 void clampMax(coor_t max) { 00367 if (x_ > max) 00368 x_ = max; 00369 if (y_ > max) 00370 y_ = max; 00371 if (z_ > max) 00372 z_ = max; 00373 if (w_ > max) 00374 w_ = max; 00375 } 00376 00380 void absolute() { 00381 if (x_ < 0) 00382 x_ = -x_; 00383 if (y_ < 0) 00384 y_ = -y_; 00385 if (z_ < 0) 00386 z_ = -z_; 00387 if (w_ < 0) 00388 w_ = -w_; 00389 } 00390 00398 void interpolate(const Tuple4& t1, const Tuple4& t2, scale_t alpha) { 00399 set(t1); 00400 interpolate(t2, alpha); 00401 } 00402 00409 void interpolate(const Tuple4& t1, scale_t alpha) { 00410 scale_t beta = SCALE_RES - alpha; 00411 x_ = CoorT::fromScale(beta*x_ + alpha*t1.x_); 00412 y_ = CoorT::fromScale(beta*y_ + alpha*t1.y_); 00413 z_ = CoorT::fromScale(beta*z_ + alpha*t1.z_); 00414 w_ = CoorT::fromScale(beta*w_ + alpha*t1.w_); 00415 } 00416 00421 char* toString(char* buffer) const; 00422 const char* toLog() const; 00423 00424 00425 // copy constructor and operator = is made by complier 00426 00427 bool operator==(const Tuple4& t1) const { 00428 return equals(t1); 00429 } 00430 00431 coor_t operator[](size_type index) const { 00432 Assert(index < (size_type)DIMENSION); 00433 switch (index) { 00434 case 0: 00435 return x_; 00436 case 1: 00437 return y_; 00438 case 2: 00439 return z_; 00440 case 3: 00441 return w_; 00442 default: 00443 // error ! 00444 return 0; 00445 } 00446 } 00447 coor_t& operator[](size_type index) { 00448 static coor_t dummy; 00449 Assert(index < (size_type)DIMENSION); 00450 switch (index) { 00451 case 0: 00452 return x_; 00453 case 1: 00454 return y_; 00455 case 2: 00456 return z_; 00457 case 3: 00458 return w_; 00459 default: 00460 // error ! 00461 return dummy; 00462 } 00463 } 00464 00465 Tuple4& operator=(const Tuple4& t1) { 00466 set(t1); 00467 return *this; 00468 } 00469 00470 Tuple4& operator+=(const Tuple4& t1) { 00471 add(t1); 00472 return *this; 00473 } 00474 Tuple4& operator-=(const Tuple4& t1) { 00475 sub(t1); 00476 return *this; 00477 } 00478 Tuple4& operator*=(scale_t s) { 00479 scale(s); 00480 return *this; 00481 } 00482 Tuple4 operator+(const Tuple4& t1) const { 00483 return (Tuple4(*this)).operator+=(t1); 00484 } 00485 Tuple4 operator-(const Tuple4& t1) const { 00486 return (Tuple4(*this)).operator-=(t1); 00487 } 00488 Tuple4 operator*(scale_t s) const { 00489 return (Tuple4(*this)).operator*=(s); 00490 } 00491 }; 00492 00493 } // Namespace 00494 00495 inline 00496 se_core::Tuple4 operator*(scale_t s, const se_core::Tuple4& t1) { 00497 return (se_core::Tuple4(t1)).operator*=(s); 00498 } 00499 00500 00501 #endif /* TUPLE4_H */
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:13 2007 by Doxygen version 1.3.9.1.