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

FXHVec.h

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