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

FXVec3d.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *       D o u b l e - P r e c i s i o n   3 - E l e m e n t   V e c t o r       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1994,2004 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: FXVec3d.h,v 1.3 2004/02/08 17:17:34 fox Exp $                            *
00023 ********************************************************************************/
00024 #ifndef FXVEC3D_H
00025 #define FXVEC3D_H
00026 
00027 
00028 namespace FX {
00029 
00030 
00031 /// Double-precision 3-element vector
00032 class FXAPI FXVec3d {
00033 public:
00034   FXdouble x;
00035   FXdouble y;
00036   FXdouble z;
00037 public:
00038 
00039   /// Default constructor
00040   FXVec3d(){}
00041 
00042   /// Copy constructor
00043   FXVec3d(const FXVec3d& v){x=v.x;y=v.y;z=v.z;}
00044 
00045   // Initialize from array of floats
00046   FXVec3d(const FXdouble v[]){x=v[0];y=v[1];z=v[2];}
00047 
00048   /// Initialize with components
00049   FXVec3d(FXdouble xx,FXdouble yy,FXdouble zz=1.0){x=xx;y=yy;z=zz;}
00050 
00051   /// Initialize with color
00052   FXVec3d(FXColor color);
00053 
00054   /// Return a non-const reference to the ith element
00055   FXdouble& operator[](FXint i){return (&x)[i];}
00056 
00057   /// Return a const reference to the ith element
00058   const FXdouble& operator[](FXint i) const {return (&x)[i];}
00059 
00060   /// Assign color
00061   FXVec3d& operator=(FXColor color);
00062 
00063   /// Assignment
00064   FXVec3d& operator=(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
00065 
00066   /// Assignment from array of floats
00067   FXVec3d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
00068 
00069   /// Assigning operators
00070   FXVec3d& operator*=(FXdouble n){x*=n;y*=n;z*=n;return *this;}
00071   FXVec3d& operator/=(FXdouble n){x/=n;y/=n;z/=n;return *this;}
00072   FXVec3d& operator+=(const FXVec3d& v){x+=v.x;y+=v.y;z+=v.z;return *this;}
00073   FXVec3d& operator-=(const FXVec3d& v){x-=v.x;y-=v.y;z-=v.z;return *this;}
00074 
00075   /// Conversions
00076   operator FXdouble*(){return &x;}
00077   operator const FXdouble*() const {return &x;}
00078   operator FXVec2d&(){return *reinterpret_cast<FXVec2d*>(this);}
00079   operator const FXVec2d&() const {return *reinterpret_cast<const FXVec2d*>(this);}
00080 
00081   /// Convert to color
00082   operator FXColor() const;
00083 
00084   /// Unary
00085   friend FXAPI FXVec3d operator+(const FXVec3d& v){return v;}
00086   friend FXAPI FXVec3d operator-(const FXVec3d& v){return FXVec3d(-v.x,-v.y,-v.z);}
00087 
00088   /// Adding
00089   friend FXAPI FXVec3d operator+(const FXVec3d& a,const FXVec3d& b){return FXVec3d(a.x+b.x,a.y+b.y,a.z+b.z);}
00090   friend FXAPI FXVec3d operator-(const FXVec3d& a,const FXVec3d& b){return FXVec3d(a.x-b.x,a.y-b.y,a.z-b.z);}
00091 
00092   /// Scaling
00093   friend FXAPI FXVec3d operator*(const FXVec3d& a,FXdouble n){return FXVec3d(a.x*n,a.y*n,a.z*n);}
00094   friend FXAPI FXVec3d operator*(FXdouble n,const FXVec3d& a){return FXVec3d(n*a.x,n*a.y,n*a.z);}
00095   friend FXAPI FXVec3d operator/(const FXVec3d& a,FXdouble n){return FXVec3d(a.x/n,a.y/n,a.z/n);}
00096   friend FXAPI FXVec3d operator/(FXdouble n,const FXVec3d& a){return FXVec3d(n/a.x,n/a.y,n/a.z);}
00097 
00098   /// Dot and cross products
00099   friend FXAPI FXdouble operator*(const FXVec3d& a,const FXVec3d& b){return a.x*b.x+a.y*b.y+a.z*b.z;}
00100   friend FXAPI FXVec3d operator^(const FXVec3d& a,const FXVec3d& b){return FXVec3d(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);}
00101 
00102   /// Test if zero
00103   friend FXAPI int operator!(const FXVec3d& a){return a.x==0.0 && a.y==0.0 && a.z==0.0;}
00104 
00105   /// Equality tests
00106   friend FXAPI int operator==(const FXVec3d& a,const FXVec3d& b){return a.x==b.x && a.y==b.y && a.z==b.z;}
00107   friend FXAPI int operator!=(const FXVec3d& a,const FXVec3d& b){return a.x!=b.x || a.y!=b.y || a.z!=b.z;}
00108 
00109   friend FXAPI int operator==(const FXVec3d& a,FXdouble n){return a.x==n && a.y==n && a.z==n;}
00110   friend FXAPI int operator!=(const FXVec3d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n;}
00111 
00112   friend FXAPI int operator==(FXdouble n,const FXVec3d& a){return n==a.x && n==a.y && n==a.z;}
00113   friend FXAPI int operator!=(FXdouble n,const FXVec3d& a){return n!=a.x || n!=a.y || n!=a.z;}
00114 
00115   /// Inequality tests
00116   friend FXAPI int operator<(const FXVec3d& a,const FXVec3d& b){return a.x<b.x && a.y<b.y && a.z<b.z;}
00117   friend FXAPI int operator<=(const FXVec3d& a,const FXVec3d& b){return a.x<=b.x && a.y<=b.y && a.z<=b.z;}
00118   friend FXAPI int operator>(const FXVec3d& a,const FXVec3d& b){return a.x>b.x && a.y>b.y && a.z>b.z;}
00119   friend FXAPI int operator>=(const FXVec3d& a,const FXVec3d& b){return a.x>=b.x && a.y>=b.y && a.z>=b.z;}
00120 
00121   friend FXAPI int operator<(const FXVec3d& a,FXdouble n){return a.x<n && a.y<n && a.z<n;}
00122   friend FXAPI int operator<=(const FXVec3d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n;}
00123   friend FXAPI int operator>(const FXVec3d& a,FXdouble n){return a.x>n && a.y>n && a.z>n;}
00124   friend FXAPI int operator>=(const FXVec3d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n;}
00125 
00126   friend FXAPI int operator<(FXdouble n,const FXVec3d& a){return n<a.x && n<a.y && n<a.z;}
00127   friend FXAPI int operator<=(FXdouble n,const FXVec3d& a){return n<=a.x && n<=a.y && n<=a.z;}
00128   friend FXAPI int operator>(FXdouble n,const FXVec3d& a){return n>a.x && n>a.y && n>a.z;}
00129   friend FXAPI int operator>=(FXdouble n,const FXVec3d& a){return n>=a.x && n>=a.y && n>=a.z;}
00130 
00131   /// Length and square of length
00132   friend FXAPI FXdouble len2(const FXVec3d& a){ return a.x*a.x+a.y*a.y+a.z*a.z; }
00133   friend FXAPI FXdouble len(const FXVec3d& a){ return sqrt(len2(a)); }
00134 
00135   /// Normalize vector
00136   friend FXAPI FXVec3d normalize(const FXVec3d& a);
00137 
00138   /// Lowest or highest components
00139   friend FXAPI FXVec3d lo(const FXVec3d& a,const FXVec3d& b){return FXVec3d(FXMIN(a.x,b.x),FXMIN(a.y,b.y),FXMIN(a.z,b.z));}
00140   friend FXAPI FXVec3d hi(const FXVec3d& a,const FXVec3d& b){return FXVec3d(FXMAX(a.x,b.x),FXMAX(a.y,b.y),FXMAX(a.z,b.z));}
00141 
00142   /// Save vector to a stream
00143   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec3d& v);
00144 
00145   /// Load vector from a stream
00146   friend FXAPI FXStream& operator>>(FXStream& store,FXVec3d& v);
00147   };
00148 
00149 }
00150 
00151 #endif

Copyright © 1997-2004 Jeroen van der Zijp