vec3

Header file: include/vec3.h

vec3 is a vector with 3 scalars, x, y and z.

\(vec3 = \begin{bmatrix} x \\ y \\ z \end{bmatrix}\)

Class variables

Type

Name

Description

float

x

The first element of the vec3.

float

y

The second element of the vec3.

float

z

The third element of the vec3.

Class functions

Constructors

Name

Description

vec3()

Construct a (0.0, 0.0, 0.0) vector.

vec3(float _value)

Construct a (_value, _value, _value) vector.

vec3(float _x, float _y, float _z)

Construct a (_x, _y, _z) vector.

vec3(float _x, const nml::vec2& _yz)

Construct a (_x, _yz.x, _yz.y) vector.

vec3(const nml::vec2& _xy, float _z)

Construct a (_xy.x, _xy.y, _z) vector.

vec3(const float* _ptr)

Construct a vector from a pointer.

vec3(const nml::vec4& _xyzw)

Construct a (_xyzw.x, _xyzw.y, _xyzw.z) vector.

Operators

Name

Description

nml::vec3& operator+=(const nml::vec3& other)

Add a vec3 to the current vec3.

nml::vec3& operator-=(const nml::vec3& other)

Substract a vec3 from the current vec3.

nml::vec3& operator*=(const float other)

Multiply the current vec3 by a scalar.

nml::vec3& operator/=(const float other)

Divide the current vec3 by a scalar.

nml::vec3 operator-()

Return the negative of the current vec3.

float& operator[](size_t index)

Return a reference to x if index is 0, a reference to y if index is 1 or a reference to z if index is 2, else, throw an exception.

const float operator[](size_t index) const

Return the value of x if index is 0, the value of y if index is 1 or the value to z if index is 2, else, throw an exception.

Functions

Name

Description

float length()

Return the length of the vector.

float* data()

Return a pointer to the vector’s elements.

Namespace functions

Operators

Name

Description

nml::vec3 operator+(nml::vec3 lhs, const nml::vec3& rhs)

Return a vec3 that is the sum between two vec3.

nml::vec3 operator-(nml::vec3 lhs, const nml::vec3& rhs)

Return a vec3 that is the difference between two vec3.

nml::vec3 operator*(nml::vec3 lhs, const float rhs)

Return a vec3 that is the product between a vec3 and a scalar.

nml::vec3 operator*(float lhs, const nml::vec3& rhs)

Return a vec3 that is the product between a scalar and a vec3.

nml::vec3 operator/(nml::vec3 lhs, const float rhs)

Return a vec3 that is the quotient between a vec3 and a scalar.

bool operator==(const nml::vec3& lhs, const nml::vec3& rhs)

Return true if the two vec3 are identical, else, return false.

bool operator!=(const nml::vec3& lhs, const nml::vec3& rhs)

Return true if the two vec3 are different, else, return false.

Functions

Name

Description

nml::vec3 nml::normalize(const nml::vec3& vec)

Return a vec3 with the same direction as vec but with length 1.

float nml::dot(const nml::vec3& a, const nml::vec3& b)

Return the dot product between two vec3.

nml::vec3 nml::cross(const nml::vec3& a, const nml::vec3& b)

Return the cross product between two vec3.

nml::vec3 nml::reflect(const nml::vec3& i, const nml::vec3& n)

Return the reflected direction between the incident vector i and the normal n. n should be normalized.

nml::vec3 nml::refract(const nml::vec3& i, const nml::vec3& n, float ior)

Return the refracted direction between the incident vector i, the normal n and the ratio of indices of refraction ior. n should be normalized.

nml::vec3 nml::quatToEulerAngles(const nml::quat& qua)

Return a vec3 representing euler angles in radians from a quaternion.

nml::vec3 nml::rotationMatrixToEulerAngles(const nml::mat4& mat)

Return a vec3 representing euler angles in radians from a 3D rotation matrix.

std::string nml::to_string(const nml::vec3& vec)

Return a vec3 as a string under the format “[x, y, z]”.