Tuple3.hpp

Go to the documentation of this file.
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, involving conversion from floating point values to fixed point values.
00020    Kenji Hiranabe's license apply.
00021 */
00022 #ifndef base_vecmath_Tuple3_hpp
00023 #define base_vecmath_Tuple3_hpp
00024 
00025 #include "util_vecmath.hpp"
00026 #include "../math/CoorT.hpp"
00027 #include "../error/Log.hpp"
00028 
00029 namespace se_core {
00035     class _SeCoreExport Tuple3 {
00036     public:
00040         enum { DIMENSION = 3 };
00041 
00049         coor_t x_;
00050 
00056         coor_t y_;
00057 
00065         coor_t z_;
00066 
00073         Tuple3(coor_t xvalue, coor_t yvalue, coor_t zvalue): x_(xvalue), y_(yvalue), z_(zvalue) { }
00074 
00079         Tuple3(const coor_t t[]): x_(t[0]), y_(t[1]), z_(t[2]) { }
00080 
00084         Tuple3() { }
00085 
00092         void set(coor_t xvalue, coor_t yvalue, coor_t zvalue) {
00093             x_ = xvalue;
00094             y_ = yvalue;
00095             z_ = zvalue;
00096         }
00097 
00102         void set(const coor_t t[]) {
00103             x_ = t[0];
00104             y_ = t[1];
00105             z_ = t[2];
00106         }
00107 
00112         void set(const Tuple3& t1) {
00113             x_ = t1.x_;
00114             y_ = t1.y_;
00115             z_ = t1.z_;
00116         }
00117 
00118 
00122         void reset() {
00123             x_ = y_ = z_ = 0;
00124         }
00125 
00126         bool isZero() const {
00127             return (x_ == 0 && y_ == 0 && z_ == 0);
00128         }
00129 
00134         void get(coor_t t[]) const {
00135             t[0] = x_;
00136             t[1] = y_;
00137             t[2] = z_;
00138         }
00139 
00144         void get(Tuple3* t) const {
00145             Assert(t);
00146             t->x_ = x_;
00147             t->y_ = y_;
00148             t->z_ = z_;
00149         }
00150 
00156         void add(const Tuple3& t1, const Tuple3& t2) {
00157             x_ = t1.x_ + t2.x_;
00158             y_ = t1.y_ + t2.y_;
00159             z_ = t1.z_ + t2.z_;
00160         }
00161 
00166         void add(const Tuple3& t1) {
00167             x_ += t1.x_;
00168             y_ += t1.y_;
00169             z_ += t1.z_;
00170         }
00171 
00172 
00180         inline void add(const coor_t dx, const coor_t dy, const coor_t dz) {
00181             x_ += dx;
00182             y_ += dy;
00183             z_ += dz;
00184         }
00185 
00186 
00192         void sub(const Tuple3& t1, const Tuple3& t2) {
00193             x_ = t1.x_ - t2.x_;
00194             y_ = t1.y_ - t2.y_;
00195             z_ = t1.z_ - t2.z_;
00196         }
00197 
00202         void sub(const Tuple3& t1) {
00203             x_ -= t1.x_;
00204             y_ -= t1.y_;
00205             z_ -= t1.z_;
00206         }
00207 
00212         void negate(const Tuple3& t1) {
00213             x_ = -t1.x_;
00214             y_ = -t1.y_;
00215             z_ = -t1.z_;
00216         }
00217 
00221         void negate() {
00222             x_ = -x_;
00223             y_ = -y_;
00224             z_ = -z_;
00225         }
00226 
00232         void scale(scale_t s, const Tuple3& t1);
00233 
00238         void scale(scale_t s);
00239 
00246         void scaleAdd(scale_t s, const Tuple3& t1) {
00247             x_ = CoorT::scale(s, x_) + t1.x_;
00248             y_ = CoorT::scale(s, y_) + t1.y_;
00249             z_ = CoorT::scale(s, z_) + t1.z_;
00250         }
00251 
00259         void scaleAdd(scale_t s, const Tuple3& t1, const Tuple3& t2) {
00260             x_ = CoorT::scale(s, t1.x_) + t2.x_;
00261             y_ = CoorT::scale(s, t1.y_) + t2.y_;
00262             z_ = CoorT::scale(s, t1.z_) + t2.z_;
00263         }
00264 
00270         bool equals(const Tuple3& t1) const {
00271             return t1.x_ == x_ && t1.y_ == y_ && t1.z_ == z_;
00272         }
00273 
00274         bool isNan() const;
00275 
00283         bool epsilonEquals(const Tuple3& t1, coor_t epsilon) const;
00284 
00290         void absolute(const Tuple3& t) {
00291             set(t);
00292             absolute();
00293         }
00294 
00298         void absolute() {
00299             if (x_ < 0)
00300                 x_ = -x_;
00301             if (y_ < 0)
00302                 y_ = -y_;
00303             if (z_ < 0)
00304                 z_ = -z_;
00305         }
00306 
00312         void clamp(coor_t min, coor_t max) {
00313             clampMin(min);
00314             clampMax(max);
00315         }
00316 
00324         void clamp(coor_t min, coor_t max, const Tuple3& t) {
00325             set(t);
00326             clamp(min, max);
00327         }
00328 
00333         void clampMin(coor_t min) {
00334             if (x_ < min)
00335                 x_ = min;
00336             if (y_ < min)
00337                 y_ = min;
00338             if (z_ < min)
00339                 z_ = min;
00340         }
00341 
00348         void clampMin(coor_t min, const Tuple3& t) {
00349             set(t);
00350             clampMin(min);
00351         }
00352 
00359         void clampMax(coor_t max, const Tuple3& t) {
00360             set(t);
00361             clampMax(max);
00362         }
00363 
00368         void clampMax(coor_t max) {
00369             if (x_ > max)
00370                 x_ = max;
00371             if (y_ > max)
00372                 y_ = max;
00373             if (z_ > max)
00374                 z_ = max;
00375         }
00376 
00384         void interpolate(const Tuple3& t1, const Tuple3& t2, scale_t alpha) {
00385             set(t1);
00386             interpolate(t2, alpha);
00387         }
00388 
00395         void interpolate(const Tuple3& t1, scale_t alpha) {
00396             x_ += CoorT::scale(alpha, t1.x_ - x_);
00397             y_ += CoorT::scale(alpha, t1.y_ - y_);
00398             z_ += CoorT::scale(alpha, t1.z_ - z_);
00399         }
00400 
00405         char* toString(char* buffer) const;
00406         const char* toLog() const;
00407 
00408 
00409         // copy constructor and operator = is made by complier
00410         bool operator==(const Tuple3& t1) const {
00411             return equals(t1);
00412         }
00413 
00414         coor_t operator[](short index) const {
00415             Assert(index < (short)DIMENSION);
00416             switch (index) {
00417             case 0:
00418                 return x_;
00419             case 1:
00420                 return y_;
00421             case 2:
00422                 return z_;
00423             default:
00424                 // error !
00425                 return 0;
00426             }
00427         }
00428         coor_t& operator[](short index) {
00429             static coor_t dummy = 0;
00430             Assert(index < (short)DIMENSION);
00431             switch (index) {
00432             case 0:
00433                 return x_;
00434             case 1:
00435                 return y_;
00436             case 2:
00437                 return z_;
00438             default:
00439                 // error !
00440                 return dummy;
00441             }
00442         }
00443 
00444         Tuple3& operator=(const Tuple3& t1) {
00445             set(t1);
00446             return *this;
00447         }
00448         Tuple3& operator+=(const Tuple3& t1) {
00449             add(t1);
00450             return *this;
00451         }
00452         Tuple3& operator-=(const Tuple3& t1) {
00453             sub(t1);
00454             return *this;
00455         }
00456         Tuple3& operator*=(scale_t s) {
00457             scale(s);
00458             return *this;
00459         }
00460         Tuple3 operator+(const Tuple3& t1) const {
00461             return (Tuple3(*this)).operator+=(t1);
00462         }
00463         Tuple3 operator-(const Tuple3& t1) const {
00464             return (Tuple3(*this)).operator-=(t1);
00465         }
00466         Tuple3 operator*(coor_t s) const {
00467             return (Tuple3(*this)).operator*=(s);
00468         }
00469 
00470     };
00471 
00472     se_err::Log& operator<< (se_err::Log& log, const Tuple3& b);
00473 
00474 } // Namespace
00475 
00476 
00477 inline
00478 se_core::Tuple3 operator*(scale_t s, const se_core::Tuple3& t1) {
00479     return (se_core::Tuple3(t1)).operator*=(s);
00480 }
00481 
00482 #endif /* TUPLE3_H */

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:13 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo