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 "../utility/utility.hpp"
10#include "../macros.hpp"
11#include <hikocpu/hikocpu.hpp>
12#include <format>
13#include <concepts>
14#include <exception>
15#include <compare>
16
17hi_export_module(hikogui.geometry : point2);
18
19hi_export namespace hi::inline v1 {
20
28class point2 {
29public:
30 using array_type = simd<float, 4>;
31 using value_type = array_type::value_type;
32
33 constexpr point2(point2 const&) noexcept = default;
34 constexpr point2(point2&&) noexcept = default;
35 constexpr point2& operator=(point2 const&) noexcept = default;
36 constexpr point2& operator=(point2&&) noexcept = default;
37
40 [[nodiscard]] constexpr explicit operator array_type() const noexcept
41 {
42 return _v;
43 }
44
47 [[nodiscard]] constexpr explicit point2(array_type const& other) noexcept : _v(other)
48 {
49 hi_axiom(holds_invariant());
50 }
51
54 [[nodiscard]] constexpr point2() noexcept : _v(0.0f, 0.0f, 0.0f, 1.0f) {}
55
60 [[nodiscard]] constexpr point2(float x, float y) noexcept : _v(x, y, 0.0f, 1.0f) {}
61
65 [[nodiscard]] constexpr float& x() noexcept
66 {
67 return _v.x();
68 }
69
73 [[nodiscard]] constexpr float& y() noexcept
74 {
75 return _v.y();
76 }
77
81 [[nodiscard]] constexpr float x() const noexcept
82 {
83 return _v.x();
84 }
85
89 [[nodiscard]] constexpr float y() const noexcept
90 {
91 return _v.y();
92 }
93
94 constexpr point2& operator+=(vector2 const& rhs) noexcept
95 {
96 return *this = *this + rhs;
97 }
98
99 constexpr point2& operator-=(vector2 const& rhs) noexcept
100 {
101 return *this = *this - rhs;
102 }
103
109 [[nodiscard]] constexpr friend point2 operator+(point2 const& lhs, vector2 const& rhs) noexcept
110 {
111 return point2{lhs._v + array_type{rhs}};
112 }
113
119 [[nodiscard]] constexpr friend point2 operator+(vector2 const& lhs, point2 const& rhs) noexcept
120 {
121 return point2{array_type{lhs} + rhs._v};
122 }
123
129 [[nodiscard]] constexpr friend point2 operator-(point2 const& lhs, vector2 const& rhs) noexcept
130 {
131 return point2{lhs._v - array_type{rhs}};
132 }
133
139 [[nodiscard]] constexpr friend vector2 operator-(point2 const& lhs, point2 const& rhs) noexcept
140 {
141 return vector2{lhs._v - rhs._v};
142 }
143
149 [[nodiscard]] constexpr friend bool operator==(point2 const& lhs, point2 const& rhs) noexcept
150 {
151 return equal(lhs._v, rhs._v);
152 }
153
154 [[nodiscard]] friend constexpr point2 midpoint(point2 const& lhs, point2 const& rhs) noexcept
155 {
156 return point2{(lhs._v + rhs._v) * array_type::broadcast(0.5f)};
157 }
158
165 [[nodiscard]] friend constexpr point2 reflect(point2 const& lhs, point2 const& rhs) noexcept
166 {
167 return point2{rhs._v - (lhs._v - rhs._v)};
168 }
169
175 [[nodiscard]] friend constexpr point2 min(point2 const& lhs, point2 const& rhs) noexcept
176 {
177 return point2{min(lhs._v, rhs._v)};
178 }
179
185 [[nodiscard]] friend constexpr point2 max(point2 const& lhs, point2 const& rhs) noexcept
186 {
187 return point2{max(lhs._v, rhs._v)};
188 }
189
192 [[nodiscard]] friend constexpr point2 round(point2 const& rhs) noexcept
193 {
194 return point2{round(rhs._v)};
195 }
196
199 [[nodiscard]] friend constexpr point2 ceil(point2 const& rhs) noexcept
200 {
201 return point2{ceil(rhs._v)};
202 }
203
206 [[nodiscard]] friend constexpr point2 floor(point2 const& rhs) noexcept
207 {
208 return point2{floor(rhs._v)};
209 }
210
213 [[nodiscard]] friend constexpr point2 ceil(point2 const& lhs, extent2 rhs) noexcept
214 {
215 auto const rhs_ = array_type{rhs}.xy11();
216 return point2{ceil(lhs._v / rhs_) * rhs_};
217 }
218
221 [[nodiscard]] friend constexpr point2 floor(point2 const& lhs, extent2 rhs) noexcept
222 {
223 auto const rhs_ = array_type{rhs}.xy11();
224 return point2{floor(lhs._v / rhs_) * rhs_};
225 }
226
227 [[nodiscard]] friend float distance(point2 const& lhs, point2 const& rhs) noexcept
228 {
229 return hypot(rhs - lhs);
230 }
231
235 [[nodiscard]] constexpr bool holds_invariant() const noexcept
236 {
237 return _v.z() == 0.0f and _v.w() != 0.0f;
238 }
239
240 [[nodiscard]] friend std::string to_string(point2 const& rhs) noexcept
241 {
242 return std::format("<{}, {}>", rhs._v.x(), rhs._v.y());
243 }
244
245 friend std::ostream& operator<<(std::ostream& lhs, point2 const& rhs) noexcept
246 {
247 return lhs << to_string(rhs);
248 }
249
250private:
251 array_type _v;
252};
253
254} // namespace hi::inline v1
255
256// XXX #617 MSVC bug does not handle partial specialization in modules.
257hi_export template<>
258struct std::formatter<hi::point2, char> : std::formatter<std::string, char> {
259 auto format(hi::point2 const& t, auto& fc) const
260 {
261 return std::formatter<std::string, char>::format(std::format("<{}, {}>", t.x(), t.y()), fc);
262 }
263};
Defined the geo::extent, extent2 and extent3 types.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point2.hpp:28
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:175
constexpr bool holds_invariant() const noexcept
Check if the point is valid.
Definition point2.hpp:235
constexpr friend vector2 operator-(point2 const &lhs, point2 const &rhs) noexcept
Find the vector between two points.
Definition point2.hpp:139
constexpr friend bool operator==(point2 const &lhs, point2 const &rhs) noexcept
Compare if two points are equal.
Definition point2.hpp:149
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:213
constexpr float & y() noexcept
Access the y element from the point.
Definition point2.hpp:73
friend constexpr point2 reflect(point2 const &lhs, point2 const &rhs) noexcept
Reflect a point.
Definition point2.hpp:165
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:221
friend constexpr point2 round(point2 const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point2.hpp:192
constexpr point2(float x, float y) noexcept
Construct a 3D point from x, y and z elements.
Definition point2.hpp:60
constexpr friend point2 operator-(point2 const &lhs, vector2 const &rhs) noexcept
Move a point backward along the vector.
Definition point2.hpp:129
constexpr float & x() noexcept
Access the x element from the point.
Definition point2.hpp:65
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:185
friend constexpr point2 ceil(point2 const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point2.hpp:199
constexpr float y() const noexcept
Access the y element from the point.
Definition point2.hpp:89
constexpr point2(array_type const &other) noexcept
Construct a point from a array_type-simd.
Definition point2.hpp:47
constexpr friend point2 operator+(point2 const &lhs, vector2 const &rhs) noexcept
Move a point along a vector.
Definition point2.hpp:109
constexpr friend point2 operator+(vector2 const &lhs, point2 const &rhs) noexcept
Move a point along a vector.
Definition point2.hpp:119
friend constexpr point2 floor(point2 const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point2.hpp:206
constexpr float x() const noexcept
Access the x element from the point.
Definition point2.hpp:81
constexpr point2() noexcept
Construct a point at the origin of the coordinate system.
Definition point2.hpp:54