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,2005 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.8 2005/01/16 16:06:06 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 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=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 doubles 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 00091 /// Subtracting 00092 friend FXAPI FXVec3d operator-(const FXVec3d& a,const FXVec3d& b){return FXVec3d(a.x-b.x,a.y-b.y,a.z-b.z);} 00093 00094 /// Scaling 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 friend FXAPI FXVec3d operator/(const FXVec3d& a,FXdouble n){return FXVec3d(a.x/n,a.y/n,a.z/n);} 00098 friend FXAPI FXVec3d operator/(FXdouble n,const FXVec3d& a){return FXVec3d(n/a.x,n/a.y,n/a.z);} 00099 00100 /// Dot product 00101 friend FXAPI FXdouble operator*(const FXVec3d& a,const FXVec3d& b){return a.x*b.x+a.y*b.y+a.z*b.z;} 00102 00103 /// Cross product 00104 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);} 00105 00106 /// Test if zero 00107 friend FXAPI int operator!(const FXVec3d& a){return a.x==0.0 && a.y==0.0 && a.z==0.0;} 00108 00109 /// Equality tests 00110 friend FXAPI int operator==(const FXVec3d& a,const FXVec3d& b){return a.x==b.x && a.y==b.y && a.z==b.z;} 00111 friend FXAPI int operator!=(const FXVec3d& a,const FXVec3d& b){return a.x!=b.x || a.y!=b.y || a.z!=b.z;} 00112 00113 friend FXAPI int operator==(const FXVec3d& a,FXdouble n){return a.x==n && a.y==n && a.z==n;} 00114 friend FXAPI int operator!=(const FXVec3d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n;} 00115 00116 friend FXAPI int operator==(FXdouble n,const FXVec3d& a){return n==a.x && n==a.y && n==a.z;} 00117 friend FXAPI int operator!=(FXdouble n,const FXVec3d& a){return n!=a.x || n!=a.y || n!=a.z;} 00118 00119 /// Inequality tests 00120 friend FXAPI int operator<(const FXVec3d& a,const FXVec3d& b){return a.x<b.x && a.y<b.y && a.z<b.z;} 00121 friend FXAPI int operator<=(const FXVec3d& a,const FXVec3d& b){return a.x<=b.x && a.y<=b.y && a.z<=b.z;} 00122 friend FXAPI int operator>(const FXVec3d& a,const FXVec3d& b){return a.x>b.x && a.y>b.y && a.z>b.z;} 00123 friend FXAPI int operator>=(const FXVec3d& a,const FXVec3d& b){return a.x>=b.x && a.y>=b.y && a.z>=b.z;} 00124 00125 friend FXAPI int operator<(const FXVec3d& a,FXdouble n){return a.x<n && a.y<n && a.z<n;} 00126 friend FXAPI int operator<=(const FXVec3d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n;} 00127 friend FXAPI int operator>(const FXVec3d& a,FXdouble n){return a.x>n && a.y>n && a.z>n;} 00128 friend FXAPI int operator>=(const FXVec3d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n;} 00129 00130 friend FXAPI int operator<(FXdouble n,const FXVec3d& a){return n<a.x && n<a.y && n<a.z;} 00131 friend FXAPI int operator<=(FXdouble n,const FXVec3d& a){return n<=a.x && n<=a.y && n<=a.z;} 00132 friend FXAPI int operator>(FXdouble n,const FXVec3d& a){return n>a.x && n>a.y && n>a.z;} 00133 friend FXAPI int operator>=(FXdouble n,const FXVec3d& a){return n>=a.x && n>=a.y && n>=a.z;} 00134 00135 /// Length and square of length 00136 friend FXAPI FXdouble len2(const FXVec3d& a){ return a.x*a.x+a.y*a.y+a.z*a.z; } 00137 friend FXAPI FXdouble len(const FXVec3d& a){ return sqrt(len2(a)); } 00138 00139 /// Normalize vector 00140 friend FXAPI FXVec3d normalize(const FXVec3d& a); 00141 00142 /// Lowest or highest components 00143 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));} 00144 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));} 00145 00146 /// Compute normal from three points a,b,c 00147 friend FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c); 00148 00149 /// Compute approximate normal from four points a,b,c,d 00150 friend FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c,const FXVec3d& d); 00151 00152 /// Save vector to a stream 00153 friend FXAPI FXStream& operator<<(FXStream& store,const FXVec3d& v); 00154 00155 /// Load vector from a stream 00156 friend FXAPI FXStream& operator>>(FXStream& store,FXVec3d& v); 00157 }; 00158 00159 } 00160 00161 #endif

Copyright © 1997-2005 Jeroen van der Zijp