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 "../SIMD/module.hpp"
11#include "../utility/module.hpp"
12#include <format>
13#include <concepts>
14
15namespace hi::inline v1 {
16
24class point3 {
25public:
26 using array_type = simd<float, 4>;
27 using value_type = array_type::value_type;
28
29 constexpr point3(point3 const&) noexcept = default;
30 constexpr point3(point3&&) noexcept = default;
31 constexpr point3& operator=(point3 const&) noexcept = default;
32 constexpr point3& operator=(point3&&) noexcept = default;
33
36 [[nodiscard]] constexpr point3(point2 const& other) noexcept : _v(static_cast<array_type>(other)) {}
37
38 [[nodiscard]] constexpr explicit operator point2() const noexcept
39 {
40 auto tmp = _v;
41 tmp.z() = 0.0f;
42 return point2{tmp};
43 }
44
47 [[nodiscard]] constexpr point3(point2 const& other, float z) noexcept : _v(static_cast<array_type>(other))
48 {
49 _v.z() = z;
50 }
51
54 [[nodiscard]] constexpr explicit operator array_type() const noexcept
55 {
56 return _v;
57 }
58
61 [[nodiscard]] constexpr explicit point3(array_type const& other) noexcept : _v(other)
62 {
63 hi_axiom(holds_invariant());
64 }
65
68 [[nodiscard]] constexpr point3() noexcept : _v(0.0f, 0.0f, 0.0f, 1.0f) {}
69
75 [[nodiscard]] constexpr point3(float x, float y, float z = 0.0f) noexcept : _v(x, y, z, 1.0f) {}
76
80 [[nodiscard]] constexpr float& x() noexcept
81 {
82 return _v.x();
83 }
84
88 [[nodiscard]] constexpr float& y() noexcept
89 {
90 return _v.y();
91 }
92
96 [[nodiscard]] constexpr float& z() noexcept
97 {
98 return _v.z();
99 }
100
104 [[nodiscard]] constexpr float x() const noexcept
105 {
106 return _v.x();
107 }
108
112 [[nodiscard]] constexpr float y() const noexcept
113 {
114 return _v.y();
115 }
116
120 [[nodiscard]] constexpr float z() const noexcept
121 {
122 return _v.z();
123 }
124
125 constexpr point3& operator+=(vector3 const& rhs) noexcept
126 {
127 return *this = *this + rhs;
128 }
129
130 constexpr point3& operator-=(vector3 const& rhs) noexcept
131 {
132 return *this = *this - rhs;
133 }
134
140 [[nodiscard]] constexpr friend point3 operator+(point3 const& lhs, vector3 const& rhs) noexcept
141 {
142 return point3{lhs._v + array_type{rhs}};
143 }
144
150 [[nodiscard]] constexpr friend point3 operator+(vector3 const& lhs, point3 const& rhs) noexcept
151 {
152 return point3{array_type{lhs} + rhs._v};
153 }
154
160 [[nodiscard]] constexpr friend point3 operator-(point3 const& lhs, vector3 const& rhs) noexcept
161 {
162 return point3{lhs._v - array_type{rhs}};
163 }
164
170 [[nodiscard]] constexpr friend vector3 operator-(point3 const& lhs, point3 const& rhs) noexcept
171 {
172 return vector3{lhs._v - rhs._v};
173 }
174
180 [[nodiscard]] constexpr friend bool operator==(point3 const& lhs, point3 const& rhs) noexcept
181 {
182 return equal(lhs._v, rhs._v);
183 }
184
185 [[nodiscard]] friend constexpr point3 midpoint(point3 const& lhs, point3 const& rhs) noexcept
186 {
187 return point3{midpoint(lhs._v, rhs._v)};
188 }
189
190 [[nodiscard]] friend constexpr point3 reflect(point3 const& lhs, point3 const& rhs) noexcept
191 {
192 return point3{reflect_point(lhs._v, rhs._v)};
193 }
194
200 [[nodiscard]] friend constexpr point3 min(point3 const& lhs, point3 const& rhs) noexcept
201 {
202 return point3{min(lhs._v, rhs._v)};
203 }
204
210 [[nodiscard]] friend constexpr point3 max(point3 const& lhs, point3 const& rhs) noexcept
211 {
212 return point3{max(lhs._v, rhs._v)};
213 }
214
217 [[nodiscard]] friend constexpr point3 round(point3 const& rhs) noexcept
218 {
219 return point3{round(rhs._v)};
220 }
221
224 [[nodiscard]] friend constexpr point3 ceil(point3 const& rhs) noexcept
225 {
226 return point3{ceil(rhs._v)};
227 }
228
231 [[nodiscard]] friend constexpr point3 floor(point3 const& rhs) noexcept
232 {
233 return point3{floor(rhs._v)};
234 }
235
238 [[nodiscard]] friend constexpr point3 ceil(point3 const& lhs, extent3 rhs) noexcept
239 {
240 hilet rhs_ = array_type{rhs}.xyz1();
241 return point3{ceil(lhs._v / rhs_) * rhs_};
242 }
243
246 [[nodiscard]] friend constexpr point3 floor(point3 const& lhs, extent3 rhs) noexcept
247 {
248 hilet rhs_ = array_type{rhs}.xyz1();
249 return point3{floor(lhs._v / rhs_) * rhs_};
250 }
251
252 [[nodiscard]] friend float distance(point3 const& lhs, point3 const& rhs) noexcept
253 {
254 return hypot(rhs - lhs);
255 }
256
260 [[nodiscard]] constexpr bool holds_invariant() const noexcept
261 {
262 return _v.w() != 0.0f;
263 }
264
265 [[nodiscard]] friend std::string to_string(point3 const& rhs) noexcept
266 {
267 return std::format("<{}, {}, {}>", rhs._v.x(), rhs._v.y(), rhs._v.z());
268 }
269
270 friend std::ostream& operator<<(std::ostream& lhs, point3 const& rhs) noexcept
271 {
272 return lhs << to_string(rhs);
273 }
274
275private:
276 array_type _v;
277};
278
279[[nodiscard]] constexpr point3 operator+(point2 const& lhs, vector3 const& rhs) noexcept
280{
281 return point3{f32x4{lhs} + f32x4{rhs}};
282}
283
284[[nodiscard]] constexpr point3 operator+(vector3 const& lhs, point2 const& rhs) noexcept
285{
286 return point3{f32x4{lhs} + f32x4{rhs}};
287}
288
289[[nodiscard]] constexpr point3 operator-(point2 const& lhs, vector3 const& rhs) noexcept
290{
291 return point3{f32x4{lhs} - f32x4{rhs}};
292}
293
294
295
296
297} // namespace hi::inline v1
298
299template<typename CharT>
300struct std::formatter<hi::point3, CharT> {
301 auto parse(auto& pc)
302 {
303 return pc.end();
304 }
305
306 auto format(hi::point3 const& t, auto& fc)
307 {
308 return std::vformat_to(fc.out(), "<{}, {}, {}>", std::make_format_args(t.x(), t.y(), t.z()));
309 }
310};
Defined the geo::extent, extent2 and extent3 types.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point2.hpp:23
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point3.hpp:24
constexpr point3(float x, float y, float z=0.0f) noexcept
Construct a 3D point from x, y and z elements.
Definition point3.hpp:75
constexpr friend bool operator==(point3 const &lhs, point3 const &rhs) noexcept
Compare if two points are equal.
Definition point3.hpp:180
constexpr point3(point2 const &other, float z) noexcept
Construct a point from a lower dimension point.
Definition point3.hpp:47
constexpr point3(point2 const &other) noexcept
Construct a point from a lower dimension point.
Definition point3.hpp:36
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:238
constexpr point3() noexcept
Construct a point at the origin of the coordinate system.
Definition point3.hpp:68
constexpr float & z() noexcept
Access the z element from the point.
Definition point3.hpp:96
constexpr friend point3 operator+(vector3 const &lhs, point3 const &rhs) noexcept
Move a point along a vector.
Definition point3.hpp:150
constexpr float y() const noexcept
Access the y element from the point.
Definition point3.hpp:112
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:246
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:210
constexpr bool holds_invariant() const noexcept
Check if the point is valid.
Definition point3.hpp:260
constexpr point3(array_type const &other) noexcept
Construct a point from a array_type-simd.
Definition point3.hpp:61
constexpr float & x() noexcept
Access the x element from the point.
Definition point3.hpp:80
constexpr float z() const noexcept
Access the z element from the point.
Definition point3.hpp:120
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:200
constexpr float & y() noexcept
Access the y element from the point.
Definition point3.hpp:88
constexpr friend vector3 operator-(point3 const &lhs, point3 const &rhs) noexcept
Find the vector between two points.
Definition point3.hpp:170
friend constexpr point3 round(point3 const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point3.hpp:217
constexpr friend point3 operator+(point3 const &lhs, vector3 const &rhs) noexcept
Move a point along a vector.
Definition point3.hpp:140
friend constexpr point3 ceil(point3 const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point3.hpp:224
constexpr float x() const noexcept
Access the x element from the point.
Definition point3.hpp:104
constexpr friend point3 operator-(point3 const &lhs, vector3 const &rhs) noexcept
Move a point backward along the vector.
Definition point3.hpp:160
friend constexpr point3 floor(point3 const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point3.hpp:231