Skip to main content

vec3 Struct Template

Represents a 3-dimensional vector of the generic type <T>. More...

Declaration

template <helios::math::concepts::IsNumeric T> struct helios::math::vec3<T> { ... }

Public Constructors Index

template <helios::math::concepts::IsNumeric T>
constexprvec3 () noexcept

Creates a new vec3 with its values initialized to (0, 0, 0) More...

template <helios::math::concepts::IsNumeric T>
constexprvec3 (const T x, const T y, const T z) noexcept

Constructs a new vec3 with the specified x, y, z components. More...

template <helios::math::concepts::IsNumeric T>
constexprvec3 (const T v) noexcept

Constructs a new vec3 with the specified value as the x,y,z components. More...

template <helios::math::concepts::IsNumeric T>
constexprvec3 (const helios::math::vec2< T > v) noexcept

Constructs a new vec3 with x,y components initialized to those of the vec2 and vec3 set to 0. More...

template <helios::math::concepts::IsNumeric T>
constexprvec3 (const helios::math::vec2< T > v, T f) noexcept

Constructs a new vec3 with x,y components initialized to those of the vec2 and the z component set to the specified value. More...

Public Operators Index

template <helios::math::concepts::IsNumeric T>
constexpr const T &operator[] (const size_t i) const noexcept

Provides read only access to the vector components. Bounds checking is performed via assert in debug builds. More...

template <helios::math::concepts::IsNumeric T>
constexpr T &operator[] (const size_t i) noexcept

Provides read-write access to the vector components. Bounds checking is performed via assert in debug builds. More...

template <helios::math::concepts::IsNumeric T>
constexpr booloperator== (const vec3< T > &rgt) const

Strictly compares the elements of this vector with the elements of the rgt vector. More...

Public Member Functions Index

template <helios::math::concepts::IsNumeric T>
auto length () const noexcept -> FloatingPointType< T >

Computes the Euclidean norm (magnitude) of this vector and returns it. More...

template <helios::math::concepts::IsNumeric T>
auto cross (const helios::math::vec3< T > &v2) const noexcept -> vec3< T >

Computes the cross product of this vector and v2. More...

template <helios::math::concepts::IsNumeric T>
Tdot (const helios::math::vec3< T > &v2) const noexcept

Computes the dot product of this vector and v2. More...

template <helios::math::concepts::IsNumeric T>
auto toVec4 () const noexcept -> vec4< T >

Converts this 3D vector to a 4D homogeneous vector. More...

template <helios::math::concepts::IsNumeric T>
auto toVec2 () const noexcept -> vec2< T >

Converts this 3D vector to a 2D vector by discarding the z-component. More...

template <helios::math::concepts::IsNumeric T>
auto toVec4 (T w) const noexcept -> vec4< T >

Converts this 3D vector to a 4D homogeneous vector. More...

template <helios::math::concepts::IsNumeric T>
auto normalize () const noexcept -> vec3< FloatingPointType< T > >

Returns a normalized version of this vector. More...

template <helios::math::concepts::IsNumeric T>
constexpr boolsame (const vec3< T > &rgt, T epsilon=0.0001) const

Compares this vector's elements with the rgt vector considering an epsilon value. Returns true if for all elements the equation |a-b| <= epsilon holds. More...

template <helios::math::concepts::IsNumeric T>
auto flipY () const noexcept -> constexpr helios::math::vec3< T >

Returns a new vector with the y-component negated. More...

template <helios::math::concepts::IsNumeric T>
auto withY (T y) const noexcept -> constexpr helios::math::vec3< T >

Returns a new vector with the y-component replaced by the given value. More...

template <helios::math::concepts::IsNumeric T>
auto flipX () const noexcept -> constexpr helios::math::vec3< T >

Returns a new vector with the x-component negated. More...

template <helios::math::concepts::IsNumeric T>
auto withX (T x) const noexcept -> constexpr helios::math::vec3< T >

Returns a new vector with the x-component replaced by the given value. More...

template <helios::math::concepts::IsNumeric T>
constexpr boolisNormalized () const noexcept

