Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | 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 Vector4( const Vector4& rkVector )
00086             : x( rkVector.x ), y( rkVector.y ), z( rkVector.z ), w (rkVector.w)
00087         {
00088         }
00089 
00090         inline Real operator [] ( const size_t i ) const
00091         {
00092             assert( i < 4 );
00093 
00094             return *(&x+i);
00095         }
00096 
00097         inline Real& operator [] ( const size_t i )
00098         {
00099             assert( i < 4 );
00100 
00101             return *(&x+i);
00102         }
00103 
00108         inline Vector4& operator = ( const Vector4& rkVector )
00109         {
00110             x = rkVector.x;
00111             y = rkVector.y;
00112             z = rkVector.z;
00113             w = rkVector.w;
00114 
00115             return *this;
00116         }
00117 
00118         inline Vector4& operator = ( const Real fScalar)
00119         {
00120             x = fScalar;
00121             y = fScalar;
00122             z = fScalar;
00123             w = fScalar;
00124             return *this;
00125         }
00126 
00127         inline bool operator == ( const Vector4& rkVector ) const
00128         {
00129             return ( x == rkVector.x &&
00130                 y == rkVector.y &&
00131                 z == rkVector.z &&
00132                 w == rkVector.w );
00133         }
00134 
00135         inline bool operator != ( const Vector4& rkVector ) const
00136         {
00137             return ( x != rkVector.x ||
00138                 y != rkVector.y ||
00139                 z != rkVector.z ||
00140                 w != rkVector.w );
00141         }
00142 
00143         inline Vector4& operator = (const Vector3& rhs)
00144         {
00145             x = rhs.x;
00146             y = rhs.y;
00147             z = rhs.z;
00148             w = 1.0f;
00149             return *this;
00150         }
00151 
00152         // arithmetic operations
00153         inline Vector4 operator + ( const Vector4& rkVector ) const
00154         {
00155             Vector4 kSum;
00156 
00157             kSum.x = x + rkVector.x;
00158             kSum.y = y + rkVector.y;
00159             kSum.z = z + rkVector.z;
00160             kSum.w = w + rkVector.w;
00161 
00162             return kSum;
00163         }
00164 
00165         inline Vector4 operator - ( const Vector4& rkVector ) const
00166         {
00167             Vector4 kDiff;
00168 
00169             kDiff.x = x - rkVector.x;
00170             kDiff.y = y - rkVector.y;
00171             kDiff.z = z - rkVector.z;
00172             kDiff.w = w - rkVector.w;
00173 
00174             return kDiff;
00175         }
00176 
00177         inline Vector4 operator * ( const Real fScalar ) const
00178         {
00179             Vector4 kProd;
00180 
00181             kProd.x = fScalar*x;
00182             kProd.y = fScalar*y;
00183             kProd.z = fScalar*z;
00184             kProd.w = fScalar*w;
00185 
00186             return kProd;
00187         }
00188 
00189         inline Vector4 operator * ( const Vector4& rhs) const
00190         {
00191             Vector4 kProd;
00192 
00193             kProd.x = rhs.x * x;
00194             kProd.y = rhs.y * y;
00195             kProd.z = rhs.z * z;
00196             kProd.w = rhs.w * w;
00197 
00198             return kProd;
00199         }
00200 
00201         inline Vector4 operator / ( const Real fScalar ) const
00202         {
00203             assert( fScalar != 0.0 );
00204 
00205             Vector4 kDiv;
00206 
00207             Real fInv = 1.0 / fScalar;
00208             kDiv.x = x * fInv;
00209             kDiv.y = y * fInv;
00210             kDiv.z = z * fInv;
00211             kDiv.w = w * fInv;
00212 
00213             return kDiv;
00214         }
00215 
00216         inline Vector4 operator / ( const Vector4& rhs) const
00217         {
00218             Vector4 kDiv;
00219 
00220             kDiv.x = x / rhs.x;
00221             kDiv.y = y / rhs.y;
00222             kDiv.z = z / rhs.z;
00223             kDiv.w = w / rhs.w;
00224 
00225             return kDiv;
00226         }
00227 
00228 
00229         inline Vector4 operator - () const
00230         {
00231             Vector4 kNeg;
00232 
00233             kNeg.x = -x;
00234             kNeg.y = -y;
00235             kNeg.z = -z;
00236             kNeg.w = -w;
00237 
00238             return kNeg;
00239         }
00240 
00241         inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector )
00242         {
00243             Vector4 kProd;
00244 
00245             kProd.x = fScalar * rkVector.x;
00246             kProd.y = fScalar * rkVector.y;
00247             kProd.z = fScalar * rkVector.z;
00248             kProd.w = fScalar * rkVector.w;
00249 
00250             return kProd;
00251         }
00252 
00253         inline friend Vector4 operator + (const Vector4& lhs, const Real rhs)
00254         {
00255             Vector4 ret;
00256             ret = rhs;
00257             return ret += lhs;
00258         }
00259 
00260         inline friend Vector4 operator + (const Real lhs, const Vector4& rhs)
00261         {
00262             Vector4 ret;
00263             ret = lhs;
00264             return ret += rhs;
00265         }
00266 
00267         inline friend Vector4 operator - (const Vector4& lhs, Real rhs)
00268         {
00269             Vector4 ret;
00270             ret = lhs;
00271             return ret -= rhs;
00272         }
00273 
00274         inline friend Vector4 operator - (const Real lhs, const Vector4& rhs)
00275         {
00276             Vector4 ret;
00277             ret = lhs;
00278             return ret -= rhs;
00279         }
00280 
00281         // arithmetic updates
00282         inline Vector4& operator += ( const Vector4& rkVector )
00283         {
00284             x += rkVector.x;
00285             y += rkVector.y;
00286             z += rkVector.z;
00287             w += rkVector.w;
00288 
00289             return *this;
00290         }
00291 
00292         inline Vector4& operator -= ( const Vector4& rkVector )
00293         {
00294             x -= rkVector.x;
00295             y -= rkVector.y;
00296             z -= rkVector.z;
00297             w -= rkVector.w;
00298 
00299             return *this;
00300         }
00301 
00302         inline Vector4& operator *= ( const Real fScalar )
00303         {
00304             x *= fScalar;
00305             y *= fScalar;
00306             z *= fScalar;
00307             w *= fScalar;
00308             return *this;
00309         }
00310 
00311         inline Vector4& operator += ( const Real fScalar )
00312         {
00313             x += fScalar;
00314             y += fScalar;
00315             z += fScalar;
00316             w += fScalar;
00317             return *this;
00318         }
00319 
00320         inline Vector4& operator -= ( const Real fScalar )
00321         {
00322             x -= fScalar;
00323             y -= fScalar;
00324             z -= fScalar;
00325             w -= fScalar;
00326             return *this;
00327         }
00328 
00329         inline Vector4& operator *= ( const Vector4& rkVector )
00330         {
00331             x *= rkVector.x;
00332             y *= rkVector.y;
00333             z *= rkVector.z;
00334             w *= rkVector.w;
00335 
00336             return *this;
00337         }
00338 
00339         inline Vector4& operator /= ( const Real fScalar )
00340         {
00341             assert( fScalar != 0.0 );
00342 
00343             Real fInv = 1.0 / fScalar;
00344 
00345             x *= fInv;
00346             y *= fInv;
00347             z *= fInv;
00348             w *= fInv;
00349 
00350             return *this;
00351         }
00352 
00353         inline Vector4& operator /= ( const Vector4& rkVector )
00354         {
00355             x /= rkVector.x;
00356             y /= rkVector.y;
00357             z /= rkVector.z;
00358             w /= rkVector.w;
00359 
00360             return *this;
00361         }
00362 
00370         inline Real dotProduct(const Vector4& vec) const
00371         {
00372             return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
00373         }
00376         inline _OgreExport friend std::ostream& operator <<
00377             ( std::ostream& o, const Vector4& v )
00378         {
00379             o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
00380             return o;
00381         }
00382         // special
00383         static const Vector4 ZERO;
00384     };
00385 
00386 }
00387 #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 Mar 12 14:37:51 2006