[5789] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #ifndef __Vector4_H__ |
---|
| 30 | #define __Vector4_H__ |
---|
| 31 | |
---|
| 32 | #include "OgrePrerequisites.h" |
---|
| 33 | #include "OgreVector3.h" |
---|
| 34 | #include <ostream> |
---|
| 35 | |
---|
| 36 | namespace Ogre |
---|
| 37 | { |
---|
| 38 | |
---|
| 39 | /** 4-dimensional homogeneous vector. |
---|
| 40 | */ |
---|
| 41 | class _OgreExport Vector4 |
---|
| 42 | { |
---|
| 43 | public: |
---|
| 44 | Real x, y, z, w; |
---|
| 45 | |
---|
| 46 | public: |
---|
| 47 | inline Vector4() |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | inline Vector4( const Real fX, const Real fY, const Real fZ, const Real fW ) |
---|
| 52 | : x( fX ), y( fY ), z( fZ ), w( fW) |
---|
| 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | inline explicit Vector4( const Real afCoordinate[4] ) |
---|
| 57 | : x( afCoordinate[0] ), |
---|
| 58 | y( afCoordinate[1] ), |
---|
| 59 | z( afCoordinate[2] ), |
---|
| 60 | w( afCoordinate[3] ) |
---|
| 61 | { |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | inline explicit Vector4( const int afCoordinate[4] ) |
---|
| 65 | { |
---|
| 66 | x = (Real)afCoordinate[0]; |
---|
| 67 | y = (Real)afCoordinate[1]; |
---|
| 68 | z = (Real)afCoordinate[2]; |
---|
| 69 | w = (Real)afCoordinate[3]; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | inline explicit Vector4( Real* const r ) |
---|
| 73 | : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] ) |
---|
| 74 | { |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | inline explicit Vector4( const Real scaler ) |
---|
| 78 | : x( scaler ) |
---|
| 79 | , y( scaler ) |
---|
| 80 | , z( scaler ) |
---|
| 81 | , w( scaler ) |
---|
| 82 | { |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | inline explicit Vector4(const Vector3& rhs) |
---|
| 86 | : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f) |
---|
| 87 | { |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | inline Real operator [] ( const size_t i ) const |
---|
| 91 | { |
---|
| 92 | assert( i < 4 ); |
---|
| 93 | |
---|
| 94 | return *(&x+i); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | inline Real& operator [] ( const size_t i ) |
---|
| 98 | { |
---|
| 99 | assert( i < 4 ); |
---|
| 100 | |
---|
| 101 | return *(&x+i); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /// Pointer accessor for direct copying |
---|
| 105 | inline Real* ptr() |
---|
| 106 | { |
---|
| 107 | return &x; |
---|
| 108 | } |
---|
| 109 | /// Pointer accessor for direct copying |
---|
| 110 | inline const Real* ptr() const |
---|
| 111 | { |
---|
| 112 | return &x; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** Assigns the value of the other vector. |
---|
| 116 | @param |
---|
| 117 | rkVector The other vector |
---|
| 118 | */ |
---|
| 119 | inline Vector4& operator = ( const Vector4& rkVector ) |
---|
| 120 | { |
---|
| 121 | x = rkVector.x; |
---|
| 122 | y = rkVector.y; |
---|
| 123 | z = rkVector.z; |
---|
| 124 | w = rkVector.w; |
---|
| 125 | |
---|
| 126 | return *this; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | inline Vector4& operator = ( const Real fScalar) |
---|
| 130 | { |
---|
| 131 | x = fScalar; |
---|
| 132 | y = fScalar; |
---|
| 133 | z = fScalar; |
---|
| 134 | w = fScalar; |
---|
| 135 | return *this; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | inline bool operator == ( const Vector4& rkVector ) const |
---|
| 139 | { |
---|
| 140 | return ( x == rkVector.x && |
---|
| 141 | y == rkVector.y && |
---|
| 142 | z == rkVector.z && |
---|
| 143 | w == rkVector.w ); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | inline bool operator != ( const Vector4& rkVector ) const |
---|
| 147 | { |
---|
| 148 | return ( x != rkVector.x || |
---|
| 149 | y != rkVector.y || |
---|
| 150 | z != rkVector.z || |
---|
| 151 | w != rkVector.w ); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | inline Vector4& operator = (const Vector3& rhs) |
---|
| 155 | { |
---|
| 156 | x = rhs.x; |
---|
| 157 | y = rhs.y; |
---|
| 158 | z = rhs.z; |
---|
| 159 | w = 1.0f; |
---|
| 160 | return *this; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | // arithmetic operations |
---|
| 164 | inline Vector4 operator + ( const Vector4& rkVector ) const |
---|
| 165 | { |
---|
| 166 | return Vector4( |
---|
| 167 | x + rkVector.x, |
---|
| 168 | y + rkVector.y, |
---|
| 169 | z + rkVector.z, |
---|
| 170 | w + rkVector.w); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | inline Vector4 operator - ( const Vector4& rkVector ) const |
---|
| 174 | { |
---|
| 175 | return Vector4( |
---|
| 176 | x - rkVector.x, |
---|
| 177 | y - rkVector.y, |
---|
| 178 | z - rkVector.z, |
---|
| 179 | w - rkVector.w); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | inline Vector4 operator * ( const Real fScalar ) const |
---|
| 183 | { |
---|
| 184 | return Vector4( |
---|
| 185 | x * fScalar, |
---|
| 186 | y * fScalar, |
---|
| 187 | z * fScalar, |
---|
| 188 | w * fScalar); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | inline Vector4 operator * ( const Vector4& rhs) const |
---|
| 192 | { |
---|
| 193 | return Vector4( |
---|
| 194 | rhs.x * x, |
---|
| 195 | rhs.y * y, |
---|
| 196 | rhs.z * z, |
---|
| 197 | rhs.w * w); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | inline Vector4 operator / ( const Real fScalar ) const |
---|
| 201 | { |
---|
| 202 | assert( fScalar != 0.0 ); |
---|
| 203 | |
---|
| 204 | Real fInv = 1.0 / fScalar; |
---|
| 205 | |
---|
| 206 | return Vector4( |
---|
| 207 | x * fInv, |
---|
| 208 | y * fInv, |
---|
| 209 | z * fInv, |
---|
| 210 | w * fInv); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | inline Vector4 operator / ( const Vector4& rhs) const |
---|
| 214 | { |
---|
| 215 | return Vector4( |
---|
| 216 | x / rhs.x, |
---|
| 217 | y / rhs.y, |
---|
| 218 | z / rhs.z, |
---|
| 219 | w / rhs.w); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | inline const Vector4& operator + () const |
---|
| 223 | { |
---|
| 224 | return *this; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | inline Vector4 operator - () const |
---|
| 228 | { |
---|
| 229 | return Vector4(-x, -y, -z, -w); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector ) |
---|
| 233 | { |
---|
| 234 | return Vector4( |
---|
| 235 | fScalar * rkVector.x, |
---|
| 236 | fScalar * rkVector.y, |
---|
| 237 | fScalar * rkVector.z, |
---|
| 238 | fScalar * rkVector.w); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | inline friend Vector4 operator / ( const Real fScalar, const Vector4& rkVector ) |
---|
| 242 | { |
---|
| 243 | return Vector4( |
---|
| 244 | fScalar / rkVector.x, |
---|
| 245 | fScalar / rkVector.y, |
---|
| 246 | fScalar / rkVector.z, |
---|
| 247 | fScalar / rkVector.w); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | inline friend Vector4 operator + (const Vector4& lhs, const Real rhs) |
---|
| 251 | { |
---|
| 252 | return Vector4( |
---|
| 253 | lhs.x + rhs, |
---|
| 254 | lhs.y + rhs, |
---|
| 255 | lhs.z + rhs, |
---|
| 256 | lhs.w + rhs); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | inline friend Vector4 operator + (const Real lhs, const Vector4& rhs) |
---|
| 260 | { |
---|
| 261 | return Vector4( |
---|
| 262 | lhs + rhs.x, |
---|
| 263 | lhs + rhs.y, |
---|
| 264 | lhs + rhs.z, |
---|
| 265 | lhs + rhs.w); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | inline friend Vector4 operator - (const Vector4& lhs, Real rhs) |
---|
| 269 | { |
---|
| 270 | return Vector4( |
---|
| 271 | lhs.x - rhs, |
---|
| 272 | lhs.y - rhs, |
---|
| 273 | lhs.z - rhs, |
---|
| 274 | lhs.w - rhs); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | inline friend Vector4 operator - (const Real lhs, const Vector4& rhs) |
---|
| 278 | { |
---|
| 279 | return Vector4( |
---|
| 280 | lhs - rhs.x, |
---|
| 281 | lhs - rhs.y, |
---|
| 282 | lhs - rhs.z, |
---|
| 283 | lhs - rhs.w); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | // arithmetic updates |
---|
| 287 | inline Vector4& operator += ( const Vector4& rkVector ) |
---|
| 288 | { |
---|
| 289 | x += rkVector.x; |
---|
| 290 | y += rkVector.y; |
---|
| 291 | z += rkVector.z; |
---|
| 292 | w += rkVector.w; |
---|
| 293 | |
---|
| 294 | return *this; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | inline Vector4& operator -= ( const Vector4& rkVector ) |
---|
| 298 | { |
---|
| 299 | x -= rkVector.x; |
---|
| 300 | y -= rkVector.y; |
---|
| 301 | z -= rkVector.z; |
---|
| 302 | w -= rkVector.w; |
---|
| 303 | |
---|
| 304 | return *this; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | inline Vector4& operator *= ( const Real fScalar ) |
---|
| 308 | { |
---|
| 309 | x *= fScalar; |
---|
| 310 | y *= fScalar; |
---|
| 311 | z *= fScalar; |
---|
| 312 | w *= fScalar; |
---|
| 313 | return *this; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | inline Vector4& operator += ( const Real fScalar ) |
---|
| 317 | { |
---|
| 318 | x += fScalar; |
---|
| 319 | y += fScalar; |
---|
| 320 | z += fScalar; |
---|
| 321 | w += fScalar; |
---|
| 322 | return *this; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | inline Vector4& operator -= ( const Real fScalar ) |
---|
| 326 | { |
---|
| 327 | x -= fScalar; |
---|
| 328 | y -= fScalar; |
---|
| 329 | z -= fScalar; |
---|
| 330 | w -= fScalar; |
---|
| 331 | return *this; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | inline Vector4& operator *= ( const Vector4& rkVector ) |
---|
| 335 | { |
---|
| 336 | x *= rkVector.x; |
---|
| 337 | y *= rkVector.y; |
---|
| 338 | z *= rkVector.z; |
---|
| 339 | w *= rkVector.w; |
---|
| 340 | |
---|
| 341 | return *this; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | inline Vector4& operator /= ( const Real fScalar ) |
---|
| 345 | { |
---|
| 346 | assert( fScalar != 0.0 ); |
---|
| 347 | |
---|
| 348 | Real fInv = 1.0 / fScalar; |
---|
| 349 | |
---|
| 350 | x *= fInv; |
---|
| 351 | y *= fInv; |
---|
| 352 | z *= fInv; |
---|
| 353 | w *= fInv; |
---|
| 354 | |
---|
| 355 | return *this; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | inline Vector4& operator /= ( const Vector4& rkVector ) |
---|
| 359 | { |
---|
| 360 | x /= rkVector.x; |
---|
| 361 | y /= rkVector.y; |
---|
| 362 | z /= rkVector.z; |
---|
| 363 | w /= rkVector.w; |
---|
| 364 | |
---|
| 365 | return *this; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | /** Calculates the dot (scalar) product of this vector with another. |
---|
| 369 | @param |
---|
| 370 | vec Vector with which to calculate the dot product (together |
---|
| 371 | with this one). |
---|
| 372 | @returns |
---|
| 373 | A float representing the dot product value. |
---|
| 374 | */ |
---|
| 375 | inline Real dotProduct(const Vector4& vec) const |
---|
| 376 | { |
---|
| 377 | return x * vec.x + y * vec.y + z * vec.z + w * vec.w; |
---|
| 378 | } |
---|
| 379 | /** Function for writing to a stream. |
---|
| 380 | */ |
---|
| 381 | inline _OgreExport friend std::ostream& operator << |
---|
| 382 | ( std::ostream& o, const Vector4& v ) |
---|
| 383 | { |
---|
| 384 | o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")"; |
---|
| 385 | return o; |
---|
| 386 | } |
---|
| 387 | // special |
---|
| 388 | static const Vector4 ZERO; |
---|
| 389 | }; |
---|
| 390 | |
---|
| 391 | } |
---|
| 392 | #endif |
---|