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

Copyright © 1997-2005 Jeroen van der Zijp