Checks if this vector is normalized (unit length). More...

Private Member Attributes Index

template <helios::math::concepts::IsNumeric T>
Tv[3]

Internal array storing the vector components. More...

Description

Represents a 3-dimensional vector of the generic type <T>.

The vec3 struct provides a lightweight and efficient way to handle 3D vector mathematics for the numeric types float, int, double. For convenient access, the shorthands vec3f, vec3d and vec3i are available.

Template Parameters
T

The numeric type of the vector components.

Definition at line 34 of file vec3.ixx.

Public Constructors

vec3()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T >::vec3 ()
inline constexpr noexcept

Creates a new vec3 with its values initialized to (0, 0, 0)

Definition at line 48 of file vec3.ixx.

48 constexpr vec3() noexcept : v{static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)} {}

vec3()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T >::vec3 (const T x, const T y, const T z)
inline constexpr noexcept

Constructs a new vec3 with the specified x, y, z components.

Parameters
x

The value for the x component.

y

The value for the y component.

z

The value for the z component.

Definition at line 59 of file vec3.ixx.

59 constexpr vec3(const T x, const T y, const T z) noexcept : v{x, y, z} {}

vec3()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T >::vec3 (const T v)
inline explicit constexpr noexcept

Constructs a new vec3 with the specified value as the x,y,z components.

Parameters
v

The value for the components.

Definition at line 68 of file vec3.ixx.

68 constexpr explicit vec3(const T v) noexcept : v{v, v, v} {}

vec3()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T >::vec3 (const helios::math::vec2< T > v)
inline explicit constexpr noexcept

Constructs a new vec3 with x,y components initialized to those of the vec2 and vec3 set to 0.

Parameters
v

The vec2 to use for the x,y components.

Definition at line 77 of file vec3.ixx.

77 constexpr explicit vec3(const helios::math::vec2<T> v) noexcept : v{v[0], v[1], static_cast<T>(0)} {}

vec3()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T >::vec3 (const helios::math::vec2< T > v, T f)
inline explicit constexpr noexcept

Constructs a new vec3 with x,y components initialized to those of the vec2 and the z component set to the specified value.

Parameters
v

The vec2 to use for the x,y components.

f

The value for the z component.

Definition at line 86 of file vec3.ixx.

86 constexpr explicit vec3(const helios::math::vec2<T> v, T f) noexcept : v{v[0], v[1], f} {}

Public Operators

operator[]()

template <helios::math::concepts::IsNumeric T>
constexpr const T & helios::math::vec3< T >::operator[] (const size_t i)
inline constexpr noexcept

Provides read only access to the vector components. Bounds checking is performed via assert in debug builds.

Parameters
i

The index to query

Returns

A const ref to the requested component.

Definition at line 96 of file vec3.ixx.

96 constexpr const T& operator[](const size_t i) const noexcept {
97 assert(i <= 2 && "vec3 - Index out of bounds.");
98 return this->v[i];
99 }

operator[]()

template <helios::math::concepts::IsNumeric T>
constexpr T & helios::math::vec3< T >::operator[] (const size_t i)
inline constexpr noexcept

Provides read-write access to the vector components. Bounds checking is performed via assert in debug builds.

Parameters
i

The index to query

Returns

A const ref to the requested component.

Definition at line 110 of file vec3.ixx.

110 constexpr T& operator[](const size_t i) noexcept {
111 assert(i <= 2 && "vec3 - Index out of bounds.");
112 return this->v[i];
113 }

operator==()

template <helios::math::concepts::IsNumeric T>
constexpr bool helios::math::vec3< T >::operator== (const vec3< T > & rgt)
inline constexpr

Strictly compares the elements of this vector with the elements of the rgt vector.

Parameters
rgt

The right vector to compare for equal values.

Returns

True if all elements are equal (==), false otherwise.

Definition at line 193 of file vec3.ixx.

193 constexpr bool operator==(const vec3<T>& rgt) const {
194 return v[0] == rgt[0] && v[1] == rgt[1] && v[2] == rgt[2];
195 }

Public Member Functions

cross()

