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

FXVec4f.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *       S i n g l e - P r e c i s i o n   4 - E l e m e n t   V e c t o r       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1994,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXVec4f.h,v 1.39 2009/02/04 18:11:55 fox Exp $                           *
00022 ********************************************************************************/
00023 #ifndef FXVEC4F_H
00024 #define FXVEC4F_H
00025 
00026 
00027 namespace FX {
00028 
00029 
00030 class FXMat4f;
00031 
00032 
00033 /// Single-precision 4-element vector
00034 class FXAPI FXVec4f {
00035 public:
00036   FXfloat x;
00037   FXfloat y;
00038   FXfloat z;
00039   FXfloat w;
00040 public:
00041 
00042   /// Default constructor; value is not initialized
00043   FXVec4f(){}
00044 
00045   /// Initialize from another vector
00046   FXVec4f(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;}
00047 
00048   /// Construct with 3-vector and optional scalar
00049   FXVec4f(const FXVec3f& v,FXfloat ww=1.0f){x=v.x;y=v.y;z=v.z;w=ww;}
00050 
00051   /// Construct from array of floats
00052   FXVec4f(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];}
00053 
00054   /// Construct from components
00055   FXVec4f(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww=1.0f){x=xx;y=yy;z=zz;w=ww;}
00056 
00057   /// Return a non-const reference to the ith element
00058   FXfloat& operator[](FXint i){return (&x)[i];}
00059 
00060   /// Return a const reference to the ith element
00061   const FXfloat& operator[](FXint i) const {return (&x)[i];}
00062 
00063   /// Assignment
00064   FXVec4f& operator=(const FXVec3f& v){x=v.x;y=v.y;z=v.z;w=1.0f;return *this;}
00065   FXVec4f& operator=(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
00066 
00067   /// Assignment from array of floats
00068   FXVec4f& operator=(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
00069 
00070   /// Set value from another vector
00071   FXVec4f& set(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
00072 
00073   /// Set value from array of floats
00074   FXVec4f& set(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
00075 
00076   /// Set value from components
00077   FXVec4f& set(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww){x=xx;y=yy;z=zz;w=ww;return *this;}
00078 
00079   /// Assigning operators
00080   FXVec4f& operator*=(FXfloat n){x*=n;y*=n;z*=n;w*=n;return *this;}
00081   FXVec4f& operator/=(FXfloat n){x/=n;y/=n;z/=n;w/=n;return *this;}
00082   FXVec4f& operator+=(const FXVec4f& v){x+=v.x;y+=v.y;z+=v.z;w+=v.w;return *this;}
00083   FXVec4f& operator-=(const FXVec4f& v){x-=v.x;y-=v.y;z-=v.z;w-=v.w;return *this;}
00084 
00085   /// Conversion
00086   operator FXfloat*(){return &x;}
00087   operator const FXfloat*() const {return &x;}
00088   operator FXVec3f&(){return *reinterpret_cast<FXVec3f*>(this);}
00089   operator const FXVec3f&() const {return *reinterpret_cast<const FXVec3f*>(this);}
00090 
00091   /// Unary
00092   FXVec4f operator+() const { return *this; }
00093   FXVec4f operator-() const { return FXVec4f(-x,-y,-z,-w); }
00094 
00095   /// Vector and vector
00096   FXVec4f operator+(const FXVec4f& v) const { return FXVec4f(x+v.x,y+v.y,z+v.z,w+v.w); }
00097   FXVec4f operator-(const FXVec4f& v) const { return FXVec4f(x-v.x,y-v.y,z-v.z,w-v.w); }
00098 
00099   /// Vector and matrix
00100   FXVec4f operator*(const FXMat4f& m) const;
00101 
00102   /// Scaling
00103   friend inline FXVec4f operator*(const FXVec4f& a,FXfloat n);
00104   friend inline FXVec4f operator*(FXfloat n,const FXVec4f& a);
00105   friend inline FXVec4f operator/(const FXVec4f& a,FXfloat n);
00106   friend inline FXVec4f operator/(FXfloat n,const FXVec4f& a);
00107 
00108   /// Dot product
00109   FXfloat operator*(const FXVec4f& v) const { return x*v.x+y*v.y+z*v.z+w*v.w; }
00110 
00111   /// Test if zero
00112   FXbool operator!() const { return x==0.0f && y==0.0f && z==0.0f && w==0.0f; }
00113 
00114   /// Equality tests
00115   FXbool operator==(const FXVec4f& v) const {return x==v.x && y==v.y && z==v.z && w==v.w; }
00116   FXbool operator!=(const FXVec4f& v) const {return x!=v.x || y!=v.y || z!=v.z || w!=v.w; }
00117 
00118   friend inline FXbool operator==(const FXVec4f& a,FXfloat n);
00119   friend inline FXbool operator!=(const FXVec4f& a,FXfloat n);
00120   friend inline FXbool operator==(FXfloat n,const FXVec4f& a);
00121   friend inline FXbool operator!=(FXfloat n,const FXVec4f& a);
00122 
00123   /// Inequality tests
00124   FXbool operator<(const FXVec4f& v) const { return x<v.x && y<v.y && z<v.z && w<v.w; }
00125   FXbool operator<=(const FXVec4f& v) const { return x<=v.x && y<=v.y && z<=v.z && w<=v.w; }
00126   FXbool operator>(const FXVec4f& v) const { return x>v.x && y>v.y && z>v.z && w>v.w; }
00127   FXbool operator>=(const FXVec4f& v) const { return x>=v.x && y>=v.y && z>=v.z && w>=v.w; }
00128 
00129   friend inline FXbool operator<(const FXVec4f& a,FXfloat n);
00130   friend inline FXbool operator<=(const FXVec4f& a,FXfloat n);
00131   friend inline FXbool operator>(const FXVec4f& a,FXfloat n);
00132   friend inline FXbool operator>=(const FXVec4f& a,FXfloat n);
00133 
00134   friend inline FXbool operator<(FXfloat n,const FXVec4f& a);
00135   friend inline FXbool operator<=(FXfloat n,const FXVec4f& a);
00136   friend inline FXbool operator>(FXfloat n,const FXVec4f& a);
00137   friend inline FXbool operator>=(FXfloat n,const FXVec4f& a);
00138 
00139   /// Length and square of length
00140   FXfloat length2() const { return x*x+y*y+z*z+w*w; }
00141   FXfloat length() const { return sqrtf(length2()); }
00142 
00143   /// Clamp values of vector between limits
00144   FXVec4f& clamp(FXfloat lo,FXfloat hi){x=FXCLAMP(lo,x,hi);y=FXCLAMP(lo,y,hi);z=FXCLAMP(lo,z,hi);w=FXCLAMP(lo,w,hi);return *this;}
00145   
00146   /// Lowest or highest components
00147   friend inline FXVec4f lo(const FXVec4f& a,const FXVec4f& b);
00148   friend inline FXVec4f hi(const FXVec4f& a,const FXVec4f& b);
00149 
00150   /// Compute normalized plane equation ax+by+cz+d=0
00151   friend FXAPI FXVec4f plane(const FXVec4f& vec);
00152   friend FXAPI FXVec4f plane(const FXVec3f& vec,FXfloat dist);
00153   friend FXAPI FXVec4f plane(const FXVec3f& vec,const FXVec3f& p);
00154   friend FXAPI FXVec4f plane(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
00155 
00156   /// Convert vector to color and back
00157   friend FXAPI FXColor colorFromVec4f(const FXVec4f& vec);
00158   friend FXAPI FXVec4f colorToVec4f(FXColor clr);
00159 
00160   /// Signed distance normalized plane and point
00161   FXfloat distance(const FXVec3f& p) const;
00162 
00163   /// Return true if edge a-b crosses plane
00164   FXbool crosses(const FXVec3f& a,const FXVec3f& b) const;
00165 
00166   /// Normalize vector
00167   friend FXAPI FXVec4f normalize(const FXVec4f& v);
00168 
00169   /// Save to a stream
00170   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec4f& v);
00171 
00172   /// Load from a stream
00173   friend FXAPI FXStream& operator>>(FXStream& store,FXVec4f& v);
00174   };
00175 
00176 
00177 inline FXVec4f operator*(const FXVec4f& a,FXfloat n){return FXVec4f(a.x*n,a.y*n,a.z*n,a.w*n);}
00178 inline FXVec4f operator*(FXfloat n,const FXVec4f& a){return FXVec4f(n*a.x,n*a.y,n*a.z,n*a.w);}
00179 inline FXVec4f operator/(const FXVec4f& a,FXfloat n){return FXVec4f(a.x/n,a.y/n,a.z/n,a.w/n);}
00180 inline FXVec4f operator/(FXfloat n,const FXVec4f& a){return FXVec4f(n/a.x,n/a.y,n/a.z,n/a.w);}
00181 
00182 inline FXbool operator==(const FXVec4f& a,FXfloat n){return a.x==n && a.y==n && a.z==n && a.w==n;}
00183 inline FXbool operator!=(const FXVec4f& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
00184 inline FXbool operator==(FXfloat n,const FXVec4f& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
00185 inline FXbool operator!=(FXfloat n,const FXVec4f& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
00186 
00187 inline FXbool operator<(const FXVec4f& a,FXfloat n){return a.x<n && a.y<n && a.z<n && a.w<n;}
00188 inline FXbool operator<=(const FXVec4f& a,FXfloat n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
00189 inline FXbool operator>(const FXVec4f& a,FXfloat n){return a.x>n && a.y>n && a.z>n && a.w>n;}
00190 inline FXbool operator>=(const FXVec4f& a,FXfloat n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
00191 
00192 inline FXbool operator<(FXfloat n,const FXVec4f& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
00193 inline FXbool operator<=(FXfloat n,const FXVec4f& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
00194 inline FXbool operator>(FXfloat n,const FXVec4f& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
00195 inline FXbool operator>=(FXfloat n,const FXVec4f& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
00196 
00197 inline FXVec4f lo(const FXVec4f& a,const FXVec4f& b){return FXVec4f(FXMIN(a.x,b.x),FXMIN(a.y,b.y),FXMIN(a.z,b.z),FXMIN(a.w,b.w));}
00198 inline FXVec4f hi(const FXVec4f& a,const FXVec4f& b){return FXVec4f(FXMAX(a.x,b.x),FXMAX(a.y,b.y),FXMAX(a.z,b.z),FXMAX(a.w,b.w));}
00199 
00200 extern FXAPI FXVec4f plane(const FXVec4f& vec);
00201 extern FXAPI FXVec4f plane(const FXVec3f& vec,FXfloat dist);
00202 extern FXAPI FXVec4f plane(const FXVec3f& vec,const FXVec3f& p);
00203 extern FXAPI FXVec4f plane(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
00204 
00205 extern FXAPI FXColor colorFromVec4f(const FXVec4f& vec);
00206 extern FXAPI FXVec4f colorToVec4f(FXColor clr);
00207 
00208 extern FXAPI FXVec4f normalize(const FXVec4f& v);
00209 
00210 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4f& v);
00211 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4f& v);
00212 
00213 }
00214 
00215 #endif

Copyright © 1997-2009 Jeroen van der Zijp