HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
point3.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 "point2.hpp"
8#include "vector3.hpp"
9#include "extent3.hpp"
10#include "../utility/utility.hpp"
11#include "../macros.hpp"
12#include <hikocpu/hikocpu.hpp>
13#include <format>
14#include <concepts>
15#include <exception>
16#include <compare>
17
18hi_export_module(hikogui.geometry : point3);
19
20hi_export namespace hi::inline v1 {
21
29class point3 {
30public:
31 using array_type = simd<float, 4>;
32 using value_type = array_type::value_type;
33
34 constexpr point3(point3 const&) noexcept = default;
35 constexpr point3(point3&&) noexcept = default;
36 constexpr point3& operator=(point3 const&) noexcept = default;
37 constexpr point3& operator=(point3&&) noexcept = default;
38
41 [[nodiscard]] constexpr point3(point2 const& other) noexcept : _v(static_cast<array_type>(other)) {}
42
43 [[nodiscard]] constexpr explicit operator point2() const noexcept
44 {
45 auto tmp = _v;
46 tmp.z() = 0.0f;
47 return point2{tmp};
48 }
49
52 [[nodiscard]] constexpr point3(point2 const& other, float z) noexcept : _v(static_cast<array_type>(other))
53 {
54 _v.z() = z;
55 }
56
59 [[nodiscard]] constexpr explicit operator array_type() const noexcept
60 {
61 return _v;
62 }
63
66 [[nodiscard]] constexpr explicit point3(array_type const& other) noexcept : _v(other)
67 {
68 hi_axiom(holds_invariant());
69 }
70
73 [[nodiscard]] constexpr point3() noexcept : _v(0.0f, 0.0f, 0.0f, 1.0f) {}
74
80 [[nodiscard]] constexpr point3(float x, float y, float z = 0.0f) noexcept : _v(x, y, z, 1.0f) {}
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
102 {
103 return _v.z();
104 }
105
109 [[nodiscard]] constexpr float x() const noexcept
110 {
111 return _v.x();
112 }
113
117 [[nodiscard]] constexpr float y() const noexcept
118 {
119 return _v.y();
120 }
121
125 [[nodiscard]] constexpr float z() const noexcept
126 {
127 return _v.z();
128 }
129
130 constexpr point3& operator+=(vector3 const& rhs) noexcept
131 {
132 return *this = *this + rhs;
133 }
134
135 constexpr point3& operator-=(vector3 const& rhs) noexcept
136 {
137 return *this = *this - rhs;
138 }
139
145 [[nodiscard]] constexpr friend point3 operator+(point3 const& lhs, vector3 const& rhs) noexcept
146 {
147 return point3{lhs._v + array_type{rhs}};
148 }
149
155 [[nodiscard]] constexpr friend point3 operator+(vector3 const& lhs, point3 const& rhs) noexcept
156 {
157 return point3{array_type{lhs} + rhs._v};
158 }
159
165 [[nodiscard]] constexpr friend point3 operator-(point3 const& lhs, vector3 const& rhs) noexcept
166 {
167 return point3{lhs._v - array_type{rhs}};
168 }
169
175 [[nodiscard]] constexpr friend vector3 operator-(point3 const& lhs, point3 const& rhs) noexcept
176 {
177 return vector3{lhs._v - rhs._v};
178 }
179
185 [[nodiscard]] constexpr friend bool operator==(point3 const& lhs, point3 const& rhs) noexcept
186 {
187 return equal(lhs._v, rhs._v);
188 }
189
190 [[nodiscard]] friend constexpr point3 midpoint(point3 const& lhs, point3 const& rhs) noexcept
191 {
192 return point3{(lhs._v + rhs._v) * array_type::broadcast(0.5f)};
193 }
194
201 [[nodiscard]] friend constexpr point3 reflect(point3 const& lhs, point3 const& rhs) noexcept
202 {
203 return point3{rhs._v - (lhs._v - rhs._v)};
204 }
205
211 [[nodiscard]] friend constexpr point3 min(point3 const& lhs, point3 const& rhs) noexcept
212 {
213 return point3{min(lhs._v, rhs._v)};
214 }
215
221 [[nodiscard]] friend constexpr point3 max(point3 const& lhs, point3 const& rhs) noexcept
222 {
223 return point3{max(lhs._v, rhs._v)};
224 }
225
228 [[nodiscard]] friend constexpr point3 round(point3 const& rhs) noexcept
229 {
230 return point3{round(rhs._v)};
231 }
232
235 [[nodiscard]] friend constexpr point3 ceil(point3 const& rhs) noexcept
236 {
237 return point3{ceil(rhs._v)};
238 }
239
242 [[nodiscard]] friend constexpr point3 floor(point3 const& rhs) noexcept
243 {
244 return point3{floor(rhs._v)};
245 }
246
249 [[nodiscard]] friend constexpr point3 ceil(point3 const& lhs, extent3 rhs) noexcept
250 {
251 auto const rhs_ = array_type{rhs}.xyz1();
252 return point3{ceil(lhs._v / rhs_) * rhs_};
253 }
254
257 [[nodiscard]] friend constexpr point3 floor(point3 const& lhs, extent3 rhs) noexcept
258 {
259 auto const rhs_ = array_type{rhs}.xyz1();
260 return point3{floor(lhs._v / rhs_) * rhs_};
261 }
262
263 [[nodiscard]] friend float distance(point3 const& lhs, point3 const& rhs) noexcept
264 {
265 return hypot(rhs - lhs);
266 }
267
271 [[nodiscard]] constexpr bool holds_invariant() const noexcept
272 {
273 return _v.w() != 0.0f;
274 }
275
276 [[nodiscard]] friend std::string to_string(point3 const& rhs) noexcept
277 {
278 return std::format("<{}, {}, {}>", rhs._v.x(), rhs._v.y(), rhs._v.z());
279 }
280
281 friend std::ostream& operator<<(std::ostream& lhs, point3 const& rhs) noexcept
282 {
283 return lhs << to_string(rhs);
284 }
285
286private:
287 array_type _v;
288};
289
290[[nodiscard]] constexpr point3 operator+(point2 const& lhs, vector3 const& rhs) noexcept
291{
292 return point3{f32x4{lhs} + f32x4{rhs}};
293}
294
295[[nodiscard]] constexpr point3 operator+(vector3 const& lhs, point2 const& rhs) noexcept
296{
297 return point3{f32x4{lhs} + f32x4{rhs}};
298}
299
300[[nodiscard]] constexpr point3 operator-(point2 const& lhs, vector3 const& rhs) noexcept
301{
302 return point3{f32x4{lhs} - f32x4{rhs}};
303}
304
305
306
307
308} // namespace hi::inline v1
309
310// XXX #617 MSVC bug does not handle partial specialization in modules.
311hi_export template<>
312struct std::formatter<hi::point3, char> : std::formatter<std::string, char> {
313 auto format(hi::point3 const& t, auto& fc) const
314 {
315 return std::formatter<std::string, char>::format(std::format("<{}, {}, {}>", t.x(), t.y(), t.z()), fc);
316 }
317};
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
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point3.hpp:29
constexpr point3(float x, float y, float z=0.0f) noexcept
Construct a 3D point from x, y and z elements.
Definition point3.hpp:80
constexpr friend bool operator==(point3 const &lhs, point3 const &rhs) noexcept
Compare if two points are equal.
Definition point3.hpp:185
constexpr point3(point2 const &other, float z) noexcept
Construct a point from a lower dimension point.
Definition point3.hpp:52
constexpr point3(point2 const &other) noexcept
Construct a point from a lower dimension point.
Definition point3.hpp:41
friend constexpr point3 ceil(point3 const &lhs, extent3 rhs) noexcept
Round the coordinates of a point toward the top-right with the given granularity.
Definition point3.hpp:249
constexpr point3() noexcept
Construct a point at the origin of the coordinate system.
Definition point3.hpp:73
constexpr float & z() noexcept
Access the z element from the point.
Definition point3.hpp:101
constexpr friend point3 operator+(vector3 const &lhs, point3 const &rhs) noexcept
Move a point along a vector.
Definition point3.hpp:155
constexpr float y() const noexcept
Access the y element from the point.
Definition point3.hpp:117
friend constexpr point3 floor(point3 const &lhs, extent3 rhs) noexcept
Round the coordinates of a point toward the left-bottom with the given granularity.
Definition point3.hpp:257
friend constexpr point3 max(point3 const &lhs, point3 const &rhs) noexcept
Mix the two points and get the highest value of each element.
Definition point3.hpp:221
constexpr bool holds_invariant() const noexcept
Check if the point is valid.
Definition point3.hpp:271
friend constexpr point3 reflect(point3 const &lhs, point3 const &rhs) noexcept
Reflect a point.
Definition point3.hpp:201
constexpr point3(array_type const &other) noexcept
Construct a point from a array_type-simd.
Definition point3.hpp:66
constexpr float & x() noexcept
Access the x element from the point.
Definition point3.hpp:85
constexpr float z() const noexcept
Access the z element from the point.
Definition point3.hpp:125
friend constexpr point3 min(point3 const &lhs, point3 const &rhs) noexcept
Mix the two points and get the lowest value of each element.
Definition point3.hpp:211
constexpr float & y() noexcept
Access the y element from the point.
Definition point3.hpp:93
constexpr friend vector3 operator-(point3 const &lhs, point3 const &rhs) noexcept
Find the vector between two points.
Definition point3.hpp:175
friend constexpr point3 round(point3 const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point3.hpp:228
constexpr friend point3 operator+(point3 const &lhs, vector3 const &rhs) noexcept
Move a point along a vector.
Definition point3.hpp:145
friend constexpr point3 ceil(point3 const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point3.hpp:235
constexpr float x() const noexcept
Access the x element from the point.
Definition point3.hpp:109
constexpr friend point3 operator-(point3 const &lhs, vector3 const &rhs) noexcept
Move a point backward along the vector.
Definition point3.hpp:165
friend constexpr point3 floor(point3 const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point3.hpp:242