Skip to main content

vec4.ixx File

4D vector type and utilities. More...

Included Headers

#include <cassert> #include <memory> #include <helios.math.types:vec3> #include <helios.math.concepts>

Namespaces Index

namespacehelios
namespacemath

Mathematical operations and types. More...

Classes Index

structvec4<T>

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

Description

4D vector type and utilities.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file vec4.ixx
3 * @brief 4D vector type and utilities.
4 */
5module;
6
7#include <cassert>
8#include <memory>
9
10
11export module helios.math.types:vec4;
12
13import helios.math.concepts;
14
15import :vec3;
16
17
18export namespace helios::math {
19
20 template<helios::math::Numeric T>
21 struct vec4;
22
23 /**
24 * @brief Represents a 4-dimensional vector of the generic type <T>.
25 *
26 * The `vec4` struct provides a lightweight and efficient way to handle 4D
27 * vector mathematics for the numeric types float, int, double. For convenient access,
28 * the shorthands `vec4f`, `vec4d` and `vec4i` are available.
29 *
30 * @tparam T The numeric type of the vector components.
31 */
32 template<helios::math::Numeric T>
33 struct vec4 {
34
35 private:
36 /**
37 * @brief Internal array storing the vector components.
38 */
39 T v[4];
40
41 public:
42
43 /**
44 * @brief Creates a new vec4 with its values initialized to (0, 0, 0, 0)
45 */
46 constexpr vec4() noexcept : v{static_cast<T>(0), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)} {}
47
48
49 /**
50 * @brief Constructs a new vec4 with the specified x, y, z, w components.
51 *
52 * @param x The value for the x component.
53 * @param y The value for the y component.
54 * @param z The value for the z component.
55 * @param w The value for the w component.
56 */
57 constexpr vec4(const T x, const T y, const T z, const T w) noexcept : v{x, y, z, w} {}
58
59 /**
60 * @brief Constructs a new vec4 with v used for the x, y, z, w components.
61 *
62 * @param v The value for the x, y, z, w component.
63 */
64 constexpr vec4(const T v) noexcept : v{v, v, v, v} {}
65
66
67 /**
68 * @brief Constructs a new vec4 from the vec3 vector and the w value.
69 *
70 * @param vec The vec3<T> vector.
71 * @param w The value for the w component.
72 */
73 constexpr vec4(const vec3<T>& vec, const T w) noexcept : v{vec[0], vec[1], vec[2], w} {}
74
75
76 /**
77 * @brief Constructs a new vec4 from the vec3 vector and sets w to 1
78 *
79 * @param vec The vec3<T> vector.
80 */
81 explicit constexpr vec4(const vec3<T>& vec) noexcept : v{vec[0], vec[1], vec[2], static_cast<T>(1)} {}
82
83 /**
84 * @brief Creates a new vec4 with the same x, y, z components but a modified w component.
85 *
86 * This method returns a new vec4 instance where the x, y, and z components remain unchanged,
87 * and the w component is set to the specified value.
88 *
89 * @param w The new value for the w component.
90 *
91 * @return A new vec4<T> instance with the updated w component.
92 */
93 [[nodiscard]] constexpr vec4<T> withW(T w) const noexcept {
94 return helios::math::vec4<T>{v[0], v[1], v[2], w};
95 }
96
97 /**
98 * @brief Provides read only access to the vector components.
99 * Bounds checking is performed via `assert` in debug builds.
100 *
101 * @param i The index to query
102 *
103 * @return A const ref to the requested component.
104 */
105 constexpr const T& operator[](const size_t i) const noexcept {
106 assert(i <= 3 && "vec4 - Index out of bounds.");
107 return this->v[i];
108 }
109
110
111 /**
112 * @brief Provides read-write access to the vector components.
113 * Bounds checking is performed via `assert` in debug builds.
114 *
115 * @param i The index to query
116 *
117 * @return A ref to the requested component.
118 */
119 constexpr T& operator[](const size_t i) noexcept {
120 assert(i <= 3 && "vec4 - Index out of bounds.");
121 return this->v[i];
122 }
123
124 /**
125 * @brief Converts this 4D vector to a 3D vector.
126 *
127 * Extracts the x, y, z components from this vec4, discarding the w component.
128 * This is useful when converting from homogeneous coordinates back to 3D space.
129 *
130 * @return A new vec3<T> instance with components (x, y, z).
131 */
132 [[nodiscard]] vec3<T> toVec3() const noexcept;
133
134 /**
135 * @brief Converts this 4D vector to a 2D vector.
136 *
137 * Extracts the x, y components from this vec4, discarding the z,w components.
138 *
139 * @return A new vec2<T> instance with components (x, y).
140 */
141 [[nodiscard]] vec2<T> toVec2() const noexcept;
142
143 };
144
145 template<helios::math::Numeric T>
146 inline vec3<T> vec4<T>::toVec3() const noexcept {
147 return vec3<T>{v[0], v[1], v[2]};
148 }
149
150 template<helios::math::Numeric T>
151 inline vec2<T> vec4<T>::toVec2() const noexcept {
152 return vec2<T>{v[0], v[1]};
153 }
154
155
156 /**
157 * @brief Returns a const pointer to the first element of the vector's components.
158 *
159 * Useful for APIs that expect a pointer to vector data, like OpenGL
160 * @param m A reference to the `vec4<T>` vector.
161 * @tparam T The numeric type of the vector components.
162 */
163 template<helios::math::Numeric T>
164 const T* value_ptr(const vec4<T>& m) noexcept {
165 return &m[0];
166 }
167
168 /**
169 * @brief Single-precision floating-point 4D vector.
170 */
172
173 /**
174 * @brief Double-precision floating-point 4D vector.
175 */
177
178 /**
179 * @brief Integer 4D vector.
180 */
182
183}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.