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_Tuple2_hpp 00022 #define base_vecmath_Tuple2_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 Tuple2 { 00039 public: 00043 enum { DIMENSION = 2 }; 00044 00048 coor_t x_; 00049 00053 coor_t y_; 00054 00060 Tuple2(coor_t xvalue, coor_t yvalue): x_(xvalue), y_(yvalue) { } 00061 00066 Tuple2(const coor_t t[]): x_(t[0]), y_(t[1]) { } 00067 00068 00072 Tuple2(): x_(0), y_(0) { } 00073 00079 void set(coor_t xvalue, coor_t yvalue) { 00080 x_ = xvalue; 00081 y_ = yvalue; 00082 } 00083 00088 void set(const coor_t t[]) { 00089 x_ = t[0]; 00090 y_ = t[1]; 00091 } 00092 00097 void set(const Tuple2& t1) { 00098 x_ = t1.x_; 00099 y_ = t1.y_; 00100 } 00101 00107 void get(coor_t t[]) const { 00108 t[0] = x_; 00109 t[1] = y_; 00110 } 00111 00116 void get(Tuple2* t) const { 00117 Assert(t); 00118 t->x_ = x_; 00119 t->y_ = y_; 00120 } 00121 00127 void add(const Tuple2& t1, const Tuple2& t2) { 00128 x_ = t1.x_ + t2.x_; 00129 y_ = t1.y_ + t2.y_; 00130 } 00131 00136 void add(const Tuple2& t1) { 00137 x_ += t1.x_; 00138 y_ += t1.y_; 00139 } 00140 00146 void sub(const Tuple2& t1, const Tuple2& t2) { 00147 x_ = t1.x_ - t2.x_; 00148 y_ = t1.y_ - t2.y_; 00149 } 00150 00155 void sub(const Tuple2& t1) { 00156 x_ -= t1.x_; 00157 y_ -= t1.y_; 00158 } 00159 00164 void negate(const Tuple2& t1) { 00165 x_ = -t1.x_; 00166 y_ = -t1.y_; 00167 } 00168 00172 void negate() { 00173 x_ = -x_; 00174 y_ = -y_; 00175 } 00176 00182 void scale(scale_t s, const Tuple2& t1) { 00183 x_ = CoorT::scale(s, t1.x_); 00184 y_ = CoorT::scale(s, t1.y_); 00185 } 00186 00191 void scale(scale_t s) { 00192 x_ = CoorT::scale(s, x_); 00193 y_ = CoorT::scale(s, y_); 00194 } 00195 00203 void scaleAdd(scale_t s, const Tuple2& t1, const Tuple2& t2) { 00204 x_ = CoorT::scale(s, t1.x_ + t2.x_); 00205 y_ = CoorT::scale(s, t1.y_ + t2.y_); 00206 } 00207 00214 void scaleAdd(scale_t s, const Tuple2& t1) { 00215 x_ = CoorT::scale(s, x_) + t1.x_; 00216 y_ = CoorT::scale(s, y_) + t1.y_; 00217 } 00218 00224 bool equals(const Tuple2& t1) const { 00225 return t1.x_ == x_ && t1.y_ == y_; 00226 } 00227 00228 bool isNan() const; 00229 00237 bool epsilonEquals(const Tuple2& t1, coor_t epsilon) const; 00238 00246 void clamp(coor_t min, coor_t max, const Tuple2& t) { 00247 set(t); 00248 clamp(min, max); 00249 } 00250 00257 void clampMin(coor_t min, const Tuple2& t) { 00258 set(t); 00259 clampMin(min); 00260 } 00261 00268 void clampMax(coor_t max, const Tuple2& t) { 00269 set(t); 00270 clampMax(max); 00271 } 00272 00273 00279 void absolute(const Tuple2& t) { 00280 set(t); 00281 absolute(); 00282 } 00283 00289 void clamp(coor_t min, coor_t max) { 00290 clampMin(min); 00291 clampMax(max); 00292 } 00293 00298 void clampMin(coor_t min) { 00299 if (x_ < min) 00300 x_ = min; 00301 if (y_ < min) 00302 y_ = min; 00303 } 00304 00309 void clampMax(coor_t max) { 00310 if (x_ > max) 00311 x_ = max; 00312 if (y_ > max) 00313 y_ = max; 00314 } 00315 00319 void absolute() { 00320 if (x_ < 0) 00321 x_ = -x_; 00322 if (y_ < 0) 00323 y_ = -y_; 00324 } 00325 00333 void interpolate(const Tuple2& t1, const Tuple2& t2, scale_t alpha) { 00334 set(t1); 00335 interpolate(t2, alpha); 00336 } 00337 00338 00346 void interpolate(const Tuple2& t1, scale_t alpha) { 00347 scale_t beta = SCALE_RES - alpha; 00348 x_ = CoorT::fromScale(beta*x_ + alpha*t1.x_); 00349 y_ = CoorT::fromScale(beta*y_ + alpha*t1.y_); 00350 } 00351 00356 char* toString(char* buffer) const; 00357 00358 // copy constructor and operator = is made by complier 00359 00360 bool operator==(const Tuple2& t1) const { 00361 return equals(t1); 00362 } 00363 00364 coor_t operator[](short index) const { 00365 Assert(index < (short)DIMENSION); 00366 switch (index) { 00367 case 0: 00368 return x_; 00369 case 1: 00370 return y_; 00371 default: 00372 // error ! 00373 return 0; 00374 } 00375 } 00376 coor_t& operator[](short index) { 00377 static coor_t dummy; 00378 Assert(index < (short)DIMENSION); 00379 switch (index) { 00380 case 0: 00381 return x_; 00382 case 1: 00383 return y_; 00384 default: 00385 // error ! 00386 return dummy; 00387 } 00388 } 00389 00390 Tuple2& operator=(const Tuple2& t1) { 00391 set(t1); 00392 return *this; 00393 } 00394 00395 Tuple2& operator+=(const Tuple2& t1) { 00396 add(t1); 00397 return *this; 00398 } 00399 Tuple2& operator-=(const Tuple2& t1) { 00400 sub(t1); 00401 return *this; 00402 } 00403 Tuple2& operator*=(scale_t s) { 00404 scale(s); 00405 return *this; 00406 } 00407 Tuple2 operator+(const Tuple2& t1) const { 00408 return (Tuple2(*this)).operator+=(t1); 00409 } 00410 Tuple2 operator-(const Tuple2& t1) const { 00411 return (Tuple2(*this)).operator-=(t1); 00412 } 00413 Tuple2 operator*(scale_t s) const { 00414 return (Tuple2(*this)).operator*=(s); 00415 } 00416 00417 }; 00418 00419 se_err::Log& operator<< (se_err::Log& log, const Tuple2& b); 00420 00421 } // Namespace 00422 00423 inline 00424 se_core::Tuple2 operator*(scale_t s, const se_core::Tuple2& t1) { 00425 return (se_core::Tuple2(t1)).operator*=(s); 00426 } 00427 00428 #endif /* TUPLE2_H */
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:13 2007 by Doxygen version 1.3.9.1.