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/utility.hpp"
12#include "../macros.hpp"
13#include <format>
14#include <concepts>
15
16
17
18namespace hi::inline v1 {
19
27class point3 {
28public:
29 using array_type = simd<float, 4>;
30 using value_type = array_type::value_type;
31
32 constexpr point3(point3 const&) noexcept = default;
33 constexpr point3(point3&&) noexcept = default;
34 constexpr point3& operator=(point3 const&) noexcept = default;
35 constexpr point3& operator=(point3&&) noexcept = default;
36
39 [[nodiscard]] constexpr point3(point2 const& other) noexcept : _v(static_cast<array_type>(other)) {}
40
41 [[nodiscard]] constexpr explicit operator point2() const noexcept
42 {
43 auto tmp = _v;
44 tmp.z() = 0.0f;
45 return point2{tmp};
46 }
47
50 [[nodiscard]] constexpr point3(point2 const& other, float z) noexcept : _v(static_cast<array_type>(other))
51 {
52 _v.z() = z;
53 }
54
57 [[nodiscard]] constexpr explicit operator array_type() const noexcept
58 {
59 return _v;
60 }
61
64 [[nodiscard]] constexpr explicit point3(array_type const& other) noexcept : _v(other)
65 {
66 hi_axiom(holds_invariant());
67 }
68
71 [[nodiscard]] constexpr point3() noexcept : _v(0.0f, 0.0f, 0.0f, 1.0f) {}
72
78 [[nodiscard]] constexpr point3(float x, float y, float z = 0.0f) noexcept : _v(x, y, z, 1.0f) {}
79
83 [[nodiscard]] constexpr float& x() noexcept
84 {
85 return _v.x();
86 }
87
91 [[nodiscard]] constexpr float& y() noexcept
92 {
93 return _v.y();
94 }
95
99 [[nodiscard]] constexpr float& z() noexcept
100 {
101 return _v.z();
102 }
103
107 [[nodiscard]] constexpr float x() const noexcept
108 {
109 return _v.x();
110 }
111
115 [[nodiscard]] constexpr float y() const noexcept
116 {
117 return _v.y();
118 }
119
123 [[nodiscard]] constexpr float z() const noexcept
124 {
125 return _v.z();
126 }
127
128 constexpr point3& operator+=(vector3 const& rhs) noexcept
129 {
130 return *this = *this + rhs;
131 }
132
133 constexpr point3& operator-=(vector3 const& rhs) noexcept
134 {
135 return *this = *this - rhs;
136 }
137
143 [[nodiscard]] constexpr friend point3 operator+(point3 const& lhs, vector3 const& rhs) noexcept
144 {
145 return point3{lhs._v + array_type{rhs}};
146 }
147
153 [[nodiscard]] constexpr friend point3 operator+(vector3 const& lhs, point3 const& rhs) noexcept
154 {
155 return point3{array_type{lhs} + rhs._v};
156 }
157
163 [[nodiscard]] constexpr friend point3 operator-(point3 const& lhs, vector3 const& rhs) noexcept
164 {
165 return point3{lhs._v - array_type{rhs}};
166 }
167
173 [[nodiscard]] constexpr friend vector3 operator-(point3 const& lhs, point3 const& rhs) noexcept
174 {
175 return vector3{lhs._v - rhs._v};
176 }
177
183 [[nodiscard]] constexpr friend bool operator==(point3 const& lhs, point3 const& rhs) noexcept
184 {
185 return equal(lhs._v, rhs._v);
186 }
187
188 [[nodiscard]] friend constexpr point3 midpoint(point3 const& lhs, point3 const& rhs) noexcept
189 {
190 return point3{midpoint(lhs._v, rhs._v)};
191 }
192
193 [[nodiscard]] friend constexpr point3 reflect(point3 const& lhs, point3 const& rhs) noexcept
194 {
195 return point3{reflect_point(lhs._v, rhs._v)};
196 }
197
203 [[nodiscard]] friend constexpr point3 min(point3 const& lhs, point3 const& rhs) noexcept
204 {
205 return point3{min(lhs._v, rhs._v)};
206 }
207
213 [[nodiscard]] friend constexpr point3 max(point3 const& lhs, point3 const& rhs) noexcept
214 {
215 return point3{max(lhs._v, rhs._v)};
216 }
217
220 [[nodiscard]] friend constexpr point3 round(point3 const& rhs) noexcept
221 {
222 return point3{round(rhs._v)};
223 }
224
227 [[nodiscard]] friend constexpr point3 ceil(point3 const& rhs) noexcept
228 {
229 return point3{ceil(rhs._v)};
230 }
231
234 [[nodiscard]] friend constexpr point3 floor(point3 const& rhs) noexcept
235 {
236 return point3{floor(rhs._v)};
237 }
238
241 [[nodiscard]] friend constexpr point3 ceil(point3 const& lhs, extent3 rhs) noexcept
242 {
243 hilet rhs_ = array_type{rhs}.xyz1();
244 return point3{ceil(lhs._v / rhs_) * rhs_};
245 }
246
249 [[nodiscard]] friend constexpr point3 floor(point3 const& lhs, extent3 rhs) noexcept
250 {
251 hilet rhs_ = array_type{rhs}.xyz1();
252 return point3{floor(lhs._v / rhs_) * rhs_};
253 }
254
255 [[nodiscard]] friend float distance(point3 const& lhs, point3 const& rhs) noexcept
256 {
257 return hypot(rhs - lhs);
258 }
259
263 [[nodiscard]] constexpr bool holds_invariant() const noexcept
264 {
265 return _v.w() != 0.0f;
266 }
267
268 [[nodiscard]] friend std::string to_string(point3 const& rhs) noexcept
269 {
270 return std::format("<{}, {}, {}>", rhs._v.x(), rhs._v.y(), rhs._v.z());
271 }
272
273 friend std::ostream& operator<<(std::ostream& lhs, point3 const& rhs) noexcept
274 {
275 return lhs << to_string(rhs);
276 }
277
278private:
279 array_type _v;
280};
281
282[[nodiscard]] constexpr point3 operator+(point2 const& lhs, vector3 const& rhs) noexcept
283{
284 return point3{f32x4{lhs} + f32x4{rhs}};
285}
286
287[[nodiscard]] constexpr point3 operator+(vector3 const& lhs, point2 const& rhs) noexcept
288{
289 return point3{f32x4{lhs} + f32x4{rhs}};
290}
291
292[[nodiscard]] constexpr point3 operator-(point2 const& lhs, vector3 const& rhs) noexcept
293{
294 return point3{f32x4{lhs} - f32x4{rhs}};
295}
296
297
298
299
300} // namespace hi::inline v1
301
302template<typename CharT>
303struct std::formatter<hi::point3, CharT> {
304 auto parse(auto& pc)
305 {
306 return pc.end();
307 }
308
309 auto format(hi::point3 const& t, auto& fc) const
310 {
311 return std::vformat_to(fc.out(), "<{}, {}, {}>", std::make_format_args(t.x(), t.y(), t.z()));
312 }
313};
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
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point3.hpp:27
constexpr point3(float x, float y, float z=0.0f) noexcept
Construct a 3D point from x, y and z elements.
Definition point3.hpp:78
constexpr friend bool operator==(point3 const &lhs, point3 const &rhs) noexcept
Compare if two points are equal.
Definition point3.hpp:183
constexpr point3(point2 const &other, float z) noexcept
Construct a point from a lower dimension point.
Definition point3.hpp:50
constexpr point3(point2 const &other) noexcept
Construct a point from a lower dimension point.
Definition point3.hpp:39
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:241
constexpr point3() noexcept
Construct a point at the origin of the coordinate system.
Definition point3.hpp:71
constexpr float & z() noexcept
Access the z element from the point.
Definition point3.hpp:99
constexpr friend point3 operator+(vector3 const &lhs, point3 const &rhs) noexcept
Move a point along a vector.
Definition point3.hpp:153
constexpr float y() const noexcept
Access the y element from the point.
Definition point3.hpp:115
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:249
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:213
constexpr bool holds_invariant() const noexcept
Check if the point is valid.
Definition point3.hpp:263
constexpr point3(array_type const &other) noexcept
Construct a point from a array_type-simd.
Definition point3.hpp:64
constexpr float & x() noexcept
Access the x element from the point.
Definition point3.hpp:83
constexpr float z() const noexcept
Access the z element from the point.
Definition point3.hpp:123
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:203
constexpr float & y() noexcept
Access the y element from the point.
Definition point3.hpp:91
constexpr friend vector3 operator-(point3 const &lhs, point3 const &rhs) noexcept
Find the vector between two points.
Definition point3.hpp:173
friend constexpr point3 round(point3 const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point3.hpp:220
constexpr friend point3 operator+(point3 const &lhs, vector3 const &rhs) noexcept
Move a point along a vector.
Definition point3.hpp:143
friend constexpr point3 ceil(point3 const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point3.hpp:227
constexpr float x() const noexcept
Access the x element from the point.
Definition point3.hpp:107
constexpr friend point3 operator-(point3 const &lhs, vector3 const &rhs) noexcept
Move a point backward along the vector.
Definition point3.hpp:163
friend constexpr point3 floor(point3 const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point3.hpp:234