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, const T epsilon=static_cast< T >(EPSILON_LENGTH)) const noexcept

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>
constexpr boolsame (const vec3< T > &rgt) const noexcept

Compares vectors with exact component equality for integral types. 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>
auto withZ (T z) const noexcept -> constexpr helios::math::vec3< T >

Returns a new vector with the z-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.

428 inline vec3<T> vec3<T>::cross(const vec3<T>& v2) const noexcept {
429 return vec3{
430 v[1]*v2[2] - v[2]*v2[1],
431 v[2]*v2[0] - v[0]*v2[2],
432 v[0]*v2[1] - v[1]*v2[0]
433 };
434 }

Referenced by helios::math::quat< T >::operator*.

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.

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

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 258 of file vec3.ixx.

258 constexpr helios::math::vec3<T> flipX() const noexcept {
259 return helios::math::vec3<T>{-v[0], v[1], v[2]};
260 }

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 239 of file vec3.ixx.

239 constexpr helios::math::vec3<T> flipY() const noexcept {
240 return helios::math::vec3<T>{v[0], -v[1], v[2]};
241 }

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 290 of file vec3.ixx.

290 constexpr bool isNormalized() const noexcept {
291 const auto lenSquared =
292 static_cast<FloatingPointType<T>>(v[0]) * static_cast<FloatingPointType<T>>(v[0]) +
293 static_cast<FloatingPointType<T>>(v[1]) * static_cast<FloatingPointType<T>>(v[1]) +
294 static_cast<FloatingPointType<T>>(v[2]) * static_cast<FloatingPointType<T>>(v[2]);
295
296 return std::abs(lenSquared - static_cast<FloatingPointType<T>>(1.0)) <= helios::math::EPSILON_LENGTH;
297 }

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.

121 FloatingPointType<T> length() const noexcept {
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.

459 if (this->length() == static_cast<FloatingPointType<T>>(0)) {
461 static_cast<FloatingPointType<T>>(0),
462 static_cast<FloatingPointType<T>>(0),
463 static_cast<FloatingPointType<T>>(0)
464 );
465 }
466
468 static_cast<FloatingPointType<T>>(v[0]) / this->length(),
469 static_cast<FloatingPointType<T>>(v[1]) / this->length(),
470 static_cast<FloatingPointType<T>>(v[2]) / this->length()
471 );
472 }

Referenced by helios::math::frustumPlanes.

same()

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

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, const T epsilon = static_cast<T>(EPSILON_LENGTH)) const noexcept requires std::floating_point<T> {
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 }

same()

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

Compares vectors with exact component equality for integral types.

Parameters
rgt

Vector to compare against.

Returns

true if all components match exactly; otherwise false.

Definition at line 227 of file vec3.ixx.

227 constexpr bool same(const vec3<T>& rgt) const noexcept requires std::integral<T> {
228 return v[0] == rgt[0] &&
229 v[1] == rgt[1] &&
230 v[2] == rgt[2];
231 }

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.

442 inline vec2<T> vec3<T>::toVec2() const noexcept {
443 return vec2<T>{v[0], v[1]};
444 }

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.

437 inline vec4<T> vec3<T>::toVec4() const noexcept {
438 return vec4<T>{v[0], v[1], v[2], static_cast<T>(0)};
439 }

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.

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

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 268 of file vec3.ixx.

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

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 249 of file vec3.ixx.

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

withZ()

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

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

Parameters
z

The new z value.

Returns

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

Definition at line 278 of file vec3.ixx.

278 constexpr helios::math::vec3<T> withZ(T z) const noexcept {
279 return helios::math::vec3<T>{v[0], v[1], z};
280 }

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.