Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OgreVector4.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __Vector4_H__
00026 #define __Vector4_H__
00027 
00028 #include "OgrePrerequisites.h"
00029 #include "OgreVector3.h"
00030 
00031 namespace Ogre
00032 {
00033 
00036     class _OgreExport Vector4
00037     {
00038     public:
00039         union {
00040             struct {
00041                 Real x, y, z, w;
00042             };
00043             Real val[4];
00044         };
00045 
00046     public:
00047         inline Vector4()
00048         {
00049         }
00050 
00051         inline Vector4( const Real fX, const Real fY, const Real fZ, const Real fW )
00052             : x( fX ), y( fY ), z( fZ ), w( fW)
00053         {
00054         }
00055 
00056         inline explicit Vector4( const Real afCoordinate[4] )
00057             : x( afCoordinate[0] ),
00058               y( afCoordinate[1] ),
00059               z( afCoordinate[2] ),
00060               w( afCoordinate[3] )
00061         {
00062         }
00063 
00064         inline explicit Vector4( const int afCoordinate[4] )
00065         {
00066             x = (Real)afCoordinate[0];
00067             y = (Real)afCoordinate[1];
00068             z = (Real)afCoordinate[2];
00069             w = (Real)afCoordinate[3];
00070         }
00071 
00072         inline explicit Vector4( Real* const r )
00073             : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] )
00074         {
00075         }
00076 
00077         inline explicit Vector4( const Real scaler )
00078             : x( scaler )
00079             , y( scaler )
00080             , z( scaler )
00081             , w( scaler )
00082         {
00083         }
00084 
00085         inline explicit Vector4(const Vector3& rhs)
00086             : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f)
00087         {
00088         }
00089 
00090         inline Vector4( const Vector4& rkVector )
00091             : x( rkVector.x ), y( rkVector.y ), z( rkVector.z ), w (rkVector.w)
00092         {
00093         }
00094 
00095         inline Real operator [] ( const size_t i ) const
00096         {
00097             assert( i < 4 );
00098 
00099             return *(&x+i);
00100         }
00101 
00102         inline Real& operator [] ( const size_t i )
00103         {
00104             assert( i < 4 );
00105 
00106             return *(&x+i);
00107         }
00108 
00113         inline Vector4& operator = ( const Vector4& rkVector )
00114         {
00115             x = rkVector.x;
00116             y = rkVector.y;
00117             z = rkVector.z;
00118             w = rkVector.w;
00119 
00120             return *this;
00121         }
00122 
00123         inline Vector4& operator = ( const Real fScalar)
00124         {
00125             x = fScalar;
00126             y = fScalar;
00127             z = fScalar;
00128             w = fScalar;
00129             return *this;
00130         }
00131 
00132         inline bool operator == ( const Vector4& rkVector ) const
00133         {
00134             return ( x == rkVector.x &&
00135                 y == rkVector.y &&
00136                 z == rkVector.z &&
00137                 w == rkVector.w );
00138         }
00139 
00140         inline bool operator != ( const Vector4& rkVector ) const
00141         {
00142             return ( x != rkVector.x ||
00143                 y != rkVector.y ||
00144                 z != rkVector.z ||
00145                 w != rkVector.w );
00146         }
00147 
00148         inline Vector4& operator = (const Vector3& rhs)
00149         {
00150             x = rhs.x;
00151             y = rhs.y;
00152             z = rhs.z;
00153             w = 1.0f;
00154             return *this;
00155         }
00156 
00157         // arithmetic operations
00158         inline Vector4 operator + ( const Vector4& rkVector ) const
00159         {
00160             return Vector4(
00161                 x + rkVector.x,
00162                 y + rkVector.y,
00163                 z + rkVector.z,
00164                 w + rkVector.w);
00165         }
00166 
00167         inline Vector4 operator - ( const Vector4& rkVector ) const
00168         {
00169             return Vector4(
00170                 x - rkVector.x,
00171                 y - rkVector.y,
00172                 z - rkVector.z,
00173                 w - rkVector.w);
00174         }
00175 
00176         inline Vector4 operator * ( const Real fScalar ) const
00177         {
00178             return Vector4(
00179                 x * fScalar,
00180                 y * fScalar,
00181                 z * fScalar,
00182                 w * fScalar);
00183         }
00184 
00185         inline Vector4 operator * ( const Vector4& rhs) const
00186         {
00187             return Vector4(
00188                 rhs.x * x,
00189                 rhs.y * y,
00190                 rhs.z * z,
00191                 rhs.w * w);
00192         }
00193 
00194         inline Vector4 operator / ( const Real fScalar ) const
00195         {
00196             assert( fScalar != 0.0 );
00197 
00198             Real fInv = 1.0 / fScalar;
00199 
00200             return Vector4(
00201                 x * fInv,
00202                 y * fInv,
00203                 z * fInv,
00204                 w * fInv);
00205         }
00206 
00207         inline Vector4 operator / ( const Vector4& rhs) const
00208         {
00209             return Vector4(
00210                 x / rhs.x,
00211                 y / rhs.y,
00212                 z / rhs.z,
00213                 w / rhs.w);
00214         }
00215 
00216         inline const Vector4& operator + () const
00217         {
00218             return *this;
00219         }
00220 
00221         inline Vector4 operator - () const
00222         {
00223             return Vector4(-x, -y, -z, -w);
00224         }
00225 
00226         inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector )
00227         {
00228             return Vector4(
00229                 fScalar * rkVector.x,
00230                 fScalar * rkVector.y,
00231                 fScalar * rkVector.z,
00232                 fScalar * rkVector.w);
00233         }
00234 
00235         inline friend Vector4 operator / ( const Real fScalar, const Vector4& rkVector )
00236         {
00237             return Vector4(
00238                 fScalar / rkVector.x,
00239                 fScalar / rkVector.y,
00240                 fScalar / rkVector.z,
00241                 fScalar / rkVector.w);
00242         }
00243 
00244         inline friend Vector4 operator + (const Vector4& lhs, const Real rhs)
00245         {
00246             return Vector4(
00247                 lhs.x + rhs,
00248                 lhs.y + rhs,
00249                 lhs.z + rhs,
00250                 lhs.w + rhs);
00251         }
00252 
00253         inline friend Vector4 operator + (const Real lhs, const Vector4& rhs)
00254         {
00255             return Vector4(
00256                 lhs + rhs.x,
00257                 lhs + rhs.y,
00258                 lhs + rhs.z,
00259                 lhs + rhs.w);
00260         }
00261 
00262         inline friend Vector4 operator - (const Vector4& lhs, Real rhs)
00263         {
00264             return Vector4(
00265                 lhs.x - rhs,
00266                 lhs.y - rhs,
00267                 lhs.z - rhs,
00268                 lhs.w - rhs);
00269         }
00270 
00271         inline friend Vector4 operator - (const Real lhs, const Vector4& rhs)
00272         {
00273             return Vector4(
00274                 lhs - rhs.x,
00275                 lhs - rhs.y,
00276                 lhs - rhs.z,
00277                 lhs - rhs.w);
00278         }
00279 
00280         // arithmetic updates
00281         inline Vector4& operator += ( const Vector4& rkVector )
00282         {
00283             x += rkVector.x;
00284             y += rkVector.y;
00285             z += rkVector.z;
00286             w += rkVector.w;
00287 
00288             return *this;
00289         }
00290 
00291         inline Vector4& operator -= ( const Vector4& rkVector )
00292         {
00293             x -= rkVector.x;
00294             y -= rkVector.y;
00295             z -= rkVector.z;
00296             w -= rkVector.w;
00297 
00298             return *this;
00299         }
00300 
00301         inline Vector4& operator *= ( const Real fScalar )
00302         {
00303             x *= fScalar;
00304             y *= fScalar;
00305             z *= fScalar;
00306             w *= fScalar;
00307             return *this;
00308         }
00309 
00310         inline Vector4& operator += ( const Real fScalar )
00311         {
00312             x += fScalar;
00313             y += fScalar;
00314             z += fScalar;
00315             w += fScalar;
00316             return *this;
00317         }
00318 
00319         inline Vector4& operator -= ( const Real fScalar )
00320         {
00321             x -= fScalar;
00322             y -= fScalar;
00323             z -= fScalar;
00324             w -= fScalar;
00325             return *this;
00326         }
00327 
00328         inline Vector4& operator *= ( const Vector4& rkVector )
00329         {
00330             x *= rkVector.x;
00331             y *= rkVector.y;
00332             z *= rkVector.z;
00333             w *= rkVector.w;
00334 
00335             return *this;
00336         }
00337 
00338         inline Vector4& operator /= ( const Real fScalar )
00339         {
00340             assert( fScalar != 0.0 );
00341 
00342             Real fInv = 1.0 / fScalar;
00343 
00344             x *= fInv;
00345             y *= fInv;
00346             z *= fInv;
00347             w *= fInv;
00348 
00349             return *this;
00350         }
00351 
00352         inline Vector4& operator /= ( const Vector4& rkVector )
00353         {
00354             x /= rkVector.x;
00355             y /= rkVector.y;
00356             z /= rkVector.z;
00357             w /= rkVector.w;
00358 
00359             return *this;
00360         }
00361 
00369         inline Real dotProduct(const Vector4& vec) const
00370         {
00371             return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
00372         }
00375         inline _OgreExport friend std::ostream& operator <<
00376             ( std::ostream& o, const Vector4& v )
00377         {
00378             o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
00379             return o;
00380         }
00381         // special
00382         static const Vector4 ZERO;
00383     };
00384 
00385 }
00386 #endif

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 17 15:39:12 2006