Template class for working with 4D vectors(quaternions).
It works the same way as TVec3 does. 4th component is not used and either unchanged or is set to 1.
Besides operators defined in the template and standalone ones described below the template description there are several additional useful operators for working with vectors and matrices togather. You can find them at TMat4 matrix template description page.
You can find more information in comments below.
template<class TYPE> union TVec4
{
struct
{
TYPE X;
TYPE Y;
TYPE Z;
TYPE W;
};
TYPE Data[4];
TVec4() = default;
TVec4(const TVec4 &other) = default;
TVec4(TVec4 &&other) = default;
TVec4(const TYPE x, const TYPE y, const TYPE z); // Construct with specified values. 4th component is 1
TVec4(const TYPE x, const TYPE y, const TYPE z, const TYPE w); // Construct with specified values
TVec4(const TVec2<TYPE> &v); // Create from 2D vector
TVec4(const TVec3<TYPE> &v); // Create from 3D vector
const TYPE &operator[](const size_t index) const; // Return component value constant reference by index
TYPE &operator[](const size_t index); // Return component value reference by index
bool operator==(const TVec4 &other) const; // Strict comparison with other vector for equality
bool operator!=(const TVec4 &other) const; // Strict comparison with other vector for equality
TVec4 &operator=(const TVec4 &other) = default;
TVec4 &operator=(TVec4 &&other) = default;
operator TVec2<TYPE>() const; // Convert to 2D vector
operator TVec3<TYPE>() const; // Convert to 3D vector
TVec4 operator+(const TYPE value) const; // Add scalar value to each component and return new vector
TVec4 operator-(const TYPE value) const; // Subtract scalar value from each component and return new vector
TVec4 operator*(const TYPE value) const; // Multiply each component by scalar value and return new vector
TVec4 operator/(const TYPE value) const; // Divide each component by scalar value and return new vector
TVec4 &operator+=(const TYPE value); // Add scalar value to each component of this vector
TVec4 &operator-=(const TYPE value); // Subtract scalar value from each component of this vector
TVec4 &operator*=(const TYPE value); // Multiply each component of this vector by scalar value
TVec4 &operator/=(const TYPE value); // Divide each component of this vector by scalar value
TVec4 operator+(const TVec4 &other) const; // Add each components of this and another vectors and return new vector
TVec4 operator-(const TVec4 &other) const; // Subtract each components of this and another vectors and return new vector
TVec4 operator*(const TVec4 &other) const; // Multiply each components of this and another vectors and return new vector
TVec4 operator/(const TVec4 &other) const; // Divide each components of this and another vectors and return new vector
TVec4 operator^(const TVec4 &other) const; // Calculate cross product and return result
TVec4 &operator+=(const TVec4 &other); // Add each components of another vector to corresponding component of this one
TVec4 &operator-=(const TVec4 &other); // Subtract each components of another vector from corresponding component of this one
TVec4 &operator*=(const TVec4 &other); // Multiply each components of another vector to corresponding component of this one
TVec4 &operator/=(const TVec4 &other); // Divide each components of this vector by corresponding component of another one
TVec4 &operator^=(const TVec4 &other); // Calculate cross product and store in this vector
const TYPE *data() const; // Return pointer to components array
bool is_equal(const TVec4 &other, const TYPE tolerance) const; // Soft comparison for equality with another vector with specified tolerance
bool is_not_equal(const TVec4 &other, const TYPE tolerance) const; // Soft comparison for inequality with another vector with specified tolerance
TYPE norm() const; // Return norm/length
TYPE norm2() const; // Return squared norm/length
TYPE distance(const TVec4 &other) const; // Return distance to point
TYPE distance2(const TVec4 &other) const; // Return squared distance to point
TYPE dot(const TVec4 &other) const; // Return dot product
TVec4 cross(const TVec4 &other) const; // Calculate cross product and return result
void normalize(); // Normalize(make nor/length equal to 1) this vector
TVec4 normalized() const; // Return normalized equivalent of this vector
void reflect(const TVec4 &normal); // Reflect this vector around specified normal
TVec4 reflected(const TVec4 &normal) const; // Return vector equal to the reflection of this one around specified normal
void refract(const TVec4 &normal, const TYPE eta); // Refract this vector around specified normal
TVec4 refracted(const TVec4 &normal, const TYPE eta) const; // Return vector equal to the refraction of this one around specified normal
};
Additionally following operators exists and can be used for arithmetic operations with scalar value and 4D vectors. So as a final result you may use the 4D vector template with scalar values in both directions(scalar * vector
and vector * scalar
). Pay attention on the - and / operators. The result of operation like scalar - vector
will be different from the result of operation like vector - scalar
. For example, 5 - { 1, 2, 3, 1 }
will give you vector { 4, 3, 2, 1 }(which is { 5 - 1, 5 - 2, 5 - 3, 1 }) and { 1, 2, 3, 1 } - 5
will give you vector { -4, -3, -2, 1 }(which is { 1 - 5, 2 - 5, 3 - 5, 1 }). Please also note that the 4th component is 1 and is not used in any operations. It only exists because very often transformations are represented as 4x4 matrix and applying a transformation means multiplying vector on such a matrix. 3D vector cannot be multiplied on 4x4 matrix, but 4D vector can be.
template<class TYPE> TVec4<TYPE> operator+(const TYPE value, const TVec4<TYPE> &v);
template<class TYPE> TVec4<TYPE> operator-(const TYPE value, const TVec4<TYPE> &v);
template<class TYPE> TVec4<TYPE> operator*(const TYPE value, const TVec4<TYPE> &v);
template<class TYPE> TVec4<TYPE> operator/(const TYPE value, const TVec4<TYPE> &v);
Namespace: | nitisa::math |
Include: | Nitisa/Modules/Math/Vector.h |