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

FXDVec.h

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