feat(math): add constexpr to some functions & add equality comparison operators

This commit is contained in:
light7734 2025-09-18 19:19:32 +03:30
parent 9e6300f1aa
commit bb6485488c
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
3 changed files with 35 additions and 8 deletions

View file

@ -5,18 +5,28 @@ namespace lt::math {
template<typename T = float> template<typename T = float>
struct vec2_impl struct vec2_impl
{ {
vec2_impl(): x(), y() constexpr vec2_impl(): x(), y()
{ {
} }
explicit vec2_impl(T scalar): x(scalar), y(scalar) constexpr explicit vec2_impl(T scalar): x(scalar), y(scalar)
{ {
} }
vec2_impl(T x, T y): x(x), y(y) constexpr vec2_impl(T x, T y): x(x), y(y)
{ {
} }
[[nodiscard]] auto operator==(const vec2_impl<T> &other) const -> bool
{
return x == other.x && y == other.y;
}
[[nodiscard]] auto operator!=(const vec2_impl<T> &other) const -> bool
{
return !(*this == other);
}
[[nodiscard]] auto operator*(const vec2_impl<T> &other) const -> vec2_impl [[nodiscard]] auto operator*(const vec2_impl<T> &other) const -> vec2_impl
{ {
return { return {
@ -47,6 +57,7 @@ struct vec2_impl
T y; // NOLINT T y; // NOLINT
}; };
using vec2 = vec2_impl<float>; using vec2 = vec2_impl<float>;
using ivec2 = vec2_impl<int32_t>; using ivec2 = vec2_impl<int32_t>;

View file

@ -7,19 +7,29 @@ namespace lt::math {
template<typename T = float> template<typename T = float>
struct vec3_impl struct vec3_impl
{ {
vec3_impl(): x(), y(), z() constexpr vec3_impl(): x(), y(), z()
{ {
} }
explicit vec3_impl(T scalar): x(scalar), y(scalar), z(scalar) constexpr explicit vec3_impl(T scalar): x(scalar), y(scalar), z(scalar)
{ {
} }
vec3_impl(T x, T y, T z): x(x), y(y), z(z) constexpr vec3_impl(T x, T y, T z): x(x), y(y), z(z)
{ {
} }
[[nodiscard]] auto operator-(const vec3_impl<T> &other) const -> vec3_impl [[nodiscard]] auto operator==(const vec3_impl<T> &other) const -> bool
{
return x == other.x && y == other.y && z == other.z;
}
[[nodiscard]] auto operator!=(const vec3_impl<T> &other) const -> bool
{
return !(*this == other);
}
[[nodiscard]] constexpr auto operator-(const vec3_impl<T> &other) const -> vec3_impl
{ {
return { return {
x - other.x, x - other.x,
@ -28,7 +38,7 @@ struct vec3_impl
}; };
} }
[[nodiscard]] auto operator*(const vec3_impl<T> &other) const -> vec3_impl [[nodiscard]] constexpr auto operator*(const vec3_impl<T> &other) const -> vec3_impl
{ {
return { return {
x * other.x, x * other.x,

View file

@ -39,6 +39,12 @@ struct vec4_impl
return values[idx]; return values[idx];
} }
friend auto operator<<(std::ostream &stream, vec4_impl<T> value) -> std::ostream &
{
stream << value.x << ", " << value.y << ", " << value.z << ", " << value.w;
return stream;
}
// NOLINTNEXTLINE // NOLINTNEXTLINE
union union
{ {