template <helios::math::concepts::IsNumeric T>
vec3< T > helios::math::vec3< T >::cross (const helios::math::vec3< T > & v2)
inline noexcept

Computes the cross product of this vector and v2.

Parameters
v2

The second vec3<T> vector.

Returns

The cross product as a value of type vec3<T>.

Definition at line 136 of file vec3.ixx.

405 inline vec3<T> vec3<T>::cross(const vec3<T>& v2) const noexcept {
406 return vec3{
407 v[1]*v2[2] - v[2]*v2[1],
408 v[2]*v2[0] - v[0]*v2[2],
409 v[0]*v2[1] - v[1]*v2[0]
410 };
411 }

dot()

template <helios::math::concepts::IsNumeric T>
T helios::math::vec3< T >::dot (const helios::math::vec3< T > & v2)
inline noexcept

Computes the dot product of this vector and v2.

Parameters
v2

The second vec3<T> vector.

Returns

The dot product as a value of type T.

Definition at line 145 of file vec3.ixx.

429 inline T vec3<T>::dot(const vec3<T>& v2) const noexcept {
430 return v[0]*v2[0] + v[1]*v2[1] + v[2]*v2[2];
431 }

flipX()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T > helios::math::vec3< T >::flipX ()
inline constexpr noexcept

Returns a new vector with the x-component negated.

Returns

A new vec3<T> with (-x, y, z).

Definition at line 245 of file vec3.ixx.

246 return helios::math::vec3<T>{-v[0], v[1], v[2]};
247 }

flipY()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T > helios::math::vec3< T >::flipY ()
inline constexpr noexcept

Returns a new vector with the y-component negated.

Returns

A new vec3<T> with (x, -y, z).

Definition at line 226 of file vec3.ixx.

227 return helios::math::vec3<T>{v[0], -v[1], v[2]};
228 }

isNormalized()

template <helios::math::concepts::IsNumeric T>
constexpr bool helios::math::vec3< T >::isNormalized ()
inline constexpr noexcept

Checks if this vector is normalized (unit length).

A vector is considered normalized if its squared length equals 1 within the tolerance defined by EPSILON_LENGTH.

Returns

true if the vector is approximately unit length, false otherwise.

Definition at line 267 of file vec3.ixx.

267 constexpr bool isNormalized() const noexcept {
268 const auto lenSquared =
269 static_cast<FloatingPointType<T>>(v[0]) * static_cast<FloatingPointType<T>>(v[0]) +
270 static_cast<FloatingPointType<T>>(v[1]) * static_cast<FloatingPointType<T>>(v[1]) +
271 static_cast<FloatingPointType<T>>(v[2]) * static_cast<FloatingPointType<T>>(v[2]);
272
273 return std::abs(lenSquared - static_cast<FloatingPointType<T>>(1.0)) <= helios::math::EPSILON_LENGTH;
274 }

Reference helios::math::EPSILON_LENGTH.

length()

template <helios::math::concepts::IsNumeric T>
FloatingPointType< T > helios::math::vec3< T >::length ()
inline noexcept

Computes the Euclidean norm (magnitude) of this vector and returns it.

Returns

The norm (magnitude) of this vector as a value of type FloatingPointType<T>.

Definition at line 121 of file vec3.ixx.

122 return static_cast<FloatingPointType<T>>(std::sqrt(
123 static_cast<double>(this->v[0]) * static_cast<double>(this->v[0]) +
124 static_cast<double>(this->v[1]) * static_cast<double>(this->v[1]) +
125 static_cast<double>(this->v[2]) * static_cast<double>(this->v[2])
126 ));
127 }

normalize()

template <helios::math::concepts::IsNumeric T>
vec3< FloatingPointType< T > > helios::math::vec3< T >::normalize ()
inline noexcept

Returns a normalized version of this vector.

Returns

A new vec3<FloatingPointType<T>> instance representing the normalized vector.

Definition at line 183 of file vec3.ixx.

