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

FXVec.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                    F l o a t - V e c t o r   O p e r a t i o n s              *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1994,2002 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: FXVec.h,v 1.7 2002/01/18 22:42:55 jeroen Exp $                           *
00023 ********************************************************************************/
00024 #ifndef FXVEC_H
00025 #define FXVEC_H
00026 
00027 
00028 
00029 /*********************  FXVec Float Vector Class Definition  *******************/
00030 
00031 /// Single-precision vector class
00032 class FXAPI FXVec {
00033 protected:
00034   FXfloat v[3];
00035 public:
00036 
00037   /// Default constructor
00038   FXVec(){}
00039 
00040   /// Copy constructor
00041   FXVec(const FXVec& w){v[0]=w.v[0];v[1]=w.v[1];v[2]=w.v[2];}
00042 
00043   /// Initialize with components
00044   FXVec(FXfloat x,FXfloat y,FXfloat z){v[0]=x;v[1]=y;v[2]=z;}
00045 
00046   /// Initialize with color
00047   FXVec(FXColor color);
00048 
00049   /// Return a non-const reference to the ith element
00050   FXfloat& operator[](FXint i){return v[i];}
00051 
00052   /// Return a const reference to the ith element
00053   const FXfloat& operator[](FXint i) const {return v[i];}
00054 
00055   /// Assign color
00056   FXVec& operator=(FXColor color);
00057 
00058   /// Assignment
00059   FXVec& operator=(const FXVec& w){v[0]=w.v[0];v[1]=w.v[1];v[2]=w.v[2];return *this;}
00060 
00061   /// Assigning operators
00062   FXVec& operator+=(const FXVec& a){v[0]+=a.v[0];v[1]+=a.v[1];v[2]+=a.v[2];return *this;}
00063   FXVec& operator-=(const FXVec& a){v[0]-=a.v[0];v[1]-=a.v[1];v[2]-=a.v[2];return *this;}
00064   FXVec& operator*=(FXfloat n){v[0]*=n;v[1]*=n;v[2]*=n;return *this;}
00065   FXVec& operator/=(FXfloat n){v[0]/=n;v[1]/=n;v[2]/=n;return *this;}
00066 
00067   /// Conversions
00068   operator FXfloat*(){return v;}
00069   operator const FXfloat*() const {return v;}
00070 
00071   /// Convert to color
00072   operator FXColor() const;
00073 
00074   /// Other operators
00075   friend FXAPI FXVec operator-(const FXVec& a){return FXVec(-a.v[0],-a.v[1],-a.v[2]);}
00076   friend FXAPI FXVec operator!(const FXVec& a){return a.v[0]==0.0 && a.v[1]==0.0 && a.v[2]==0.0;}
00077   friend FXAPI FXVec operator+(const FXVec& a,const FXVec& b){return FXVec(a.v[0]+b.v[0],a.v[1]+b.v[1],a.v[2]+b.v[2]);}
00078   friend FXAPI FXVec operator-(const FXVec& a,const FXVec& b){return FXVec(a.v[0]-b.v[0],a.v[1]-b.v[1],a.v[2]-b.v[2]);}
00079   friend FXAPI FXVec operator*(const FXVec& a,FXfloat n){return FXVec(a.v[0]*n,a.v[1]*n,a.v[2]*n);}
00080   friend FXAPI FXVec operator*(FXfloat n,const FXVec& a){return FXVec(n*a.v[0],n*a.v[1],n*a.v[2]);}
00081   friend FXAPI FXVec operator/(const FXVec& a,FXfloat n){return FXVec(a.v[0]/n,a.v[1]/n,a.v[2]/n);}
00082   friend FXAPI FXVec operator/(FXfloat n,const FXVec& a){return FXVec(n/a.v[0],n/a.v[1],n/a.v[2]);}
00083 
00084   /// Dot and cross products
00085   friend FXAPI FXfloat operator*(const FXVec& a,const FXVec& b){return a.v[0]*b.v[0]+a.v[1]*b.v[1]+a.v[2]*b.v[2];}
00086   friend FXAPI FXVec operator^(const FXVec& a,const FXVec& b){return FXVec(a.v[1]*b.v[2]-a.v[2]*b.v[1], a.v[2]*b.v[0]-a.v[0]*b.v[2], a.v[0]*b.v[1]-a.v[1]*b.v[0]);}
00087 
00088   /// Equality tests
00089   friend FXAPI int operator==(const FXVec& a,const FXVec& b){return a.v[0]==b.v[0] && a.v[1]==b.v[1] && a.v[2]==b.v[2];}
00090   friend FXAPI int operator==(const FXVec& a,FXfloat n){return a.v[0]==n && a.v[1]==n && a.v[2]==n;}
00091   friend FXAPI int operator==(FXfloat n,const FXVec& a){return n==a.v[0] && n==a.v[1] && n==a.v[2];}
00092   friend FXAPI int operator!=(const FXVec& a,const FXVec& b){return a.v[0]!=b.v[0] || a.v[1]!=b.v[1] || a.v[2]!=b.v[2];}
00093   friend FXAPI int operator!=(const FXVec& a,FXfloat n){return a.v[0]!=n || a.v[1]!=n || a.v[2]!=n;}
00094   friend FXAPI int operator!=(FXfloat n,const FXVec& a){return n!=a.v[0] || n!=a.v[1] || n!=a.v[2];}
00095 
00096   /// Other functions
00097   friend FXAPI FXfloat len(const FXVec& a);
00098   friend FXAPI FXVec normalize(const FXVec& a);
00099   friend FXAPI FXVec lo(const FXVec& a,const FXVec& b);
00100   friend FXAPI FXVec hi(const FXVec& a,const FXVec& b);
00101 
00102   /// Save vector to a stream
00103   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec& v);
00104 
00105   /// Load vector from a stream
00106   friend FXAPI FXStream& operator>>(FXStream& store,FXVec& v);
00107   };
00108 
00109 
00110 
00111 #endif