HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
vector3.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 "vector2.hpp"
8#include "../macros.hpp"
9#include <hikocpu/hikocpu.hpp>
10#include <exception>
11#include <format>
12#include <ostream>
13#include <compare>
14
15hi_export_module(hikogui.geometry : vector3);
16
17hi_export namespace hi { inline namespace v1 {
18
26class vector3 {
27public:
29 using value_type = array_type::value_type;
30
31 constexpr vector3(vector3 const&) noexcept = default;
32 constexpr vector3(vector3&&) noexcept = default;
33 constexpr vector3& operator=(vector3 const&) noexcept = default;
34 constexpr vector3& operator=(vector3&&) noexcept = default;
35
38 [[nodiscard]] constexpr vector3(vector2 const& other) noexcept : _v(static_cast<array_type>(other))
39 {
40 hi_axiom(holds_invariant());
41 }
42
46 [[nodiscard]] constexpr explicit operator vector2() noexcept
47 {
48 auto tmp = _v;
49 tmp.z() = 0.0f;
50 return vector2{tmp};
51 }
52
55 [[nodiscard]] constexpr explicit operator array_type() const noexcept
56 {
57 return _v;
58 }
59
62 [[nodiscard]] constexpr explicit vector3(array_type const& other) noexcept : _v(other)
63 {
64 hi_axiom(holds_invariant());
65 }
66
69 [[nodiscard]] constexpr vector3() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
70
76 [[nodiscard]] constexpr vector3(float x, float y, float z = 0.0f) noexcept : _v(x, y, z, 0.0f) {}
77
81 [[nodiscard]] constexpr float& x() noexcept
82 {
83 return _v.x();
84 }
85
89 [[nodiscard]] constexpr float& y() noexcept
90 {
91 return _v.y();
92 }
93
97 [[nodiscard]] constexpr float& z() noexcept
98 {
99 return _v.z();
100 }
101
105 [[nodiscard]] constexpr float x() const noexcept
106 {
107 return _v.x();
108 }
109
113 [[nodiscard]] constexpr float y() const noexcept
114 {
115 return _v.y();
116 }
117
121 [[nodiscard]] constexpr float z() const noexcept
122 {
123 return _v.z();
124 }
125
129 [[nodiscard]] constexpr vector3 operator-() const noexcept
130 {
131 return vector3{-_v};
132 }
133
134 constexpr vector3& operator+=(vector3 const& rhs) noexcept
135 {
136 return *this = *this + rhs;
137 }
138
139 constexpr vector3& operator-=(vector3 const& rhs) noexcept
140 {
141 return *this = *this - rhs;
142 }
143
144 constexpr vector3& operator*=(float const& rhs) noexcept
145 {
146 return *this = *this * rhs;
147 }
148
154 [[nodiscard]] constexpr friend vector3 operator+(vector3 const& lhs, vector3 const& rhs) noexcept
155 {
156 return vector3{lhs._v + rhs._v};
157 }
158
164 [[nodiscard]] constexpr friend vector3 operator-(vector3 const& lhs, vector3 const& rhs) noexcept
165 {
166 return vector3{lhs._v - rhs._v};
167 }
168
174 [[nodiscard]] constexpr friend vector3 operator*(vector3 const& lhs, float const& rhs) noexcept
175 {
176 return vector3{lhs._v * array_type::broadcast(rhs)};
177 }
178
184 [[nodiscard]] constexpr friend vector3 operator*(float const& lhs, vector3 const& rhs) noexcept
185 {
186 return vector3{array_type::broadcast(lhs) * rhs._v};
187 }
188
194 [[nodiscard]] constexpr friend bool operator==(vector3 const& lhs, vector3 const& rhs) noexcept
195 {
196 return equal(lhs._v, rhs._v);
197 }
198
203 [[nodiscard]] constexpr friend float squared_hypot(vector3 const& rhs) noexcept
204 {
205 return dot<0b0111>(rhs._v, rhs._v).x();
206 }
207
212 [[nodiscard]] friend float hypot(vector3 const& rhs) noexcept
213 {
214 return hypot<0b0111>(rhs._v).x();
215 }
216
221 [[nodiscard]] constexpr friend float rcp_hypot(vector3 const& rhs) noexcept
222 {
223 return rhypot<0b0111>(rhs._v).x();
224 }
225
230 [[nodiscard]] constexpr friend vector3 normalize(vector3 const& rhs) noexcept
231 {
232 return vector3{normalize<0b0111>(rhs._v)};
233 }
234
240 [[nodiscard]] constexpr friend float dot(vector3 const& lhs, vector3 const& rhs) noexcept
241 {
242 return dot<0b0111>(lhs._v, rhs._v).x();
243 }
244
250 [[nodiscard]] constexpr friend vector3 normal(vector3 const& rhs, float angle) noexcept
251 {
252 if (angle != float{0}) {
253 hi_not_implemented();
254 }
255 return normal(vector2{array_type{rhs}.xy00()});
256 }
257
263 [[nodiscard]] constexpr friend vector3 cross(vector3 const& lhs, vector3 const& rhs) noexcept
264 {
265 auto const a_left = lhs._v.yzxw();
266 auto const b_left = rhs._v.zxyw();
267 auto const left = a_left * b_left;
268
269 auto const a_right = lhs._v.zxyw();
270 auto const b_right = rhs._v.yzxw();
271 auto const right = a_right * b_right;
272 return vector3{left - right};
273 }
274
280 [[nodiscard]] friend constexpr vector3 min(vector3 const& lhs, vector3 const& rhs) noexcept
281 {
282 return vector3{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
283 }
284
290 [[nodiscard]] friend constexpr vector3 max(vector3 const& lhs, vector3 const& rhs) noexcept
291 {
292 return vector3{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
293 }
294
297 [[nodiscard]] friend constexpr vector3 round(vector3 const& rhs) noexcept
298 {
299 return vector3{round(static_cast<array_type>(rhs))};
300 }
301
304 [[nodiscard]] friend constexpr vector3 ceil(vector3 const& rhs) noexcept
305 {
306 return vector3{ceil(static_cast<array_type>(rhs))};
307 }
308
311 [[nodiscard]] friend constexpr vector3 floor(vector3 const& rhs) noexcept
312 {
313 return vector3{floor(static_cast<array_type>(rhs))};
314 }
315
319 [[nodiscard]] constexpr bool holds_invariant() const noexcept
320 {
321 return _v.w() == 0.0f;
322 }
323
324 [[nodiscard]] friend std::string to_string(vector3 const& rhs) noexcept
325 {
326 return std::format("({}, {}, {})", rhs._v.x(), rhs._v.y(), rhs._v.z());
327 }
328
329 friend std::ostream& operator<<(std::ostream& lhs, vector3 const& rhs) noexcept
330 {
331 return lhs << to_string(rhs);
332 }
333
334private:
335 array_type _v;
336};
337
338
339
340}} // namespace hi::v1
341
342// XXX #617 MSVC bug does not handle partial specialization in modules.
343hi_export template<>
344struct std::formatter<hi::vector3, char> : std::formatter<std::string, char> {
345 auto format(hi::vector3 const& t, auto& fc) const
346 {
347 return std::formatter<std::string, char>::format(std::format("({}, {}, {})", t.x(), t.y(), t.z()), fc);
348 }
349};
@ right
Align the text to the right side.
@ left
Align the text to the left side.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector3.hpp:26
constexpr vector3 operator-() const noexcept
Mirror this vector.
Definition vector3.hpp:129
constexpr friend float rcp_hypot(vector3 const &rhs) noexcept
Get the length of the vector.
Definition vector3.hpp:221
constexpr friend vector3 operator+(vector3 const &lhs, vector3 const &rhs) noexcept
Add two vectors from each other.
Definition vector3.hpp:154
constexpr friend vector3 operator*(vector3 const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector3.hpp:174
constexpr friend vector3 operator*(float const &lhs, vector3 const &rhs) noexcept
Scale the vector by a scaler.
Definition vector3.hpp:184
constexpr friend bool operator==(vector3 const &lhs, vector3 const &rhs) noexcept
Compare if two vectors are equal.
Definition vector3.hpp:194
friend constexpr vector3 max(vector3 const &lhs, vector3 const &rhs) noexcept
Mix the two vectors and get the highest value of each element.
Definition vector3.hpp:290
friend constexpr vector3 min(vector3 const &lhs, vector3 const &rhs) noexcept
Mix the two vectors and get the lowest value of each element.
Definition vector3.hpp:280
constexpr vector3(float x, float y, float z=0.0f) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector3.hpp:76
constexpr friend vector3 operator-(vector3 const &lhs, vector3 const &rhs) noexcept
Subtract two vectors from each other.
Definition vector3.hpp:164
constexpr float z() const noexcept
Access the z element from the vector.
Definition vector3.hpp:121
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector3.hpp:81
constexpr friend float dot(vector3 const &lhs, vector3 const &rhs) noexcept
Get the dot product between two vectors.
Definition vector3.hpp:240
constexpr float x() const noexcept
Access the x element from the vector.
Definition vector3.hpp:105
constexpr vector3(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector3.hpp:62
constexpr friend float squared_hypot(vector3 const &rhs) noexcept
Get the squared length of the vector.
Definition vector3.hpp:203
friend constexpr vector3 round(vector3 const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector3.hpp:297
constexpr friend vector3 normalize(vector3 const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector3.hpp:230
friend float hypot(vector3 const &rhs) noexcept
Get the length of the vector.
Definition vector3.hpp:212
constexpr friend vector3 cross(vector3 const &lhs, vector3 const &rhs) noexcept
Get the cross product between two 3D vectors.
Definition vector3.hpp:263
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector3.hpp:319
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector3.hpp:89
constexpr vector3() noexcept
Construct a empty vector / zero length.
Definition vector3.hpp:69
constexpr vector3(vector2 const &other) noexcept
Construct a vector from a lower dimension vector.
Definition vector3.hpp:38
constexpr float y() const noexcept
Access the y element from the vector.
Definition vector3.hpp:113
friend constexpr vector3 ceil(vector3 const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector3.hpp:304
friend constexpr vector3 floor(vector3 const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector3.hpp:311
constexpr friend vector3 normal(vector3 const &rhs, float angle) noexcept
Get the normal on a 3D vector.
Definition vector3.hpp:250
constexpr float & z() noexcept
Access the z element from the vector.
Definition vector3.hpp:97