HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
point2.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "vector2.hpp"
8#include "extent2.hpp"
9#include "../SIMD/module.hpp"
10#include "../utility/utility.hpp"
11#include "../macros.hpp"
12#include <format>
13#include <concepts>
14
15
16
17namespace hi::inline v1 {
18
26class point2 {
27public:
28 using array_type = simd<float, 4>;
29 using value_type = array_type::value_type;
30
31 constexpr point2(point2 const&) noexcept = default;
32 constexpr point2(point2&&) noexcept = default;
33 constexpr point2& operator=(point2 const&) noexcept = default;
34 constexpr point2& operator=(point2&&) noexcept = default;
35
38 [[nodiscard]] constexpr explicit operator array_type() const noexcept
39 {
40 return _v;
41 }
42
45 [[nodiscard]] constexpr explicit point2(array_type const& other) noexcept : _v(other)
46 {
47 hi_axiom(holds_invariant());
48 }
49
52 [[nodiscard]] constexpr point2() noexcept : _v(0.0f, 0.0f, 0.0f, 1.0f) {}
53
58 [[nodiscard]] constexpr point2(float x, float y) noexcept : _v(x, y, 0.0f, 1.0f) {}
59
63 [[nodiscard]] constexpr float& x() noexcept
64 {
65 return _v.x();
66 }
67
71 [[nodiscard]] constexpr float& y() noexcept
72 {
73 return _v.y();
74 }
75
79 [[nodiscard]] constexpr float x() const noexcept
80 {
81 return _v.x();
82 }
83
87 [[nodiscard]] constexpr float y() const noexcept
88 {
89 return _v.y();
90 }
91
92 constexpr point2& operator+=(vector2 const& rhs) noexcept
93 {
94 return *this = *this + rhs;
95 }
96
97 constexpr point2& operator-=(vector2 const& rhs) noexcept
98 {
99 return *this = *this - rhs;
100 }
101
107 [[nodiscard]] constexpr friend point2 operator+(point2 const& lhs, vector2 const& rhs) noexcept
108 {
109 return point2{lhs._v + array_type{rhs}};
110 }
111
117 [[nodiscard]] constexpr friend point2 operator+(vector2 const& lhs, point2 const& rhs) noexcept
118 {
119 return point2{array_type{lhs} + rhs._v};
120 }
121
127 [[nodiscard]] constexpr friend point2 operator-(point2 const& lhs, vector2 const& rhs) noexcept
128 {
129 return point2{lhs._v - array_type{rhs}};
130 }
131
137 [[nodiscard]] constexpr friend vector2 operator-(point2 const& lhs, point2 const& rhs) noexcept
138 {
139 return vector2{lhs._v - rhs._v};
140 }
141
147 [[nodiscard]] constexpr friend bool operator==(point2 const& lhs, point2 const& rhs) noexcept
148 {
149 return equal(lhs._v, rhs._v);
150 }
151
152 [[nodiscard]] friend constexpr point2 midpoint(point2 const& lhs, point2 const& rhs) noexcept
153 {
154 return point2{midpoint(lhs._v, rhs._v)};
155 }
156
157 [[nodiscard]] friend constexpr point2 reflect(point2 const& lhs, point2 const& rhs) noexcept
158 {
159 return point2{reflect_point(lhs._v, rhs._v)};
160 }
161
167 [[nodiscard]] friend constexpr point2 min(point2 const& lhs, point2 const& rhs) noexcept
168 {
169 return point2{min(lhs._v, rhs._v)};
170 }
171
177 [[nodiscard]] friend constexpr point2 max(point2 const& lhs, point2 const& rhs) noexcept
178 {
179 return point2{max(lhs._v, rhs._v)};
180 }
181
184 [[nodiscard]] friend constexpr point2 round(point2 const& rhs) noexcept
185 {
186 return point2{round(rhs._v)};
187 }
188
191 [[nodiscard]] friend constexpr point2 ceil(point2 const& rhs) noexcept
192 {
193 return point2{ceil(rhs._v)};
194 }
195
198 [[nodiscard]] friend constexpr point2 floor(point2 const& rhs) noexcept
199 {
200 return point2{floor(rhs._v)};
201 }
202
205 [[nodiscard]] friend constexpr point2 ceil(point2 const& lhs, extent2 rhs) noexcept
206 {
207 hilet rhs_ = array_type{rhs}.xy11();
208 return point2{ceil(lhs._v / rhs_) * rhs_};
209 }
210
213 [[nodiscard]] friend constexpr point2 floor(point2 const& lhs, extent2 rhs) noexcept
214 {
215 hilet rhs_ = array_type{rhs}.xy11();
216 return point2{floor(lhs._v / rhs_) * rhs_};
217 }
218
219 [[nodiscard]] friend float distance(point2 const& lhs, point2 const& rhs) noexcept
220 {
221 return hypot(rhs - lhs);
222 }
223
227 [[nodiscard]] constexpr bool holds_invariant() const noexcept
228 {
229 return _v.z() == 0.0f and _v.w() != 0.0f;
230 }
231
232 [[nodiscard]] friend std::string to_string(point2 const& rhs) noexcept
233 {
234 return std::format("<{}, {}>", rhs._v.x(), rhs._v.y());
235 }
236
237 friend std::ostream& operator<<(std::ostream& lhs, point2 const& rhs) noexcept
238 {
239 return lhs << to_string(rhs);
240 }
241
242private:
243 array_type _v;
244};
245
246} // namespace hi::inline v1
247
248template<typename CharT>
249struct std::formatter<hi::point2, CharT> {
250 auto parse(auto& pc)
251 {
252 return pc.end();
253 }
254
255 auto format(hi::point2 const& t, auto& fc) const
256 {
257 return std::vformat_to(fc.out(), "<{}, {}>", std::make_format_args(t.x(), t.y()));
258 }
259};
Defined the geo::extent, extent2 and extent3 types.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point2.hpp:26
friend constexpr point2 min(point2 const &lhs, point2 const &rhs) noexcept
Mix the two points and get the lowest value of each element.
Definition point2.hpp:167
constexpr bool holds_invariant() const noexcept
Check if the point is valid.
Definition point2.hpp:227
constexpr friend vector2 operator-(point2 const &lhs, point2 const &rhs) noexcept
Find the vector between two points.
Definition point2.hpp:137
constexpr friend bool operator==(point2 const &lhs, point2 const &rhs) noexcept
Compare if two points are equal.
Definition point2.hpp:147
friend constexpr point2 ceil(point2 const &lhs, extent2 rhs) noexcept
Round the coordinates of a point toward the top-right with the given granularity.
Definition point2.hpp:205
constexpr float & y() noexcept
Access the y element from the point.
Definition point2.hpp:71
friend constexpr point2 floor(point2 const &lhs, extent2 rhs) noexcept
Round the coordinates of a point toward the left-bottom with the given granularity.
Definition point2.hpp:213
friend constexpr point2 round(point2 const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point2.hpp:184
constexpr point2(float x, float y) noexcept
Construct a 3D point from x, y and z elements.
Definition point2.hpp:58
constexpr friend point2 operator-(point2 const &lhs, vector2 const &rhs) noexcept
Move a point backward along the vector.
Definition point2.hpp:127
constexpr float & x() noexcept
Access the x element from the point.
Definition point2.hpp:63
friend constexpr point2 max(point2 const &lhs, point2 const &rhs) noexcept
Mix the two points and get the highest value of each element.
Definition point2.hpp:177
friend constexpr point2 ceil(point2 const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point2.hpp:191
constexpr float y() const noexcept
Access the y element from the point.
Definition point2.hpp:87
constexpr point2(array_type const &other) noexcept
Construct a point from a array_type-simd.
Definition point2.hpp:45
constexpr friend point2 operator+(point2 const &lhs, vector2 const &rhs) noexcept
Move a point along a vector.
Definition point2.hpp:107
constexpr friend point2 operator+(vector2 const &lhs, point2 const &rhs) noexcept
Move a point along a vector.
Definition point2.hpp:117
friend constexpr point2 floor(point2 const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point2.hpp:198
constexpr float x() const noexcept
Access the x element from the point.
Definition point2.hpp:79
constexpr point2() noexcept
Construct a point at the origin of the coordinate system.
Definition point2.hpp:52