7#include "../rapid/numeric_array.hpp"
13namespace hi::inline
v1 {
23template<
typename T,
int D>
29 static_assert(D == 2 || D == 3,
"Only 2D or 3D points are supported");
31 constexpr point(
point const&)
noexcept =
default;
33 constexpr point& operator=(
point const&)
noexcept =
default;
34 constexpr point& operator=(
point&&)
noexcept =
default;
63 _v[i] = value_type{0};
70 [[nodiscard]]
constexpr explicit operator array_type() const noexcept
78 [[nodiscard]]
constexpr explicit point(
array_type const& other) noexcept : _v(other)
85 [[nodiscard]]
constexpr point() noexcept : _v(value_type{0}, value_type{0}, value_type{0}, value_type{1}) {}
91 [[nodiscard]]
constexpr point(value_type x, value_type y)
noexcept
93 : _v(x, y, value_type{0}, value_type{1})
102 [[nodiscard]]
constexpr point(value_type x, value_type y, value_type z = value_type{0})
noexcept
104 : _v(x, y, z, value_type{1})
111 [[nodiscard]]
constexpr value_type&
x() noexcept
119 [[nodiscard]]
constexpr value_type&
y() noexcept
127 [[nodiscard]]
constexpr value_type&
z() noexcept
136 [[nodiscard]]
constexpr value_type
const&
x() const noexcept
144 [[nodiscard]]
constexpr value_type
const&
y() const noexcept
152 [[nodiscard]]
constexpr value_type
const&
z() const noexcept
162 hi_axiom(holds_invariant() && rhs.holds_invariant());
163 _v = _v +
static_cast<array_type
>(rhs);
169 constexpr point&
operator-=(vector<value_type, E>
const& rhs)
noexcept
171 hi_axiom(holds_invariant() && rhs.holds_invariant());
172 _v = _v -
static_cast<array_type
>(rhs);
184 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
196 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
208 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
219 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
230 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
231 return lhs._v == rhs._v;
237 return point<value_type,
std::max(D, E)>{midpoint(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
241 [[nodiscard]]
friend constexpr auto reflect(point
const& lhs, point<value_type, E>
const& rhs)
noexcept
243 return point<value_type,
std::max(D, E)>{reflect_point(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
271 requires std::is_same_v<value_type, float>
279 requires std::is_same_v<value_type, float>
287 requires std::is_same_v<value_type, float>
294 [[nodiscard]]
friend constexpr point ceil(
point const& lhs, extent<value_type, D> rhs)
noexcept
295 requires std::is_same_v<value_type, float>
303 [[nodiscard]]
friend constexpr point ceil(
point const& lhs, extent<value_type, D> rhs)
noexcept
304 requires std::is_same_v<value_type, int>
308 return point{(lhs_ + (rhs_ - 1)) / rhs_ * rhs_};
313 [[nodiscard]]
friend constexpr point floor(
point const& lhs, extent<value_type, D> rhs)
noexcept
314 requires std::is_same_v<value_type, float>
322 [[nodiscard]]
friend constexpr point floor(
point const& lhs, extent<value_type, D> rhs)
noexcept
323 requires std::is_same_v<value_type, int>
327 return point{lhs_ / rhs_ * rhs_};
330 [[nodiscard]]
friend constexpr value_type distance(
point const& lhs,
point const& rhs)
noexcept
332 return hypot(rhs - lhs);
340 return _v.w() != value_type{0} && (D == 3 || _v.z() == value_type{0});
345 if constexpr (D == 2) {
346 return std::format(
"<{}, {}>", rhs._v.x(), rhs._v.y());
347 }
else if constexpr (D == 3) {
348 return std::format(
"<{}, {}, {}>", rhs._v.x(), rhs._v.y(), rhs._v.z());
365using point2 = geo::point<float, 2>;
366using point3 = geo::point<float, 3>;
367using point2i = geo::point<int, 2>;
368using point3i = geo::point<int, 3>;
371[[nodiscard]]
constexpr point2
narrow_cast(point2i
const& rhs)
noexcept
373 return {narrow_cast<float>(rhs.x()), narrow_cast<float>(rhs.y())};
378template<
typename CharT>
379struct std::formatter<
hi::geo::point<float, 2>, CharT> {
385 auto format(hi::geo::point<float, 2>
const& t,
auto& fc)
387 return std::vformat_to(fc.out(),
"<{}, {}>", std::make_format_args(t.x(), t.y()));
391template<
typename CharT>
392struct std::formatter<
hi::geo::point<float, 3>, CharT> {
398 auto format(hi::geo::point<float, 3>
const& t,
auto& fc)
400 return std::vformat_to(fc.out(),
"<{}, {}, {}>", std::make_format_args(t.x(), t.y(), t.z()));
404template<
typename CharT>
405struct std::formatter<
hi::geo::point<int, 2>, CharT> {
411 auto format(hi::geo::point<int, 2>
const& t,
auto& fc)
413 return std::vformat_to(fc.out(),
"<{}, {}>", std::make_format_args(t.x(), t.y()));
417template<
typename CharT>
418struct std::formatter<
hi::geo::point<int, 3>, CharT> {
424 auto format(hi::geo::point<int, 3>
const& t,
auto& fc)
426 return std::vformat_to(fc.out(),
"<{}, {}, {}>", std::make_format_args(t.x(), t.y(), t.z()));
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:181
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
Defined the geo::extent, extent2 and extent3 types.
DOXYGEN BUG.
Definition algorithm.hpp:15
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:178
geometry/margins.hpp
Definition assert.hpp:18
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point.hpp:24
friend constexpr point ceil(point const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point.hpp:278
constexpr value_type & y() noexcept
Access the y element from the point.
Definition point.hpp:119
constexpr value_type const & x() const noexcept
Access the x element from the point.
Definition point.hpp:136
constexpr point() noexcept
Construct a point at the origin of the coordinate system.
Definition point.hpp:85
friend constexpr auto max(point const &lhs, point< value_type, E > const &rhs) noexcept
Mix the two points and get the highest value of each element.
Definition point.hpp:263
constexpr friend auto operator-(point const &lhs, vector< value_type, E > const &rhs) noexcept
Move a point backward along the vector.
Definition point.hpp:206
constexpr point(value_type x, value_type y) noexcept
Construct a 2D point from x and y elements.
Definition point.hpp:91
constexpr friend vector< value_type, D > operator-(point const &lhs, point const &rhs) noexcept
Find the vector between two points.
Definition point.hpp:217
constexpr point(point< value_type, E > const &other) noexcept
Construct a point from a lower dimension point.
Definition point.hpp:40
constexpr point(point< value_type, E > const &other) noexcept
Construct a point from a higher dimension point.
Definition point.hpp:60
friend constexpr point floor(point const &lhs, extent< value_type, D > rhs) noexcept
Round the coordinates of a point toward the left-bottom with the given granularity.
Definition point.hpp:322
friend constexpr point ceil(point const &lhs, extent< value_type, D > rhs) noexcept
Round the coordinates of a point toward the top-right with the given granularity.
Definition point.hpp:294
friend constexpr auto min(point const &lhs, point< value_type, E > const &rhs) noexcept
Mix the two points and get the lowest value of each element.
Definition point.hpp:252
constexpr friend auto operator+(point const &lhs, vector< value_type, E > const &rhs) noexcept
Move a point along a vector.
Definition point.hpp:182
friend constexpr point floor(point const &lhs, extent< value_type, D > rhs) noexcept
Round the coordinates of a point toward the left-bottom with the given granularity.
Definition point.hpp:313
friend constexpr point ceil(point const &lhs, extent< value_type, D > rhs) noexcept
Round the coordinates of a point toward the top-right with the given granularity.
Definition point.hpp:303
constexpr point(array_type const &other) noexcept
Construct a point from a array_type-numeric_array.
Definition point.hpp:78
friend constexpr point floor(point const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point.hpp:286
constexpr value_type & x() noexcept
Access the x element from the point.
Definition point.hpp:111
constexpr value_type const & y() const noexcept
Access the y element from the point.
Definition point.hpp:144
constexpr bool holds_invariant() const noexcept
Check if the point is valid.
Definition point.hpp:338
constexpr friend bool operator==(point const &lhs, point const &rhs) noexcept
Compare if two points are equal.
Definition point.hpp:228
constexpr point(value_type x, value_type y, value_type z=value_type{0}) noexcept
Construct a 3D point from x, y and z elements.
Definition point.hpp:102
constexpr value_type const & z() const noexcept
Access the z element from the point.
Definition point.hpp:152
friend constexpr point round(point const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point.hpp:270
constexpr point(point< value_type, 2 > const &other, value_type z) noexcept
Construct a point from a lower dimension point.
Definition point.hpp:47
constexpr value_type & z() noexcept
Access the z element from the point.
Definition point.hpp:127
constexpr friend auto operator+(vector< value_type, E > const &rhs, point const &lhs) noexcept
Move a point along a vector.
Definition point.hpp:194
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:21