Advertisement
Ember

Vectors

Jan 14th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.90 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.         // This is used to self-reference the entire vector as a member, for inheritance
  117.         //struct { Data<T> data; };
  118.         // A single component of the vector
  119.         struct { T x, y, z, w; };
  120.         // Swizzle; Upper three values of the vector
  121.         struct { Vector3<T> xyz; T w; };
  122.         // Swizzle; Lower three values of the vector
  123.         struct { T x; Vector3<T>ywz; };
  124.         // Swizzle; Upper or Lower two values of the vector
  125.         struct { Vector2<T> xy; Vector2<T> zw; };
  126.         // Swizzle; Middle two values of the vector
  127.         struct { T x; Vector2<T> yz; T w; };
  128.         // An array of components
  129.         struct { T m[4]; };
  130.     };
  131.  
  132.     //--------------------------------------------------------------------------------
  133.     // Constructor; Empty intialization
  134.     explicit Vector4() {}
  135.     // Constructor; 2D Vector promotion with explicit z and w
  136.     explicit Vector4(const Vector2<T> &v, T z, T w) : x(v.x), y(v.y), z(z), w(w) {}
  137.     // Constructor; 2D Vector promotion with explicit x and w
  138.     explicit Vector4(T x, const Vector2<T> &v, T w) : x(x), y(v.x), z(v.y), w(w) {}
  139.     // Constructor; 2D Vector promotion with explicit x and y
  140.     explicit Vector4(T x, T y, const Vector2<T> &v) : x(x), y(y), z(v.x), w(v.y) {}
  141.     // Constructor; Two 2D Vectors merging
  142.     explicit Vector4(const Vector2<T> &v1, const Vector2<T> &v2) : x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
  143.     // Constructor; 3D Vector promotion with explicit w
  144.     explicit Vector4(const Vector3<T> &v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
  145.     // Constructor; 3D Vector promotion with explicit x
  146.     explicit Vector4(T x, const Vector3<T> &v) : x(x), y(v.x), z(v.y), w(v.z) {}
  147.     // Constructor; Directly initialize each value
  148.     explicit Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
  149.     // Constructor; Initialize values from an array
  150.     explicit Vector4(const T *array) : x(array[0]), y(array[1]), z(array[2]), w(array[3]) {}
  151.     //Vector4(const Data<T>& data) : data(data) {}
  152.  
  153.     // Vector assignment
  154.     template <typename O> Vector4<T>& operator = (const Vector4<O> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
  155.  
  156.     // Assignment operators
  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 Vector4<T>& other) { x -= other.x; y -= other.y; z -= other.z; w -= other.w; return *this; }
  160.     Vector4<T>& operator *= (const Vector4<T>& other) { x *= other.x; y *= other.y; z *= other.z; w *= other.w; return *this; }
  161.     Vector4<T>& operator /= (const Vector4<T>& other) { x /= other.x; y /= other.y; z /= other.z; w /= other.w; return *this; }
  162.     Vector4<T>& operator *= (const Float& other) { x *= other; y *= other; z *= other; w *= other; return *this; }
  163.     Vector4<T>& operator /= (const Float& other) { x /= other; y /= other; z /= other; w /= other; return *this; }
  164.  
  165.     // Unary operators
  166.     Vector4<T> operator + () { return *this; }
  167.     Vector4<T> operator - () { return Vector4<T>(-x, -y, -z, -w); }
  168.  
  169.     // Binary operators
  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 Vector4<T>& other) { return Vector4<T>(x - other.x, y - other.y, z - other.z, w - other.w); }
  172.     Vector4<T> operator * (const Vector4<T>& other) { return Vector4<T>(x * other.x, y * other.y, z * other.z, w * other.w); }
  173.     Vector4<T> operator / (const Vector4<T>& other) { return Vector4<T>(x / other.x, y / other.y, z / other.z, w / other.w); }
  174.     Vector4<T> operator * (const Float& other) { return Vector4<T>(x * other, y * other, z * other, w * other); }
  175.     Vector4<T> operator / (const Float& other) { return Vector4<T>(x / other, y / other, z / other, w / other); }
  176.  
  177.     // Boolean operators
  178.     bool operator == (const Vector4<T>& other) { return x == other.x && y == other.y && z == other.z && w == other.w; }
  179.     bool operator != (const Vector4<T>& other) { return x != other.x && y != other.y && z != other.z && w != other.w; }
  180. };
  181.  
  182. //-------------------------------------------------------------------------------------
  183. // 2D Vector; Templated components aligned to a 16-byte boundary
  184. //template <typename T> struct Align(16) Vector2A : Vector2<T> {};
  185. // 3D Vector; Templated components aligned to a 16-byte boundary
  186. //template <typename T> struct Align(16) Vector3A : Vector3<T> {};
  187. // 4D Vector; Templated components aligned to a 16-byte boundary
  188. //template <typename T> struct Align(16) Vector4A : Vector4<T> {};
  189.  
  190. //-------------------------------------------------------------------------------------
  191. // Vectors
  192. //-------------------------------------------------------------------------------------
  193.  
  194. //-------------------------------------------------------------------------------------
  195. // 2D Vector; 8-bit signed integer components
  196. typedef Vector2<Byte> Byte2;
  197. // 3D Vector; 8-bit signed integer components
  198. typedef Vector3<Byte> Byte3;
  199. // 4D Vector; 8-bit signed integer components
  200. typedef Vector4<Byte> Byte4;
  201. //-------------------------------------------------------------------------------------
  202. // 2D Vector; 8-bit unsigned integer components
  203. typedef Vector2<UByte> UByte2;
  204. // 3D Vector; 8-bit unsigned integer components
  205. typedef Vector3<UByte> UByte3;
  206. // 4D Vector; 8-bit unsigned integer components
  207. typedef Vector4<UByte> UByte4;
  208.  
  209. //-------------------------------------------------------------------------------------
  210. // 2D Vector; 16-bit signed integer components
  211. typedef Vector2<Short> Short2;
  212. // 3D Vector; 16-bit signed integer components
  213. typedef Vector3<Short> Short3;
  214. // 4D Vector; 16-bit signed integer components
  215. typedef Vector4<Short> Short4;
  216. //-------------------------------------------------------------------------------------
  217. // 2D Vector; 16-bit unsigned integer components
  218. typedef Vector2<UShort> UShort2;
  219. // 3D Vector; 16-bit unsigned integer components
  220. typedef Vector3<UShort> UShort3;
  221. // 4D Vector; 16-bit unsigned integer components
  222. typedef Vector4<UShort> UShort4;
  223.  
  224. //-------------------------------------------------------------------------------------
  225. // 2D Vector; 32-bit signed integer components
  226. typedef Vector2<Int> Int2;
  227. // 3D Vector; 32-bit signed integer components
  228. typedef Vector3<Int> Int3;
  229. // 4D Vector; 32-bit signed integer components
  230. typedef Vector4<Int> Int4;
  231. //-------------------------------------------------------------------------------------
  232. // 2D Vector; 32-bit unsigned integer components
  233. typedef Vector2<UInt> UInt2;
  234. // 3D Vector; 32-bit unsigned integer components
  235. typedef Vector3<UInt> UInt3;
  236. // 4D Vector; 32-bit unsigned integer components
  237. typedef Vector4<UInt> UInt4;
  238.  
  239. //-------------------------------------------------------------------------------------
  240. // 2D Vector; 16-bit floating-point components
  241. typedef Vector2<Half> Half2;
  242. // 3D Vector; 16-bit floating-point components
  243. typedef Vector3<Half> Half3;
  244. // 4D Vector; 16-bit floating-point components
  245. typedef Vector4<Half> Half4;
  246. //-------------------------------------------------------------------------------------
  247. // 2D Vector; 32-bit floating-point components
  248. typedef Vector2<Float> Float2;
  249. // 3D Vector; 32-bit floating-point components
  250. typedef Vector3<Float> Float3;
  251. // 4D Vector; 32-bit floating-point components aligned to a 16-byte boundary
  252. typedef Align(16) struct Vector4<Float> Float4;
  253.  
  254. //-------------------------------------------------------------------------------------
  255. // Matrices
  256. //-------------------------------------------------------------------------------------
  257.  
  258. //-------------------------------------------------------------------------------------
  259. // 2x2 Matrix; Two 2D 32-bit floating point vectors aligned to a 16-byte boundary
  260. typedef Align(16) struct Vector2<Float2> Float2x2;
  261. // 2x3 Matrix; Three 2D 32-bit floating point vectors aligned to a 16-byte boundary
  262. typedef Align(16) struct Vector3<Float2> Float2x3;
  263. // 2x4 Matrix; Four 2D 32-bit floating point vectors aligned to a 16-byte boundary
  264. typedef Align(16) struct Vector4<Float2> Float2x4;
  265.  
  266. //-------------------------------------------------------------------------------------
  267. // 3x2 Matrix; Two 3D 32-bit floating point vectors aligned to a 16-byte boundary
  268. typedef Align(16) struct Vector2<Float3> Float3x2;
  269. // 3x3 Matrix; Three 3D 32-bit floating point vectors aligned to a 16-byte boundary
  270. typedef Align(16) struct Vector3<Float3> Float3x3;
  271. // 3x4 Matrix; Three 3D 32-bit floating point vectors aligned to a 16-byte boundary
  272. typedef Align(16) struct Vector4<Float3> Float3x4;
  273.  
  274. //-------------------------------------------------------------------------------------
  275. // 4x2 Matrix; Two 4D 32-bit floating point vectors aligned to a 16-byte boundary
  276. typedef Align(16) struct Vector2<Float4> Float4x2;
  277. // 4x3 Matrix; Three 4D 32-bit floating point vectors aligned to a 16-byte boundary
  278. typedef Align(16) struct Vector3<Float4> Float4x3;
  279. // 4x4 Matrix; Four 4D 32-bit floating point vectors aligned to a 16-byte boundary
  280. typedef Align(16) struct Vector4<Float4> Float4x4;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement