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

FXVec4d.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   4 - 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 FXVEC4D_H
00022 #define FXVEC4D_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00028 /// Double-precision 4-element vector
00029 class FXAPI FXVec4d {
00030 public:
00031   FXdouble x;
00032   FXdouble y;
00033   FXdouble z;
00034   FXdouble w;
00035 public:
00036 
00037   /// Default constructor; value is not initialized
00038   FXVec4d(){}
00039 
00040   /// Construct with 3-vector
00041   FXVec4d(const FXVec3d& v,FXdouble s=0.0):x(v.x),y(v.y),z(v.z),w(s){}
00042 
00043   /// Initialize from another vector
00044   FXVec4d(const FXVec4d& v):x(v.x),y(v.y),z(v.z),w(v.w){}
00045 
00046   /// Initialize from array of doubles
00047   FXVec4d(const FXdouble v[]):x(v[0]),y(v[1]),z(v[2]),w(v[3]){}
00048 
00049   /// Initialize with components
00050   FXVec4d(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww):x(xx),y(yy),z(zz),w(ww){}
00051 
00052   /// Return a non-const reference to the ith element
00053   FXdouble& operator[](FXint i){return (&x)[i];}
00054 
00055   /// Return a const reference to the ith element
00056   const FXdouble& operator[](FXint i) const {return (&x)[i];}
00057 
00058   /// Assignment
00059   FXVec4d& operator=(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
00060 
00061   /// Assignment from array of doubles
00062   FXVec4d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
00063 
00064   /// Set value from another vector
00065   FXVec4d& set(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
00066 
00067   /// Set value from array of floats
00068   FXVec4d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
00069 
00070   /// Set value from components
00071   FXVec4d& set(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww){x=xx;y=yy;z=zz;w=ww;return *this;}
00072 
00073   /// Assigning operators
00074   FXVec4d& operator*=(FXdouble n){ return set(x*n,y*n,z*n,w*n); }
00075   FXVec4d& operator/=(FXdouble n){ return set(x/n,y/n,z/n,w/n); }
00076   FXVec4d& operator+=(const FXVec4d& v){ return set(x+v.x,y+v.y,z+v.z,w+v.w); }
00077   FXVec4d& operator-=(const FXVec4d& v){ return set(x-v.x,y-v.y,z-v.z,w-v.w); }
00078 
00079   /// Conversion
00080   operator FXdouble*(){return &x;}
00081   operator const FXdouble*() const {return &x;}
00082   operator FXVec3d&(){return *reinterpret_cast<FXVec3d*>(this);}
00083   operator const FXVec3d&() const {return *reinterpret_cast<const FXVec3d*>(this);}
00084 
00085   /// Test if zero
00086   FXbool operator!() const {return x==0.0 && y==0.0 && z==0.0 && w==0.0; }
00087 
00088   /// Unary
00089   FXVec4d operator+() const { return *this; }
00090   FXVec4d operator-() const { return FXVec4d(-x,-y,-z,-w); }
00091 
00092   /// Length and square of length
00093   FXdouble length2() const { return x*x+y*y+z*z+w*w; }
00094   FXdouble length() const { return sqrt(length2()); }
00095 
00096   /// Clamp values of vector between limits
00097   FXVec4d& clamp(FXdouble lo,FXdouble hi){ return set(FXCLAMP(lo,x,hi),FXCLAMP(lo,y,hi),FXCLAMP(lo,z,hi),FXCLAMP(lo,w,hi)); }
00098 
00099   /// Signed distance normalized plane and point
00100   FXdouble distance(const FXVec3d& p) const;
00101 
00102   /// Return true if edge a-b crosses plane
00103   FXbool crosses(const FXVec3d& a,const FXVec3d& b) const;
00104   };
00105 
00106 
00107 /// Dot product
00108 inline FXdouble operator*(const FXVec4d& a,const FXVec4d& b){ return a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w; }
00109 
00110 /// Scaling
00111 inline FXVec4d operator*(const FXVec4d& a,FXdouble n){return FXVec4d(a.x*n,a.y*n,a.z*n,a.w*n);}
00112 inline FXVec4d operator*(FXdouble n,const FXVec4d& a){return FXVec4d(n*a.x,n*a.y,n*a.z,n*a.w);}
00113 inline FXVec4d operator/(const FXVec4d& a,FXdouble n){return FXVec4d(a.x/n,a.y/n,a.z/n,a.w/n);}
00114 inline FXVec4d operator/(FXdouble n,const FXVec4d& a){return FXVec4d(n/a.x,n/a.y,n/a.z,n/a.w);}
00115 
00116 /// Vector and vector addition
00117 inline FXVec4d operator+(const FXVec4d& a,const FXVec4d& b){ return FXVec4d(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w); }
00118 inline FXVec4d operator-(const FXVec4d& a,const FXVec4d& b){ return FXVec4d(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w); }
00119 
00120 /// Equality tests
00121 inline FXbool operator==(const FXVec4d& a,FXdouble n){return a.x==n && a.y==n && a.z==n && a.w==n;}
00122 inline FXbool operator!=(const FXVec4d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
00123 inline FXbool operator==(FXdouble n,const FXVec4d& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
00124 inline FXbool operator!=(FXdouble n,const FXVec4d& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
00125 
00126 /// Equality tests
00127 inline FXbool operator==(const FXVec4d& a,const FXVec4d& b){ return a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w; }
00128 inline FXbool operator!=(const FXVec4d& a,const FXVec4d& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z || a.w!=b.w; }
00129 
00130 /// Inequality tests
00131 inline FXbool operator<(const FXVec4d& a,FXdouble n){return a.x<n && a.y<n && a.z<n && a.w<n;}
00132 inline FXbool operator<=(const FXVec4d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
00133 inline FXbool operator>(const FXVec4d& a,FXdouble n){return a.x>n && a.y>n && a.z>n && a.w>n;}
00134 inline FXbool operator>=(const FXVec4d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
00135 
00136 /// Inequality tests
00137 inline FXbool operator<(FXdouble n,const FXVec4d& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
00138 inline FXbool operator<=(FXdouble n,const FXVec4d& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
00139 inline FXbool operator>(FXdouble n,const FXVec4d& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
00140 inline FXbool operator>=(FXdouble n,const FXVec4d& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
00141 
00142 /// Inequality tests
00143 inline FXbool operator<(const FXVec4d& a,const FXVec4d& b){ return a.x<b.x && a.y<b.y && a.z<b.z && a.w<b.w; }
00144 inline FXbool operator<=(const FXVec4d& a,const FXVec4d& b){ return a.x<=b.x && a.y<=b.y && a.z<=b.z && a.w<=b.w; }
00145 inline FXbool operator>(const FXVec4d& a,const FXVec4d& b){ return a.x>b.x && a.y>b.y && a.z>b.z && a.w>b.w; }
00146 inline FXbool operator>=(const FXVec4d& a,const FXVec4d& b){ return a.x>=b.x && a.y>=b.y && a.z>=b.z && a.w>=b.w; }
00147 
00148 /// Lowest or highest components
00149 inline FXVec4d lo(const FXVec4d& a,const FXVec4d& b){return FXVec4d(FXMIN(a.x,b.x),FXMIN(a.y,b.y),FXMIN(a.z,b.z),FXMIN(a.w,b.w));}
00150 inline FXVec4d hi(const FXVec4d& a,const FXVec4d& b){return FXVec4d(FXMAX(a.x,b.x),FXMAX(a.y,b.y),FXMAX(a.z,b.z),FXMAX(a.w,b.w));}
00151 
00152 /// Compute normalized plane equation ax+by+cz+d=0
00153 extern FXAPI FXVec4d plane(const FXVec4d& vec);
00154 extern FXAPI FXVec4d plane(const FXVec3d& vec,FXdouble dist);
00155 extern FXAPI FXVec4d plane(const FXVec3d& vec,const FXVec3d& p);
00156 extern FXAPI FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
00157 
00158 /// Convert vector to color
00159 extern FXAPI FXColor colorFromVec4d(const FXVec4d& vec);
00160 
00161 /// Convert color to vector
00162 extern FXAPI FXVec4d colorToVec4d(FXColor clr);
00163 
00164 /// Normalize vector
00165 extern FXAPI FXVec4d normalize(const FXVec4d& v);
00166 
00167 /// Save vector to a stream
00168 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4d& v);
00169 
00170 /// Load vector from a stream
00171 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4d& v);
00172 
00173 }
00174 
00175 #endif

Copyright © 1997-2010 Jeroen van der Zijp