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

FXVec3f.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   3 - 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: FXVec3f.h,v 1.33 2009/02/04 18:11:55 fox Exp $                           *
00022 ********************************************************************************/
00023 #ifndef FXVEC3F_H
00024 #define FXVEC3F_H
00025 
00026 
00027 namespace FX {
00028 
00029 
00030 class FXMat3f;
00031 class FXMat4f;
00032 
00033 
00034 /// Single-precision 3-element vector
00035 class FXAPI FXVec3f {
00036 public:
00037   FXfloat x;
00038   FXfloat y;
00039   FXfloat z;
00040 public:
00041 
00042   /// Default constructor; value is not initialized
00043   FXVec3f(){}
00044 
00045   /// Initialize from another vector
00046   FXVec3f(const FXVec3f& v){x=v.x;y=v.y;z=v.z;}
00047 
00048   /// Initialize from array of floats
00049   FXVec3f(const FXfloat v[]){x=v[0];y=v[1];z=v[2];}
00050 
00051   /// Initialize from components
00052   FXVec3f(FXfloat xx,FXfloat yy,FXfloat zz=1.0f){x=xx;y=yy;z=zz;}
00053 
00054   /// Return a non-const reference to the ith element
00055   FXfloat& operator[](FXint i){return (&x)[i];}
00056 
00057   /// Return a const reference to the ith element
00058   const FXfloat& operator[](FXint i) const {return (&x)[i];}
00059 
00060   /// Assignment
00061   FXVec3f& operator=(const FXVec3f& v){x=v.x;y=v.y;z=v.z;return *this;}
00062 
00063   /// Assignment from array of floats
00064   FXVec3f& operator=(const FXfloat v[]){x=v[0];y=v[1];z=v[2];return *this;}
00065 
00066   /// Set value from another vector
00067   FXVec3f& set(const FXVec3f& v){x=v.x;y=v.y;z=v.z;return *this;}
00068 
00069   /// Set value from array of floats
00070   FXVec3f& set(const FXfloat v[]){x=v[0];y=v[1];z=v[2];return *this;}
00071 
00072   /// Set value from components
00073   FXVec3f& set(FXfloat xx,FXfloat yy,FXfloat zz){x=xx;y=yy;z=zz;return *this;}
00074 
00075   /// Assigning operators
00076   FXVec3f& operator*=(FXfloat n){x*=n;y*=n;z*=n;return *this;}
00077   FXVec3f& operator/=(FXfloat n){x/=n;y/=n;z/=n;return *this;}
00078   FXVec3f& operator+=(const FXVec3f& v){x+=v.x;y+=v.y;z+=v.z;return *this;}
00079   FXVec3f& operator-=(const FXVec3f& v){x-=v.x;y-=v.y;z-=v.z;return *this;}
00080 
00081   /// Conversions
00082   operator FXfloat*(){return &x;}
00083   operator const FXfloat*() const {return &x;}
00084   operator FXVec2f&(){return *reinterpret_cast<FXVec2f*>(this);}
00085   operator const FXVec2f&() const {return *reinterpret_cast<const FXVec2f*>(this);}
00086 
00087   /// Unary
00088   FXVec3f operator+() const { return *this; }
00089   FXVec3f operator-() const { return FXVec3f(-x,-y,-z); }
00090 
00091   /// Vector and vector
00092   FXVec3f operator+(const FXVec3f& v) const { return FXVec3f(x+v.x,y+v.y,z+v.z); }
00093   FXVec3f operator-(const FXVec3f& v) const { return FXVec3f(x-v.x,y-v.y,z-v.z); }
00094 
00095   /// Vector and matrix
00096   FXVec3f operator*(const FXMat3f& m) const;
00097   FXVec3f operator*(const FXMat4f& m) const;
00098 
00099   /// Scaling
00100   friend inline FXVec3f operator*(const FXVec3f& a,FXfloat n);
00101   friend inline FXVec3f operator*(FXfloat n,const FXVec3f& a);
00102   friend inline FXVec3f operator/(const FXVec3f& a,FXfloat n);
00103   friend inline FXVec3f operator/(FXfloat n,const FXVec3f& a);
00104 
00105   /// Dot product
00106   FXfloat operator*(const FXVec3f& v) const { return x*v.x+y*v.y+z*v.z; }
00107 
00108   /// Cross product
00109   FXVec3f operator^(const FXVec3f& v) const { return FXVec3f(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
00110 
00111   /// Test if zero
00112   FXbool operator!() const { return x==0.0f && y==0.0f && z==0.0f; }
00113 
00114   /// Equality tests
00115   FXbool operator==(const FXVec3f& v) const { return x==v.x && y==v.y && z==v.z; }
00116   FXbool operator!=(const FXVec3f& v) const { return x!=v.x || y!=v.y || z!=v.z; }
00117 
00118   friend inline FXbool operator==(const FXVec3f& a,FXfloat n);
00119   friend inline FXbool operator!=(const FXVec3f& a,FXfloat n);
00120   friend inline FXbool operator==(FXfloat n,const FXVec3f& a);
00121   friend inline FXbool operator!=(FXfloat n,const FXVec3f& a);
00122 
00123   /// Inequality tests
00124   FXbool operator<(const FXVec3f& v) const { return x<v.x && y<v.y && z<v.z; }
00125   FXbool operator<=(const FXVec3f& v) const { return x<=v.x && y<=v.y && z<=v.z; }
00126   FXbool operator>(const FXVec3f& v) const { return x>v.x && y>v.y && z>v.z; }
00127   FXbool operator>=(const FXVec3f& v) const { return x>=v.x && y>=v.y && z>=v.z; }
00128 
00129   friend inline FXbool operator<(const FXVec3f& a,FXfloat n);
00130   friend inline FXbool operator<=(const FXVec3f& a,FXfloat n);
00131   friend inline FXbool operator>(const FXVec3f& a,FXfloat n);
00132   friend inline FXbool operator>=(const FXVec3f& a,FXfloat n);
00133 
00134   friend inline FXbool operator<(FXfloat n,const FXVec3f& a);
00135   friend inline FXbool operator<=(FXfloat n,const FXVec3f& a);
00136   friend inline FXbool operator>(FXfloat n,const FXVec3f& a);
00137   friend inline FXbool operator>=(FXfloat n,const FXVec3f& a);
00138 
00139   /// Length and square of length
00140   FXfloat length2() const { return x*x+y*y+z*z; }
00141   FXfloat length() const { return sqrtf(length2()); }
00142 
00143   /// Clamp values of vector between limits
00144   FXVec3f& clamp(FXfloat lo,FXfloat hi){x=FXCLAMP(lo,x,hi);y=FXCLAMP(lo,y,hi);z=FXCLAMP(lo,z,hi);return *this;}
00145 
00146   /// Lowest or highest components
00147   friend inline FXVec3f lo(const FXVec3f& a,const FXVec3f& b);
00148   friend inline FXVec3f hi(const FXVec3f& a,const FXVec3f& b);
00149 
00150   /// Convert vector to color and back
00151   friend FXAPI FXColor colorFromVec3f(const FXVec3f& vec);
00152   friend FXAPI FXVec3f colorToVec3f(FXColor clr);
00153 
00154   /// Compute normal from three points a,b,c
00155   friend FXAPI FXVec3f normal(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
00156 
00157   /// Compute approximate normal from four points a,b,c,d
00158   friend FXAPI FXVec3f normal(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c,const FXVec3f& d);
00159 
00160   /// Normalize vector
00161   friend FXAPI FXVec3f normalize(const FXVec3f& v);
00162 
00163   /// Save vector to a stream
00164   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec3f& v);
00165 
00166   /// Load vector from a stream
00167   friend FXAPI FXStream& operator>>(FXStream& store,FXVec3f& v);
00168   };
00169 
00170 
00171 inline FXVec3f operator*(const FXVec3f& a,FXfloat n){return FXVec3f(a.x*n,a.y*n,a.z*n);}
00172 inline FXVec3f operator*(FXfloat n,const FXVec3f& a){return FXVec3f(n*a.x,n*a.y,n*a.z);}
00173 inline FXVec3f operator/(const FXVec3f& a,FXfloat n){return FXVec3f(a.x/n,a.y/n,a.z/n);}
00174 inline FXVec3f operator/(FXfloat n,const FXVec3f& a){return FXVec3f(n/a.x,n/a.y,n/a.z);}
00175 
00176 inline FXbool operator==(const FXVec3f& a,FXfloat n){return a.x==n && a.y==n && a.z==n;}
00177 inline FXbool operator!=(const FXVec3f& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n;}
00178 inline FXbool operator==(FXfloat n,const FXVec3f& a){return n==a.x && n==a.y && n==a.z;}
00179 inline FXbool operator!=(FXfloat n,const FXVec3f& a){return n!=a.x || n!=a.y || n!=a.z;}
00180 
00181 inline FXbool operator<(const FXVec3f& a,FXfloat n){return a.x<n && a.y<n && a.z<n;}
00182 inline FXbool operator<=(const FXVec3f& a,FXfloat n){return a.x<=n && a.y<=n && a.z<=n;}
00183 inline FXbool operator>(const FXVec3f& a,FXfloat n){return a.x>n && a.y>n && a.z>n;}
00184 inline FXbool operator>=(const FXVec3f& a,FXfloat n){return a.x>=n && a.y>=n && a.z>=n;}
00185 
00186 inline FXbool operator<(FXfloat n,const FXVec3f& a){return n<a.x && n<a.y && n<a.z;}
00187 inline FXbool operator<=(FXfloat n,const FXVec3f& a){return n<=a.x && n<=a.y && n<=a.z;}
00188 inline FXbool operator>(FXfloat n,const FXVec3f& a){return n>a.x && n>a.y && n>a.z;}
00189 inline FXbool operator>=(FXfloat n,const FXVec3f& a){return n>=a.x && n>=a.y && n>=a.z;}
00190 
00191 inline FXVec3f lo(const FXVec3f& a,const FXVec3f& b){return FXVec3f(FXMIN(a.x,b.x),FXMIN(a.y,b.y),FXMIN(a.z,b.z));}
00192 inline FXVec3f hi(const FXVec3f& a,const FXVec3f& b){return FXVec3f(FXMAX(a.x,b.x),FXMAX(a.y,b.y),FXMAX(a.z,b.z));}
00193 
00194 extern FXAPI FXColor colorFromVec3f(const FXVec3f& vec);
00195 extern FXAPI FXVec3f colorToVec3f(FXColor clr);
00196 
00197 extern FXAPI FXVec3f normal(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
00198 extern FXAPI FXVec3f normal(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c,const FXVec3f& d);
00199 
00200 extern FXAPI FXVec3f normalize(const FXVec3f& v);
00201 
00202 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec3f& v);
00203 extern FXAPI FXStream& operator>>(FXStream& store,FXVec3f& v);
00204 
00205 }
00206 
00207 #endif

Copyright © 1997-2009 Jeroen van der Zijp