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

FXVec3f.h

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,2004 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.4 2004/02/08 17:17:34 fox Exp $                            *
00023 ********************************************************************************/
00024 #ifndef FXVEC3F_H
00025 #define FXVEC3F_H
00026 
00027 
00028 namespace FX {
00029 
00030 
00031 /// Single-precision 3-element vector
00032 class FXAPI FXVec3f {
00033 public:
00034   FXfloat x;
00035   FXfloat y;
00036   FXfloat z;
00037 public:
00038 
00039   /// Default constructor
00040   FXVec3f(){}
00041 
00042   /// Copy constructor
00043   FXVec3f(const FXVec3f& v){x=v.x;y=v.y;z=v.z;}
00044 
00045   // Initialize from array of floats
00046   FXVec3f(const FXfloat v[]){x=v[0];y=v[1];z=v[2];}
00047 
00048   /// Initialize with components
00049   FXVec3f(FXfloat xx,FXfloat yy,FXfloat zz=1.0f){x=xx;y=yy;z=zz;}
00050 
00051   /// Initialize with color
00052   FXVec3f(FXColor color);
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   /// Assign color
00061   FXVec3f& operator=(FXColor color);
00062 
00063   /// Assignment
00064   FXVec3f& operator=(const FXVec3f& v){x=v.x;y=v.y;z=v.z;return *this;}
00065 
00066   /// Assignment from array of floats
00067   FXVec3f& operator=(const FXfloat v[]){x=v[0];y=v[1];z=v[2];return *this;}
00068 
00069   /// Assigning operators
00070   FXVec3f& operator*=(FXfloat n){x*=n;y*=n;z*=n;return *this;}
00071   FXVec3f& operator/=(FXfloat n){x/=n;y/=n;z/=n;return *this;}
00072   FXVec3f& operator+=(const FXVec3f& v){x+=v.x;y+=v.y;z+=v.z;return *this;}
00073   FXVec3f& operator-=(const FXVec3f& v){x-=v.x;y-=v.y;z-=v.z;return *this;}
00074 
00075   /// Conversions
00076   operator FXfloat*(){return &x;}
00077   operator const FXfloat*() const {return &x;}
00078   operator FXVec2f&(){return *reinterpret_cast<FXVec2f*>(this);}
00079   operator const FXVec2f&() const {return *reinterpret_cast<const FXVec2f*>(this);}
00080 
00081   /// Convert to color
00082   operator FXColor() const;
00083 
00084   /// Unary
00085   friend FXAPI FXVec3f operator+(const FXVec3f& v){return v;}
00086   friend FXAPI FXVec3f operator-(const FXVec3f& v){return FXVec3f(-v.x,-v.y,-v.z);}
00087 
00088   /// Adding
00089   friend FXAPI FXVec3f operator+(const FXVec3f& a,const FXVec3f& b){return FXVec3f(a.x+b.x,a.y+b.y,a.z+b.z);}
00090   friend FXAPI FXVec3f operator-(const FXVec3f& a,const FXVec3f& b){return FXVec3f(a.x-b.x,a.y-b.y,a.z-b.z);}
00091 
00092   /// Scaling
00093   friend FXAPI FXVec3f operator*(const FXVec3f& a,FXfloat n){return FXVec3f(a.x*n,a.y*n,a.z*n);}
00094   friend FXAPI FXVec3f operator*(FXfloat n,const FXVec3f& a){return FXVec3f(n*a.x,n*a.y,n*a.z);}
00095   friend FXAPI FXVec3f operator/(const FXVec3f& a,FXfloat n){return FXVec3f(a.x/n,a.y/n,a.z/n);}
00096   friend FXAPI FXVec3f operator/(FXfloat n,const FXVec3f& a){return FXVec3f(n/a.x,n/a.y,n/a.z);}
00097 
00098   /// Dot and cross products
00099   friend FXAPI FXfloat operator*(const FXVec3f& a,const FXVec3f& b){return a.x*b.x+a.y*b.y+a.z*b.z;}
00100   friend FXAPI FXVec3f operator^(const FXVec3f& a,const FXVec3f& b){return FXVec3f(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);}
00101 
00102   /// Test if zero
00103   friend FXAPI int operator!(const FXVec3f& a){return a.x==0.0f && a.y==0.0f && a.z==0.0f;}
00104 
00105   /// Equality tests
00106   friend FXAPI int operator==(const FXVec3f& a,const FXVec3f& b){return a.x==b.x && a.y==b.y && a.z==b.z;}
00107   friend FXAPI int operator!=(const FXVec3f& a,const FXVec3f& b){return a.x!=b.x || a.y!=b.y || a.z!=b.z;}
00108 
00109   friend FXAPI int operator==(const FXVec3f& a,FXfloat n){return a.x==n && a.y==n && a.z==n;}
00110   friend FXAPI int operator!=(const FXVec3f& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n;}
00111 
00112   friend FXAPI int operator==(FXfloat n,const FXVec3f& a){return n==a.x && n==a.y && n==a.z;}
00113   friend FXAPI int operator!=(FXfloat n,const FXVec3f& a){return n!=a.x || n!=a.y || n!=a.z;}
00114 
00115   /// Inequality tests
00116   friend FXAPI int operator<(const FXVec3f& a,const FXVec3f& b){return a.x<b.x && a.y<b.y && a.z<b.z;}
00117   friend FXAPI int operator<=(const FXVec3f& a,const FXVec3f& b){return a.x<=b.x && a.y<=b.y && a.z<=b.z;}
00118   friend FXAPI int operator>(const FXVec3f& a,const FXVec3f& b){return a.x>b.x && a.y>b.y && a.z>b.z;}
00119   friend FXAPI int operator>=(const FXVec3f& a,const FXVec3f& b){return a.x>=b.x && a.y>=b.y && a.z>=b.z;}
00120 
00121   friend FXAPI int operator<(const FXVec3f& a,FXfloat n){return a.x<n && a.y<n && a.z<n;}
00122   friend FXAPI int operator<=(const FXVec3f& a,FXfloat n){return a.x<=n && a.y<=n && a.z<=n;}
00123   friend FXAPI int operator>(const FXVec3f& a,FXfloat n){return a.x>n && a.y>n && a.z>n;}
00124   friend FXAPI int operator>=(const FXVec3f& a,FXfloat n){return a.x>=n && a.y>=n && a.z>=n;}
00125 
00126   friend FXAPI int operator<(FXfloat n,const FXVec3f& a){return n<a.x && n<a.y && n<a.z;}
00127   friend FXAPI int operator<=(FXfloat n,const FXVec3f& a){return n<=a.x && n<=a.y && n<=a.z;}
00128   friend FXAPI int operator>(FXfloat n,const FXVec3f& a){return n>a.x && n>a.y && n>a.z;}
00129   friend FXAPI int operator>=(FXfloat n,const FXVec3f& a){return n>=a.x && n>=a.y && n>=a.z;}
00130 
00131   /// Length and square of length
00132   friend FXAPI FXfloat len2(const FXVec3f& a){ return a.x*a.x+a.y*a.y+a.z*a.z; }
00133   friend FXAPI FXfloat len(const FXVec3f& a){ return sqrtf(len2(a)); }
00134 
00135   /// Normalize vector
00136   friend FXAPI FXVec3f normalize(const FXVec3f& a);
00137 
00138   /// Lowest or highest components
00139   friend FXAPI 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));}
00140   friend FXAPI 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));}
00141 
00142   /// Save vector to a stream
00143   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec3f& v);
00144 
00145   /// Load vector from a stream
00146   friend FXAPI FXStream& operator>>(FXStream& store,FXVec3f& v);
00147   };
00148 
00149 }
00150 
00151 #endif

Copyright © 1997-2004 Jeroen van der Zijp