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

FXVec3f.h

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

Copyright © 1997-2005 Jeroen van der Zijp