Skip to main content

vec2.ixx File

2D vector type and utility functions. More...

Included Headers

#include <cassert> #include <cmath> #include <memory> #include <cstddef> #include <helios.math.utils> #include <helios.math.traits.FloatingPointType> #include <helios.math.concepts>

Namespaces Index

namespacehelios
namespacemath

Classes Index

structvec2<T>

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

Description

2D vector type and utility functions.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <cassert>
8#include <cmath>
9#include <memory>
10#include <cstddef>
11
12export module helios.math.types:vec2;
13
14
15import helios.math.concepts;
16import helios.math.utils;
17import helios.math.traits.FloatingPointType;
18
19export namespace helios::math {
20
21 template<helios::math::concepts::IsNumeric T>
22 struct vec3;
23
33 template<helios::math::concepts::IsNumeric T>
34 struct vec2 {
35
36 private:
40 T v[2];
41
42 public:
43
44 using Numeric_type = T;
45
49 constexpr vec2() noexcept : v{static_cast<T>(0), static_cast<T>(0)} {}
50
51
58 constexpr vec2(const T x, const T y) noexcept : v{x, y} {}
59
65 constexpr helios::math::vec3<T> toVec3() const {
66 return helios::math::vec3<T>{v[0], v[1], static_cast<T>(0)};
67 }
68
77 constexpr const T& operator[](const size_t i) const noexcept {
78 assert(i <= 1 && "vec2 - Index out of bounds.");
79 return this->v[i];
80 }
81
90 constexpr T& operator[](const size_t i) noexcept {
91 assert(i <= 1 && "vec2 - Index out of bounds.");
92 return this->v[i];
93 }
94
100 inline FloatingPointType<T> length() const noexcept {
101 if (v[0] == 0 && v[1] == 0) {
102 return static_cast<FloatingPointType<T>>(0);
103 }
104 return std::hypot(v[0], v[1]);
105 }
106
107
116 constexpr bool isNormalized() const noexcept {
117 const auto lenSquared =
118 static_cast<FloatingPointType<T>>(v[0]) * static_cast<FloatingPointType<T>>(v[0]) +
119 static_cast<FloatingPointType<T>>(v[1]) * static_cast<FloatingPointType<T>>(v[1]);
120
121 return std::abs(lenSquared - static_cast<FloatingPointType<T>>(1.0)) <= helios::math::EPSILON_LENGTH;
122 }
123
129 inline vec2<FloatingPointType<T>> normalize() const noexcept {
130 if (v[0] == 0 && v[1] == 0) {
132 static_cast<FloatingPointType<T>>(0),
133 static_cast<FloatingPointType<T>>(0)
134 );
135 }
137 static_cast<FloatingPointType<T>>(v[0]) / length(),
138 static_cast<FloatingPointType<T>>(v[1]) / length()
139 );
140 }
141
150 constexpr bool operator==(const vec2<T>& rgt) const {
151 return v[0] == rgt[0] && v[1] == rgt[1];
152 }
153
171 constexpr bool same(const vec2<T>& rgt, T epsilon = 0.0001) const requires std::is_floating_point_v<T> {
172 if constexpr (std::is_integral_v<T>) {
173 return *this == rgt;
174 }
175
176 return std::fabs(v[0] - rgt[0]) <= epsilon &&
177 std::fabs(v[1] - rgt[1]) <= epsilon;
178 }
179
180 constexpr bool same(const vec2<T>& rgt) const requires std::is_integral_v<T> {
181 return *this == rgt;
182 }
183
184
185 };
186
197 template<helios::math::concepts::IsNumeric T>
198 constexpr vec2<T> operator*(const vec2<T>& v, const T n) noexcept {
199 return vec2<T>{v[0] * n, v[1] * n};
200 }
201
202
212 template<helios::math::concepts::IsNumeric T>
213 constexpr T dot(const vec2<T>& v1, const vec2<T>& v2) noexcept {
214 return v1[0]*v2[0] + v1[1]*v2[1];
215 }
216
221
222}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.