This union represents 4-component floating point vector also known as quaternion. It is widely used in transformation calculations.
You can find more information in comments below.
// 4th component is not used and either unchanged or is set to 1
union Vector
{
struct
{
float X;
float Y;
float Z;
float W;
};
struct
{
PointF XY;
PointF ZW;
};
float Data[4];
Vector() = default;
Vector(const Vector &other) = default;
Vector(Vector &&other) = default;
Vector(const float x, const float y, const float z, const float w = 1); // Construct with specified values
Vector &operator=(const Vector &other) = default;
Vector &operator=(Vector &&other) = default;
float operator[](const size_t index) const; // Return component value by index
float &operator[](const size_t index); // Return component value reference by index
float norm() const; // Return norm/length
float norm2() const; // Return squared norm/length
float distance(const Vector &other) const; // Return distance to point
float distance2(const Vector &other) const; // Return squared distance to point
void normalize(); // Normalize(make norm/length equal to 1) this vector
Vector normalized() const; // Return normalized equivalent of this vector
void reflect(const Vector &normal); // Reflect this vector around specified normal
Vector reflected(const Vector &normal) const; // Return vector equal to the reflection of this one around specified normal
void refract(const Vector &normal, const float eta); // Refract this vector around specified normal
Vector refracted(const Vector &normal, const float eta) const; // Return vector equal to the refraction of this one around specified normal
#ifdef _DEBUG
void print() const;
#endif
};
Following operators are also available.
Vector operator+(const Vector &a, const float value); // Add scalar value to each component and return new vector
Vector operator-(const Vector &a, const float value); // Subtract scalar value from each component and return new vector
Vector operator*(const Vector &a, const float value); // Multiply each component by scalar value and return new vector
Vector operator/(const Vector &a, const float value); // Divide each component by scalar value and return new vector
Vector operator+(const float value, const Vector &a); // Add value to each component and return new vector
Vector operator-(const float value, const Vector &a); // Subtract from value each component and return new vector
Vector operator*(const float value, const Vector &a); // Multiply value by each component and return new vector
Vector operator/(const float value, const Vector &a); // Divide value by each component and return new vector
Vector &operator+=(Vector &a, const float value); // Add scalar value to each component of this vector
Vector &operator-=(Vector &a, const float value); // Subtract scalar value from each component of this vector
Vector &operator*=(Vector &a, const float value); // Multiply each component of this vector by scalar value
Vector &operator/=(Vector &a, const float value); // Divide each component of this vector by scalar value
Vector operator+(const Vector &a, const Vector &b); // Add each components of this and another vectors and return new vector
Vector operator-(const Vector &a, const Vector &b); // Subtract each components of this and another vectors and return new vector
Vector operator/(const Vector &a, const Vector &b); // Divide vectors elements and return new vector
float operator*(const Vector &a, const Vector &b); // Return dot product
Vector operator^(const Vector &a, const Vector &b); // Calculate cross product and return result
Vector &operator+=(Vector &a, const Vector &b); // Add each components of another vector to corresponding component of this one
Vector &operator-=(Vector &a, const Vector &b); // Subtract each components of another vector from corresponding component of this one
Vector &operator/=(Vector &a, const Vector &b); // Divide this vector elements on corresponding elements of other vector
Vector &operator^=(Vector &a, const Vector &b); // Calculate cross product and store in this vector
std::wostream &operator<<(std::wostream &stream, const Vector &a); // Output as source code
There are also several additional operators related to interaction with matrices. You can find them at Matrix documentation page.
Namespace: | nitisa |
Include: |
Nitisa/Core/Math/Vector.h (For union declaration) Nitisa/Core/Math/VectorUtils.h (For standalone operators) |