| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #pragma once
- #include "Prerequisites/BsPrerequisitesUtil.h"
- #include "Math/BsVector3.h"
- namespace bs
- {
- /** @addtogroup Math
- * @{
- */
- /** A four dimensional vector. */
- class BS_UTILITY_EXPORT Vector4
- {
- public:
- float x, y, z, w;
- public:
- Vector4()
- { }
- Vector4(BS_ZERO zero)
- :x(0.0f), y(0.0f), z(0.0f), w(0.0f)
- { }
- Vector4(float x, float y, float z, float w)
- :x(x), y(y), z(z), w(w)
- { }
- explicit Vector4(const Vector3& vec, float w = 0.0f)
- :x(vec.x), y(vec.y), z(vec.z), w(w)
- { }
- /** Exchange the contents of this vector with another. */
- void swap(Vector4& other)
- {
- std::swap(x, other.x);
- std::swap(y, other.y);
- std::swap(z, other.z);
- std::swap(w, other.w);
- }
-
- float operator[] (UINT32 i) const
- {
- assert (i < 4);
- return *(&x+i);
- }
- float& operator[] (UINT32 i)
- {
- assert(i < 4);
- return *(&x+i);
- }
- /** Pointer accessor for direct copying. */
- float* ptr()
- {
- return &x;
- }
- /** Pointer accessor for direct copying. */
- const float* ptr() const
- {
- return &x;
- }
- Vector4& operator= (const Vector4& rhs)
- {
- x = rhs.x;
- y = rhs.y;
- z = rhs.z;
- w = rhs.w;
- return *this;
- }
- Vector4& operator= (float rhs)
- {
- x = rhs;
- y = rhs;
- z = rhs;
- w = rhs;
- return *this;
- }
- bool operator== (const Vector4& rhs) const
- {
- return (x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w);
- }
- bool operator!= (const Vector4& rhs) const
- {
- return (x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w);
- }
- Vector4& operator= (const Vector3& rhs)
- {
- x = rhs.x;
- y = rhs.y;
- z = rhs.z;
- w = 1.0f;
- return *this;
- }
- Vector4 operator+ (const Vector4& rhs) const
- {
- return Vector4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
- }
- Vector4 operator- (const Vector4& rhs) const
- {
- return Vector4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
- }
- Vector4 operator* (float rhs) const
- {
- return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
- }
- Vector4 operator* (const Vector4& rhs) const
- {
- return Vector4(rhs.x * x, rhs.y * y, rhs.z * z, rhs.w * w);
- }
- Vector4 operator/ (float rhs) const
- {
- assert(rhs != 0.0f);
- float inv = 1.0f / rhs;
- return Vector4(x * inv, y * inv, z * inv, w * inv);
- }
- Vector4 operator/ (const Vector4& rhs) const
- {
- return Vector4(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w);
- }
- const Vector4& operator+ () const
- {
- return *this;
- }
- Vector4 operator- () const
- {
- return Vector4(-x, -y, -z, -w);
- }
- friend Vector4 operator* (float lhs, const Vector4& rhs)
- {
- return Vector4(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z, lhs * rhs.w);
- }
- friend Vector4 operator/ (float lhs, const Vector4& rhs)
- {
- return Vector4(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z, lhs / rhs.w);
- }
- friend Vector4 operator+ (const Vector4& lhs, float rhs)
- {
- return Vector4(lhs.x + rhs, lhs.y + rhs, lhs.z + rhs, lhs.w + rhs);
- }
- friend Vector4 operator+ (float lhs, const Vector4& rhs)
- {
- return Vector4(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z, lhs + rhs.w);
- }
- friend Vector4 operator- (const Vector4& lhs, float rhs)
- {
- return Vector4(lhs.x - rhs, lhs.y - rhs, lhs.z - rhs, lhs.w - rhs);
- }
- friend Vector4 operator- (float lhs, Vector4& rhs)
- {
- return Vector4(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z, lhs - rhs.w);
- }
- Vector4& operator+= (const Vector4& rhs)
- {
- x += rhs.x;
- y += rhs.y;
- z += rhs.z;
- w += rhs.w;
- return *this;
- }
- Vector4& operator-= (const Vector4& rhs)
- {
- x -= rhs.x;
- y -= rhs.y;
- z -= rhs.z;
- w -= rhs.w;
- return *this;
- }
- Vector4& operator*= (float rhs)
- {
- x *= rhs;
- y *= rhs;
- z *= rhs;
- w *= rhs;
- return *this;
- }
- Vector4& operator+= (float rhs)
- {
- x += rhs;
- y += rhs;
- z += rhs;
- w += rhs;
- return *this;
- }
- Vector4& operator-= (float rhs)
- {
- x -= rhs;
- y -= rhs;
- z -= rhs;
- w -= rhs;
- return *this;
- }
- Vector4& operator*= (Vector4& rhs)
- {
- x *= rhs.x;
- y *= rhs.y;
- z *= rhs.z;
- w *= rhs.w;
- return *this;
- }
- Vector4& operator/= (float rhs)
- {
- assert(rhs != 0.0f);
- float inv = 1.0f / rhs;
- x *= inv;
- y *= inv;
- z *= inv;
- w *= inv;
- return *this;
- }
- Vector4& operator/= (const Vector4& rhs)
- {
- x /= rhs.x;
- y /= rhs.y;
- z /= rhs.z;
- w /= rhs.w;
- return *this;
- }
- /** Calculates the dot (scalar) product of this vector with another. */
- float dot(const Vector4& vec) const
- {
- return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
- }
- /** Checks are any of the vector components NaN. */
- inline bool isNaN() const;
- static const Vector4 ZERO;
- };
- /** @} */
- /** @cond SPECIALIZATIONS */
- BS_ALLOW_MEMCPY_SERIALIZATION(Vector4);
- /** @endcond */
- }
|