Skip to main content

vec3 Struct Template

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

Declaration

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

Public Constructors Index

template <helios::math::Numeric T>
constexprvec3 () noexcept

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

template <helios::math::Numeric 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::Numeric T>
constexprvec3 (const T v) noexcept

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

template <helios::math::Numeric 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::Numeric 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::Numeric 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::Numeric 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::Numeric 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::Numeric T>
auto length () const noexcept -> FloatingPointType< T >

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

template <helios::math::Numeric 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::Numeric T>
Tdot (const helios::math::vec3< T > &v2) const noexcept

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

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

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

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

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

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

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

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

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

template <helios::math::Numeric 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::Numeric T>
constexpr auto flipY () const noexcept -> helios::math::vec3< T >

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

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

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

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

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

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

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

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

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

Private Member Attributes Index

template <helios::math::Numeric 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 33 of file vec3.ixx.

Public Constructors

vec3()

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

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

Definition at line 47 of file vec3.ixx.

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

Referenced by helios::math::vec3< T >::cross, helios::math::vec3< T >::dot and helios::math::vec3< T >::normalize.

vec3()

template <helios::math::Numeric T>
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 58 of file vec3.ixx.

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

vec3()

template <helios::math::Numeric T>
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 67 of file vec3.ixx.

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

vec3()

template <helios::math::Numeric T>
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 76 of file vec3.ixx.

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

vec3()

template <helios::math::Numeric T>
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 85 of file vec3.ixx.

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

Public Operators

operator[]()

template <helios::math::Numeric T>
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 95 of file vec3.ixx.

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

operator[]()

template <helios::math::Numeric T>
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 109 of file vec3.ixx.

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

operator==()

template <helios::math::Numeric T>
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 192 of file vec3.ixx.

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

Public Member Functions

cross()

template <helios::math::Numeric T>
vec3< T > helios::math::vec3< T >::cross (const helios::math::vec3< T > & v2)
inline nodiscard 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 135 of file vec3.ixx.

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

Reference helios::math::vec3< T >::vec3.

dot()

template <helios::math::Numeric T>
T helios::math::vec3< T >::dot (const helios::math::vec3< T > & v2)
inline nodiscard 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 144 of file vec3.ixx.

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

Reference helios::math::vec3< T >::vec3.

flipX()

template <helios::math::Numeric T>
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 244 of file vec3.ixx.

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

flipY()

template <helios::math::Numeric T>
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 225 of file vec3.ixx.

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

isNormalized()

template <helios::math::Numeric T>
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 266 of file vec3.ixx.

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

Referenced by helios::engine::runtime::spawn::behavior::placements::AxisSpawnPlacer::AxisSpawnPlacer and helios::engine::modules::physics::motion::components::SteeringComponent::setSteeringIntent.

length()

template <helios::math::Numeric 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 120 of file vec3.ixx.

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

Referenced by helios::ext::imgui::widgets::CameraWidget::draw and helios::math::vec3< T >::normalize.

normalize()

template <helios::math::Numeric T>
vec3< FloatingPointType< T > > helios::math::vec3< T >::normalize ()
inline nodiscard noexcept

Returns a normalized version of this vector.

Returns

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

Definition at line 182 of file vec3.ixx.

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

References helios::math::vec3< T >::length and helios::math::vec3< T >::vec3.

Referenced by helios::scene::CameraSceneNode::lookAt, helios::scene::CameraSceneNode::lookAtLocal and helios::engine::modules::ai::systems::ChaseSystem::update.

same()

template <helios::math::Numeric T>
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

Todo

account for abs (values close to zero) and rel (larger values), move epsilon to global constant?

Definition at line 214 of file vec3.ixx.

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

toVec2()

template <helios::math::Numeric T>
vec2< T > helios::math::vec3< T >::toVec2 ()
inline nodiscard 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 162 of file vec3.ixx.

418 inline vec2<T> vec3<T>::toVec2() const noexcept {
419 return vec2<T>{v[0], v[1]};
420 }

toVec4()

template <helios::math::Numeric T>
vec4< T > helios::math::vec3< T >::toVec4 ()
inline nodiscard 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 155 of file vec3.ixx.

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

Referenced by helios::engine::mechanics::bounds::systems::LevelBoundsBehaviorSystem::update.

toVec4()

template <helios::math::Numeric T>
vec4< T > helios::math::vec3< T >::toVec4 (T w)
inline nodiscard 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 175 of file vec3.ixx.

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

withX()

template <helios::math::Numeric T>
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 254 of file vec3.ixx.

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

withY()

template <helios::math::Numeric T>
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 235 of file vec3.ixx.

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

Private Member Attributes

v

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

Internal array storing the vector components.

Definition at line 39 of file vec3.ixx.

39 T v[3];

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.