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-2013 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __Vector4_H__
00029 #define __Vector4_H__
00030 
00031 #include "OgrePrerequisites.h"
00032 #include "OgreVector3.h"
00033 
00034 namespace Ogre
00035 {
00036 
00045     class _OgreExport Vector4
00046     {
00047     public:
00048         Real x, y, z, w;
00049 
00050     public:
00055         inline Vector4()
00056         {
00057         }
00058 
00059         inline Vector4( const Real fX, const Real fY, const Real fZ, const Real fW )
00060             : x( fX ), y( fY ), z( fZ ), w( fW )
00061         {
00062         }
00063 
00064         inline explicit Vector4( const Real afCoordinate[4] )
00065             : x( afCoordinate[0] ),
00066               y( afCoordinate[1] ),
00067               z( afCoordinate[2] ),
00068               w( afCoordinate[3] )
00069         {
00070         }
00071 
00072         inline explicit Vector4( const int afCoordinate[4] )
00073         {
00074             x = (Real)afCoordinate[0];
00075             y = (Real)afCoordinate[1];
00076             z = (Real)afCoordinate[2];
00077             w = (Real)afCoordinate[3];
00078         }
00079 
00080         inline explicit Vector4( Real* const r )
00081             : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] )
00082         {
00083         }
00084 
00085         inline explicit Vector4( const Real scaler )
00086             : x( scaler )
00087             , y( scaler )
00088             , z( scaler )
00089             , w( scaler )
00090         {
00091         }
00092 
00093         inline explicit Vector4(const Vector3& rhs)
00094             : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f)
00095         {
00096         }
00097 
00100         inline void swap(Vector4& other)
00101         {
00102             std::swap(x, other.x);
00103             std::swap(y, other.y);
00104             std::swap(z, other.z);
00105             std::swap(w, other.w);
00106         }
00107     
00108         inline Real operator [] ( const size_t i ) const
00109         {
00110             assert( i < 4 );
00111 
00112             return *(&x+i);
00113         }
00114 
00115         inline Real& operator [] ( const size_t i )
00116         {
00117             assert( i < 4 );
00118 
00119             return *(&x+i);
00120         }
00121 
00123         inline Real* ptr()
00124         {
00125             return &x;
00126         }
00128         inline const Real* ptr() const
00129         {
00130             return &x;
00131         }
00132 
00137         inline Vector4& operator = ( const Vector4& rkVector )
00138         {
00139             x = rkVector.x;
00140             y = rkVector.y;
00141             z = rkVector.z;
00142             w = rkVector.w;
00143 
00144             return *this;
00145         }
00146 
00147         inline Vector4& operator = ( const Real fScalar)
00148         {
00149             x = fScalar;
00150             y = fScalar;
00151             z = fScalar;
00152             w = fScalar;
00153             return *this;
00154         }
00155 
00156         inline bool operator == ( const Vector4& rkVector ) const
00157         {
00158             return ( x == rkVector.x &&
00159                 y == rkVector.y &&
00160                 z == rkVector.z &&
00161                 w == rkVector.w );
00162         }
00163 
00164         inline bool operator != ( const Vector4& rkVector ) const
00165         {
00166             return ( x != rkVector.x ||
00167                 y != rkVector.y ||
00168                 z != rkVector.z ||
00169                 w != rkVector.w );
00170         }
00171 
00172         inline Vector4& operator = (const Vector3& rhs)
00173         {
00174             x = rhs.x;
00175             y = rhs.y;
00176             z = rhs.z;
00177             w = 1.0f;
00178             return *this;
00179         }
00180 
00181         // arithmetic operations
00182         inline Vector4 operator + ( const Vector4& rkVector ) const
00183         {
00184             return Vector4(
00185                 x + rkVector.x,
00186                 y + rkVector.y,
00187                 z + rkVector.z,
00188                 w + rkVector.w);
00189         }
00190 
00191         inline Vector4 operator - ( const Vector4& rkVector ) const
00192         {
00193             return Vector4(
00194                 x - rkVector.x,
00195                 y - rkVector.y,
00196                 z - rkVector.z,
00197                 w - rkVector.w);
00198         }
00199 
00200         inline Vector4 operator * ( const Real fScalar ) const
00201         {
00202             return Vector4(
00203                 x * fScalar,
00204                 y * fScalar,
00205                 z * fScalar,
00206                 w * fScalar);
00207         }
00208 
00209         inline Vector4 operator * ( const Vector4& rhs) const
00210         {
00211             return Vector4(
00212                 rhs.x * x,
00213                 rhs.y * y,
00214                 rhs.z * z,
00215                 rhs.w * w);
00216         }
00217 
00218         inline Vector4 operator / ( const Real fScalar ) const
00219         {
00220             assert( fScalar != 0.0 );
00221 
00222             Real fInv = 1.0f / fScalar;
00223 
00224             return Vector4(
00225                 x * fInv,
00226                 y * fInv,
00227                 z * fInv,
00228                 w * fInv);
00229         }
00230 
00231         inline Vector4 operator / ( const Vector4& rhs) const
00232         {
00233             return Vector4(
00234                 x / rhs.x,
00235                 y / rhs.y,
00236                 z / rhs.z,
00237                 w / rhs.w);
00238         }
00239 
00240         inline const Vector4& operator + () const
00241         {
00242             return *this;
00243         }
00244 
00245         inline Vector4 operator - () const
00246         {
00247             return Vector4(-x, -y, -z, -w);
00248         }
00249 
00250         inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector )
00251         {
00252             return Vector4(
00253                 fScalar * rkVector.x,
00254                 fScalar * rkVector.y,
00255                 fScalar * rkVector.z,
00256                 fScalar * rkVector.w);
00257         }
00258 
00259         inline friend Vector4 operator / ( const Real fScalar, const Vector4& rkVector )
00260         {
00261             return Vector4(
00262                 fScalar / rkVector.x,
00263                 fScalar / rkVector.y,
00264                 fScalar / rkVector.z,
00265                 fScalar / rkVector.w);
00266         }
00267 
00268         inline friend Vector4 operator + (const Vector4& lhs, const Real rhs)
00269         {
00270             return Vector4(
00271                 lhs.x + rhs,
00272                 lhs.y + rhs,
00273                 lhs.z + rhs,
00274                 lhs.w + rhs);
00275         }
00276 
00277         inline friend Vector4 operator + (const Real lhs, const Vector4& rhs)
00278         {
00279             return Vector4(
00280                 lhs + rhs.x,
00281                 lhs + rhs.y,
00282                 lhs + rhs.z,
00283                 lhs + rhs.w);
00284         }
00285 
00286         inline friend Vector4 operator - (const Vector4& lhs, Real rhs)
00287         {
00288             return Vector4(
00289                 lhs.x - rhs,
00290                 lhs.y - rhs,
00291                 lhs.z - rhs,
00292                 lhs.w - rhs);
00293         }
00294 
00295         inline friend Vector4 operator - (const Real lhs, const Vector4& rhs)
00296         {
00297             return Vector4(
00298                 lhs - rhs.x,
00299                 lhs - rhs.y,
00300                 lhs - rhs.z,
00301                 lhs - rhs.w);
00302         }
00303 
00304         // arithmetic updates
00305         inline Vector4& operator += ( const Vector4& rkVector )
00306         {
00307             x += rkVector.x;
00308             y += rkVector.y;
00309             z += rkVector.z;
00310             w += rkVector.w;
00311 
00312             return *this;
00313         }
00314 
00315         inline Vector4& operator -= ( const Vector4& rkVector )
00316         {
00317             x -= rkVector.x;
00318             y -= rkVector.y;
00319             z -= rkVector.z;
00320             w -= rkVector.w;
00321 
00322             return *this;
00323         }
00324 
00325         inline Vector4& operator *= ( const Real fScalar )
00326         {
00327             x *= fScalar;
00328             y *= fScalar;
00329             z *= fScalar;
00330             w *= fScalar;
00331             return *this;
00332         }
00333 
00334         inline Vector4& operator += ( const Real fScalar )
00335         {
00336             x += fScalar;
00337             y += fScalar;
00338             z += fScalar;
00339             w += fScalar;
00340             return *this;
00341         }
00342 
00343         inline Vector4& operator -= ( const Real fScalar )
00344         {
00345             x -= fScalar;
00346             y -= fScalar;
00347             z -= fScalar;
00348             w -= fScalar;
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 
00362         inline Vector4& operator /= ( const Real fScalar )
00363         {
00364             assert( fScalar != 0.0 );
00365 
00366             Real fInv = 1.0f / fScalar;
00367 
00368             x *= fInv;
00369             y *= fInv;
00370             z *= fInv;
00371             w *= fInv;
00372 
00373             return *this;
00374         }
00375 
00376         inline Vector4& operator /= ( const Vector4& rkVector )
00377         {
00378             x /= rkVector.x;
00379             y /= rkVector.y;
00380             z /= rkVector.z;
00381             w /= rkVector.w;
00382 
00383             return *this;
00384         }
00385 
00393         inline Real dotProduct(const Vector4& vec) const
00394         {
00395             return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
00396         }
00398         inline bool isNaN() const
00399         {
00400             return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
00401         }
00404         inline _OgreExport friend std::ostream& operator <<
00405             ( std::ostream& o, const Vector4& v )
00406         {
00407             o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
00408             return o;
00409         }
00410         // special
00411         static const Vector4 ZERO;
00412     };
00416 }
00417 #endif
00418 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:48