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