light/modules/math/vec2.cppm

112 lines
2.1 KiB
Text
Raw Normal View History

export module math.vec2;
2026-01-20 09:58:35 +03:30
import preliminary;
namespace lt::math {
2026-01-20 09:58:35 +03:30
export template<typename T = f32>
2026-01-20 11:07:35 +03:30
requires(std::is_arithmetic_v<T>)
struct vec2_impl
{
2026-01-20 11:07:35 +03:30
static constexpr auto num_elements = 2u;
constexpr vec2_impl(): x(), y()
{
}
constexpr explicit vec2_impl(T scalar): x(scalar), y(scalar)
{
}
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);
}
2026-01-20 11:07:35 +03:30
[[nodiscard]] constexpr auto operator+(const vec2_impl<T> &other) const -> vec2_impl
{
return {
2026-01-20 11:07:35 +03:30
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,
};
}
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
{
return {
2026-01-20 11:07:35 +03:30
x / other.x,
y / other.y,
};
}
2026-01-20 11:07:35 +03:30
[[nodiscard]] constexpr auto operator[](u8 idx) -> T &
{
debug_check(idx <= num_elements, "vec2 out of bound: {}", idx);
return ((T *)this)[idx];
}
[[nodiscard]] constexpr auto operator[](u8 idx) const -> const T &
{
debug_check(idx < num_elements, "vec2 out of bound: {}", idx);
return ((T *)this)[idx];
}
friend auto operator<<(std::ostream &stream, vec2_impl<T> value) -> std::ostream &
{
stream << value.x << ", " << value.y;
return stream;
}
T x;
2026-01-20 11:07:35 +03:30
T y;
};
2026-01-20 09:58:35 +03:30
export using vec2 = vec2_impl<f32>;
2026-01-20 09:58:35 +03:30
export using ivec2 = vec2_impl<i32>;
2026-01-20 09:58:35 +03:30
export using uvec2 = vec2_impl<u32>;
} // namespace lt::math
export template<typename T>
struct std::formatter<lt::math::vec2_impl<T>>
{
constexpr auto parse(std::format_parse_context &context)
{
return context.begin();
}
auto format(const lt::math::vec2_impl<T> &val, std::format_context &context) const
{
return std::format_to(context.out(), "{}, {}", val.x, val.y);
}
};