436 if (this->length() == static_cast<FloatingPointType<T>>(0)) {
438 static_cast<FloatingPointType<T>>(0),
439 static_cast<FloatingPointType<T>>(0),
440 static_cast<FloatingPointType<T>>(0)
441 );
442 }
443
445 static_cast<FloatingPointType<T>>(v[0]) / this->length(),
446 static_cast<FloatingPointType<T>>(v[1]) / this->length(),
447 static_cast<FloatingPointType<T>>(v[2]) / this->length()
448 );
449 }

same()

template <helios::math::concepts::IsNumeric T>
constexpr bool helios::math::vec3< T >::same (const vec3< T > & rgt, T epsilon=0.0001)
inline constexpr

Compares this vector's elements with the rgt vector considering an epsilon value. Returns true if for all elements the equation |a-b| <= epsilon holds.

Parameters
rgt

The other vector to compare with this vector for equality.

epsilon

The epsilon value to use for comparison. If omitted, the default epsilon (0.0001) is used.

Returns

True if the elements of the vectors are considered equal, otherwise false.

See Also

https://realtimecollisiondetection.net/blog/?p=89

Definition at line 215 of file vec3.ixx.

215 constexpr bool same(const vec3<T>& rgt, T epsilon = 0.0001) const {
216 return std::fabs(v[0] - rgt[0]) <= epsilon &&
217 std::fabs(v[1] - rgt[1]) <= epsilon &&
218 std::fabs(v[2] - rgt[2]) <= epsilon;
219 }

toVec2()

template <helios::math::concepts::IsNumeric T>
vec2< T > helios::math::vec3< T >::toVec2 ()
inline noexcept

Converts this 3D vector to a 2D vector by discarding the z-component.

Returns

A new vec2<T> instance with components (x, y).

Definition at line 163 of file vec3.ixx.

420 return vec2<T>{v[0], v[1]};
421 }

toVec4()

template <helios::math::concepts::IsNumeric T>
vec4< T > helios::math::vec3< T >::toVec4 ()
inline noexcept

Converts this 3D vector to a 4D homogeneous vector.

Creates a vec4 with the x, y, z components from this vector and sets the w component to 0, representing a direction in homogeneous coordinates.

Returns

A new vec4<T> instance with components (x, y, z, 0).

Definition at line 156 of file vec3.ixx.

415 return vec4<T>{v[0], v[1], v[2], static_cast<T>(0)};
416 }

toVec4()

template <helios::math::concepts::IsNumeric T>
vec4< T > helios::math::vec3< T >::toVec4 (T w)
inline noexcept

Converts this 3D vector to a 4D homogeneous vector.

Creates a vec4 with the x, y, z components from this vector and sets the w component accordingly, representing a direction in homogeneous coordinates.

Parameters
w

The value of the w component.

Returns

A new vec4<T> instance with components (x, y, z, w).

Definition at line 176 of file vec3.ixx.

424 inline vec4<T> vec3<T>::toVec4(T w) const noexcept {
425 return vec4<T>{v[0], v[1], v[2], w};
426 }

withX()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T > helios::math::vec3< T >::withX (T x)
inline constexpr noexcept

Returns a new vector with the x-component replaced by the given value.

Parameters
x

The new x value.

Returns

A new vec3<T> with (x, y, z).

Definition at line 255 of file vec3.ixx.

255 constexpr helios::math::vec3<T> withX(T x) const noexcept {
256 return helios::math::vec3<T>{x, v[1], v[2]};
257 }

withY()

template <helios::math::concepts::IsNumeric T>
constexpr helios::math::vec3< T > helios::math::vec3< T >::withY (T y)
inline constexpr noexcept

Returns a new vector with the y-component replaced by the given value.

Parameters
y

The new y value.

Returns

A new vec3<T> with (x, y, z).

Definition at line 236 of file vec3.ixx.

236 constexpr helios::math::vec3<T> withY(T y) const noexcept {
237 return helios::math::vec3<T>{v[0], y, v[2]};
238 }

Private Member Attributes

v

template <helios::math::concepts::IsNumeric T>
T helios::math::vec3< T >::v[3]

Internal array storing the vector components.

Definition at line 40 of file vec3.ixx.

40 T v[3];

The documentation for this struct was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.