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