2025-11-30 09:46:48 +03:30
|
|
|
export module math.vec2;
|
2025-11-04 18:50:59 +03:30
|
|
|
|
2026-01-20 09:58:35 +03:30
|
|
|
import preliminary;
|
2025-07-17 10:44:00 +03:30
|
|
|
|
2026-01-20 13:22:30 +03:30
|
|
|
export namespace lt::math {
|
2025-11-30 09:46:48 +03:30
|
|
|
|
2026-01-20 13:22:30 +03:30
|
|
|
template<typename T = f32>
|
2026-01-20 11:07:35 +03:30
|
|
|
requires(std::is_arithmetic_v<T>)
|
2025-07-17 10:44:00 +03:30
|
|
|
struct vec2_impl
|
|
|
|
|
{
|
2026-02-02 13:55:59 +03:30
|
|
|
using Underlying_T = T;
|
|
|
|
|
|
2026-01-20 11:07:35 +03:30
|
|
|
static constexpr auto num_elements = 2u;
|
|
|
|
|
|
2025-11-30 09:46:48 +03:30
|
|
|
constexpr vec2_impl(): x(), y()
|
|
|
|
|
{
|
|
|
|
|
}
|
2025-07-17 10:44:00 +03:30
|
|
|
|
2025-09-18 19:19:32 +03:30
|
|
|
constexpr explicit vec2_impl(T scalar): x(scalar), y(scalar)
|
2025-07-17 10:44:00 +03:30
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 19:19:32 +03:30
|
|
|
constexpr vec2_impl(T x, T y): x(x), y(y)
|
2025-07-17 10:44:00 +03:30
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 19:19:32 +03:30
|
|
|
[[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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 11:07:35 +03:30
|
|
|
[[nodiscard]] constexpr auto operator+(const vec2_impl<T> &other) const -> vec2_impl
|
2025-07-17 10:44:00 +03:30
|
|
|
{
|
|
|
|
|
return {
|
2026-01-20 11:07:35 +03:30
|
|
|
x + other.x,
|
|
|
|
|
y + other.y,
|
2025-07-17 10:44:00 +03:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 11:07:35 +03:30
|
|
|
[[nodiscard]] constexpr auto operator-(const vec2_impl<T> &other) const -> vec2_impl
|
2025-07-17 10:44:00 +03:30
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
x - other.x,
|
|
|
|
|
y - other.y,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 11:07:35 +03:30
|
|
|
[[nodiscard]] constexpr auto operator*(const vec2_impl<T> &other) const -> vec2_impl
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
x * other.x,
|
|
|
|
|
y * other.y,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto operator/(const vec2_impl<T> &other) const -> vec2_impl
|
2025-07-17 10:44:00 +03:30
|
|
|
{
|
|
|
|
|
return {
|
2026-01-20 11:07:35 +03:30
|
|
|
x / other.x,
|
|
|
|
|
y / other.y,
|
2025-07-17 10:44:00 +03:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 11:07:35 +03:30
|
|
|
[[nodiscard]] constexpr auto operator[](u8 idx) -> T &
|
|
|
|
|
{
|
2026-02-02 13:55:59 +03:30
|
|
|
debug_check(idx < num_elements, "vec2 out of bound access: {}", idx);
|
2026-01-20 11:07:35 +03:30
|
|
|
return ((T *)this)[idx];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto operator[](u8 idx) const -> const T &
|
|
|
|
|
{
|
2026-02-02 13:55:59 +03:30
|
|
|
debug_check(idx < num_elements, "vec2 out of bound access: {}", idx);
|
2026-01-20 11:07:35 +03:30
|
|
|
return ((T *)this)[idx];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T x;
|
2025-07-17 10:44:00 +03:30
|
|
|
|
2026-01-20 11:07:35 +03:30
|
|
|
T y;
|
2025-07-17 10:44:00 +03:30
|
|
|
};
|
|
|
|
|
|
2026-01-20 13:22:30 +03:30
|
|
|
using vec2 = vec2_impl<f32>;
|
2025-07-17 10:44:00 +03:30
|
|
|
|
2026-02-02 13:55:59 +03:30
|
|
|
using vec2_f32 = vec2;
|
|
|
|
|
using vec2_f64 = vec2_impl<f64>;
|
|
|
|
|
|
|
|
|
|
using vec2_i8 = vec2_impl<i8>;
|
|
|
|
|
using vec2_i16 = vec2_impl<i16>;
|
|
|
|
|
using vec2_i32 = vec2_impl<i32>;
|
|
|
|
|
using vec2_i64 = vec2_impl<i64>;
|
2025-07-17 10:44:00 +03:30
|
|
|
|
2026-02-02 13:55:59 +03:30
|
|
|
using vec2_u8 = vec2_impl<u8>;
|
|
|
|
|
using vec2_u16 = vec2_impl<u16>;
|
|
|
|
|
using vec2_u32 = vec2_impl<u32>;
|
|
|
|
|
using vec2_u64 = vec2_impl<u64>;
|
2025-07-17 10:44:00 +03:30
|
|
|
|
2025-11-30 09:46:48 +03:30
|
|
|
} // namespace lt::math
|
2025-09-18 19:21:52 +03:30
|
|
|
|
2025-11-04 18:50:59 +03:30
|
|
|
export template<typename T>
|
2025-11-30 09:46:48 +03:30
|
|
|
struct std::formatter<lt::math::vec2_impl<T>>
|
2025-09-18 19:21:52 +03:30
|
|
|
{
|
2025-11-30 09:46:48 +03:30
|
|
|
constexpr auto parse(std::format_parse_context &context)
|
2025-09-18 19:21:52 +03:30
|
|
|
{
|
|
|
|
|
return context.begin();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 09:46:48 +03:30
|
|
|
auto format(const lt::math::vec2_impl<T> &val, std::format_context &context) const
|
2025-09-18 19:21:52 +03:30
|
|
|
{
|
2025-11-30 09:46:48 +03:30
|
|
|
return std::format_to(context.out(), "{}, {}", val.x, val.y);
|
2025-09-18 19:21:52 +03:30
|
|
|
}
|
|
|
|
|
};
|