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