Ember

4D Vector

Mar 9th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------
  2. // 4D Vector; Templated components
  3. template <typename T> struct Vector4
  4. {
  5.     // Members
  6.     union
  7.     {
  8.         // Swizzle; Lower three values of the vector
  9.         struct { T x; Vector3<T>ywz; };
  10.         // Swizzle; Upper three values of the vector
  11.         struct { Vector3<T> xyz; T w; };
  12.         // Swizzle; Middle two values of the vector
  13.         struct { T x; Vector2<T> yz; T w; };
  14.         // Swizzle; Upper or Lower two values of the vector
  15.         struct { Vector2<T> xy; Vector2<T> zw; };
  16.         // A single component of the vector
  17.         struct { T x, y, z, w; };
  18.     };
  19.  
  20.     //--------------------------------------------------------------------------------
  21.     // Constructor; Empty intialization
  22.     Vector4() {}
  23.     // Constructor; 2D Vector promotion with explicite z and w
  24.     Vector4(Vector2<T> &v, T z, T w) : x(v.x), y(v.y), z(z), w(w) {}
  25.     // Constructor; 2D Vector promotion with explicite x and w
  26.     Vector4(T x, Vector2<T> &v, T w) : x(x), y(v.x), z(v.y), w(w) {}
  27.     // Constructor; 2D Vector promotion with explicite x and y
  28.     Vector4(T x, T y, Vector2<T> &v) : x(x), y(y), z(v.x), w(v.y) {}
  29.     // Constructor; Two 2D Vectors merging
  30.     Vector4(Vector2<T> &v1, Vector2<T> &v2) : x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
  31.     // Constructor; 3D Vector promotion with explicite w
  32.     Vector4(Vector3<T> &v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
  33.     // Constructor; 3D Vector promotion with explicite x
  34.     Vector4(T x, Vector3<T> &v) : x(x), y(v.x), z(v.y), w(v.z) {}
  35.     // Constructor; Directly initialize each value
  36.     Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
  37.     // Constructor; Initialize values from an array
  38.     explicit Vector4(In(4) const T *array) : x(array[0]), y(array[1]), z(array[2]), w(array[3]) {}
  39.  
  40.     // Vector assignment
  41.     Vector4<T>& operator = (const Vector4<T> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
  42.  
  43.     // Vector convertion to 32-bit floating-point components
  44.     operator Vector4<Float>() const { return Vector4<Float>(x, y, z, w); };
  45. };
Advertisement
Add Comment
Please, Sign In to add comment