Template class for working with 3D vectors.
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 TMat3 matrix template description page.
You can find more information in comments below.
template<class TYPE> union TVec3
{
struct
{
TYPE X;
TYPE Y;
TYPE Z;
};
TYPE Data[3];
TVec3() = default;
TVec3(const TVec3 &other) = default;
TVec3(TVec3 &&other) = default;
TVec3(const TYPE x, const TYPE y, const TYPE z); // Create with specified elements
TVec3(const TVec2<TYPE> &v); // Create from 2D vector
TVec3(const TVec4<TYPE> &v); // Create from 4D 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 TVec3 &other) const; // Strict comparison with other vector for equality
bool operator!=(const TVec3 &other) const; // Strict comparison with other vector for inequality
TVec3 &operator=(const TVec3 &other) = default;
TVec3 &operator=(TVec3 &&other) = default;
operator TVec2<TYPE>() const; // Convert to 2D vector
operator TVec4<TYPE>() const; // Convert to 4D vector
TVec3 operator+(const TYPE value) const; // Add scalar value to each component and return new vector
TVec3 operator-(const TYPE value) const; // Subtract scalar value from each component and return new vector
TVec3 operator*(const TYPE value) const; // Multiply each component by scalar value and return new vector
TVec3 operator/(const TYPE value) const; // Divide each component by scalar value and return new vector
TVec3 &operator+=(const TYPE value); // Add scalar value to each component of this vector
TVec3 &operator-=(const TYPE value); // Subtract scalar value from each component of this vector
TVec3 &operator*=(const TYPE value); // Multiply each component of this vector by scalar value
TVec3 &operator/=(const TYPE value); // Divide each component of this vector by scalar value
TVec3 operator+(const TVec3 &other) const; // Add each components of this and another vectors and return new vector
TVec3 operator-(const TVec3 &other) const; // Subtract each components of this and another vectors and return new vector
TVec3 operator*(const TVec3 &other) const; // Multiply each components of this and another vectors and return new vector
TVec3 operator/(const TVec3 &other) const; // Divide each components of this and another vectors and return new vector
TVec3 operator^(const TVec3 &other) const; // Calculate cross product and return result
TVec3 &operator+=(const TVec3 &other); // Add each components of another vector to corresponding component of this one
TVec3 &operator-=(const TVec3 &other); // Subtract each components of another vector from corresponding component of this one
TVec3 &operator*=(const TVec3 &other); // Multiply each components of another vector to corresponding component of this one
TVec3 &operator/=(const TVec3 &other); // Divide each components of this vector by corresponding component of another one
TVec3 &operator^=(const TVec3 &other); // Calculate cross product and store in this vector
const TYPE *data() const; // Return pointer to components array
bool is_equal(const TVec3 &other, const TYPE tolerance) const; // Soft comparison for equality with another vector with specified tolerance
bool is_not_equal(const TVec3 &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 TVec3 &other) const; // Return distance to point
TYPE distance2(const TVec3 &other) const; // Return squared distance to point
TYPE dot(const TVec3 &other) const; // Return dot product
TVec3 cross(const TVec3 &other) const; // Calculate cross product and return result
void normalize(); // Normalize(make nor/length equal to 1) this vector
TVec3 normalized() const; // Return normalized equivalent of this vector
void reflect(const TVec3 &normal); // Reflect this vector around specified normal
TVec3 reflected(const TVec3 &normal) const; // Return vector equal to the reflection of this one around specified normal
void refract(const TVec3 &normal, const TYPE eta); // Refract this vector around specified normal
TVec3 refracted(const TVec3 &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 3D vectors. So as a final result you may use the 3D 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 }
will give you vector { 4, 3, 2 }(which is { 5 - 1, 5 - 2, 5 - 3 }) and { 1, 2, 3 } - 5
will give you vector { -4, -3, -2 }(which is { 1 - 5, 2 - 5, 3 - 5 }).
template<class TYPE> TVec3<TYPE> operator+(const TYPE value, const TVec3<TYPE> &v);
template<class TYPE> TVec3<TYPE> operator-(const TYPE value, const TVec3<TYPE> &v);
template<class TYPE> TVec3<TYPE> operator*(const TYPE value, const TVec3<TYPE> &v);
template<class TYPE> TVec3<TYPE> operator/(const TYPE value, const TVec3<TYPE> &v);
Namespace: | nitisa::math |
Include: | Nitisa/Modules/Math/Vector.h |