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/utility.hpp"
15#include "../macros.hpp"
16#include <compare>
17#include <concepts>
18
19
20
21namespace hi { inline namespace v1 {
22
30class extent3 {
31public:
32 using array_type = simd<float, 4>;
33 using value_type = array_type::value_type;
34
35 constexpr extent3(extent3 const&) noexcept = default;
36 constexpr extent3(extent3&&) noexcept = default;
37 constexpr extent3& operator=(extent3 const&) noexcept = default;
38 constexpr extent3& operator=(extent3&&) noexcept = default;
39
42 [[nodiscard]] constexpr extent3(extent2 const& other) noexcept : _v(static_cast<array_type>(other))
43 {
44 hi_axiom(holds_invariant());
45 }
46
47 [[nodiscard]] constexpr explicit operator extent2() const noexcept
48 {
49 auto tmp = _v;
50 tmp.z() = 0.0f;
51 return extent2{tmp};
52 }
53
56 [[nodiscard]] constexpr explicit operator array_type() const noexcept
57 {
58 return _v;
59 }
60
61 [[nodiscard]] constexpr explicit extent3(array_type const& other) noexcept : _v(other)
62 {
63 hi_axiom(holds_invariant());
64 }
65
66 [[nodiscard]] constexpr explicit operator bool() const noexcept
67 {
68 return _v.x() != 0.0f or _v.y() != 0.0f or _v.z() != 0.0f;
69 }
70
71 [[nodiscard]] constexpr explicit operator vector3() const noexcept
72 {
73 return vector3{static_cast<array_type>(*this)};
74 }
75
78 [[nodiscard]] constexpr extent3() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
79
85 [[nodiscard]] constexpr extent3(float width, float height, float depth = 0.0f) noexcept : _v(width, height, depth, 0.0f)
86 {
87 hi_axiom(holds_invariant());
88 }
89
90 [[nodiscard]] constexpr static extent3 infinity() noexcept
91 {
92 return extent3{
96 }
97
98 [[nodiscard]] constexpr static extent3 large() noexcept
99 {
101 }
102
103 [[nodiscard]] constexpr static extent3 nan() noexcept
104 {
105 auto r = extent3{};
109 return r;
110 }
111
118 [[nodiscard]] constexpr float& width() noexcept
119 {
120 return _v.x();
121 }
122
129 [[nodiscard]] constexpr float& height() noexcept
130 {
131 return _v.y();
132 }
133
140 [[nodiscard]] constexpr float& depth() noexcept
141 {
142 return _v.z();
143 }
144
151 [[nodiscard]] constexpr float width() const noexcept
152 {
153 return _v.x();
154 }
155
162 [[nodiscard]] constexpr float height() const noexcept
163 {
164 return _v.y();
165 }
166
173 [[nodiscard]] constexpr float depth() const noexcept
174 {
175 return _v.z();
176 }
177
178 [[nodiscard]] constexpr vector3 right() const noexcept
179 {
180 return vector3{_v.x000()};
181 }
182
183 [[nodiscard]] constexpr vector3 up() const noexcept
184 {
185 return vector3{_v._0y00()};
186 }
187
188 constexpr extent3& operator+=(extent3 const& rhs) noexcept
189 {
190 return *this = *this + rhs;
191 }
192
198 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, extent3 const& rhs) noexcept
199 {
200 return extent3{lhs._v + rhs._v};
201 }
202
208 [[nodiscard]] constexpr friend extent3 operator-(extent3 const& lhs, extent3 const& rhs) noexcept
209 {
210 return extent3{lhs._v - rhs._v};
211 }
212
218 [[nodiscard]] constexpr friend extent3 operator*(extent3 const& lhs, float const& rhs) noexcept
219 {
220 return extent3{lhs._v * rhs};
221 }
222
223 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, vector2 const& rhs) noexcept
224 {
225 return extent3{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
226 }
227
228 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, vector3 const& rhs) noexcept
229 {
230 return extent3{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
231 }
232
233 [[nodiscard]] constexpr friend vector3 operator+(vector3 const& lhs, extent3 const& rhs) noexcept
234 {
235 return vector3{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
236 }
237
243 [[nodiscard]] constexpr friend extent3 operator+(extent3 const& lhs, float const& rhs) noexcept
244 {
245 auto r = extent3{};
246 for (std::size_t i = 0; i != 3; ++i) {
247 r._v[i] = lhs._v[i] + rhs;
248 }
249
250 return r;
251 }
252
258 [[nodiscard]] constexpr friend extent3 operator*(float const& lhs, extent3 const& rhs) noexcept
259 {
260 return extent3{lhs * rhs._v};
261 }
262
268 [[nodiscard]] constexpr friend bool operator==(extent3 const& lhs, extent3 const& rhs) noexcept
269 {
270 return equal(lhs._v, rhs._v);
271 }
272
273 [[nodiscard]] constexpr friend std::partial_ordering operator<=>(extent3 const& lhs, extent3 const& rhs) noexcept
274 {
275 constexpr std::size_t mask = 0b0111;
276
277 hilet equal = (lhs._v == rhs._v).mask() & mask;
278 if (equal == mask) {
279 // Only equivalent if all elements are equal.
280 return std::partial_ordering::equivalent;
281 }
282
283 hilet less = (lhs._v < rhs._v).mask() & mask;
284 if ((less | equal) == mask) {
285 // If one or more elements is less (but none are greater) then the ordering is less.
286 return std::partial_ordering::less;
287 }
288
289 hilet greater = (lhs._v < rhs._v).mask() & mask;
290 if ((greater | equal) == mask) {
291 // If one or more elements is greater (but none are less) then the ordering is greater.
292 return std::partial_ordering::greater;
293 }
294
295 // Some elements are less and others are greater, we don't have an ordering.
296 return std::partial_ordering::unordered;
297 }
298
303 [[nodiscard]] hi_force_inline constexpr friend float squared_hypot(extent3 const& rhs) noexcept
304 {
305 return squared_hypot<0b0111>(rhs._v);
306 }
307
312 [[nodiscard]] friend float hypot(extent3 const& rhs) noexcept
313 {
314 return hypot<0b0111>(rhs._v);
315 }
316
321 [[nodiscard]] constexpr friend float rcp_hypot(extent3 const& rhs) noexcept
322 {
323 return rcp_hypot<0b0111>(rhs._v);
324 }
325
330 [[nodiscard]] constexpr friend extent3 normalize(extent3 const& rhs) noexcept
331 {
332 return extent3{normalize<0b0111>(rhs._v)};
333 }
334
335 [[nodiscard]] constexpr friend extent3 ceil(extent3 const& rhs) noexcept
336 {
337 return extent3{ceil(array_type{rhs})};
338 }
339
340 [[nodiscard]] constexpr friend extent3 floor(extent3 const& rhs) noexcept
341 {
342 return extent3{floor(static_cast<array_type>(rhs))};
343 }
344
345 [[nodiscard]] constexpr friend extent3 round(extent3 const& rhs) noexcept
346 {
347 return extent3{round(static_cast<array_type>(rhs))};
348 }
349
350 [[nodiscard]] constexpr friend extent3 min(extent3 const& lhs, extent3 const& rhs) noexcept
351 {
352 return extent3{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
353 }
354
355 [[nodiscard]] constexpr friend extent3 max(extent3 const& lhs, extent3 const& rhs) noexcept
356 {
357 return extent3{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
358 }
359
360 [[nodiscard]] constexpr friend extent3 clamp(extent3 const& value, extent3 const& min, extent3 const& max) noexcept
361 {
362 return extent3{clamp(static_cast<array_type>(value), static_cast<array_type>(min), static_cast<array_type>(max))};
363 }
364
370 {
371 return _v.x() >= 0.0f and _v.y() >= 0.0f and _v.z() >= 0.0f and _v.w() == 0.0f;
372 }
373
374 [[nodiscard]] friend std::string to_string(extent3 const& rhs) noexcept
375 {
376 return std::format("[{}, {}, {}]", rhs._v.x(), rhs._v.y(), rhs._v.z());
377 }
378
379 friend std::ostream& operator<<(std::ostream& lhs, extent3 const& rhs) noexcept
380 {
381 return lhs << to_string(rhs);
382 }
383
384private:
385 array_type _v;
386};
387
388}} // namespace hi::v1
389
390template<typename CharT>
391struct std::formatter<hi::extent3, CharT> {
392 auto parse(auto& pc)
393 {
394 return pc.end();
395 }
396
397 auto format(hi::extent3 const& t, auto& fc) const
398 {
399 return std::vformat_to(fc.out(), "[{}, {}, {}]", std::make_format_args(t.width(), t.height(), t.depth()));
400 }
401};
Defined the geo::extent, extent2 and extent3 types.
@ other
The gui_event does not have associated data.
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 extent.
Definition extent2.hpp:29
A high-level geometric extent.
Definition extent3.hpp:30
constexpr extent3(extent2 const &other) noexcept
Construct a extent from a lower dimension extent.
Definition extent3.hpp:42
constexpr friend extent3 operator+(extent3 const &lhs, float const &rhs) noexcept
Add a scaler to the extent.
Definition extent3.hpp:243
constexpr float height() const noexcept
Access the y-as-height element from the extent.
Definition extent3.hpp:162
constexpr friend float rcp_hypot(extent3 const &rhs) noexcept
Get the length of the extent.
Definition extent3.hpp:321
constexpr extent3(float width, float height, float depth=0.0f) noexcept
Construct a 3D extent from width, height and depth.
Definition extent3.hpp:85
constexpr friend extent3 normalize(extent3 const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent3.hpp:330
constexpr friend extent3 operator-(extent3 const &lhs, extent3 const &rhs) noexcept
Subtract two extents from each other.
Definition extent3.hpp:208
constexpr friend extent3 operator*(extent3 const &lhs, float const &rhs) noexcept
Scale the extent by a scaler.
Definition extent3.hpp:218
constexpr float depth() const noexcept
Access the z-as-depth element from the extent.
Definition extent3.hpp:173
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent3.hpp:129
constexpr float & depth() noexcept
Access the z-as-depth element from the extent.
Definition extent3.hpp:140
constexpr float width() const noexcept
Access the x-as-width element from the extent.
Definition extent3.hpp:151
constexpr friend bool operator==(extent3 const &lhs, extent3 const &rhs) noexcept
Compare if two extents are equal.
Definition extent3.hpp:268
constexpr extent3() noexcept
Construct a empty extent / zero length.
Definition extent3.hpp:78
constexpr bool holds_invariant() const noexcept
Check if the extent is valid.
Definition extent3.hpp:369
constexpr friend extent3 operator*(float const &lhs, extent3 const &rhs) noexcept
Scale the extent by a scaler.
Definition extent3.hpp:258
friend float hypot(extent3 const &rhs) noexcept
Get the length of the extent.
Definition extent3.hpp:312
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent3.hpp:118
hi_force_inline constexpr friend float squared_hypot(extent3 const &rhs) noexcept
Get the squared length of the extent.
Definition extent3.hpp:303
constexpr friend extent3 operator+(extent3 const &lhs, extent3 const &rhs) noexcept
Add two extents from each other.
Definition extent3.hpp:198
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:19
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector3.hpp:20
T infinity(T... args)
T signaling_NaN(T... args)