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,2004 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.6 2004/02/08 17:17:34 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
00047   FXVec4f(const FXVec3f& v){x=v.x;y=v.y;z=v.z;w=1.0f;}
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   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);}
00096 
00097   /// Scaling
00098   friend FXAPI FXVec4f operator*(const FXVec4f& a,FXfloat n){return FXVec4f(a.x*n,a.y*n,a.z*n,a.w*n);}
00099   friend FXAPI FXVec4f operator*(FXfloat n,const FXVec4f& a){return FXVec4f(n*a.x,n*a.y,n*a.z,n*a.w);}
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 
00103   /// Dot product
00104   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;}
00105 
00106   /// Test if zero
00107   friend FXAPI FXVec4f operator!(const FXVec4f& a){return a.x==0.0f && a.y==0.0f && a.z==0.0f && a.w==0.0f;}
00108 
00109   /// Equality tests
00110   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;}
00111   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;}
00112 
00113   friend FXAPI int operator==(const FXVec4f& a,FXfloat n){return a.x==n && a.y==n && a.z==n && a.w==n;}
00114   friend FXAPI int operator!=(const FXVec4f& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
00115 
00116   friend FXAPI int operator==(FXfloat n,const FXVec4f& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
00117   friend FXAPI int operator!=(FXfloat n,const FXVec4f& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
00118 
00119   /// Inequality tests
00120   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;}
00121   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;}
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 
00125   friend FXAPI int operator<(const FXVec4f& a,FXfloat n){return a.x<n && a.y<n && a.z<n && a.w<n;}
00126   friend FXAPI int operator<=(const FXVec4f& a,FXfloat n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
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 
00130   friend FXAPI int operator<(FXfloat n,const FXVec4f& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
00131   friend FXAPI int operator<=(FXfloat n,const FXVec4f& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
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 
00135   /// Length and square of length
00136   friend FXAPI FXfloat len2(const FXVec4f& a){ return a.x*a.x+a.y*a.y+a.z*a.z+a.w*a.w; }
00137   friend FXAPI FXfloat len(const FXVec4f& a){ return sqrtf(len2(a)); }
00138 
00139   /// Normalize vector
00140   friend FXAPI FXVec4f normalize(const FXVec4f& a);
00141 
00142   /// Lowest or highest components
00143   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));}
00144   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));}
00145 
00146   /// Save to a stream
00147   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec4f& v);
00148 
00149   /// Load from a stream
00150   friend FXAPI FXStream& operator>>(FXStream& store,FXVec4f& v);
00151   };
00152 
00153 }
00154 
00155 #endif

Copyright © 1997-2004 Jeroen van der Zijp