| 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 | |
|---|
| 35 | namespace Ogre |
|---|
| 36 | { |
|---|
| 37 | |
|---|
| 38 | /** 4-dimensional homogenous vector. |
|---|
| 39 | */ |
|---|
| 40 | class _OgreExport Vector4 |
|---|
| 41 | { |
|---|
| 42 | public: |
|---|
| 43 | Real x, y, z, w; |
|---|
| 44 | |
|---|
| 45 | public: |
|---|
| 46 | inline Vector4() |
|---|
| 47 | { |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | inline Vector4( const Real fX, const Real fY, const Real fZ, const Real fW ) |
|---|
| 51 | : x( fX ), y( fY ), z( fZ ), w( fW) |
|---|
| 52 | { |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | inline explicit Vector4( const Real afCoordinate[4] ) |
|---|
| 56 | : x( afCoordinate[0] ), |
|---|
| 57 | y( afCoordinate[1] ), |
|---|
| 58 | z( afCoordinate[2] ), |
|---|
| 59 | w( afCoordinate[3] ) |
|---|
| 60 | { |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | inline explicit Vector4( const int afCoordinate[4] ) |
|---|
| 64 | { |
|---|
| 65 | x = (Real)afCoordinate[0]; |
|---|
| 66 | y = (Real)afCoordinate[1]; |
|---|
| 67 | z = (Real)afCoordinate[2]; |
|---|
| 68 | w = (Real)afCoordinate[3]; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | inline explicit Vector4( Real* const r ) |
|---|
| 72 | : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] ) |
|---|
| 73 | { |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | inline explicit Vector4( const Real scaler ) |
|---|
| 77 | : x( scaler ) |
|---|
| 78 | , y( scaler ) |
|---|
| 79 | , z( scaler ) |
|---|
| 80 | , w( scaler ) |
|---|
| 81 | { |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | inline explicit Vector4(const Vector3& rhs) |
|---|
| 85 | : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f) |
|---|
| 86 | { |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | inline Vector4( const Vector4& rkVector ) |
|---|
| 90 | : x( rkVector.x ), y( rkVector.y ), z( rkVector.z ), w (rkVector.w) |
|---|
| 91 | { |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | inline Real operator [] ( const size_t i ) const |
|---|
| 95 | { |
|---|
| 96 | assert( i < 4 ); |
|---|
| 97 | |
|---|
| 98 | return *(&x+i); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | inline Real& operator [] ( const size_t i ) |
|---|
| 102 | { |
|---|
| 103 | assert( i < 4 ); |
|---|
| 104 | |
|---|
| 105 | return *(&x+i); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /// Pointer accessor for direct copying |
|---|
| 109 | inline Real* ptr() |
|---|
| 110 | { |
|---|
| 111 | return &x; |
|---|
| 112 | } |
|---|
| 113 | /// Pointer accessor for direct copying |
|---|
| 114 | inline const Real* ptr() const |
|---|
| 115 | { |
|---|
| 116 | return &x; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** Assigns the value of the other vector. |
|---|
| 120 | @param |
|---|
| 121 | rkVector The other vector |
|---|
| 122 | */ |
|---|
| 123 | inline Vector4& operator = ( const Vector4& rkVector ) |
|---|
| 124 | { |
|---|
| 125 | x = rkVector.x; |
|---|
| 126 | y = rkVector.y; |
|---|
| 127 | z = rkVector.z; |
|---|
| 128 | w = rkVector.w; |
|---|
| 129 | |
|---|
| 130 | return *this; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | inline Vector4& operator = ( const Real fScalar) |
|---|
| 134 | { |
|---|
| 135 | x = fScalar; |
|---|
| 136 | y = fScalar; |
|---|
| 137 | z = fScalar; |
|---|
| 138 | w = fScalar; |
|---|
| 139 | return *this; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | inline bool operator == ( const Vector4& rkVector ) const |
|---|
| 143 | { |
|---|
| 144 | return ( x == rkVector.x && |
|---|
| 145 | y == rkVector.y && |
|---|
| 146 | z == rkVector.z && |
|---|
| 147 | w == rkVector.w ); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | inline bool operator != ( const Vector4& rkVector ) const |
|---|
| 151 | { |
|---|
| 152 | return ( x != rkVector.x || |
|---|
| 153 | y != rkVector.y || |
|---|
| 154 | z != rkVector.z || |
|---|
| 155 | w != rkVector.w ); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | inline Vector4& operator = (const Vector3& rhs) |
|---|
| 159 | { |
|---|
| 160 | x = rhs.x; |
|---|
| 161 | y = rhs.y; |
|---|
| 162 | z = rhs.z; |
|---|
| 163 | w = 1.0f; |
|---|
| 164 | return *this; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // arithmetic operations |
|---|
| 168 | inline Vector4 operator + ( const Vector4& rkVector ) const |
|---|
| 169 | { |
|---|
| 170 | return Vector4( |
|---|
| 171 | x + rkVector.x, |
|---|
| 172 | y + rkVector.y, |
|---|
| 173 | z + rkVector.z, |
|---|
| 174 | w + rkVector.w); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | inline Vector4 operator - ( const Vector4& rkVector ) const |
|---|
| 178 | { |
|---|
| 179 | return Vector4( |
|---|
| 180 | x - rkVector.x, |
|---|
| 181 | y - rkVector.y, |
|---|
| 182 | z - rkVector.z, |
|---|
| 183 | w - rkVector.w); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | inline Vector4 operator * ( const Real fScalar ) const |
|---|
| 187 | { |
|---|
| 188 | return Vector4( |
|---|
| 189 | x * fScalar, |
|---|
| 190 | y * fScalar, |
|---|
| 191 | z * fScalar, |
|---|
| 192 | w * fScalar); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | inline Vector4 operator * ( const Vector4& rhs) const |
|---|
| 196 | { |
|---|
| 197 | return Vector4( |
|---|
| 198 | rhs.x * x, |
|---|
| 199 | rhs.y * y, |
|---|
| 200 | rhs.z * z, |
|---|
| 201 | rhs.w * w); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | inline Vector4 operator / ( const Real fScalar ) const |
|---|
| 205 | { |
|---|
| 206 | assert( fScalar != 0.0 ); |
|---|
| 207 | |
|---|
| 208 | Real fInv = 1.0 / fScalar; |
|---|
| 209 | |
|---|
| 210 | return Vector4( |
|---|
| 211 | x * fInv, |
|---|
| 212 | y * fInv, |
|---|
| 213 | z * fInv, |
|---|
| 214 | w * fInv); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | inline Vector4 operator / ( const Vector4& rhs) const |
|---|
| 218 | { |
|---|
| 219 | return Vector4( |
|---|
| 220 | x / rhs.x, |
|---|
| 221 | y / rhs.y, |
|---|
| 222 | z / rhs.z, |
|---|
| 223 | w / rhs.w); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | inline const Vector4& operator + () const |
|---|
| 227 | { |
|---|
| 228 | return *this; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | inline Vector4 operator - () const |
|---|
| 232 | { |
|---|
| 233 | return Vector4(-x, -y, -z, -w); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector ) |
|---|
| 237 | { |
|---|
| 238 | return Vector4( |
|---|
| 239 | fScalar * rkVector.x, |
|---|
| 240 | fScalar * rkVector.y, |
|---|
| 241 | fScalar * rkVector.z, |
|---|
| 242 | fScalar * rkVector.w); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | inline friend Vector4 operator / ( const Real fScalar, const Vector4& rkVector ) |
|---|
| 246 | { |
|---|
| 247 | return Vector4( |
|---|
| 248 | fScalar / rkVector.x, |
|---|
| 249 | fScalar / rkVector.y, |
|---|
| 250 | fScalar / rkVector.z, |
|---|
| 251 | fScalar / rkVector.w); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | inline friend Vector4 operator + (const Vector4& lhs, const Real rhs) |
|---|
| 255 | { |
|---|
| 256 | return Vector4( |
|---|
| 257 | lhs.x + rhs, |
|---|
| 258 | lhs.y + rhs, |
|---|
| 259 | lhs.z + rhs, |
|---|
| 260 | lhs.w + rhs); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | inline friend Vector4 operator + (const Real lhs, const Vector4& rhs) |
|---|
| 264 | { |
|---|
| 265 | return Vector4( |
|---|
| 266 | lhs + rhs.x, |
|---|
| 267 | lhs + rhs.y, |
|---|
| 268 | lhs + rhs.z, |
|---|
| 269 | lhs + rhs.w); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | inline friend Vector4 operator - (const Vector4& lhs, Real rhs) |
|---|
| 273 | { |
|---|
| 274 | return Vector4( |
|---|
| 275 | lhs.x - rhs, |
|---|
| 276 | lhs.y - rhs, |
|---|
| 277 | lhs.z - rhs, |
|---|
| 278 | lhs.w - rhs); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | inline friend Vector4 operator - (const Real lhs, const Vector4& rhs) |
|---|
| 282 | { |
|---|
| 283 | return Vector4( |
|---|
| 284 | lhs - rhs.x, |
|---|
| 285 | lhs - rhs.y, |
|---|
| 286 | lhs - rhs.z, |
|---|
| 287 | lhs - rhs.w); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | // arithmetic updates |
|---|
| 291 | inline Vector4& operator += ( const Vector4& rkVector ) |
|---|
| 292 | { |
|---|
| 293 | x += rkVector.x; |
|---|
| 294 | y += rkVector.y; |
|---|
| 295 | z += rkVector.z; |
|---|
| 296 | w += rkVector.w; |
|---|
| 297 | |
|---|
| 298 | return *this; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | inline Vector4& operator -= ( const Vector4& rkVector ) |
|---|
| 302 | { |
|---|
| 303 | x -= rkVector.x; |
|---|
| 304 | y -= rkVector.y; |
|---|
| 305 | z -= rkVector.z; |
|---|
| 306 | w -= rkVector.w; |
|---|
| 307 | |
|---|
| 308 | return *this; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | inline Vector4& operator *= ( const Real fScalar ) |
|---|
| 312 | { |
|---|
| 313 | x *= fScalar; |
|---|
| 314 | y *= fScalar; |
|---|
| 315 | z *= fScalar; |
|---|
| 316 | w *= fScalar; |
|---|
| 317 | return *this; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | inline Vector4& operator += ( const Real fScalar ) |
|---|
| 321 | { |
|---|
| 322 | x += fScalar; |
|---|
| 323 | y += fScalar; |
|---|
| 324 | z += fScalar; |
|---|
| 325 | w += fScalar; |
|---|
| 326 | return *this; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | inline Vector4& operator -= ( const Real fScalar ) |
|---|
| 330 | { |
|---|
| 331 | x -= fScalar; |
|---|
| 332 | y -= fScalar; |
|---|
| 333 | z -= fScalar; |
|---|
| 334 | w -= fScalar; |
|---|
| 335 | return *this; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | inline Vector4& operator *= ( const Vector4& rkVector ) |
|---|
| 339 | { |
|---|
| 340 | x *= rkVector.x; |
|---|
| 341 | y *= rkVector.y; |
|---|
| 342 | z *= rkVector.z; |
|---|
| 343 | w *= rkVector.w; |
|---|
| 344 | |
|---|
| 345 | return *this; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | inline Vector4& operator /= ( const Real fScalar ) |
|---|
| 349 | { |
|---|
| 350 | assert( fScalar != 0.0 ); |
|---|
| 351 | |
|---|
| 352 | Real fInv = 1.0 / fScalar; |
|---|
| 353 | |
|---|
| 354 | x *= fInv; |
|---|
| 355 | y *= fInv; |
|---|
| 356 | z *= fInv; |
|---|
| 357 | w *= fInv; |
|---|
| 358 | |
|---|
| 359 | return *this; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | inline Vector4& operator /= ( const Vector4& rkVector ) |
|---|
| 363 | { |
|---|
| 364 | x /= rkVector.x; |
|---|
| 365 | y /= rkVector.y; |
|---|
| 366 | z /= rkVector.z; |
|---|
| 367 | w /= rkVector.w; |
|---|
| 368 | |
|---|
| 369 | return *this; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | /** Calculates the dot (scalar) product of this vector with another. |
|---|
| 373 | @param |
|---|
| 374 | vec Vector with which to calculate the dot product (together |
|---|
| 375 | with this one). |
|---|
| 376 | @returns |
|---|
| 377 | A float representing the dot product value. |
|---|
| 378 | */ |
|---|
| 379 | inline Real dotProduct(const Vector4& vec) const |
|---|
| 380 | { |
|---|
| 381 | return x * vec.x + y * vec.y + z * vec.z + w * vec.w; |
|---|
| 382 | } |
|---|
| 383 | /** Function for writing to a stream. |
|---|
| 384 | */ |
|---|
| 385 | inline _OgreExport friend std::ostream& operator << |
|---|
| 386 | ( std::ostream& o, const Vector4& v ) |
|---|
| 387 | { |
|---|
| 388 | o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")"; |
|---|
| 389 | return o; |
|---|
| 390 | } |
|---|
| 391 | // special |
|---|
| 392 | static const Vector4 ZERO; |
|---|
| 393 | }; |
|---|
| 394 | |
|---|
| 395 | } |
|---|
| 396 | #endif |
|---|