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

FXDHVec.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *    H o m o g e n e o u s   D o u b l e - 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: FXDHVec.h,v 1.11 2002/04/03 13:50:30 fox Exp $                           *
00023 ********************************************************************************/
00024 #ifndef FXDHVEC_H
00025 #define FXDHVEC_H
00026 
00027 namespace FX {
00028 
00029 
00030 /// Homogeneous vector (double-precision version)
00031 class FXAPI FXDHVec {
00032 protected:
00033   FXdouble v[4];
00034 public:
00035 
00036   /// Default constructor
00037   FXDHVec(){}
00038 
00039   /// Copy constructor
00040   FXDHVec(const FXDHVec& w){v[0]=w.v[0];v[1]=w.v[1];v[2]=w.v[2];v[3]=w.v[3];}
00041 
00042   /// Initialize with components
00043   FXDHVec(FXdouble x,FXdouble y,FXdouble z,FXdouble w=1.0){v[0]=x;v[1]=y;v[2]=z;v[3]=w;}
00044 
00045   /// Initialize with 3-vector
00046   FXDHVec(const FXDVec& w){v[0]=w[0];v[1]=w[1];v[2]=w[2];v[3]=1.0;}
00047 
00048 
00049   /// Initialize with color
00050   FXDHVec(FXColor color);
00051 
00052   /// Return a non-const reference to the ith element
00053   FXdouble& operator[](FXint i){return v[i];}
00054 
00055   /// Return a const reference to the ith element
00056   const FXdouble& operator[](FXint i) const {return v[i];}
00057 
00058   /// Assign color
00059   FXDHVec& operator=(FXColor color);
00060 
00061   /// Assignment
00062   FXDHVec& operator=(const FXDHVec& w){v[0]=w.v[0];v[1]=w.v[1];v[2]=w.v[2];v[3]=w.v[3];return *this;}
00063   FXDHVec& operator=(const FXDVec& w){v[0]=w[0];v[1]=w[1];v[2]=w[2];v[3]=1.0F;return *this;}
00064 
00065 
00066   /// Assigning operators
00067   FXDHVec& operator+=(const FXDHVec& a){v[0]+=a.v[0];v[1]+=a.v[1];v[2]+=a.v[2];v[3]+=a.v[3];return *this;}
00068   FXDHVec& operator-=(const FXDHVec& a){v[0]-=a.v[0];v[1]-=a.v[1];v[2]-=a.v[2];v[3]-=a.v[3];return *this;}
00069   FXDHVec& operator*=(FXdouble n){v[0]*=n;v[1]*=n;v[2]*=n;v[3]*=n;return *this;}
00070   FXDHVec& operator/=(FXdouble n){v[0]/=n;v[1]/=n;v[2]/=n;v[3]/=n;return *this;}
00071 
00072   /// Conversions
00073   operator FXdouble*(){return v;}
00074   operator const FXdouble*() const {return v;}
00075 
00076   /// Convert to color
00077   operator FXColor() const;
00078 
00079   /// Other operators
00080   friend FXDHVec operator-(const FXDHVec& a){return FXDHVec(-a.v[0],-a.v[1],-a.v[2],-a.v[3]);}
00081   friend FXDHVec operator!(const FXDHVec& a){return a.v[0]==0.0 && a.v[1]==0.0 && a.v[2]==0.0 && a.v[3]==0.0;}
00082   friend FXDHVec operator+(const FXDHVec& a,const FXDHVec& b){return FXDHVec(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 FXDHVec operator-(const FXDHVec& a,const FXDHVec& b){return FXDHVec(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 FXDHVec operator*(const FXDHVec& a,FXdouble n){return FXDHVec(a.v[0]*n,a.v[1]*n,a.v[2]*n,a.v[3]*n);}
00085   friend FXDHVec operator*(FXdouble n,const FXDHVec& a){return FXDHVec(n*a.v[0],n*a.v[1],n*a.v[2],n*a.v[3]);}
00086   friend FXDHVec operator/(const FXDHVec& a,FXdouble n){return FXDHVec(a.v[0]/n,a.v[1]/n,a.v[2]/n,a.v[3]/n);}
00087   friend FXDHVec operator/(FXdouble n,const FXDHVec& a){return FXDHVec(n/a.v[0],n/a.v[1],n/a.v[2],n/a.v[3]);}
00088 
00089   /// Dot product
00090   friend FXdouble operator*(const FXDHVec& a,const FXDHVec& 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 FXDHVec& a,const FXDHVec& 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 FXDHVec& a,FXdouble n){return a.v[0]==n && a.v[1]==n && a.v[2]==n && a.v[3]==n;}
00095   friend int operator==(FXdouble n,const FXDHVec& a){return n==a.v[0] && n==a.v[1] && n==a.v[2] && n==a.v[3];}
00096   friend int operator!=(const FXDHVec& a,const FXDHVec& 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 FXDHVec& a,FXdouble n){return a.v[0]!=n || a.v[1]!=n || a.v[2]!=n || a.v[3]!=n;}
00098   friend int operator!=(FXdouble n,const FXDHVec& 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 FXdouble len(const FXDHVec& a);
00102   friend FXAPI FXDHVec normalize(const FXDHVec& a);
00103   friend FXAPI FXDHVec lo(const FXDHVec& a,const FXDHVec& b);
00104   friend FXAPI FXDHVec hi(const FXDHVec& a,const FXDHVec& b);
00105 
00106   /// Save to a stream
00107   friend FXAPI FXStream& operator<<(FXStream& store,const FXDHVec& v);
00108 
00109   /// Load from a stream
00110   friend FXAPI FXStream& operator>>(FXStream& store,FXDHVec& v);
00111   };
00112 
00113 }
00114 
00115 #endif