Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------
- // 2D Vector; Templated components
- template <typename T> struct Vector2
- {
- // Members
- union
- {
- // A single component of the vector
- struct { T x, y; };
- // An array of components
- struct { T m[2]; };
- };
- // Constructor; Empty initalization
- explicit Vector2() {}
- // Constructor; Directly initialize each value
- explicit Vector2(T x, T y) : x(x), y(y) {}
- // Constructor; Initialize values from an array
- explicit Vector2(const T* array) : x(array[0]), y(array[1]) {}
- // Vector assignment
- template <typename O> Vector2<T>& operator = (const Vector2<O> &other) { x = other.x; y = other.y; return *this; }
- // Assignment operators
- Vector2<T>& operator = (const Vector2<T>& other) { x = other.x; y = other.y; return *this; }
- Vector2<T>& operator += (const Vector2<T>& other) { x += other.x; y += other.y; return *this; }
- Vector2<T>& operator -= (const Vector2<T>& other) { x -= other.x; y -= other.y; return *this; }
- Vector2<T>& operator *= (const Vector2<T>& other) { x *= other.x; y *= other.y; return *this; }
- Vector2<T>& operator /= (const Vector2<T>& other) { x /= other.x; y /= other.y; return *this; }
- Vector2<T>& operator *= (const Float& other) { x *= other; y *= other; return *this; }
- Vector2<T>& operator /= (const Float& other) { x /= other; y /= other; return *this; }
- // Unary operators
- Vector2<T> operator + () { return *this; }
- Vector2<T> operator - () { return Vector2<T>(-x, -y); }
- // Binary operators
- Vector2<T> operator + (const Vector2<T>& other) { return Vector2<T>(x + other.x, y + other.y); }
- Vector2<T> operator - (const Vector2<T>& other) { return Vector2<T>(x - other.x, y - other.y); }
- Vector2<T> operator * (const Vector2<T>& other) { return Vector2<T>(x * other.x, y * other.y); }
- Vector2<T> operator / (const Vector2<T>& other) { return Vector2<T>(x / other.x, y / other.y); }
- Vector2<T> operator * (const Float& other) { return Vector2<T>(x * other, y * other); }
- Vector2<T> operator / (const Float& other) { return Vector2<T>(x / other, y / other); }
- // Boolean operators
- bool operator == (const Vector2<T>& other) { return x == other.x && y == other.y; }
- bool operator != (const Vector2<T>& other) { return x != other.x && y != other.y; }
- };
- //-------------------------------------------------------------------------------------
- // 3D Vector; Templated components
- template <typename T> struct Vector3
- {
- // Members
- union
- {
- // Swizzle; Lower two values of the vector
- struct { T x; Vector2<T> yz; };
- // Swizzle; Upper two values of the vector
- struct { Vector2<T> xy; T z; };
- // A single component of the vector
- struct { T x, y, z; };
- // An array of components
- struct { T m[3]; };
- };
- //--------------------------------------------------------------------------------
- // Constructor; Empty initialization
- explicit Vector3() {}
- // Constructor; 2D Vector promotion with explicit z
- explicit Vector3(Vector2<T> &v, T z) : x(v.x), y(v.y), z(z) {}
- // Constructor; 2D Vector promotion with explicit x
- explicit Vector3(T x, Vector2<T> &v) : x(x), y(v.x), z(v.y) {}
- // Constructor; Directly initialize each value
- explicit Vector3(T x, T y, T z) : x(x), y(y), z(z) {}
- // Constructor; Initialize values from an array
- explicit Vector3(const T *array) : x(array[0]), y(array[1]), z(array[2]) {}
- // Vector assignment
- template <typename O> Vector3<T>& operator = (const Vector3<O> &other) { x = other.x; y = other.y; z = other.z; return *this; }
- // Assignment operators
- Vector3<T>& operator = (const Vector3<T>& other) { x = other.x; y = other.y; z = other.z; return *this; }
- Vector3<T>& operator += (const Vector3<T>& other) { x += other.x; y += other.y; z += other.z; return *this; }
- Vector3<T>& operator -= (const Vector3<T>& other) { x -= other.x; y -= other.y; z -= other.z; return *this; }
- Vector3<T>& operator *= (const Vector3<T>& other) { x *= other.x; y *= other.y; z *= other.z; return *this; }
- Vector3<T>& operator /= (const Vector3<T>& other) { x /= other.x; y /= other.y; z /= other.z; return *this; }
- Vector3<T>& operator *= (const Float& other) { x *= other; y *= other; z *= other; return *this; }
- Vector3<T>& operator /= (const Float& other) { x /= other; y /= other; z /= other; return *this; }
- // Unary operators
- Vector3<T> operator + () { return *this; }
- Vector3<T> operator - () { return Vector3<T>(-x, -y, -z); }
- // Binary operators
- Vector3<T> operator + (const Vector3<T>& other) { return Vector3<T>(x + other.x, y + other.y, z + other.z); }
- Vector3<T> operator - (const Vector3<T>& other) { return Vector3<T>(x - other.x, y - other.y, z - other.z); }
- Vector3<T> operator * (const Vector3<T>& other) { return Vector3<T>(x * other.x, y * other.y, z * other.z); }
- Vector3<T> operator / (const Vector3<T>& other) { return Vector3<T>(x / other.x, y / other.y, z / other.z); }
- Vector3<T> operator * (const Float& other) { return Vector3<T>(x * other, y * other, z * other); }
- Vector3<T> operator / (const Float& other) { return Vector3<T>(x / other, y / other, z / other); }
- // Boolean operators
- bool operator == (const Vector3<T>& other) { return x == other.x && y == other.y && z == other.z; }
- bool operator != (const Vector3<T>& other) { return x != other.x && y != other.y && z != other.z; }
- };
- //-------------------------------------------------------------------------------------
- // 4D Vector; Templated components
- template <typename T> struct Vector4
- {
- // Members
- union
- {
- // Swizzle; Lower three values of the vector
- struct { T x; Vector3<T>ywz; };
- // Swizzle; Upper three values of the vector
- struct { Vector3<T> xyz; T w; };
- // Swizzle; Middle two values of the vector
- struct { T x; Vector2<T> yz; T w; };
- // Swizzle; Upper or Lower two values of the vector
- struct { Vector2<T> xy; Vector2<T> zw; };
- // A single component of the vector
- struct { T x, y, z, w; };
- // An array of components
- struct { T m[4]; };
- };
- //--------------------------------------------------------------------------------
- // Constructor; Empty intialization
- explicit Vector4() {}
- // Constructor; 2D Vector promotion with explicit z and w
- explicit Vector4(const Vector2<T> &v, T z, T w) : x(v.x), y(v.y), z(z), w(w) {}
- // Constructor; 2D Vector promotion with explicit x and w
- explicit Vector4(T x, const Vector2<T> &v, T w) : x(x), y(v.x), z(v.y), w(w) {}
- // Constructor; 2D Vector promotion with explicit x and y
- explicit Vector4(T x, T y, const Vector2<T> &v) : x(x), y(y), z(v.x), w(v.y) {}
- // Constructor; Two 2D Vectors merging
- explicit Vector4(const Vector2<T> &v1, const Vector2<T> &v2) : x(v1.x), y(v1.y), z(v2.x), w(v2.y) {}
- // Constructor; 3D Vector promotion with explicit w
- explicit Vector4(const Vector3<T> &v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
- // Constructor; 3D Vector promotion with explicit x
- explicit Vector4(T x, const Vector3<T> &v) : x(x), y(v.x), z(v.y), w(v.z) {}
- // Constructor; Directly initialize each value
- explicit Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
- // Constructor; Initialize values from an array
- explicit Vector4(const T *array) : x(array[0]), y(array[1]), z(array[2]), w(array[3]) {}
- // Vector assignment
- template <typename O> Vector4<T>& operator = (const Vector4<O> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
- // Assignment operators
- Vector4<T>& operator = (const Vector4<T>& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
- Vector4<T>& operator += (const Vector4<T>& other) { x += other.x; y += other.y; z += other.z; w += other.w; return *this; }
- Vector4<T>& operator -= (const Vector4<T>& other) { x -= other.x; y -= other.y; z -= other.z; w -= other.w; return *this; }
- Vector4<T>& operator *= (const Vector4<T>& other) { x *= other.x; y *= other.y; z *= other.z; w *= other.w; return *this; }
- Vector4<T>& operator /= (const Vector4<T>& other) { x /= other.x; y /= other.y; z /= other.z; w /= other.w; return *this; }
- Vector4<T>& operator *= (const Float& other) { x *= other; y *= other; z *= other; w *= other; return *this; }
- Vector4<T>& operator /= (const Float& other) { x /= other; y /= other; z /= other; w /= other; return *this; }
- // Unary operators
- Vector4<T> operator + () { return *this; }
- Vector4<T> operator - () { return Vector4<T>(-x, -y, -z, -w); }
- // Binary operators
- Vector4<T> operator + (const Vector4<T>& other) { return Vector4<T>(x + other.x, y + other.y, z + other.z, w + other.w); }
- Vector4<T> operator - (const Vector4<T>& other) { return Vector4<T>(x - other.x, y - other.y, z - other.z, w - other.w); }
- Vector4<T> operator * (const Vector4<T>& other) { return Vector4<T>(x * other.x, y * other.y, z * other.z, w * other.w); }
- Vector4<T> operator / (const Vector4<T>& other) { return Vector4<T>(x / other.x, y / other.y, z / other.z, w / other.w); }
- Vector4<T> operator * (const Float& other) { return Vector4<T>(x * other, y * other, z * other, w * other); }
- Vector4<T> operator / (const Float& other) { return Vector4<T>(x / other, y / other, z / other, w / other); }
- // Boolean operators
- bool operator == (const Vector4<T>& other) { return x == other.x && y == other.y && z == other.z && w == other.w; }
- bool operator != (const Vector4<T>& other) { return x != other.x && y != other.y && z != other.z && w != other.w; }
- };
- //-------------------------------------------------------------------------------------
- // 2D Vector; Templated components aligned to a 16-byte boundary
- template <typename T> struct Align(16) Vector2A : Vector2<T> {};
- // 3D Vector; Templated components aligned to a 16-byte boundary
- template <typename T> struct Align(16) Vector3A : Vector3<T> {};
- // 4D Vector; Templated components aligned to a 16-byte boundary
- template <typename T> struct Align(16) Vector4A : Vector4<T> {};
- //-------------------------------------------------------------------------------------
- // Vectors
- //-------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------
- // 2D Vector; 8-bit signed integer components
- typedef Vector2<Byte> Byte2;
- // 3D Vector; 8-bit signed integer components
- typedef Vector3<Byte> Byte3;
- // 4D Vector; 8-bit signed integer components
- typedef Vector4<Byte> Byte4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 8-bit unsigned integer components
- typedef Vector2<UByte> UByte2;
- // 3D Vector; 8-bit unsigned integer components
- typedef Vector3<UByte> UByte3;
- // 4D Vector; 8-bit unsigned integer components
- typedef Vector4<UByte> UByte4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 16-bit signed integer components
- typedef Vector2<Short> Short2;
- // 3D Vector; 16-bit signed integer components
- typedef Vector3<Short> Short3;
- // 4D Vector; 16-bit signed integer components
- typedef Vector4<Short> Short4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 16-bit unsigned integer components
- typedef Vector2<UShort> UShort2;
- // 3D Vector; 16-bit unsigned integer components
- typedef Vector3<UShort> UShort3;
- // 4D Vector; 16-bit unsigned integer components
- typedef Vector4<UShort> UShort4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 32-bit signed integer components
- typedef Vector2<Int> Int2;
- // 3D Vector; 32-bit signed integer components
- typedef Vector3<Int> Int3;
- // 4D Vector; 32-bit signed integer components
- typedef Vector4<Int> Int4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 32-bit unsigned integer components
- typedef Vector2<UInt> UInt2;
- // 3D Vector; 32-bit unsigned integer components
- typedef Vector3<UInt> UInt3;
- // 4D Vector; 32-bit unsigned integer components
- typedef Vector4<UInt> UInt4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 16-bit floating-point components
- typedef Vector2<Half> Half2;
- // 3D Vector; 16-bit floating-point components
- typedef Vector3<Half> Half3;
- // 4D Vector; 16-bit floating-point components
- typedef Vector4<Half> Half4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 32-bit floating-point components
- typedef Vector2<Float> Float2;
- // 3D Vector; 32-bit floating-point components
- typedef Vector3<Float> Float3;
- // 4D Vector; 32-bit floating-point components
- typedef Vector4<Float> Float4;
- //-------------------------------------------------------------------------------------
- // 2D Vector; 32-bit floating-point components aligned to a 16-byte boundary
- typedef Vector2A<Float> Float2A;
- // 3D Vector; 32-bit floating-point components aligned to a 16-byte boundary
- typedef Vector3A<Float> Float3A;
- // 4D Vector; 32-bit floating-point components aligned to a 16-byte boundary
- typedef Vector4A<Float> Float4A;
- //-------------------------------------------------------------------------------------
- // Matrices
- //-------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------
- // 2x2 Matrix; Two 2D 32-bit floating point vectors
- typedef Vector2<Float2> Float2x2;
- // 2x3 Matrix; Two 3D 32-bit floating point vectors
- typedef Vector2<Float3> Float2x3;
- // 2x4 Matrix; Two 4D 32-bit floating point vectors
- typedef Vector2<Float4> Float2x4;
- //-------------------------------------------------------------------------------------
- // 3x2 Matrix; Three 2D 32-bit floating point vectors
- typedef Vector3<Float2> Float3x2;
- // 3x3 Matrix; Three 3D 32-bit floating point vectors
- typedef Vector3<Float3> Float3x3;
- // 3x4 Matrix; Three 4D 32-bit floating point vectors
- typedef Vector3<Float4> Float3x4;
- //-------------------------------------------------------------------------------------
- // 4x2 Matrix; Four 2D 32-bit floating point vectors
- typedef Vector4<Float2> Float4x2;
- // 4x3 Matrix; Four 3D 32-bit floating point vectors
- typedef Vector4<Float3> Float4x3;
- // 4x4 Matrix; Four 4D 32-bit floating point vectors
- typedef Vector4<Float4> Float4x4;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement