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

FXVec3d.h

Go to the documentation of this file.
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,2010 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (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                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXVEC3D_H
00022 #define FXVEC3D_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00028 /// Double-precision 3-element vector
00029 class FXAPI FXVec3d {
00030 public:
00031   FXdouble x;
00032   FXdouble y;
00033   FXdouble z;
00034 public:
00035 
00036   /// Default constructor; value is not initialized
00037   FXVec3d(){}
00038 
00039   /// Initialize from 2-vector
00040   FXVec3d(const FXVec2d& v,FXdouble s=0.0):x(v.x),y(v.y),z(s){}
00041 
00042   /// Initialize from another vector
00043   FXVec3d(const FXVec3d& v):x(v.x),y(v.y),z(v.z){}
00044 
00045   /// Initialize from array of doubles
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):x(xx),y(yy),z(zz){}
00050 
00051   /// Return a non-const reference to the ith element
00052   FXdouble& operator[](FXint i){return (&x)[i];}
00053 
00054   /// Return a const reference to the ith element
00055   const FXdouble& operator[](FXint i) const {return (&x)[i];}
00056 
00057   /// Assignment
00058   FXVec3d& operator=(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
00059 
00060   /// Assignment from array of doubles
00061   FXVec3d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
00062 
00063   /// Set value from another vector
00064   FXVec3d& set(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
00065 
00066   /// Set value from array of floats
00067   FXVec3d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
00068 
00069   /// Set value from components
00070   FXVec3d& set(FXdouble xx,FXdouble yy,FXdouble zz){x=xx;y=yy;z=zz;return *this;}
00071 
00072   /// Assigning operators
00073   FXVec3d& operator*=(FXdouble n){ return set(x*n,y*n,z*n); }
00074   FXVec3d& operator/=(FXdouble n){ return set(x/n,y/n,z/n); }
00075   FXVec3d& operator+=(const FXVec3d& v){ return set(x+v.x,y+v.y,z+v.z); }
00076   FXVec3d& operator-=(const FXVec3d& v){ return set(x-v.x,y-v.y,z-v.z); }
00077   FXVec3d& operator^=(const FXVec3d& v){ return set(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x); }
00078 
00079   /// Conversions
00080   operator FXdouble*(){return &x;}
00081   operator const FXdouble*() const {return &x;}
00082   operator FXVec2d&(){return *reinterpret_cast<FXVec2d*>(this);}
00083   operator const FXVec2d&() const {return *reinterpret_cast<const FXVec2d*>(this);}
00084 
00085   /// Test if zero
00086   FXbool operator!() const { return x==0.0 && y==0.0 && z==0.0; }
00087 
00088   /// Unary
00089   FXVec3d operator+() const { return *this; }
00090   FXVec3d operator-() const { return FXVec3d(-x,-y,-z); }
00091 
00092   /// Length and square of length
00093   FXdouble length2() const { return x*x+y*y+z*z; }
00094   FXdouble length() const { return sqrt(length2()); }
00095 
00096   /// Clamp values of vector between limits
00097   FXVec3d& clamp(FXdouble lo,FXdouble hi){ return set(FXCLAMP(lo,x,hi),FXCLAMP(lo,y,hi),FXCLAMP(lo,z,hi)); }
00098   };
00099 
00100 
00101 /// Dot product
00102 inline FXdouble operator*(const FXVec3d& a,const FXVec3d& b){ return a.x*b.x+a.y*b.y+a.z*b.z; }
00103 
00104 /// Cross product
00105 inline 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); }
00106 
00107 /// Scaling
00108 inline FXVec3d operator*(const FXVec3d& a,FXdouble n){return FXVec3d(a.x*n,a.y*n,a.z*n);}
00109 inline FXVec3d operator*(FXdouble n,const FXVec3d& a){return FXVec3d(n*a.x,n*a.y,n*a.z);}
00110 inline FXVec3d operator/(const FXVec3d& a,FXdouble n){return FXVec3d(a.x/n,a.y/n,a.z/n);}
00111 inline FXVec3d operator/(FXdouble n,const FXVec3d& a){return FXVec3d(n/a.x,n/a.y,n/a.z);}
00112 
00113 /// Vector and vector addition
00114 inline FXVec3d operator+(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x+b.x,a.y+b.y,a.z+b.z); }
00115 inline FXVec3d operator-(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x-b.x,a.y-b.y,a.z-b.z); }
00116 
00117 /// Equality tests
00118 inline FXbool operator==(const FXVec3d& a,FXdouble n){return a.x==n && a.y==n && a.z==n;}
00119 inline FXbool operator!=(const FXVec3d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n;}
00120 inline FXbool operator==(FXdouble n,const FXVec3d& a){return n==a.x && n==a.y && n==a.z;}
00121 inline FXbool operator!=(FXdouble n,const FXVec3d& a){return n!=a.x || n!=a.y || n!=a.z;}
00122 
00123 /// Equality tests
00124 inline FXbool operator==(const FXVec3d& a,const FXVec3d& b){ return a.x==b.x && a.y==b.y && a.z==b.z; }
00125 inline FXbool operator!=(const FXVec3d& a,const FXVec3d& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z; }
00126 
00127 /// Inequality tests
00128 inline FXbool operator<(const FXVec3d& a,FXdouble n){return a.x<n && a.y<n && a.z<n;}
00129 inline FXbool operator<=(const FXVec3d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n;}
00130 inline FXbool operator>(const FXVec3d& a,FXdouble n){return a.x>n && a.y>n && a.z>n;}
00131 inline FXbool operator>=(const FXVec3d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n;}
00132 
00133 /// Inequality tests
00134 inline FXbool operator<(FXdouble n,const FXVec3d& a){return n<a.x && n<a.y && n<a.z;}
00135 inline FXbool operator<=(FXdouble n,const FXVec3d& a){return n<=a.x && n<=a.y && n<=a.z;}
00136 inline FXbool operator>(FXdouble n,const FXVec3d& a){return n>a.x && n>a.y && n>a.z;}
00137 inline FXbool operator>=(FXdouble n,const FXVec3d& a){return n>=a.x && n>=a.y && n>=a.z;}
00138 
00139 /// Inequality tests
00140 inline FXbool operator<(const FXVec3d& a,const FXVec3d& b){ return a.x<b.x && a.y<b.y && a.z<b.z; }
00141 inline FXbool operator<=(const FXVec3d& a,const FXVec3d& b){ return a.x<=b.x && a.y<=b.y && a.z<=b.z; }
00142 inline FXbool operator>(const FXVec3d& a,const FXVec3d& b){ return a.x>b.x && a.y>b.y && a.z>b.z; }
00143 inline FXbool operator>=(const FXVec3d& a,const FXVec3d& b){ return a.x>=b.x && a.y>=b.y && a.z>=b.z; }
00144 
00145 /// Lowest or highest components
00146 inline 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));}
00147 inline 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));}
00148 
00149 /// Convert vector to color
00150 extern FXAPI FXColor colorFromVec3d(const FXVec3d& vec);
00151 
00152 /// Convert color to vector
00153 extern FXAPI FXVec3d colorToVec3d(FXColor clr);
00154 
00155 /// Compute normal from three points a,b,c
00156 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
00157 
00158 /// Compute approximate normal from four points a,b,c,d
00159 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c,const FXVec3d& d);
00160 
00161 /// Normalize vector
00162 extern FXAPI FXVec3d normalize(const FXVec3d& v);
00163 
00164 /// Save vector to a stream
00165 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec3d& v);
00166 
00167 /// Load vector from a stream
00168 extern FXAPI FXStream& operator>>(FXStream& store,FXVec3d& v);
00169 
00170 }
00171 
00172 #endif

Copyright © 1997-2010 Jeroen van der Zijp