_Tuple2.h

Go to the documentation of this file.
00001 #ifndef __VECMATH_TUPLE2_HPP
00002 #define __VECMATH_TUPLE2_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 //#define DEBUGTUPLE2(d) d
00014 #define DEBUGTUPLE2(d)
00015 
00016 
00017 // general template for Tuple2
00018 // (used by Tuple2f, Tuple2d, Vector2f, Vector2d, Point2d, Point2f)
00019 //
00020 // for standard functions see documentation for Java3D
00021 //
00022 // all standard overloaded operation are also support
00023 //
00024 
00025 // predeclare _Tuple2
00026 template <class Type> class _Tuple2;
00027 
00028 // now use predeclared _Tuple2 to predeclare friend operator<<
00029 template <class Type> _Tuple2<Type> operator *(Type c, const _Tuple2<Type>& t);
00030 //template <class Type> ostream& operator <<(ostream& os, const _Tuple2<Type>& t);
00031 //template <class Type> ostream& operator <<(ostream& os, const _Tuple2<Type>* t);
00032 
00033 template <class Type> class VECMATH_EXPORT _Tuple2 {
00034 public:
00035     Type x, y;
00036 
00037     _Tuple2<Type>() {}
00038 
00039     explicit _Tuple2<Type>(Type x, Type y = 0) : x(x), y(y) {
00040         DEBUGTUPLE2(std::cout << "_Tuple2 explicit constructor x,y " << *this << endl);
00041     }
00042 
00043     _Tuple2<Type>(const _Tuple2<byte>& t) {
00044         set(t);
00045         DEBUGTUPLE2(std::cout << "_Tuple2 constructor &byte " << *this << endl);
00046     }
00047 
00048     _Tuple2<Type>(const _Tuple2<double>& t) {
00049         set(t);
00050         DEBUGTUPLE2(std::cout << "_Tuple2 constructor &double " << *this << endl);
00051     }
00052 
00053     _Tuple2<Type>(const _Tuple2<float>& t) {
00054         set(t);
00055         DEBUGTUPLE2(std::cout << "_Tuple2 constructor &float " << *this << endl);
00056     }
00057 
00058     _Tuple2<Type>(const Type t[2]) {
00059         set(t);
00060         DEBUGTUPLE2(cout << "_Tuple2 constructor t[2] " << *this << endl);
00061     }
00062 
00063     ~_Tuple2<Type>() {
00064         DEBUGTUPLE2(cout << "_Tuple2 destructor " << *this << endl);
00065     }
00066 
00067     void add(const _Tuple2<Type>& t) {
00068         x += t.x;
00069         y += t.y;
00070     }
00071 
00072     void add(const _Tuple2<Type>& t, const _Tuple2<Type>& t1) {
00073         x = t.x + t1.x;
00074         y = t.y + t1.y;
00075     }
00076 
00077     void absolute() {
00078         absolute(*this);
00079     }
00080 
00081     void absolute(const _Tuple2<Type>& t) {
00082         DEBUGTUPLE2(cout << "_Tuple2 absolute(t) " << *this << " = ");
00083         set(Math::abs(t.x), Math::abs(t.y));
00084         DEBUGTUPLE2(cout << *this << endl);
00085     }
00086 
00087     void clamp(Type min, Type max) {
00088         DEBUGTUPLE2(cout << "_Tuple2 clamp(" << min << ", " << max << ") = " << *this << endl);
00089         clamp(min, max, *this);
00090     }
00091 
00092     void clampMin(Type min) {
00093         DEBUGTUPLE2(cout << "_Tuple2 clampMin(" << min << ") = " << *this << endl);
00094         clampMin(min, *this);
00095     }
00096 
00097     void clampMax(Type max) {
00098         DEBUGTUPLE2(cout << "_Tuple2 clampMax(" << max << ") = " << *this << endl);
00099         clampMax(max, *this);
00100     }
00101 
00102     void clamp(Type min, Type max, const _Tuple2<Type> &t);
00103     void clampMin(Type min, const _Tuple2<Type> &t);
00104     void clampMax(Type min, const _Tuple2<Type> &t);
00105 
00106     bool equals(const _Tuple2<Type> &t) const {
00107         DEBUGTUPLE2(cout << "_Tuple2 epsilon() " << *this << endl);
00108         return (x == t.x && y == t.y);
00109     }
00110 
00111     bool epsilonEquals(const _Tuple2<Type> &t, Type epsilon) const {
00112         DEBUGTUPLE2(cout << "_Tuple2 epsilonEquals() " << *this << endl);
00113         // FIX - use MATH
00114         return (Math::epsilonEquals(x, t.x, epsilon)
00115                 && Math::epsilonEquals(y, t.y, epsilon));
00116     }
00117 
00118     void get(byte t[3]) const {
00119         t[0] = (byte) x;
00120         t[1] = (byte) y;
00121     }
00122 
00123     void get(double t[3]) const {
00124         t[0] = x;
00125         t[1] = y;
00126     }
00127 
00128     void get(float t[3]) const {
00129         t[0] = x;
00130         t[1] = y;
00131     }
00132 
00133     void get(_Tuple2<Type> &t) const {
00134         t.x = x;
00135         t.y = y;
00136     }
00137 
00138     void interpolate(_Tuple2<Type> &t, double alpha) {
00139         interpolate(*this, t, alpha);
00140     }
00141 
00142     void interpolate(_Tuple2<Type> &t1, const _Tuple2<Type> &t2, double alpha);
00143 
00144     void negate() {
00145         negate(*this);
00146     }
00147 
00148     void negate(const _Tuple2<Type> &t) {
00149         set(-t.x, -t.y);
00150     }
00151 
00152     // extension
00153     Type normSquared() const {
00154         return (Type) (Math::sqr(x) + Math::sqr(y));
00155     }
00156 
00157     // extension
00158     Type norm() {
00159         return (Type) sqrt(normSquared());
00160     }
00161 
00162     void scale(Type s) {
00163         x *= s;
00164         y *= s;
00165     }
00166 
00167     void scaleAdd(Type s, const _Tuple2<Type> &t) {
00168         scaleAdd(s, *this, t);
00169     }
00170 
00171     void scaleAdd(Type s, const _Tuple2<Type> &t1, const _Tuple2<Type> &t2) {
00172         x = s * t1.x + t2.x;
00173         y = s * t1.y + t2.y;
00174     }
00175 
00176     void set(Type xx = 0, Type yy = 0) {
00177         x = xx;
00178         y = yy;
00179     }
00180 
00181     void set(const _Tuple2<byte>& t) {
00182         set((Type)t.x, (Type)t.y);
00183     }
00184 
00185     void set(const _Tuple2<double>& t) {
00186         set((Type)t.x, (Type)t.y);
00187     }
00188 
00189     void set(const _Tuple2<float>& t) {
00190         set((Type)t.x, (Type)t.y);
00191     }
00192 
00193     void set(const _Tuple2<int>& t) {
00194         set((Type)t.x, (Type)t.y);
00195     }
00196 
00197     void sub(const _Tuple2<Type>& t) {
00198         sub(*this, t);
00199     }
00200 
00201     void sub(const _Tuple2<Type>& t1, const _Tuple2<Type>& t2) {
00202         x = t1.x - t2.x;
00203         y = t1.y - t2.y;
00204     }
00205 
00206 #if 0   
00207     String &toString(String &s);
00208 #endif
00209     //friend ostream& operator <<(ostream& os, const _Tuple2<Type>& t);
00210 
00211 // non Java3D extensions
00212 
00213     // assignment
00214 
00215 //      _Tuple2<Type>& operator =(const _Tuple2<Type>& v) {
00216 //      cout << "_Tuple2 operator = _Tuple2" << endl;
00217 //      x = v.x; y = v.y; z = v.z;
00218 //      return *this;
00219 //      }
00220 
00221     // math operators
00222 
00223     _Tuple2<Type>& operator +=(const _Tuple2<Type>& t) {
00224         add(t);
00225         DEBUGTUPLE2(cout << "_Tuple2 operator+= _Tuple2 " << *this  << endl);
00226         return *this;
00227     }
00228 
00229     _Tuple2<Type>& operator -=(const _Tuple2<Type>& t) {
00230         sub(t);
00231         DEBUGTUPLE2(cout << "_Tuple2 operator-= _Tuple2 " << *this  << endl);
00232         return *this;
00233     }
00234 
00235     _Tuple2<Type>& operator *=(Type c) {
00236         scale(c);
00237         DEBUGTUPLE2(cout << "_Tuple2 operator*= _Tuple2 " << *this  << endl);
00238         return *this;
00239     }
00240 
00241     _Tuple2<Type>& operator /=(Type c) {
00242         // FIXME assert zero test
00243         x /= c;
00244         y /= c;
00245         DEBUGTUPLE2(cout << "_Tuple2 operator/= _Tuple2 " << *this  << endl);
00246         return *this;
00247     }
00248 
00249     _Tuple2<Type> operator +(const _Tuple2<Type>& t) const {
00250         DEBUGTUPLE2(cout << "_Tuple2 operator+ _Tuple2 " << *this << endl);
00251         return _Tuple2<Type>(*this) += t;
00252     }
00253 
00254     _Tuple2<Type> operator -(const _Tuple2<Type>& t) const {
00255         DEBUGTUPLE2(cout << "_Tuple2 operator- _Tuple2 " << *this << endl);
00256         return _Tuple2<Type>(*this) -= t;
00257     }
00258 
00259     _Tuple2<Type> operator *(Type c) const {
00260         DEBUGTUPLE2(cout << "_Tuple2 operator* " << *this << " * " << c << endl);
00261         return _Tuple2<Type>(*this) *= c;
00262     }
00263 
00264     _Tuple2<Type> operator /(Type c) const {
00265         DEBUGTUPLE2(cout << "_Tuple2 operator/ " << *this << " / " << c <<  endl);
00266         return _Tuple2<Type>(*this) /= c;
00267     }
00268 
00269     bool operator ==(const _Tuple2<Type>& t) const {
00270         DEBUGTUPLE2(cout << "_Tuple2 operator== _Tuple2 " << *this << endl);
00271         return (epsilonEquals(t, (Type) Math::EPSILON));
00272     }
00273 
00274     bool operator !=(const _Tuple2<Type>& t) const {
00275         DEBUGTUPLE2(cout << "_Tuple2 operator!= _Tuple2 " << *this << endl);
00276         return (!(*this == t));
00277     }
00278 
00279     Type& operator [](int i) const {
00280         return (Type&) (operator const Type *())[i];
00281     }
00282 
00283     operator const Type * () const {
00284         return &x;
00285     }
00286 
00287 //    friend ostream & operator << <>( ostream &, const _Tuple2<Type> & );
00288 //    friend ostream & operator << <>( ostream &, const _Tuple2<Type> * );
00289 };
00290 
00291 #endif

Generated on Thu Sep 29 13:39:44 2005 for vecmath by  doxygen 1.4.4