Advertisement
Ember

Vector4

Mar 27th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------
  2. // 4D Vector; Templated components
  3. template <typename T> struct Vector4
  4. {
  5.     // Members
  6.     union
  7.     {
  8.         // This is used to self-reference the entire vector as a member, for inheritance
  9.         //struct { Data<T> data; };
  10.         // A single component of the vector
  11.         struct { T x, y, z, w; };
  12.         // Swizzle; Upper three values of the vector
  13.         struct { Vector3<T> xyz; T w; };
  14.         // Swizzle; Lower three values of the vector
  15.         struct { T x; Vector3<T>ywz; };
  16.         // Swizzle; Upper or Lower two values of the vector
  17.         struct { Vector2<T> xy; Vector2<T> zw; };
  18.         // Swizzle; Middle two values of the vector
  19.         struct { T x; Vector2<T> yz; T w; };
  20.         // An array of components
  21.         struct { T m[4]; };
  22.     };
  23.  
  24.     //--------------------------------------------------------------------------------
  25.     // Constructor; Empty intialization
  26.     explicit Vector4() {}
  27.     // Constructor; 2D Vector promotion with explicit z and w
  28.     explicit Vector4(const Vector2<T> &v, T z, T w) : x(v.x), y(v.y), z(z), w(w) {}
  29.     // Constructor; 2D Vector promotion with explicit x and w
  30.     explicit Vector4(T x, const Vector2<T> &v, T w) : x(x), y(v.x), z(v.y), w(w) {}
  31.     // Constructor; 2D Vector promotion with explicit x and y
  32.     explicit Vector4(T x, T y, const Vector2<T> &v) : x(x), y(y), z(v.x), w(v.y) {}
  33.     // Constructor; Two 2D Vectors merging
  34.     explicit Vector4(const Vector2<T> &v1, const Vector2<T> &v2) : x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
  35.     // Constructor; 3D Vector promotion with explicit w
  36.     explicit Vector4(const Vector3<T> &v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
  37.     // Constructor; 3D Vector promotion with explicit x
  38.     explicit Vector4(T x, const Vector3<T> &v) : x(x), y(v.x), z(v.y), w(v.z) {}
  39.     // Constructor; Directly initialize each value
  40.     explicit Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
  41.     // Constructor; Initialize values from an array
  42.     explicit Vector4(const T *array) : x(array[0]), y(array[1]), z(array[2]), w(array[3]) {}
  43.     //Vector4(const Data<T>& data) : data(data) {}
  44.  
  45.     // Vector assignment
  46.     template <typename O> Vector4<T>& operator = (const Vector4<O> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
  47.  
  48.     // Assignment operators
  49.     Vector4<T>& operator = (const Vector4<T>& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
  50.     Vector4<T>& operator += (const Vector4<T>& other) { x += other.x; y += other.y; z += other.z; w += other.w; return *this; }
  51.     Vector4<T>& operator -= (const Vector4<T>& other) { x -= other.x; y -= other.y; z -= other.z; w -= other.w; return *this; }
  52.     Vector4<T>& operator *= (const Vector4<T>& other) { x *= other.x; y *= other.y; z *= other.z; w *= other.w; return *this; }
  53.     Vector4<T>& operator /= (const Vector4<T>& other) { x /= other.x; y /= other.y; z /= other.z; w /= other.w; return *this; }
  54.     Vector4<T>& operator *= (const Float& other) { x *= other; y *= other; z *= other; w *= other; return *this; }
  55.     Vector4<T>& operator /= (const Float& other) { x /= other; y /= other; z /= other; w /= other; return *this; }
  56.  
  57.     // Unary operators
  58.     Vector4<T> operator + () { return *this; }
  59.     Vector4<T> operator - () { return Vector4<T>(-x, -y, -z, -w); }
  60.  
  61.     // Binary operators
  62.     Vector4<T> operator + (const Vector4<T>& other) { return Vector4<T>(x + other.x, y + other.y, z + other.z, w + other.w); }
  63.     Vector4<T> operator - (const Vector4<T>& other) { return Vector4<T>(x - other.x, y - other.y, z - other.z, w - other.w); }
  64.     Vector4<T> operator * (const Vector4<T>& other) { return Vector4<T>(x * other.x, y * other.y, z * other.z, w * other.w); }
  65.     Vector4<T> operator / (const Vector4<T>& other) { return Vector4<T>(x / other.x, y / other.y, z / other.z, w / other.w); }
  66.     Vector4<T> operator * (const Float& other) { return Vector4<T>(x * other, y * other, z * other, w * other); }
  67.     Vector4<T> operator / (const Float& other) { return Vector4<T>(x / other, y / other, z / other, w / other); }
  68.  
  69.     // Boolean operators
  70.     bool operator == (const Vector4<T>& other) { return x == other.x && y == other.y && z == other.z && w == other.w; }
  71.     bool operator != (const Vector4<T>& other) { return x != other.x && y != other.y && z != other.z && w != other.w; }
  72. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement