HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
point.hpp
1// Copyright Take Vos 2021.
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 "numeric_array.hpp"
8#include "vector.hpp"
9
10namespace tt {
11namespace geo {
12
20template<int D>
21class point {
22public:
23 static_assert(D == 2 || D == 3, "Only 2D or 3D points are supported");
24
25 constexpr point(point const &) noexcept = default;
26 constexpr point(point &&) noexcept = default;
27 constexpr point &operator=(point const &) noexcept = default;
28 constexpr point &operator=(point &&) noexcept = default;
29
32 template<int E>
33 requires(E < D) [[nodiscard]] constexpr point(point<E> const &other) noexcept : _v(static_cast<f32x4>(other))
34 {
35 tt_axiom(is_valid());
36 }
37
41 template<int E>
42 requires(E > D) [[nodiscard]] constexpr explicit point(point<E> const &other) noexcept : _v(static_cast<f32x4>(other))
43 {
44 for (size_t i = D; i != E; ++i) {
45 _v[i] = 0.0f;
46 }
47 tt_axiom(is_valid());
48 }
49
52 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
53 {
54 tt_axiom(is_valid());
55 return _v;
56 }
57
60 [[nodiscard]] constexpr explicit point(f32x4 const &other) noexcept : _v(other)
61 {
62 tt_axiom(is_valid());
63 }
64
67 [[nodiscard]] constexpr point() noexcept : _v(0.0, 0.0, 0.0, 1.0) {}
68
73 [[nodiscard]] constexpr point(float x, float y) noexcept requires(D == 2) : _v(x, y, 0.0, 1.0) {}
74
80 [[nodiscard]] constexpr point(float x, float y, float z = 0.0) noexcept requires(D == 3) : _v(x, y, z, 1.0) {}
81
85 [[nodiscard]] constexpr float &x() noexcept
86 {
87 return _v.x();
88 }
89
93 [[nodiscard]] constexpr float &y() noexcept
94 {
95 return _v.y();
96 }
97
101 [[nodiscard]] constexpr float &z() noexcept requires(D == 3)
102 {
103 return _v.z();
104 }
105
109 [[nodiscard]] constexpr float const &x() const noexcept
110 {
111 return _v.x();
112 }
113
117 [[nodiscard]] constexpr float const &y() const noexcept
118 {
119 return _v.y();
120 }
121
125 [[nodiscard]] constexpr float const &z() const noexcept requires(D == 3)
126 {
127 return _v.z();
128 }
129
130 template<int E> requires (E <= D)
131 constexpr point &operator+=(vector<E> const &rhs) noexcept
132 {
133 tt_axiom(is_valid() && rhs.is_valid());
134 _v = _v + static_cast<f32x4>(rhs);
135 return *this;
136 }
137
143 template<int E>
144 [[nodiscard]] constexpr friend point<std::max(D, E)> operator+(point const &lhs, vector<E> const &rhs) noexcept
145 {
146 tt_axiom(lhs.is_valid() && rhs.is_valid());
147 return point<std::max(D, E)>{lhs._v + static_cast<f32x4>(rhs)};
148 }
149
155 template<int E>
156 [[nodiscard]] constexpr friend point<std::max(D, E)> operator+(vector<E> const &rhs, point const &lhs) noexcept
157 {
158 tt_axiom(lhs.is_valid() && rhs.is_valid());
159 return point<std::max(D, E)>{lhs._v + static_cast<f32x4>(rhs)};
160 }
161
167 template<int E>
168 [[nodiscard]] constexpr friend point<std::max(D, E)> operator-(point const &lhs, vector<E> const &rhs) noexcept
169 {
170 tt_axiom(lhs.is_valid() && rhs.is_valid());
171 return point<std::max(D, E)>{lhs._v - static_cast<f32x4>(rhs)};
172 }
173
179 [[nodiscard]] constexpr friend vector<D> operator-(point const &lhs, point const &rhs) noexcept
180 {
181 tt_axiom(lhs.is_valid() && rhs.is_valid());
182 return vector<D>{lhs._v - rhs._v};
183 }
184
190 [[nodiscard]] constexpr friend bool operator==(point const &lhs, point const &rhs) noexcept
191 {
192 tt_axiom(lhs.is_valid() && rhs.is_valid());
193 return lhs._v == rhs._v;
194 }
195
196 template<int E>
197 [[nodiscard]] friend constexpr auto midpoint(point const &lhs, point<E> const &rhs) noexcept
198 {
199 return point<std::max(D,E)>{midpoint(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
200 }
201
202 template<int E>
203 [[nodiscard]] friend constexpr auto reflect(point const &lhs, point<E> const &rhs) noexcept
204 {
205 return point<std::max(D, E)>{reflect_point(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
206 }
207
213 template<int E>
214 [[nodiscard]] friend constexpr auto min(point const &lhs, point<E> const &rhs) noexcept
215 {
216 return point<std::max(D,E)>{min(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
217 }
218
224 template<int E>
225 [[nodiscard]] friend constexpr auto max(point const &lhs, point<E> const &rhs) noexcept
226 {
227 return point<std::max(D, E)>{max(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
228 }
229
232 [[nodiscard]] friend constexpr point round(point const &rhs) noexcept
233 {
234 return point{round(static_cast<f32x4>(rhs))};
235 }
236
239 [[nodiscard]] friend constexpr point ceil(point const &rhs) noexcept
240 {
241 return point{ceil(static_cast<f32x4>(rhs))};
242 }
243
246 [[nodiscard]] friend constexpr point floor(point const &rhs) noexcept
247 {
248 return point{floor(static_cast<f32x4>(rhs))};
249 }
250
254 [[nodiscard]] constexpr bool is_valid() const noexcept
255 {
256 return _v.w() != 0.0f && (D == 3 || _v.z() == 0.0f);
257 }
258
259 [[nodiscard]] friend std::string to_string(point const &rhs) noexcept
260 {
261 if constexpr (D == 2) {
262 return fmt::format("<{}, {}>", rhs._v.x(), rhs._v.y());
263 } else if constexpr (D == 3) {
264 return fmt::format("<{}, {}, {}>", rhs._v.x(), rhs._v.y(), rhs._v.z());
265 } else {
266 tt_static_no_default();
267 }
268 }
269
270 friend std::ostream &operator<<(std::ostream &lhs, point const &rhs) noexcept
271 {
272 return lhs << to_string(rhs);
273 }
274
275private:
276 f32x4 _v;
277};
278
279} // namespace geo
280
281using point2 = geo::point<2>;
282using point3 = geo::point<3>;
283
284} // namespace tt
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point.hpp:21
constexpr friend point< std::max(D, E)> operator+(vector< E > const &rhs, point const &lhs) noexcept
Move a point along a vector.
Definition point.hpp:156
friend constexpr auto max(point const &lhs, point< E > const &rhs) noexcept
Mix the two points and get the heighest value of each element.
Definition point.hpp:225
constexpr float & x() noexcept
Access the x element from the point.
Definition point.hpp:85
constexpr float const & y() const noexcept
Access the y element from the point.
Definition point.hpp:117
constexpr point(point< E > const &other) noexcept
Construct a point from a higher dimension point.
Definition point.hpp:42
constexpr point(float x, float y) noexcept
Construct a 2D point from x and y elements.
Definition point.hpp:73
constexpr friend point< std::max(D, E)> operator+(point const &lhs, vector< E > const &rhs) noexcept
Move a point along a vector.
Definition point.hpp:144
friend constexpr point round(point const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point.hpp:232
constexpr point(float x, float y, float z=0.0) noexcept
Construct a 3D point from x, y and z elements.
Definition point.hpp:80
friend constexpr point ceil(point const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point.hpp:239
constexpr point(point< E > const &other) noexcept
Construct a point from a lower dimension point.
Definition point.hpp:33
constexpr point(f32x4 const &other) noexcept
Construct a point from a f32x4-numeric_array.
Definition point.hpp:60
constexpr float const & z() const noexcept
Access the z element from the point.
Definition point.hpp:125
constexpr friend vector< D > operator-(point const &lhs, point const &rhs) noexcept
Find the vector between two points.
Definition point.hpp:179
friend constexpr auto min(point const &lhs, point< E > const &rhs) noexcept
Mix the two points and get the lowest value of each element.
Definition point.hpp:214
constexpr point() noexcept
Construct a point at the origin of the coordinate system.
Definition point.hpp:67
constexpr friend bool operator==(point const &lhs, point const &rhs) noexcept
Compare if two points are equal.
Definition point.hpp:190
constexpr bool is_valid() const noexcept
Check if the point is valid.
Definition point.hpp:254
constexpr friend point< std::max(D, E)> operator-(point const &lhs, vector< E > const &rhs) noexcept
Move a point backward along the vector.
Definition point.hpp:168
constexpr float const & x() const noexcept
Access the x element from the point.
Definition point.hpp:109
constexpr float & z() noexcept
Access the z element from the point.
Definition point.hpp:101
friend constexpr point floor(point const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point.hpp:246
constexpr float & y() noexcept
Access the y element from the point.
Definition point.hpp:93
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
constexpr bool is_valid() const noexcept
Check if the vector is valid.
Definition vector.hpp:297
T max(T... args)