Ember

Vectors

Mar 9th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.14 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------
  2. // 2D Vector; Templated components
  3. template <typename T> struct Vector2
  4. {
  5.     // Members
  6.     union
  7.     {
  8.         // A single component of the
  9.         struct { T x, y; };
  10.     };
  11.  
  12.     // Constructor; Empty initalization
  13.     explicit Vector2() {}
  14.     // Constructor; Directly initialize each value
  15.     explicit Vector2(T x, T y) : x(x), y(y) {}
  16.     // Constructor; Initialize values from an array
  17.     explicit Vector2(In(2) const T* array) : x(array[0]), y(array[1]) {}
  18.  
  19.     // Vector assignment
  20.     Vector2<T>& operator = (const Vector2<T> &other) { x = other.x; y = other.y; return *this; }
  21. };
  22.  
  23. //-------------------------------------------------------------------------------------
  24. // 3D Vector; Templated components
  25. template <typename T> struct Vector3
  26. {
  27.     // Members
  28.     union
  29.     {
  30.         // Swizzle; Lower two values of the vector
  31.         struct { T x; Vector2<T> yz; };
  32.         // Swizzle; Upper two values of the vector
  33.         struct { Vector2<T> xy; T z; };
  34.         // A single component of the vector
  35.         struct { T x, y, z; };
  36.     };
  37.  
  38.     //--------------------------------------------------------------------------------
  39.     // Constructor; Empty initialization
  40.     explicit Vector3() {}
  41.     // Constructor; 2D Vector promotion with explicite z
  42.     explicit Vector3(Vector2<T> &v, T z) : x(v.x), y(v.y), z(z) {}
  43.     // Constructor; 2D Vector promotion with explicite x
  44.     explicit Vector3(T x, Vector2<T> &v) : x(x), y(v.x), z(v.y) {}
  45.     // Constructor; Directly initialize each value
  46.     explicit Vector3(T x, T y, T z) : x(x), y(y), z(z) {}
  47.     // Constructor; Initialize values from an array
  48.     explicit Vector3(In(3) const T *array) : x(array[0]), y(array[1]), z(array[2]) {}
  49.  
  50.     // Vector assignment
  51.     Vector3<T>& operator = (const Vector3<T> &other) { x = other.x; y = other.y; z = other.z; return *this; }
  52. };
  53.  
  54. //-------------------------------------------------------------------------------------
  55. // 4D Vector; Templated components
  56. template <typename T> struct Vector4
  57. {
  58.     // Members
  59.     union
  60.     {
  61.         // Swizzle; Lower three values of the vector
  62.         struct { T x; Vector3<T>ywz; };
  63.         // Swizzle; Upper three values of the vector
  64.         struct { Vector3<T> xyz; T w; };
  65.         // Swizzle; Middle two values of the vector
  66.         struct { T x; Vector2<T> yz; T w; };
  67.         // Swizzle; Upper or Lower two values of the vector
  68.         struct { Vector2<T> xy; Vector2<T> zw; };
  69.         // A single component of the vector
  70.         struct { T x, y, z, w; };
  71.     };
  72.  
  73.     //--------------------------------------------------------------------------------
  74.     // Constructor; Empty intialization
  75.     explicit Vector4() {}
  76.     // Constructor; 2D Vector promotion with explicite z and w
  77.     explicit Vector4(Vector2<T> &v, T z, T w) : x(v.x), y(v.y), z(z), w(w) {}
  78.     // Constructor; 2D Vector promotion with explicite x and w
  79.     explicit Vector4(T x, Vector2<T> &v, T w) : x(x), y(v.x), z(v.y), w(w) {}
  80.     // Constructor; 2D Vector promotion with explicite x and y
  81.     explicit Vector4(T x, T y, Vector2<T> &v) : x(x), y(y), z(v.x), w(v.y) {}
  82.     // Constructor; Two 2D Vectors merging
  83.     explicit Vector4(Vector2<T> &v1, Vector2<T> &v2) : x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
  84.     // Constructor; 3D Vector promotion with explicite w
  85.     explicit Vector4(Vector3<T> &v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
  86.     // Constructor; 3D Vector promotion with explicite x
  87.     explicit Vector4(T x, Vector3<T> &v) : x(x), y(v.x), z(v.y), w(v.z) {}
  88.     // Constructor; Directly initialize each value
  89.     explicit Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
  90.     // Constructor; Initialize values from an array
  91.     explicit Vector4(In(4) const T *array) : x(array[0]), y(array[1]), z(array[2]), w(array[3]) {}
  92.  
  93.     // Vector assignment
  94.     Vector4<T>& operator = (const Vector4<T> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
  95.  
  96.     // Vector conversion to 32-bit floating-point components
  97.     operator Vector4<Float>() const { return Vector4<Float>(x, y, z, w); };
  98. };
  99.  
  100. //-------------------------------------------------------------------------------------
  101. // 2D Vector; 8-bit signed integer components
  102. typedef Vector2<Byte> Byte2;
  103. // 3D Vector; 8-bit signed integer components
  104. typedef Vector3<Byte> Byte3;
  105. // 4D Vector; 8-bit signed integer components
  106. typedef Vector4<Byte> Byte4;
  107. //-------------------------------------------------------------------------------------
  108. // 2D Vector; 8-bit unsigned integer components
  109. typedef Vector2<UByte> UByte2;
  110. // 3D Vector; 8-bit unsigned integer components
  111. typedef Vector3<UByte> UByte3;
  112. // 4D Vector; 8-bit unsigned integer components
  113. typedef Vector4<UByte> UByte4;
  114.  
  115. //-------------------------------------------------------------------------------------
  116. // 2D Vector; 16-bit signed integer components
  117. typedef Vector2<Short> Short2;
  118. // 3D Vector; 16-bit signed integer components
  119. typedef Vector3<Short> Short3;
  120. // 4D Vector; 16-bit signed integer components
  121. typedef Vector4<Short> Short4;
  122. //-------------------------------------------------------------------------------------
  123. // 2D Vector; 16-bit unsigned integer components
  124. typedef Vector2<UShort> UShort2;
  125. // 3D Vector; 16-bit unsigned integer components
  126. typedef Vector3<UShort> UShort3;
  127. // 4D Vector; 16-bit unsigned integer components
  128. typedef Vector4<UShort> UShort4;
  129.  
  130. //-------------------------------------------------------------------------------------
  131. // 2D Vector; 32-bit signed integer components
  132. typedef Vector2<Int> Int2;
  133. // 3D Vector; 32-bit signed integer components
  134. typedef Vector3<Int> Int3;
  135. // 4D Vector; 32-bit signed integer components
  136. typedef Vector4<Int> Int4;
  137. //-------------------------------------------------------------------------------------
  138. // 2D Vector; 32-bit unsigned integer components
  139. typedef Vector2<UInt> UInt2;
  140. // 3D Vector; 32-bit unsigned integer components
  141. typedef Vector3<UInt> UInt3;
  142. // 4D Vector; 32-bit unsigned integer components
  143. typedef Vector4<UInt> UInt4;
  144.  
  145. //-------------------------------------------------------------------------------------
  146. // 2D Vector; 16-bit floating-point components
  147. typedef Vector2<Half> Half2;
  148. // 3D Vector; 16-bit floating-point components
  149. typedef Vector3<Half> Half3;
  150. // 4D Vector; 16-bit floating-point components
  151. typedef Vector4<Half> Half4;
  152. //-------------------------------------------------------------------------------------
  153. // 2D Vector; 32-bit floating-point components
  154. typedef Vector2<Float> Float2;
  155. // 3D Vector; 32-bit floating-point components
  156. typedef Vector3<Float> Float3;
  157. // 4D Vector; 32-bit floating-point components
  158. typedef Vector4<Float> Float4;
  159. //-------------------------------------------------------------------------------------
  160. // 2D Vector; 32-bit floating-point components aligned to a 16-byte boundary
  161. typedef Align(16) Vector2<Float> Float2A;
  162. // 3D Vector; 32-bit floating-point components aligned to a 16-byte boundary
  163. typedef Align(16) Vector3<Float> Float3A;
  164. // 4D Vector; 32-bit floating-point components aligned to a 16-byte boundary
  165. typedef Align(16) Vector4<Float> Float4A;
Advertisement
Add Comment
Please, Sign In to add comment