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,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXVec4f.h,v 1.32 2006/01/22 17:58:12 fox Exp $                           *
00023 ********************************************************************************/
00024 #ifndef FXVEC4F_H
00025 #define FXVEC4F_H
00026 
00027 
00028 namespace FX {
00029 
00030 
00031 class FXMat4f;
00032 
00033 
00034 /// Single-precision 4-element vector
00035 class FXAPI FXVec4f {
00036 public:
00037   FXfloat x;
00038   FXfloat y;
00039   FXfloat z;
00040   FXfloat w;
00041 public:
00042 
00043   /// Default constructor
00044   FXVec4f(){}
00045 
00046   /// Initialize from another vector
00047   FXVec4f(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;}
00048 
00049   /// Construct with 3-vector and optional scalar
00050   FXVec4f(const FXVec3f& v,FXfloat ww=1.0f){x=v.x;y=v.y;z=v.z;w=ww;}
00051 
00052   /// Construct from array of floats
00053   FXVec4f(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];}
00054 
00055   /// Construct from components
00056   FXVec4f(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww=1.0f){x=xx;y=yy;z=zz;w=ww;}
00057 
00058   /// Construct from color
00059   FXVec4f(FXColor color);
00060 
00061   /// Return a non-const reference to the ith element
00062   FXfloat& operator[](FXint i){return (&x)[i];}
00063 
00064   /// Return a const reference to the ith element
00065   const FXfloat& operator[](FXint i) const {return (&x)[i];}
00066 
00067   /// Assign color
00068   FXVec4f& operator=(FXColor color);
00069 
00070   /// Assignment
00071   FXVec4f& operator=(const FXVec3f& v){x=v.x;y=v.y;z=v.z;w=1.0f;return *this;}
00072   FXVec4f& operator=(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
00073 
00074   /// Assignment from array of floats
00075   FXVec4f& operator=(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
00076 
00077   /// Set value from another vector
00078   FXVec4f& set(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
00079 
00080   /// Set value from array of floats
00081   FXVec4f& set(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
00082 
00083   /// Set value from components
00084   FXVec4f& set(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww){x=xx;y=yy;z=zz;w=ww;return *this;}
00085 
00086   /// Assigning operators
00087   FXVec4f& operator*=(FXfloat n){x*=n;y*=n;z*=n;w*=n;return *this;}
00088   FXVec4f& operator/=(FXfloat n){x/=n;y/=n;z/=n;w/=n;return *this;}
00089   FXVec4f& operator+=(const FXVec4f& v){x+=v.x;y+=v.y;z+=v.z;w+=v.w;return *this;}
00090   FXVec4f& operator-=(const FXVec4f& v){x-=v.x;y-=v.y;z-=v.z;w-=v.w;return *this;}
00091 
00092   /// Conversion
00093   operator FXfloat*(){return &x;}
00094   operator const FXfloat*() const {return &x;}
00095   operator FXVec3f&(){return *reinterpret_cast<FXVec3f*>(this);}
00096   operator const FXVec3f&() const {return *reinterpret_cast<const FXVec3f*>(this);}
00097 
00098   /// Convert to color
00099   operator FXColor() const;
00100 
00101   /// Unary
00102   FXVec4f operator+() const { return *this; }
00103   FXVec4f operator-() const { return FXVec4f(-x,-y,-z,-w); }
00104 
00105   /// Vector and vector
00106   FXVec4f operator+(const FXVec4f& v) const { return FXVec4f(x+v.x,y+v.y,z+v.z,w+v.w); }
00107   FXVec4f operator-(const FXVec4f& v) const { return FXVec4f(x-v.x,y-v.y,z-v.z,w-v.w); }
00108 
00109   /// Vector and matrix
00110   FXVec4f operator*(const FXMat4f& m) const;
00111 
00112   /// Scaling
00113   friend inline FXVec4f operator*(const FXVec4f& a,FXfloat n);
00114   friend inline FXVec4f operator*(FXfloat n,const FXVec4f& a);
00115   friend inline FXVec4f operator/(const FXVec4f& a,FXfloat n);
00116   friend inline FXVec4f operator/(FXfloat n,const FXVec4f& a);
00117 
00118   /// Dot product
00119   FXfloat operator*(const FXVec4f& v) const { return x*v.x+y*v.y+z*v.z+w*v.w; }
00120 
00121   /// Test if zero
00122   bool operator!() const { return x==0.0f && y==0.0f && z==0.0f && w==0.0f; }
00123 
00124   /// Equality tests
00125   bool operator==(const FXVec4f& v) const {return x==v.x && y==v.y && z==v.z && w==v.w; }
00126   bool operator!=(const FXVec4f& v) const {return x!=v.x || y!=v.y || z!=v.z || w!=v.w; }
00127 
00128   friend inline bool operator==(const FXVec4f& a,FXfloat n);
00129   friend inline bool operator!=(const FXVec4f& a,FXfloat n);
00130   friend inline bool operator==(FXfloat n,const FXVec4f& a);
00131   friend inline bool operator!=(FXfloat n,const FXVec4f& a);
00132 
00133   /// Inequality tests
00134   bool operator<(const FXVec4f& v) const { return x<v.x && y<v.y && z<v.z && w<v.w; }
00135   bool operator<=(const FXVec4f& v) const { return x<=v.x && y<=v.y && z<=v.z && w<=v.w; }
00136   bool operator>(const FXVec4f& v) const { return x>v.x && y>v.y && z>v.z && w>v.w; }
00137   bool operator>=(const FXVec4f& v) const { return x>=v.x && y>=v.y && z>=v.z && w>=v.w; }
00138 
00139   friend inline bool operator<(const FXVec4f& a,FXfloat n);
00140   friend inline bool operator<=(const FXVec4f& a,FXfloat n);
00141   friend inline bool operator>(const FXVec4f& a,FXfloat n);
00142   friend inline bool operator>=(const FXVec4f& a,FXfloat n);
00143 
00144   friend inline bool operator<(FXfloat n,const FXVec4f& a);
00145   friend inline bool operator<=(FXfloat n,const FXVec4f& a);
00146   friend inline bool operator>(FXfloat n,const FXVec4f& a);
00147   friend inline bool operator>=(FXfloat n,const FXVec4f& a);
00148 
00149   /// Length and square of length
00150   FXfloat length2() const { return x*x+y*y+z*z+w*w; }
00151   FXfloat length() const { return sqrtf(length2()); }
00152 
00153   /// Clamp values of vector between limits
00154   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;}
00155 
00156   /// Lowest or highest components
00157   friend inline FXVec4f lo(const FXVec4f& a,const FXVec4f& b);
00158   friend inline FXVec4f hi(const FXVec4f& a,const FXVec4f& b);
00159 
00160   /// Compute normalized plane equation ax+by+cz+d=0
00161   friend FXAPI FXVec4f plane(const FXVec4f& vec);
00162   friend FXAPI FXVec4f plane(const FXVec3f& vec,FXfloat dist);
00163   friend FXAPI FXVec4f plane(const FXVec3f& vec,const FXVec3f& p);
00164   friend FXAPI FXVec4f plane(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
00165 
00166   /// Signed distance normalized plane and point
00167   FXfloat distance(const FXVec3f& p) const;
00168 
00169   /// Return true if edge a-b crosses plane
00170   bool crosses(const FXVec3f& a,const FXVec3f& b) const;
00171 
00172   /// Normalize vector
00173   friend FXAPI FXVec4f normalize(const FXVec4f& v);
00174 
00175   /// Save to a stream
00176   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec4f& v);
00177 
00178   /// Load from a stream
00179   friend FXAPI FXStream& operator>>(FXStream& store,FXVec4f& v);
00180   };
00181 
00182 
00183 inline FXVec4f operator*(const FXVec4f& a,FXfloat n){return FXVec4f(a.x*n,a.y*n,a.z*n,a.w*n);}
00184 inline FXVec4f operator*(FXfloat n,const FXVec4f& a){return FXVec4f(n*a.x,n*a.y,n*a.z,n*a.w);}
00185 inline FXVec4f operator/(const FXVec4f& a,FXfloat n){return FXVec4f(a.x/n,a.y/n,a.z/n,a.w/n);}
00186 inline FXVec4f operator/(FXfloat n,const FXVec4f& a){return FXVec4f(n/a.x,n/a.y,n/a.z,n/a.w);}
00187 
00188 inline bool operator==(const FXVec4f& a,FXfloat n){return a.x==n && a.y==n && a.z==n && a.w==n;}
00189 inline bool operator!=(const FXVec4f& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
00190 inline bool operator==(FXfloat n,const FXVec4f& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
00191 inline bool operator!=(FXfloat n,const FXVec4f& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
00192 
00193 inline bool operator<(const FXVec4f& a,FXfloat n){return a.x<n && a.y<n && a.z<n && a.w<n;}
00194 inline bool operator<=(const FXVec4f& a,FXfloat n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
00195 inline bool operator>(const FXVec4f& a,FXfloat n){return a.x>n && a.y>n && a.z>n && a.w>n;}
00196 inline bool operator>=(const FXVec4f& a,FXfloat n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
00197 
00198 inline bool operator<(FXfloat n,const FXVec4f& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
00199 inline bool operator<=(FXfloat n,const FXVec4f& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
00200 inline bool operator>(FXfloat n,const FXVec4f& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
00201 inline bool operator>=(FXfloat n,const FXVec4f& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
00202 
00203 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));}
00204 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));}
00205 
00206 extern FXAPI FXVec4f plane(const FXVec4f& vec);
00207 extern FXAPI FXVec4f plane(const FXVec3f& vec,FXfloat dist);
00208 extern FXAPI FXVec4f plane(const FXVec3f& vec,const FXVec3f& p);
00209 extern FXAPI FXVec4f plane(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
00210 
00211 extern FXAPI FXVec4f normalize(const FXVec4f& v);
00212 
00213 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4f& v);
00214 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4f& v);
00215 
00216 }
00217 
00218 #endif

Copyright © 1997-2005 Jeroen van der Zijp