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

FXVec4f.h

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

Copyright © 1997-2005 Jeroen van der Zijp