Advertisement
Ember

Vectors

Nov 14th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.67 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 vector
  9.         struct { T x, y; };
  10.         // An array of components
  11.         struct { T m[2]; };
  12.     };
  13.  
  14.     // Constructor; Empty initalization
  15.     explicit Vector2() {}
  16.     // Constructor; Directly initialize each value
  17.     explicit Vector2(T x, T y) : x(x), y(y) {}
  18.     // Constructor; Initialize values from an array
  19.     explicit Vector2(const T* array) : x(array[0]), y(array[1]) {}
  20.  
  21.     // Vector assignment
  22.     template <typename O> Vector2<T>& operator = (const Vector2<O> &other) { x = other.x; y = other.y; return *this; }
  23.  
  24.     // Assignment operators
  25.     Vector2<T>& operator = (const Vector2<T>& other) { x = other.x; y = other.y; return *this; }
  26.     Vector2<T>& operator += (const Vector2<T>& other) { x += other.x; y += other.y; return *this; }
  27.     Vector2<T>& operator -= (const Vector2<T>& other) { x -= other.x; y -= other.y; return *this; }
  28.     Vector2<T>& operator *= (const Vector2<T>& other) { x *= other.x; y *= other.y; return *this; }
  29.     Vector2<T>& operator /= (const Vector2<T>& other) { x /= other.x; y /= other.y; return *this; }
  30.     Vector2<T>& operator *= (const Float& other) { x *= other; y *= other; return *this; }
  31.     Vector2<T>& operator /= (const Float& other) { x /= other; y /= other; return *this; }
  32.  
  33.     // Unary operators
  34.     Vector2<T> operator + () { return *this; }
  35.     Vector2<T> operator - () { return Vector2<T>(-x, -y); }
  36.  
  37.     // Binary operators
  38.     Vector2<T> operator + (const Vector2<T>& other) { return Vector2<T>(x + other.x, y + other.y); }
  39.     Vector2<T> operator - (const Vector2<T>& other) { return Vector2<T>(x - other.x, y - other.y); }
  40.     Vector2<T> operator * (const Vector2<T>& other) { return Vector2<T>(x * other.x, y * other.y); }
  41.     Vector2<T> operator / (const Vector2<T>& other) { return Vector2<T>(x / other.x, y / other.y); }
  42.     Vector2<T> operator * (const Float& other) { return Vector2<T>(x * other, y * other); }
  43.     Vector2<T> operator / (const Float& other) { return Vector2<T>(x / other, y / other); }
  44.  
  45.     // Boolean operators
  46.     bool operator == (const Vector2<T>& other) { return x == other.x && y == other.y; }
  47.     bool operator != (const Vector2<T>& other) { return x != other.x && y != other.y; }
  48.  
  49. };
  50.  
  51. //-------------------------------------------------------------------------------------
  52. // 3D Vector; Templated components
  53. template <typename T> struct Vector3
  54. {
  55.     // Members
  56.     union
  57.     {
  58.         // Swizzle; Lower two values of the vector
  59.         struct { T x; Vector2<T> yz; };
  60.         // Swizzle; Upper two values of the vector
  61.         struct { Vector2<T> xy; T z; };
  62.         // A single component of the vector
  63.         struct { T x, y, z; };
  64.         // An array of components
  65.         struct { T m[3]; };
  66.     };
  67.  
  68.     //--------------------------------------------------------------------------------
  69.     // Constructor; Empty initialization
  70.     explicit Vector3() {}
  71.     // Constructor; 2D Vector promotion with explicit z
  72.     explicit Vector3(Vector2<T> &v, T z) : x(v.x), y(v.y), z(z) {}
  73.     // Constructor; 2D Vector promotion with explicit x
  74.     explicit Vector3(T x, Vector2<T> &v) : x(x), y(v.x), z(v.y) {}
  75.     // Constructor; Directly initialize each value
  76.     explicit Vector3(T x, T y, T z) : x(x), y(y), z(z) {}
  77.     // Constructor; Initialize values from an array
  78.     explicit Vector3(const T *array) : x(array[0]), y(array[1]), z(array[2]) {}
  79.  
  80.     // Vector assignment
  81.     template <typename O> Vector3<T>& operator = (const Vector3<O> &other) { x = other.x; y = other.y; z = other.z; return *this; }
  82.  
  83.     // Assignment operators
  84.     Vector3<T>& operator = (const Vector3<T>& other) { x = other.x; y = other.y; z = other.z; return *this; }
  85.     Vector3<T>& operator += (const Vector3<T>& other) { x += other.x; y += other.y; z += other.z; return *this; }
  86.     Vector3<T>& operator -= (const Vector3<T>& other) { x -= other.x; y -= other.y; z -= other.z; return *this; }
  87.     Vector3<T>& operator *= (const Vector3<T>& other) { x *= other.x; y *= other.y; z *= other.z; return *this; }
  88.     Vector3<T>& operator /= (const Vector3<T>& other) { x /= other.x; y /= other.y; z /= other.z; return *this; }
  89.     Vector3<T>& operator *= (const Float& other) { x *= other; y *= other; z *= other; return *this; }
  90.     Vector3<T>& operator /= (const Float& other) { x /= other; y /= other; z /= other; return *this; }
  91.  
  92.     // Unary operators
  93.     Vector3<T> operator + () { return *this; }
  94.     Vector3<T> operator - () { return Vector3<T>(-x, -y, -z); }
  95.  
  96.     // Binary operators
  97.     Vector3<T> operator + (const Vector3<T>& other) { return Vector3<T>(x + other.x, y + other.y, z + other.z); }
  98.     Vector3<T> operator - (const Vector3<T>& other) { return Vector3<T>(x - other.x, y - other.y, z - other.z); }
  99.     Vector3<T> operator * (const Vector3<T>& other) { return Vector3<T>(x * other.x, y * other.y, z * other.z); }
  100.     Vector3<T> operator / (const Vector3<T>& other) { return Vector3<T>(x / other.x, y / other.y, z / other.z); }
  101.     Vector3<T> operator * (const Float& other) { return Vector3<T>(x * other, y * other, z * other); }
  102.     Vector3<T> operator / (const Float& other) { return Vector3<T>(x / other, y / other, z / other); }
  103.  
  104.     // Boolean operators
  105.     bool operator == (const Vector3<T>& other) { return x == other.x && y == other.y && z == other.z; }
  106.     bool operator != (const Vector3<T>& other) { return x != other.x && y != other.y && z != other.z; }
  107. };
  108.  
  109. //-------------------------------------------------------------------------------------
  110. // 4D Vector; Templated components
  111. template <typename T> struct Vector4
  112. {
  113.     // Members
  114.     union
  115.     {
  116.         // Swizzle; Lower three values of the vector
  117.         struct { T x; Vector3<T>ywz; };
  118.         // Swizzle; Upper three values of the vector
  119.         struct { Vector3<T> xyz; T w; };
  120.         // Swizzle; Middle two values of the vector
  121.         struct { T x; Vector2<T> yz; T w; };
  122.         // Swizzle; Upper or Lower two values of the vector
  123.         struct { Vector2<T> xy; Vector2<T> zw; };
  124.         // A single component of the vector
  125.         struct { T x, y, z, w; };
  126.         // An array of components
  127.         struct { T m[4]; };
  128.     };
  129.  
  130.     //--------------------------------------------------------------------------------
  131.     // Constructor; Empty intialization
  132.     explicit Vector4() {}
  133.     // Constructor; 2D Vector promotion with explicit z and w
  134.     explicit Vector4(const Vector2<T> &v, T z, T w) : x(v.x), y(v.y), z(z), w(w) {}
  135.     // Constructor; 2D Vector promotion with explicit x and w
  136.     explicit Vector4(T x, const Vector2<T> &v, T w) : x(x), y(v.x), z(v.y), w(w) {}
  137.     // Constructor; 2D Vector promotion with explicit x and y
  138.     explicit Vector4(T x, T y, const Vector2<T> &v) : x(x), y(y), z(v.x), w(v.y) {}
  139.     // Constructor; Two 2D Vectors merging
  140.     explicit Vector4(const Vector2<T> &v1, const Vector2<T> &v2) : x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
  141.     // Constructor; 3D Vector promotion with explicit w
  142.     explicit Vector4(const Vector3<T> &v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
  143.     // Constructor; 3D Vector promotion with explicit x
  144.     explicit Vector4(T x, const Vector3<T> &v) : x(x), y(v.x), z(v.y), w(v.z) {}
  145.     // Constructor; Directly initialize each value
  146.     explicit Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
  147.     // Constructor; Initialize values from an array
  148.     explicit Vector4(const T *array) : x(array[0]), y(array[1]), z(array[2]), w(array[3]) {}
  149.  
  150.     // Vector assignment
  151.     template <typename O> Vector4<T>& operator = (const Vector4<O> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
  152.  
  153.     // Assignment operators
  154.     Vector4<T>& operator = (const Vector4<T>& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
  155.     Vector4<T>& operator += (const Vector4<T>& other) { x += other.x; y += other.y; z += other.z; w += other.w; return *this; }
  156.     Vector4<T>& operator -= (const Vector4<T>& other) { x -= other.x; y -= other.y; z -= other.z; w -= other.w; return *this; }
  157.     Vector4<T>& operator *= (const Vector4<T>& other) { x *= other.x; y *= other.y; z *= other.z; w *= other.w; return *this; }
  158.     Vector4<T>& operator /= (const Vector4<T>& other) { x /= other.x; y /= other.y; z /= other.z; w /= other.w; return *this; }
  159.     Vector4<T>& operator *= (const Float& other) { x *= other; y *= other; z *= other; w *= other; return *this; }
  160.     Vector4<T>& operator /= (const Float& other) { x /= other; y /= other; z /= other; w /= other; return *this; }
  161.  
  162.     // Unary operators
  163.     Vector4<T> operator + () { return *this; }
  164.     Vector4<T> operator - () { return Vector4<T>(-x, -y, -z, -w); }
  165.  
  166.     // Binary operators
  167.     Vector4<T> operator + (const Vector4<T>& other) { return Vector4<T>(x + other.x, y + other.y, z + other.z, w + other.w); }
  168.     Vector4<T> operator - (const Vector4<T>& other) { return Vector4<T>(x - other.x, y - other.y, z - other.z, w - other.w); }
  169.     Vector4<T> operator * (const Vector4<T>& other) { return Vector4<T>(x * other.x, y * other.y, z * other.z, w * other.w); }
  170.     Vector4<T> operator / (const Vector4<T>& other) { return Vector4<T>(x / other.x, y / other.y, z / other.z, w / other.w); }
  171.     Vector4<T> operator * (const Float& other) { return Vector4<T>(x  * other, y * other, z * other, w * other); }
  172.     Vector4<T> operator / (const Float& other) { return Vector4<T>(x  / other, y / other, z / other, w / other); }
  173.  
  174.     // Boolean operators
  175.     bool operator == (const Vector4<T>& other) { return x == other.x && y == other.y && z == other.z && w == other.w; }
  176.     bool operator != (const Vector4<T>& other) { return x != other.x && y != other.y && z != other.z && w != other.w; }
  177. };
  178.  
  179. //-------------------------------------------------------------------------------------
  180. // 2D Vector; Templated components aligned to a 16-byte boundary
  181. template <typename T> struct Align(16) Vector2A : Vector2<T> {};
  182. // 3D Vector; Templated components aligned to a 16-byte boundary
  183. template <typename T> struct Align(16) Vector3A : Vector3<T> {};
  184. // 4D Vector; Templated components aligned to a 16-byte boundary
  185. template <typename T> struct Align(16) Vector4A : Vector4<T> {};
  186.  
  187. //-------------------------------------------------------------------------------------
  188. // Vectors
  189. //-------------------------------------------------------------------------------------
  190.  
  191. //-------------------------------------------------------------------------------------
  192. // 2D Vector; 8-bit signed integer components
  193. typedef Vector2<Byte> Byte2;
  194. // 3D Vector; 8-bit signed integer components
  195. typedef Vector3<Byte> Byte3;
  196. // 4D Vector; 8-bit signed integer components
  197. typedef Vector4<Byte> Byte4;
  198. //-------------------------------------------------------------------------------------
  199. // 2D Vector; 8-bit unsigned integer components
  200. typedef Vector2<UByte> UByte2;
  201. // 3D Vector; 8-bit unsigned integer components
  202. typedef Vector3<UByte> UByte3;
  203. // 4D Vector; 8-bit unsigned integer components
  204. typedef Vector4<UByte> UByte4;
  205.  
  206. //-------------------------------------------------------------------------------------
  207. // 2D Vector; 16-bit signed integer components
  208. typedef Vector2<Short> Short2;
  209. // 3D Vector; 16-bit signed integer components
  210. typedef Vector3<Short> Short3;
  211. // 4D Vector; 16-bit signed integer components
  212. typedef Vector4<Short> Short4;
  213. //-------------------------------------------------------------------------------------
  214. // 2D Vector; 16-bit unsigned integer components
  215. typedef Vector2<UShort> UShort2;
  216. // 3D Vector; 16-bit unsigned integer components
  217. typedef Vector3<UShort> UShort3;
  218. // 4D Vector; 16-bit unsigned integer components
  219. typedef Vector4<UShort> UShort4;
  220.  
  221. //-------------------------------------------------------------------------------------
  222. // 2D Vector; 32-bit signed integer components
  223. typedef Vector2<Int> Int2;
  224. // 3D Vector; 32-bit signed integer components
  225. typedef Vector3<Int> Int3;
  226. // 4D Vector; 32-bit signed integer components
  227. typedef Vector4<Int> Int4;
  228. //-------------------------------------------------------------------------------------
  229. // 2D Vector; 32-bit unsigned integer components
  230. typedef Vector2<UInt> UInt2;
  231. // 3D Vector; 32-bit unsigned integer components
  232. typedef Vector3<UInt> UInt3;
  233. // 4D Vector; 32-bit unsigned integer components
  234. typedef Vector4<UInt> UInt4;
  235.  
  236. //-------------------------------------------------------------------------------------
  237. // 2D Vector; 16-bit floating-point components
  238. typedef Vector2<Half> Half2;
  239. // 3D Vector; 16-bit floating-point components
  240. typedef Vector3<Half> Half3;
  241. // 4D Vector; 16-bit floating-point components
  242. typedef Vector4<Half> Half4;
  243. //-------------------------------------------------------------------------------------
  244. // 2D Vector; 32-bit floating-point components
  245. typedef Vector2<Float> Float2;
  246. // 3D Vector; 32-bit floating-point components
  247. typedef Vector3<Float> Float3;
  248. // 4D Vector; 32-bit floating-point components
  249. typedef Vector4<Float> Float4;
  250. //-------------------------------------------------------------------------------------
  251. // 2D Vector; 32-bit floating-point components aligned to a 16-byte boundary
  252. typedef Vector2A<Float> Float2A;
  253. // 3D Vector; 32-bit floating-point components aligned to a 16-byte boundary
  254. typedef Vector3A<Float> Float3A;
  255. // 4D Vector; 32-bit floating-point components aligned to a 16-byte boundary
  256. typedef Vector4A<Float> Float4A;
  257.  
  258. //-------------------------------------------------------------------------------------
  259. // Matrices
  260. //-------------------------------------------------------------------------------------
  261.  
  262. //-------------------------------------------------------------------------------------
  263. // 2x2 Matrix; Two 2D 32-bit floating point vectors
  264. typedef Vector2<Float2> Float2x2;
  265. // 2x3 Matrix; Two 3D 32-bit floating point vectors
  266. typedef Vector2<Float3> Float2x3;
  267. // 2x4 Matrix; Two 4D 32-bit floating point vectors
  268. typedef Vector2<Float4> Float2x4;
  269.  
  270. //-------------------------------------------------------------------------------------
  271. // 3x2 Matrix; Three 2D 32-bit floating point vectors
  272. typedef Vector3<Float2> Float3x2;
  273. // 3x3 Matrix; Three 3D 32-bit floating point vectors
  274. typedef Vector3<Float3> Float3x3;
  275. // 3x4 Matrix; Three 4D 32-bit floating point vectors
  276. typedef Vector3<Float4> Float3x4;
  277.  
  278. //-------------------------------------------------------------------------------------
  279. // 4x2 Matrix; Four 2D 32-bit floating point vectors
  280. typedef Vector4<Float2> Float4x2;
  281. // 4x3 Matrix; Four 3D 32-bit floating point vectors
  282. typedef Vector4<Float3> Float4x3;
  283. // 4x4 Matrix; Four 4D 32-bit floating point vectors
  284. typedef Vector4<Float4> Float4x4;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement