HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
extent3.hpp
Go to the documentation of this file.
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
9#pragma once
10
11#include "vector3.hpp"
12#include "extent2.hpp"
13#include "../SIMD/module.hpp"
14#include "../utility/module.hpp"
15#include <compare>
16#include <concepts>
17
18namespace hi { inline namespace v1 {
19
27class extent3 {
28public:
29 using array_type = simd<float, 4>;
30 using value_type = array_type::value_type;
31
32 constexpr extent3(extent3 const&) noexcept = default;
33 constexpr extent3(extent3&&) noexcept = default;
34 constexpr extent3& operator=(extent3 const&) noexcept = default;
35 constexpr extent3& operator=(extent3&&) noexcept = default;
36
39 [[nodiscard]] constexpr extent3(extent2 const& other) noexcept : _v(static_cast<array_type>(other))
40 {
42 }
43
44 [[nodiscard]] constexpr explicit operator extent2() const noexcept
45 {
46 auto tmp = _v;
47 tmp.z() = 0.0f;
48 return extent2{tmp};
49 }
50
53 [[nodiscard]] constexpr explicit operator array_type() const noexcept
54 {
55 return _v;
56 }
57
58 [[nodiscard]] constexpr explicit extent3(array_type const& other) noexcept : _v(other)
59 {
61 }
62
63 [[nodiscard]] constexpr explicit operator bool() const noexcept
64 {
65 return _v.x() != 0.0f or _v.y() != 0.0f or _v.z() != 0.0f;
66 }
67
68 [[nodiscard]] constexpr explicit operator vector3() const noexcept
69 {
70 return vector3{static_cast<array_type>(*this)};
71 }
72
75 [[nodiscard]] constexpr extent3() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
76
82 [[nodiscard]] constexpr extent3(float width, float height, float depth = 0.0f) noexcept : _v(width, height, depth, 0.0f)
83 {
85 }
86
87 [[nodiscard]] static constexpr extent3 infinity() noexcept
88 {
89 return extent3{
93 }
94
95 [[nodiscard]] static constexpr extent3 large() noexcept
96 {
97 return extent3{large_number_v<float>, large_number_v<float>, large_number_v<float>};
98 }
99
100 [[nodiscard]] static constexpr extent3 nan() noexcept
101 {
102 auto r = extent3{};
106 return r;
107 }
108
115 [[nodiscard]] constexpr float& width() noexcept
116 {
117 return _v.x();
118 }
119
126 [[nodiscard]] constexpr float& height() noexcept
127 {
128 return _v.y();
129 }
130
137 [[nodiscard]] constexpr float& depth() noexcept
138 {
139 return _v.z();
140 }
141
148 [[nodiscard]] constexpr float width() const noexcept
149 {
150 return _v.x();
151 }
152
159 [[nodiscard]] constexpr float height() const noexcept
160 {
161 return _v.y();
162 }
163
170 [[nodiscard]] constexpr float depth() const noexcept
171 {
172 return _v.z();
173 }
174
175 [[nodiscard]] constexpr vector3 right() const noexcept
176 {
177 return vector3{_v.x000()};
178 }
179
180 [[nodiscard]] constexpr vector3 up() const noexcept
181 {
182 return vector3{_v._0y00()};
183 }
184
185 constexpr extent3& operator+=(extent3 const& rhs) noexcept
186 {
187 return *this = *this + rhs;
188 }
189
195 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, extent3 const& rhs) noexcept
196 {
197 return extent3{lhs._v + rhs._v};
198 }
199
205 [[nodiscard]] constexpr friend extent3 operator-(extent3 const& lhs, extent3 const& rhs) noexcept
206 {
207 return extent3{lhs._v - rhs._v};
208 }
209
215 [[nodiscard]] constexpr friend extent3 operator*(extent3 const& lhs, float const& rhs) noexcept
216 {
217 return extent3{lhs._v * rhs};
218 }
219
220 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, vector2 const& rhs) noexcept
221 {
222 return extent3{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
223 }
224
225 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, vector3 const& rhs) noexcept
226 {
227 return extent3{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
228 }
229
230 [[nodiscard]] constexpr friend vector3 operator+(vector3 const& lhs, extent3 const& rhs) noexcept
231 {
232 return vector3{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
233 }
234
240 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, float const& rhs) noexcept
241 {
242 auto r = extent3{};
243 for (std::size_t i = 0; i != 3; ++i) {
244 r._v[i] = lhs._v[i] + rhs;
245 }
246
247 return r;
248 }
249
255 [[nodiscard]] constexpr friend extent3 operator*(float const& lhs, extent3 const& rhs) noexcept
256 {
257 return extent3{lhs * rhs._v};
258 }
259
265 [[nodiscard]] constexpr friend bool operator==(extent3 const& lhs, extent3 const& rhs) noexcept
266 {
267 return equal(lhs._v, rhs._v);
268 }
269
270 [[nodiscard]] constexpr friend std::partial_ordering operator<=>(extent3 const& lhs, extent3 const& rhs) noexcept
271 {
272 constexpr std::size_t mask = 0b0111;
273
274 hilet equal = (lhs._v == rhs._v).mask() & mask;
275 if (equal == mask) {
276 // Only equivalent if all elements are equal.
277 return std::partial_ordering::equivalent;
278 }
279
280 hilet less = (lhs._v < rhs._v).mask() & mask;
281 if ((less | equal) == mask) {
282 // If one or more elements is less (but none are greater) then the ordering is less.
283 return std::partial_ordering::less;
284 }
285
286 hilet greater = (lhs._v < rhs._v).mask() & mask;
287 if ((greater | equal) == mask) {
288 // If one or more elements is greater (but none are less) then the ordering is greater.
289 return std::partial_ordering::greater;
290 }
291
292 // Some elements are less and others are greater, we don't have an ordering.
293 return std::partial_ordering::unordered;
294 }
295
300 [[nodiscard]] hi_force_inline constexpr friend float squared_hypot(extent3 const& rhs) noexcept
301 {
302 return squared_hypot<0b0111>(rhs._v);
303 }
304
309 [[nodiscard]] friend float hypot(extent3 const& rhs) noexcept
310 {
311 return hypot<0b0111>(rhs._v);
312 }
313
318 [[nodiscard]] constexpr friend float rcp_hypot(extent3 const& rhs) noexcept
319 {
320 return rcp_hypot<0b0111>(rhs._v);
321 }
322
327 [[nodiscard]] constexpr friend extent3 normalize(extent3 const& rhs) noexcept
328 {
329 return extent3{normalize<0b0111>(rhs._v)};
330 }
331
332 [[nodiscard]] constexpr friend extent3 ceil(extent3 const& rhs) noexcept
333 {
334 return extent3{ceil(array_type{rhs})};
335 }
336
337 [[nodiscard]] constexpr friend extent3 floor(extent3 const& rhs) noexcept
338 {
339 return extent3{floor(static_cast<array_type>(rhs))};
340 }
341
342 [[nodiscard]] constexpr friend extent3 round(extent3 const& rhs) noexcept
343 {
344 return extent3{round(static_cast<array_type>(rhs))};
345 }
346
347 [[nodiscard]] constexpr friend extent3 min(extent3 const& lhs, extent3 const& rhs) noexcept
348 {
349 return extent3{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
350 }
351
352 [[nodiscard]] constexpr friend extent3 max(extent3 const& lhs, extent3 const& rhs) noexcept
353 {
354 return extent3{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
355 }
356
357 [[nodiscard]] constexpr friend extent3 clamp(extent3 const& value, extent3 const& min, extent3 const& max) noexcept
358 {
359 return extent3{clamp(static_cast<array_type>(value), static_cast<array_type>(min), static_cast<array_type>(max))};
360 }
361
366 [[nodiscard]] constexpr bool holds_invariant() const noexcept
367 {
368 return _v.x() >= 0.0f and _v.y() >= 0.0f and _v.z() >= 0.0f and _v.w() == 0.0f;
369 }
370
371 [[nodiscard]] friend std::string to_string(extent3 const& rhs) noexcept
372 {
373 return std::format("[{}, {}, {}]", rhs._v.x(), rhs._v.y(), rhs._v.z());
374 }
375
376 friend std::ostream& operator<<(std::ostream& lhs, extent3 const& rhs) noexcept
377 {
378 return lhs << to_string(rhs);
379 }
380
381private:
382 array_type _v;
383};
384
385}} // namespace hi::v1
386
387template<typename CharT>
388struct std::formatter<hi::extent3, CharT> {
389 auto parse(auto& pc)
390 {
391 return pc.end();
392 }
393
394 auto format(hi::extent3 const& t, auto& fc)
395 {
396 return std::vformat_to(fc.out(), "[{}, {}, {}]", std::make_format_args(t.width(), t.height(), t.depth()));
397 }
398};
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
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
A high-level geometric extent.
Definition extent2.hpp:26
A high-level geometric extent.
Definition extent3.hpp:27
constexpr extent3(extent2 const &other) noexcept
Construct a extent from a lower dimension extent.
Definition extent3.hpp:39
constexpr friend extent3 operator+(extent3 const &lhs, float const &rhs) noexcept
Add a scaler to the extent.
Definition extent3.hpp:240
constexpr float height() const noexcept
Access the y-as-height element from the extent.
Definition extent3.hpp:159
constexpr friend float rcp_hypot(extent3 const &rhs) noexcept
Get the length of the extent.
Definition extent3.hpp:318
constexpr extent3(float width, float height, float depth=0.0f) noexcept
Construct a 3D extent from width, height and depth.
Definition extent3.hpp:82
constexpr friend extent3 normalize(extent3 const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent3.hpp:327
constexpr friend extent3 operator-(extent3 const &lhs, extent3 const &rhs) noexcept
Subtract two extents from each other.
Definition extent3.hpp:205
constexpr friend extent3 operator*(extent3 const &lhs, float const &rhs) noexcept
Scale the extent by a scaler.
Definition extent3.hpp:215
constexpr float depth() const noexcept
Access the z-as-depth element from the extent.
Definition extent3.hpp:170
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent3.hpp:126
constexpr float & depth() noexcept
Access the z-as-depth element from the extent.
Definition extent3.hpp:137
constexpr float width() const noexcept
Access the x-as-width element from the extent.
Definition extent3.hpp:148
constexpr friend bool operator==(extent3 const &lhs, extent3 const &rhs) noexcept
Compare if two extents are equal.
Definition extent3.hpp:265
constexpr extent3() noexcept
Construct a empty extent / zero length.
Definition extent3.hpp:75
constexpr bool holds_invariant() const noexcept
Check if the extent is valid.
Definition extent3.hpp:366
constexpr friend extent3 operator*(float const &lhs, extent3 const &rhs) noexcept
Scale the extent by a scaler.
Definition extent3.hpp:255
friend float hypot(extent3 const &rhs) noexcept
Get the length of the extent.
Definition extent3.hpp:309
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent3.hpp:115
hi_force_inline constexpr friend float squared_hypot(extent3 const &rhs) noexcept
Get the squared length of the extent.
Definition extent3.hpp:300
constexpr friend extent3 operator+(extent3 const &lhs, extent3 const &rhs) noexcept
Add two extents from each other.
Definition extent3.hpp:195
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:18
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector3.hpp:19
T infinity(T... args)
T signaling_NaN(T... args)