HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
extent.hpp
1// Copyright Take Vos 2021.
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 "vector.hpp"
8#include "../numeric_array.hpp"
9
10namespace tt {
11namespace geo {
12
19template<int D>
20class extent {
21public:
22 static_assert(D == 2 || D == 3, "Only 2D or 3D extents are supported");
23
24 constexpr extent(extent const &) noexcept = default;
25 constexpr extent(extent &&) noexcept = default;
26 constexpr extent &operator=(extent const &) noexcept = default;
27 constexpr extent &operator=(extent &&) noexcept = default;
28
31 template<int E>
32 requires(E < D) [[nodiscard]] constexpr extent(extent<E> const &other) noexcept : _v(static_cast<f32x4>(other))
33 {
34 tt_axiom(is_valid());
35 }
36
39 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
40 {
41 return _v;
42 }
43
46 [[nodiscard]] constexpr explicit extent(f32x4 const &other) noexcept : _v(other)
47 {
48 tt_axiom(is_valid());
49 }
50
53 [[nodiscard]] constexpr extent() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
54
59 [[nodiscard]] constexpr extent(float width, float height) noexcept requires(D == 2) : _v(width, height, 0.0f, 0.0f) {}
60
66 [[nodiscard]] constexpr extent(float width, float height, float depth = 0.0f) noexcept requires(D == 3) : _v(width, height, depth, 0.0f) {}
67
74 [[nodiscard]] constexpr float &width() noexcept
75 {
76 return _v.x();
77 }
78
85 [[nodiscard]] constexpr float &height() noexcept
86 {
87 return _v.y();
88 }
89
96 [[nodiscard]] constexpr float &depth() noexcept requires(D == 3)
97 {
98 return _v.z();
99 }
100
107 [[nodiscard]] constexpr float const &width() const noexcept
108 {
109 return _v.x();
110 }
111
118 [[nodiscard]] constexpr float const &height() const noexcept
119 {
120 return _v.y();
121 }
122
129 [[nodiscard]] constexpr float const &depth() const noexcept requires(D == 3)
130 {
131 return _v.z();
132 }
133
139 [[nodiscard]] constexpr friend extent operator+(extent const &lhs, extent const &rhs) noexcept
140 {
141 tt_axiom(lhs.is_valid() && rhs.is_valid());
142 return extent{lhs._v + rhs._v};
143 }
144
150 [[nodiscard]] constexpr friend extent operator-(extent const &lhs, extent const &rhs) noexcept
151 {
152 tt_axiom(lhs.is_valid() && rhs.is_valid());
153 return extent{lhs._v - rhs._v};
154 }
155
161 [[nodiscard]] constexpr friend extent operator*(extent const &lhs, float const &rhs) noexcept
162 {
163 tt_axiom(lhs.is_valid());
164 return extent{lhs._v * rhs};
165 }
166
172 [[nodiscard]] constexpr friend extent operator*(float const &lhs, extent const &rhs) noexcept
173 {
174 tt_axiom(rhs.is_valid());
175 return extent{lhs * rhs._v};
176 }
177
183 [[nodiscard]] constexpr friend bool operator==(extent const &lhs, extent const &rhs) noexcept
184 {
185 tt_axiom(lhs.is_valid() && rhs.is_valid());
186 return lhs._v == rhs._v;
187 }
188
193 [[nodiscard]] constexpr friend float squared_hypot(extent const &rhs) noexcept
194 {
195 tt_axiom(rhs.is_valid());
196 return squared_hypot<element_mask>(rhs._v);
197 }
198
203 [[nodiscard]] constexpr friend float hypot(extent const &rhs) noexcept
204 {
205 tt_axiom(rhs.is_valid());
206 return hypot<element_mask>(rhs._v);
207 }
208
213 [[nodiscard]] constexpr friend float rcp_hypot(extent const &rhs) noexcept
214 {
215 tt_axiom(rhs.is_valid());
216 return rcp_hypot<element_mask>(rhs._v);
217 }
218
223 [[nodiscard]] constexpr friend extent normalize(extent const &rhs) noexcept
224 {
225 tt_axiom(rhs.is_valid());
226 return extent{normalize<element_mask>(rhs._v)};
227 }
228
232 [[nodiscard]] constexpr bool is_valid() const noexcept
233 {
234 return _v.w() == 0.0f && (D == 3 || _v.z() == 0.0f);
235 }
236
237private:
238 f32x4 _v;
239
240 static constexpr size_t element_mask = (1_uz << D) - 1;
241};
242
243}
244
245using extent2 = geo::extent<2>;
246using extent3 = geo::extent<3>;
247
248} // namespace tt
A high-level geometric extent.
Definition extent.hpp:20
constexpr float const & height() const noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:118
constexpr float const & width() const noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:107
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:74
constexpr friend extent operator*(float const &lhs, extent const &rhs) noexcept
Scale the extent by a scaler.
Definition extent.hpp:172
constexpr bool is_valid() const noexcept
Check if the extent is valid.
Definition extent.hpp:232
constexpr friend float squared_hypot(extent const &rhs) noexcept
Get the squared length of the extent.
Definition extent.hpp:193
constexpr friend extent operator-(extent const &lhs, extent const &rhs) noexcept
Subtract two extents from each other.
Definition extent.hpp:150
constexpr friend extent operator*(extent const &lhs, float const &rhs) noexcept
Scale the extent by a scaler.
Definition extent.hpp:161
constexpr friend float rcp_hypot(extent const &rhs) noexcept
Get the length of the extent.
Definition extent.hpp:213
constexpr friend extent normalize(extent const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent.hpp:223
constexpr extent(float width, float height, float depth=0.0f) noexcept
Construct a 3D extent from x, y and z elements.
Definition extent.hpp:66
constexpr extent(extent< E > const &other) noexcept
Construct a extent from a lower dimension extent.
Definition extent.hpp:32
constexpr extent(float width, float height) noexcept
Construct a 2D extent from x and y elements.
Definition extent.hpp:59
constexpr float & depth() noexcept
Access the z-as-depth element from the extent.
Definition extent.hpp:96
constexpr friend float hypot(extent const &rhs) noexcept
Get the length of the extent.
Definition extent.hpp:203
constexpr float const & depth() const noexcept
Access the z-as-depth element from the extent.
Definition extent.hpp:129
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:85
constexpr extent(f32x4 const &other) noexcept
Construct a extent from a f32x4-numeric_array.
Definition extent.hpp:46
constexpr friend bool operator==(extent const &lhs, extent const &rhs) noexcept
Compare if two extents are equal.
Definition extent.hpp:183
constexpr extent() noexcept
Construct a empty extent / zero length.
Definition extent.hpp:53
constexpr friend extent operator+(extent const &lhs, extent const &rhs) noexcept
Add two extents from each other.
Definition extent.hpp:139