Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Vector.h

Go to the documentation of this file.
00001 
00048 #ifndef _AMIRE_COMMON_VECTOR_H
00049 #define _AMIRE_COMMON_VECTOR_H
00050 
00051 #include <iostream>
00052 #include <cmath>
00053 
00054 #include <amire/common/Defines.h>
00055 #include <amire/common/Real.h>
00056 
00057 namespace amire {
00058   namespace common {
00062     template <class type> class TVector2D : public virtual osg::Referenced {
00063     public:
00067       type x;
00068 
00072       type y;
00073 
00077       inline TVector2D() : Referenced(), x(type(0.0)), y(type(0.0)) {
00078       }
00079 
00083       inline TVector2D(const double *v) : Referenced(), x((type)v[0]), y((type)v[1]) {
00084       }
00085 
00089       inline TVector2D(const float *v) : Referenced(), x((type)v[0]), y((type)v[1]) {
00090       }
00091 
00095       inline TVector2D(type px, type py, type pz) : Referenced(), x(px), y(py) {
00096       }
00097 
00101       inline TVector2D(const TVector2D<double> &v) : Referenced(), x((type)v.x), y((type)v.y) {
00102       }
00103 
00107       inline TVector2D(const TVector2D<float> &v) : Referenced(), x((type)v.x), y((type)v.y) {
00108       }
00109 
00113       inline TVector2D<type> &operator=(const TVector2D<float> &v) {
00114         x = (type)v.x;
00115         y = (type)v.y;
00116         return *this;
00117       }
00118 
00122       inline TVector2D<type> &operator=(const TVector2D<double> &v) {
00123         x = (type)v.x;
00124         y = (type)v.y;
00125         return *this;
00126       }
00127 
00131       inline type operator[](int index) {
00132         switch (index) {
00133           case 0:
00134             return x;
00135           case 1:
00136             return y;
00137         }
00138         INTERNAL_ERROR;
00139       }
00140 
00144       inline const type *data() const {
00145         return &x;
00146       }
00147 
00151       inline std::ostream& print(std::ostream& out) const {
00152         return out << '[' << x << ", " << y << ']';
00153       }
00154 
00158       friend inline std::ostream &operator<<(std::ostream &out, const TVector2D<type> &v) {
00159         return v.print(out);
00160       }
00161 
00165       inline type length() const {
00166         type sl = squareLength();
00167         return (sl == (type)1.0) ?  ((type)1.0) : ((type)sqrt(sl));
00168       }
00169 
00173       inline type squareLength() const {
00174         return (type)(x * x + y * y);
00175       }
00176 
00180       inline TVector2D<type> &scale(type value) {
00181         x *= value;
00182         y *= value;
00183         return *this;
00184       }
00185 
00189       inline TVector2D<type> &normalize() {
00190         type l = squareLength();
00191         if (l != 1.0) {
00192           scale(((type)1.0) / (type)sqrt(l));
00193         }
00194         return *this;
00195       }
00196 
00200       inline TVector2D<type> &set(double *v) {
00201         this->x = (type)v[0];
00202         this->y = (type)v[1];
00203         return *this;
00204       }
00205 
00209       inline TVector2D<type> &set(float *v) {
00210         this->x = (type)v[0];
00211         this->y = (type)v[1];
00212         return *this;
00213       }
00214 
00218       inline TVector2D<type> &set(type x, type y) {
00219         this->x = x;
00220         this->y = y;
00221         return *this;
00222       }
00223 
00227       inline TVector2D<type> &set(const TVector2D<type> &v) {
00228         x = v.x;
00229         y = v.y;
00230         return *this;
00231       }
00232 
00236       inline type dot(const TVector2D<type> &v) const {
00237         return x * v.x + y * v.y;
00238       }
00239 
00243       inline TVector2D<type> &mult(const TVector2D<type> &v) {
00244         x *= v.x;
00245         y *= v.y;
00246         return *this;
00247       }
00248 
00252       inline TVector2D<type> &mult(const TVector2D<type> &first, const TVector2D<type> &second) {
00253         x = first.x * second.x;
00254         y = first.y * second.y;
00255         return *this;
00256       }
00257 
00261       inline type cross(const TVector2D<type> &v) {
00262         return x * v.y - v.x * y;
00263       }
00264 
00268       inline TVector2D<type> &add(const TVector2D<type> &v) {
00269         x += v.x;
00270         y += v.y;
00271         z += v.z;
00272         return *this;
00273       }
00274 
00278       inline TVector2D<type> &add(const TVector2D<type> &first, const TVector2D<type> &second) {
00279         x = first.x + second.x;
00280         y = first.y + second.y;
00281         return *this;
00282       }
00283 
00287       inline TVector2D<type> &sub(const TVector2D<type> &v) {
00288         x -= v.x;
00289         y -= v.y;
00290         return *this;
00291       }
00292 
00296       inline TVector2D<type> &sub(const TVector2D<type> &first, const TVector2D<type> &second) {
00297         x = first.x - second.x;
00298         y = first.y - second.y;
00299         return *this;
00300       }
00301 
00305       inline type dot(type x, type y) const {
00306         return this->x * x + this->y * y;
00307       }
00308 
00312       inline TVector2D<type> &mult(type x, type y) {
00313         this->x *= x;
00314         this->y *= y;
00315         return *this;
00316       }
00317 
00321       inline TVector2D<type> &add(type x, type y) {
00322         this->x += x;
00323         this->y += y;
00324         return *this;
00325       }
00326 
00330       inline TVector2D<type> &sub(type x, type y) {
00331         this->x -= x;
00332         this->y -= y;
00333         return *this;
00334       }
00335     };
00336 
00340     template <class type> class TVector3D : public virtual osg::Referenced {
00341     public:
00345       type x;
00346 
00350       type y;
00351 
00355       type z;
00356 
00360       inline TVector3D() : Referenced(), x(type(0.0)), y(type(0.0)), z(type(0.0)) {
00361       }
00362 
00366       inline TVector3D(const double *v) : Referenced(), x((type)v[0]), y((type)v[1]), z((type)v[2]) {
00367       }
00368 
00372       inline TVector3D(const float *v) : Referenced(), x((type)v[0]), y((type)v[1]), z((type)v[2]) {
00373       }
00374 
00378       inline TVector3D(type px, type py, type pz) : Referenced(), x(px), y(py), z(pz) {
00379       }
00380 
00384       inline TVector3D(const TVector3D<double> &v) : Referenced(), x((type)v.x), y((type)v.y), z((type)v.z) {
00385       }
00386 
00390       inline TVector3D(const TVector3D<float> &v) : Referenced(), x((type)v.x), y((type)v.y), z((type)v.z) {
00391       }
00392 
00396       inline TVector3D<type> &operator=(const TVector3D<float> &v) {
00397         x = (type)v.x;
00398         y = (type)v.y;
00399         z = (type)v.z;
00400         return *this;
00401       }
00402 
00406       inline TVector3D<type> &operator=(const TVector3D<double> &v) {
00407         x = (type)v.x;
00408         y = (type)v.y;
00409         z = (type)v.z;
00410         return *this;
00411       }
00412 
00416       inline type operator[](int index) {
00417         switch (index) {
00418           case 0:
00419             return x;
00420           case 1:
00421             return y;
00422           case 2:
00423             return z;
00424         }
00425         INTERNAL_ERROR;
00426       }
00427 
00431       inline const type *data() const {
00432         return &x;
00433       }
00434 
00438       inline std::ostream& print(std::ostream& out) const {
00439         return out << '[' << x << ", " << y << ", " << z <<']';
00440       }
00441 
00445       friend inline std::ostream &operator<<(std::ostream &out, const TVector3D<type> &v) {
00446         return v.print(out);
00447       }
00448 
00452       inline type length() const {
00453         type sl = squareLength();
00454         return (sl == (type)1.0) ?  ((type)1.0) : ((type)sqrt(sl));
00455       }
00456 
00460       inline type squareLength() const {
00461         return (type)(x * x + y * y + z * z);
00462       }
00463 
00467       inline TVector3D<type> &scale(type value) {
00468         x *= value;
00469         y *= value;
00470         z *= value;
00471         return *this;
00472       }
00473 
00477       inline TVector3D<type> &normalize() {
00478         type l = squareLength();
00479         if (l != 1.0) {
00480           scale(((type)1.0) / (type)sqrt(l));
00481         }
00482         return *this;
00483       }
00484 
00488       inline TVector3D<type> &set(double *v) {
00489         this->x = (type)v[0];
00490         this->y = (type)v[1];
00491         this->z = (type)v[2];
00492         return *this;
00493       }
00494 
00498       inline TVector3D<type> &set(float *v) {
00499         this->x = (type)v[0];
00500         this->y = (type)v[1];
00501         this->z = (type)v[2];
00502         return *this;
00503       }
00504 
00508       inline TVector3D<type> &set(type x, type y, type z) {
00509         this->x = x;
00510         this->y = y;
00511         this->z = z;
00512         return *this;
00513       }
00514 
00518       inline TVector3D<type> &set(const TVector3D<type> &v) {
00519         x = v.x;
00520         y = v.y;
00521         z = v.z;
00522         return *this;
00523       }
00524 
00528       inline type dot(const TVector3D<type> &v) const {
00529         return x * v.x + y * v.y + z * v.z;
00530       }
00531 
00532       inline TVector3D<type> &mult(const TVector3D<type> &v) {
00533         x *= v.x;
00534         y *= v.y;
00535         z *= v.z;
00536         return *this;
00537       }
00538 
00542       inline TVector3D<type> &mult(const TVector3D<type> &first, const TVector3D<type> &second) {
00543         x = first.x * second.x;
00544         y = first.y * second.y;
00545         z = first.z * second.z;
00546         return *this;
00547       }
00548 
00552       inline TVector3D<type> &cross(const TVector3D<type> &v) {
00553         type tx = y * v.z - v.y * z;
00554         type ty = z * v.x - v.z * x;
00555         z = x * v.y - v.x * y;
00556         y = ty;
00557         x = tx;
00558         return *this;
00559       }
00560 
00564       inline TVector3D<type> &cross(const TVector3D<type> &first, const TVector3D<type> &second) {
00565         x = first.y * second.z - second.y * first.z;
00566         y = first.z * second.x - second.z * first.x;
00567         z = first.x * second.y - second.x * first.y;
00568         return *this;
00569       }
00570 
00574       inline TVector3D<type> &add(const TVector3D<type> &v) {
00575         x += v.x;
00576         y += v.y;
00577         z += v.z;
00578         return *this;
00579       }
00580 
00584       inline TVector3D<type> &add(const TVector3D<type> &first, const TVector3D<type> &second) {
00585         x = first.x + second.x;
00586         y = first.y + second.y;
00587         z = first.z + second.z;
00588         return *this;
00589       }
00590 
00594       inline TVector3D<type> &sub(const TVector3D<type> &v) {
00595         x -= v.x;
00596         y -= v.y;
00597         z -= v.z;
00598         return *this;
00599       }
00600 
00604       inline TVector3D<type> &sub(const TVector3D<type> &first, const TVector3D<type> &second) {
00605         x = first.x - second.x;
00606         y = first.y - second.y;
00607         z = first.z - second.z;
00608         return *this;
00609       }
00610 
00614       inline type dot(type x, type y, type z) const {
00615         return this->x * x + this->y * y + this->z * z;
00616       }
00617 
00621       inline TVector3D<type> &mult(type x, type y, type z) {
00622         this->x *= x;
00623         this->y *= y;
00624         this->z *= z;
00625         return *this;
00626       }
00627 
00631       inline TVector3D<type> &add(type x, type y, type z) {
00632         this->x += x;
00633         this->y += y;
00634         this->z += z;
00635         return *this;
00636       }
00637 
00641       inline TVector3D<type> &sub(type x, type y, type z) {
00642         this->x -= x;
00643         this->y -= y;
00644         this->z -= z;
00645         return *this;
00646       }
00647     };
00648 
00652     template <class type> class TVector4D : public virtual osg::Referenced {
00653     public:
00657       type x;
00658 
00662       type y;
00663 
00667       type z;
00668 
00672       type w;
00673 
00677       inline TVector4D() : Referenced(), x(type(0.0)), y(type(0.0)), z(type(0.0)), w(type(0.0)) {
00678       }
00679 
00683       inline TVector4D(const double *v) : Referenced(), x((type)v[0]), y((type)v[1]), z((type)v[2]), w((type)v[3]) {
00684       }
00685 
00689       inline TVector4D(const float *v) : Referenced(), x((type)v[0]), y((type)v[1]), z((type)v[2]), w((type)v[3]) {
00690       }
00691 
00695       inline TVector4D(type px, type py, type pz, type pw) : Referenced(), x(px), y(py), z(pz), w(pw) {
00696       }
00697 
00701       inline TVector4D(const TVector4D<double> &v) : Referenced(), x((type)v.x), y((type)v.y), z((type)v.z), w((type)v.w) {
00702       }
00703 
00707       inline TVector4D(const TVector4D<float> &v) : Referenced(), x((type)v.x), y((type)v.y), z((type)v.z), w((type)v.w) {
00708       }
00709 
00713       inline TVector4D<type> &operator=(const TVector4D<float> &v) {
00714         x = (type)v.x;
00715         y = (type)v.y;
00716         z = (type)v.z;
00717         w = (type)v.w;
00718         return *this;
00719       }
00720 
00724       inline TVector4D<type> &operator=(const TVector4D<double> &v) {
00725         x = (type)v.x;
00726         y = (type)v.y;
00727         z = (type)v.z;
00728         w = (type)v.w;
00729         return *this;
00730       }
00731 
00735       inline type operator[](int index) {
00736         switch (index) {
00737           case 0:
00738             return x;
00739           case 1:
00740             return y;
00741           case 2:
00742             return z;
00743           case 3:
00744             return w;
00745         }
00746         INTERNAL_ERROR;
00747       }
00748 
00752       inline const type *data() const {
00753         return &x;
00754       }
00755 
00759       inline std::ostream& print(std::ostream& out) const {
00760         return out << '[' << x << ", " << y << ", " << z << ", " << w << ']';
00761       }
00762 
00766       friend inline std::ostream &operator<<(std::ostream &out, const TVector3D<type> &v) {
00767         return v.print(out);
00768       }
00769 
00773       inline type length() const {
00774         type sl = squareLength();
00775         return (sl == (type)1.0) ?  ((type)1.0) : ((type)sqrt(sl));
00776       }
00777 
00781       inline type squareLength() const {
00782         return (type)(x * x + y * y + z * z + w * w);
00783       }
00784 
00788       inline TVector4D<type> &scale(type value) {
00789         x *= value;
00790         y *= value;
00791         z *= value;
00792         w *= value;
00793         return *this;
00794       }
00795 
00799       inline TVector4D<type> &normalize() {
00800         type l = squareLength();
00801         if (l != 1.0) {
00802           scale(((type)1.0) / (type)sqrt(l));
00803         }
00804         return *this;
00805       }
00806 
00810       inline TVector4D<type> &set(double *v) {
00811         this->x = (type)v[0];
00812         this->y = (type)v[1];
00813         this->z = (type)v[2];
00814         this->w = (type)v[3];
00815         return *this;
00816       }
00817 
00821       inline TVector4D<type> &set(float *v) {
00822         this->x = (type)v[0];
00823         this->y = (type)v[1];
00824         this->z = (type)v[2];
00825         this->w = (type)v[3];
00826         return *this;
00827       }
00828 
00832       inline TVector4D<type> &set(type x, type y, type z, type w) {
00833         this->x = x;
00834         this->y = y;
00835         this->z = z;
00836         this->w = w;
00837         return *this;
00838       }
00839 
00843       inline TVector4D<type> &set(const TVector4D<type> &v) {
00844         x = v.x;
00845         y = v.y;
00846         z = v.z;
00847         w = v.w;
00848         return *this;
00849       }
00850 
00854       inline type dot(const TVector4D<type> &v) const {
00855         return x * v.x + y * v.y + z * v.z + w * v.w;
00856       }
00857 
00861       inline TVector4D<type> &mult(const TVector4D<type> &v) {
00862         x *= v.x;
00863         y *= v.y;
00864         z *= v.z;
00865         w *= v.w;
00866         return *this;
00867       }
00868 
00872       inline TVector4D<type> &mult(const TVector4D<type> &first, const TVector4D<type> &second) {
00873         x = first.x * second.x;
00874         y = first.y * second.y;
00875         z = first.z * second.z;
00876         w = first.w * second.w;
00877         return *this;
00878       }
00879 
00883       inline TVector4D<type> &cross(const TVector4D<type> &second, const TVector4D<type> &third) {
00884         TVector4D<type> first(this);
00885         cross(first, second, third);
00886         return *this;
00887       }
00888 
00892       inline TVector4D<type> &cross(const TVector4D<type> &first, const TVector4D<type> &second, const TVector4D<type> &third) {
00893         x = -third.w * second.y * first.z + second.w * third.y * first.z + third.w * first.y second.z - first.w * third.y * second.z - second.w * first.y * third.z + first.w * second.y * third.z; 
00894         y = third.w * second.x * first.z - second.w * third.x * first.z - third.w * first.x second.z + first.w * third.x * second.z + second.w * first.x * third.z - first.w * second.x * third.z;
00895         z = -third.w * second.x * first.y + second.w * third.x * first.y + third.w * first.x second.y - first.w * third.x * second.y - second.w * first.x * third.y + first.w * second.x * third.y;
00896         w = third.x * second.y * first.z - second.x * third.y * first.z - third.x * first.y second.z + first.x * third.y * second.z + second.x * first.y * third.z - first.x * second.y * third.z;
00897         return *this;
00898       }
00899 
00903       inline TVector4D<type> &add(const TVector4D<type> &v) {
00904         x += v.x;
00905         y += v.y;
00906         z += v.z;
00907         w += v.w;
00908         return *this;
00909       }
00910 
00914       inline TVector4D<type> &add(const TVector4D<type> &first, const TVector4D<type> &second) {
00915         x = first.x + second.x;
00916         y = first.y + second.y;
00917         z = first.z + second.z;
00918         w = first.z + second.w;
00919         return *this;
00920       }
00921 
00925       inline TVector4D<type> &sub(const TVector4D<type> &v) {
00926         x -= v.x;
00927         y -= v.y;
00928         z -= v.z;
00929         w -= v.w;
00930         return *this;
00931       }
00932 
00936       inline TVector4D<type> &sub(const TVector4D<type> &first, const TVector4D<type> &second) {
00937         x = first.x - second.x;
00938         y = first.y - second.y;
00939         z = first.z - second.z;
00940         w = first.w - second.w;
00941         return *this;
00942       }
00943 
00947       inline type dot(type x, type y, type z, type w) const {
00948         return this->x * x + this->y * y + this->z * z + this->w * w;
00949       }
00950 
00954       inline TVector4D<type> &mult(type x, type y, type z, type w) {
00955         this->x *= x;
00956         this->y *= y;
00957         this->z *= z;
00958         this->w *= w;
00959         return *this;
00960       }
00961 
00965       inline TVector4D<type> &add(type x, type y, type z, type w) {
00966         this->x += x;
00967         this->y += y;
00968         this->z += z;
00969         this->w += w;
00970         return *this;
00971       }
00972 
00976       inline TVector4D<type> &sub(type x, type y, type z, type w) {
00977         this->x -= x;
00978         this->y -= y;
00979         this->z -= z;
00980         this->w -= w;
00981         return *this;
00982       }
00983     };
00984 
00988     typedef TVector2D<Real> Position2D;
00989 
00993     typedef TVector3D<Real> Position3D;
00994 
00998     typedef TVector3D<Real> Position;
00999 
01003     typedef TVector2D<Real> Dimension2D;
01004 
01008     typedef TVector3D<Real> Dimension3D;
01009 
01013     typedef TVector3D<Real> Dimension;
01014 
01018     typedef TVector2D<Real> Vector2D;
01019 
01023     typedef TVector3D<Real> Vector3D;
01024 
01028     typedef TVector3D<Real> Vector;
01029 
01033     typedef TVector2D<Real> Point2D;
01034 
01038     typedef TVector3D<Real> Point3D;
01042     typedef TVector3D<Real> Point;
01043   }
01044 }
01045 
01046 #endif

Generated on Tue May 20 17:23:54 2003 for AMIRE-Framework by doxygen1.3