[148] | 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-2013 Torus Knot Software Ltd |
---|
| 8 | |
---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
---|
| 11 | in the Software without restriction, including without limitation the rights |
---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
---|
| 14 | furnished to do so, subject to the following conditions: |
---|
| 15 | |
---|
| 16 | The above copyright notice and this permission notice shall be included in |
---|
| 17 | all copies or substantial portions of the Software. |
---|
| 18 | |
---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 25 | THE SOFTWARE. |
---|
| 26 | ----------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | #ifndef __Vector4_H__ |
---|
| 29 | #define __Vector4_H__ |
---|
| 30 | |
---|
| 31 | #include "OgrePrerequisites.h" |
---|
| 32 | #include "OgreVector3.h" |
---|
| 33 | |
---|
| 34 | namespace Ogre |
---|
| 35 | { |
---|
| 36 | |
---|
| 37 | /** \addtogroup Core |
---|
| 38 | * @{ |
---|
| 39 | */ |
---|
| 40 | /** \addtogroup Math |
---|
| 41 | * @{ |
---|
| 42 | */ |
---|
| 43 | /** 4-dimensional homogeneous vector. |
---|
| 44 | */ |
---|
| 45 | class _OgreExport Vector4 |
---|
| 46 | { |
---|
| 47 | public: |
---|
| 48 | Real x, y, z, w; |
---|
| 49 | |
---|
| 50 | public: |
---|
| 51 | /** Default constructor. |
---|
| 52 | @note |
---|
| 53 | It does <b>NOT</b> initialize the vector for efficiency. |
---|
| 54 | */ |
---|
| 55 | inline Vector4() |
---|
| 56 | { |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | inline Vector4( const Real fX, const Real fY, const Real fZ, const Real fW ) |
---|
| 60 | : x( fX ), y( fY ), z( fZ ), w( fW ) |
---|
| 61 | { |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | inline explicit Vector4( const Real afCoordinate[4] ) |
---|
| 65 | : x( afCoordinate[0] ), |
---|
| 66 | y( afCoordinate[1] ), |
---|
| 67 | z( afCoordinate[2] ), |
---|
| 68 | w( afCoordinate[3] ) |
---|
| 69 | { |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | inline explicit Vector4( const int afCoordinate[4] ) |
---|
| 73 | { |
---|
| 74 | x = (Real)afCoordinate[0]; |
---|
| 75 | y = (Real)afCoordinate[1]; |
---|
| 76 | z = (Real)afCoordinate[2]; |
---|
| 77 | w = (Real)afCoordinate[3]; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | inline explicit Vector4( Real* const r ) |
---|
| 81 | : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] ) |
---|
| 82 | { |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | inline explicit Vector4( const Real scaler ) |
---|
| 86 | : x( scaler ) |
---|
| 87 | , y( scaler ) |
---|
| 88 | , z( scaler ) |
---|
| 89 | , w( scaler ) |
---|
| 90 | { |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | inline explicit Vector4(const Vector3& rhs) |
---|
| 94 | : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f) |
---|
| 95 | { |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** Exchange the contents of this vector with another. |
---|
| 99 | */ |
---|
| 100 | inline void swap(Vector4& other) |
---|
| 101 | { |
---|
| 102 | std::swap(x, other.x); |
---|
| 103 | std::swap(y, other.y); |
---|
| 104 | std::swap(z, other.z); |
---|
| 105 | std::swap(w, other.w); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | inline Real operator [] ( const size_t i ) const |
---|
| 109 | { |
---|
| 110 | assert( i < 4 ); |
---|
| 111 | |
---|
| 112 | return *(&x+i); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | inline Real& operator [] ( const size_t i ) |
---|
| 116 | { |
---|
| 117 | assert( i < 4 ); |
---|
| 118 | |
---|
| 119 | return *(&x+i); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /// Pointer accessor for direct copying |
---|
| 123 | inline Real* ptr() |
---|
| 124 | { |
---|
| 125 | return &x; |
---|
| 126 | } |
---|
| 127 | /// Pointer accessor for direct copying |
---|
| 128 | inline const Real* ptr() const |
---|
| 129 | { |
---|
| 130 | return &x; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** Assigns the value of the other vector. |
---|
| 134 | @param |
---|
| 135 | rkVector The other vector |
---|
| 136 | */ |
---|
| 137 | inline Vector4& operator = ( const Vector4& rkVector ) |
---|
| 138 | { |
---|
| 139 | x = rkVector.x; |
---|
| 140 | y = rkVector.y; |
---|
| 141 | z = rkVector.z; |
---|
| 142 | w = rkVector.w; |
---|
| 143 | |
---|
| 144 | return *this; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | inline Vector4& operator = ( const Real fScalar) |
---|
| 148 | { |
---|
| 149 | x = fScalar; |
---|
| 150 | y = fScalar; |
---|
| 151 | z = fScalar; |
---|
| 152 | w = fScalar; |
---|
| 153 | return *this; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | inline bool operator == ( const Vector4& rkVector ) const |
---|
| 157 | { |
---|
| 158 | return ( x == rkVector.x && |
---|
| 159 | y == rkVector.y && |
---|
| 160 | z == rkVector.z && |
---|
| 161 | w == rkVector.w ); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | inline bool operator != ( const Vector4& rkVector ) const |
---|
| 165 | { |
---|
| 166 | return ( x != rkVector.x || |
---|
| 167 | y != rkVector.y || |
---|
| 168 | z != rkVector.z || |
---|
| 169 | w != rkVector.w ); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | inline Vector4& operator = (const Vector3& rhs) |
---|
| 173 | { |
---|
| 174 | x = rhs.x; |
---|
| 175 | y = rhs.y; |
---|
| 176 | z = rhs.z; |
---|
| 177 | w = 1.0f; |
---|
| 178 | return *this; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | // arithmetic operations |
---|
| 182 | inline Vector4 operator + ( const Vector4& rkVector ) const |
---|
| 183 | { |
---|
| 184 | return Vector4( |
---|
| 185 | x + rkVector.x, |
---|
| 186 | y + rkVector.y, |
---|
| 187 | z + rkVector.z, |
---|
| 188 | w + rkVector.w); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | inline Vector4 operator - ( const Vector4& rkVector ) const |
---|
| 192 | { |
---|
| 193 | return Vector4( |
---|
| 194 | x - rkVector.x, |
---|
| 195 | y - rkVector.y, |
---|
| 196 | z - rkVector.z, |
---|
| 197 | w - rkVector.w); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | inline Vector4 operator * ( const Real fScalar ) const |
---|
| 201 | { |
---|
| 202 | return Vector4( |
---|
| 203 | x * fScalar, |
---|
| 204 | y * fScalar, |
---|
| 205 | z * fScalar, |
---|
| 206 | w * fScalar); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | inline Vector4 operator * ( const Vector4& rhs) const |
---|
| 210 | { |
---|
| 211 | return Vector4( |
---|
| 212 | rhs.x * x, |
---|
| 213 | rhs.y * y, |
---|
| 214 | rhs.z * z, |
---|
| 215 | rhs.w * w); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | inline Vector4 operator / ( const Real fScalar ) const |
---|
| 219 | { |
---|
| 220 | assert( fScalar != 0.0 ); |
---|
| 221 | |
---|
| 222 | Real fInv = 1.0f / fScalar; |
---|
| 223 | |
---|
| 224 | return Vector4( |
---|
| 225 | x * fInv, |
---|
| 226 | y * fInv, |
---|
| 227 | z * fInv, |
---|
| 228 | w * fInv); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | inline Vector4 operator / ( const Vector4& rhs) const |
---|
| 232 | { |
---|
| 233 | return Vector4( |
---|
| 234 | x / rhs.x, |
---|
| 235 | y / rhs.y, |
---|
| 236 | z / rhs.z, |
---|
| 237 | w / rhs.w); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | inline const Vector4& operator + () const |
---|
| 241 | { |
---|
| 242 | return *this; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | inline Vector4 operator - () const |
---|
| 246 | { |
---|
| 247 | return Vector4(-x, -y, -z, -w); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | inline friend Vector4 operator * ( const Real fScalar, const Vector4& rkVector ) |
---|
| 251 | { |
---|
| 252 | return Vector4( |
---|
| 253 | fScalar * rkVector.x, |
---|
| 254 | fScalar * rkVector.y, |
---|
| 255 | fScalar * rkVector.z, |
---|
| 256 | fScalar * rkVector.w); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | inline friend Vector4 operator / ( const Real fScalar, const Vector4& rkVector ) |
---|
| 260 | { |
---|
| 261 | return Vector4( |
---|
| 262 | fScalar / rkVector.x, |
---|
| 263 | fScalar / rkVector.y, |
---|
| 264 | fScalar / rkVector.z, |
---|
| 265 | fScalar / rkVector.w); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | inline friend Vector4 operator + (const Vector4& lhs, const 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 | inline friend Vector4 operator - (const Vector4& lhs, Real rhs) |
---|
| 287 | { |
---|
| 288 | return Vector4( |
---|
| 289 | lhs.x - rhs, |
---|
| 290 | lhs.y - rhs, |
---|
| 291 | lhs.z - rhs, |
---|
| 292 | lhs.w - rhs); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | inline friend Vector4 operator - (const Real lhs, const Vector4& rhs) |
---|
| 296 | { |
---|
| 297 | return Vector4( |
---|
| 298 | lhs - rhs.x, |
---|
| 299 | lhs - rhs.y, |
---|
| 300 | lhs - rhs.z, |
---|
| 301 | lhs - rhs.w); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | // arithmetic updates |
---|
| 305 | inline Vector4& operator += ( const Vector4& rkVector ) |
---|
| 306 | { |
---|
| 307 | x += rkVector.x; |
---|
| 308 | y += rkVector.y; |
---|
| 309 | z += rkVector.z; |
---|
| 310 | w += rkVector.w; |
---|
| 311 | |
---|
| 312 | return *this; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | inline Vector4& operator -= ( const Vector4& rkVector ) |
---|
| 316 | { |
---|
| 317 | x -= rkVector.x; |
---|
| 318 | y -= rkVector.y; |
---|
| 319 | z -= rkVector.z; |
---|
| 320 | w -= rkVector.w; |
---|
| 321 | |
---|
| 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 Real fScalar ) |
---|
| 335 | { |
---|
| 336 | x += fScalar; |
---|
| 337 | y += fScalar; |
---|
| 338 | z += fScalar; |
---|
| 339 | w += fScalar; |
---|
| 340 | return *this; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | inline Vector4& operator -= ( const Real fScalar ) |
---|
| 344 | { |
---|
| 345 | x -= fScalar; |
---|
| 346 | y -= fScalar; |
---|
| 347 | z -= fScalar; |
---|
| 348 | w -= fScalar; |
---|
| 349 | return *this; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | inline Vector4& operator *= ( const Vector4& rkVector ) |
---|
| 353 | { |
---|
| 354 | x *= rkVector.x; |
---|
| 355 | y *= rkVector.y; |
---|
| 356 | z *= rkVector.z; |
---|
| 357 | w *= rkVector.w; |
---|
| 358 | |
---|
| 359 | return *this; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | inline Vector4& operator /= ( const Real fScalar ) |
---|
| 363 | { |
---|
| 364 | assert( fScalar != 0.0 ); |
---|
| 365 | |
---|
| 366 | Real fInv = 1.0f / fScalar; |
---|
| 367 | |
---|
| 368 | x *= fInv; |
---|
| 369 | y *= fInv; |
---|
| 370 | z *= fInv; |
---|
| 371 | w *= fInv; |
---|
| 372 | |
---|
| 373 | return *this; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | inline Vector4& operator /= ( const Vector4& rkVector ) |
---|
| 377 | { |
---|
| 378 | x /= rkVector.x; |
---|
| 379 | y /= rkVector.y; |
---|
| 380 | z /= rkVector.z; |
---|
| 381 | w /= rkVector.w; |
---|
| 382 | |
---|
| 383 | return *this; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | /** Calculates the dot (scalar) product of this vector with another. |
---|
| 387 | @param |
---|
| 388 | vec Vector with which to calculate the dot product (together |
---|
| 389 | with this one). |
---|
| 390 | @return |
---|
| 391 | A float representing the dot product value. |
---|
| 392 | */ |
---|
| 393 | inline Real dotProduct(const Vector4& vec) const |
---|
| 394 | { |
---|
| 395 | return x * vec.x + y * vec.y + z * vec.z + w * vec.w; |
---|
| 396 | } |
---|
| 397 | /// Check whether this vector contains valid values |
---|
| 398 | inline bool isNaN() const |
---|
| 399 | { |
---|
| 400 | return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w); |
---|
| 401 | } |
---|
| 402 | /** Function for writing to a stream. |
---|
| 403 | */ |
---|
| 404 | inline _OgreExport friend std::ostream& operator << |
---|
| 405 | ( std::ostream& o, const Vector4& v ) |
---|
| 406 | { |
---|
| 407 | o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")"; |
---|
| 408 | return o; |
---|
| 409 | } |
---|
| 410 | // special |
---|
| 411 | static const Vector4 ZERO; |
---|
| 412 | }; |
---|
| 413 | /** @} */ |
---|
| 414 | /** @} */ |
---|
| 415 | |
---|
| 416 | } |
---|
| 417 | #endif |
---|
| 418 | |
---|