Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------
- // 4D Vector; Templated components
- template <typename T> struct Vector4
- {
- // Members
- union
- {
- // This is used to self-reference the entire vector as a member, for inheritance
- //struct { Data<T> data; };
- // A single component of the vector
- struct { T x, y, z, w; };
- // Swizzle; Upper three values of the vector
- struct { Vector3<T> xyz; T w; };
- // Swizzle; Lower three values of the vector
- struct { T x; Vector3<T>ywz; };
- // Swizzle; Upper or Lower two values of the vector
- struct { Vector2<T> xy; Vector2<T> zw; };
- // Swizzle; Middle two values of the vector
- struct { T x; Vector2<T> yz; T w; };
- // An array of components
- struct { T m[4]; };
- };
- //--------------------------------------------------------------------------------
- // Constructor; Empty intialization
- explicit Vector4() {}
- // Constructor; 2D Vector promotion with explicit z and w
- explicit Vector4(const Vector2<T> &v, T z, T w) : x(v.x), y(v.y), z(z), w(w) {}
- // Constructor; 2D Vector promotion with explicit x and w
- explicit Vector4(T x, const Vector2<T> &v, T w) : x(x), y(v.x), z(v.y), w(w) {}
- // Constructor; 2D Vector promotion with explicit x and y
- explicit Vector4(T x, T y, const Vector2<T> &v) : x(x), y(y), z(v.x), w(v.y) {}
- // Constructor; Two 2D Vectors merging
- explicit Vector4(const Vector2<T> &v1, const Vector2<T> &v2) : x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
- // Constructor; 3D Vector promotion with explicit w
- explicit Vector4(const Vector3<T> &v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
- // Constructor; 3D Vector promotion with explicit x
- explicit Vector4(T x, const Vector3<T> &v) : x(x), y(v.x), z(v.y), w(v.z) {}
- // Constructor; Directly initialize each value
- explicit Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
- // Constructor; Initialize values from an array
- explicit Vector4(const T *array) : x(array[0]), y(array[1]), z(array[2]), w(array[3]) {}
- //Vector4(const Data<T>& data) : data(data) {}
- // Vector assignment
- template <typename O> Vector4<T>& operator = (const Vector4<O> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
- // Assignment operators
- Vector4<T>& operator = (const Vector4<T>& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
- Vector4<T>& operator += (const Vector4<T>& other) { x += other.x; y += other.y; z += other.z; w += other.w; return *this; }
- Vector4<T>& operator -= (const Vector4<T>& other) { x -= other.x; y -= other.y; z -= other.z; w -= other.w; return *this; }
- Vector4<T>& operator *= (const Vector4<T>& other) { x *= other.x; y *= other.y; z *= other.z; w *= other.w; return *this; }
- Vector4<T>& operator /= (const Vector4<T>& other) { x /= other.x; y /= other.y; z /= other.z; w /= other.w; return *this; }
- Vector4<T>& operator *= (const Float& other) { x *= other; y *= other; z *= other; w *= other; return *this; }
- Vector4<T>& operator /= (const Float& other) { x /= other; y /= other; z /= other; w /= other; return *this; }
- // Unary operators
- Vector4<T> operator + () { return *this; }
- Vector4<T> operator - () { return Vector4<T>(-x, -y, -z, -w); }
- // Binary operators
- Vector4<T> operator + (const Vector4<T>& other) { return Vector4<T>(x + other.x, y + other.y, z + other.z, w + other.w); }
- Vector4<T> operator - (const Vector4<T>& other) { return Vector4<T>(x - other.x, y - other.y, z - other.z, w - other.w); }
- Vector4<T> operator * (const Vector4<T>& other) { return Vector4<T>(x * other.x, y * other.y, z * other.z, w * other.w); }
- Vector4<T> operator / (const Vector4<T>& other) { return Vector4<T>(x / other.x, y / other.y, z / other.z, w / other.w); }
- Vector4<T> operator * (const Float& other) { return Vector4<T>(x * other, y * other, z * other, w * other); }
- Vector4<T> operator / (const Float& other) { return Vector4<T>(x / other, y / other, z / other, w / other); }
- // Boolean operators
- bool operator == (const Vector4<T>& other) { return x == other.x && y == other.y && z == other.z && w == other.w; }
- bool operator != (const Vector4<T>& other) { return x != other.x && y != other.y && z != other.z && w != other.w; }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement