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 "../SIMD/module.hpp"
9
10namespace hi { inline namespace v1 {
11
19class vector3 {
20public:
21 using array_type = simd<float, 4>;
22 using value_type = array_type::value_type;
23
24 constexpr vector3(vector3 const&) noexcept = default;
25 constexpr vector3(vector3&&) noexcept = default;
26 constexpr vector3& operator=(vector3 const&) noexcept = default;
27 constexpr vector3& operator=(vector3&&) noexcept = default;
28
31 [[nodiscard]] constexpr vector3(vector2 const& other) noexcept : _v(static_cast<array_type>(other))
32 {
34 }
35
39 [[nodiscard]] constexpr explicit operator vector2() noexcept
40 {
41 auto tmp = _v;
42 tmp.z() = 0.0f;
43 return vector2{tmp};
44 }
45
48 [[nodiscard]] constexpr explicit operator array_type() const noexcept
49 {
50 return _v;
51 }
52
55 [[nodiscard]] constexpr explicit vector3(array_type const& other) noexcept : _v(other)
56 {
58 }
59
62 [[nodiscard]] constexpr vector3() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
63
69 [[nodiscard]] constexpr vector3(float x, float y, float z = 0.0f) noexcept : _v(x, y, z, 0.0f) {}
70
74 [[nodiscard]] constexpr float& x() noexcept
75 {
76 return _v.x();
77 }
78
82 [[nodiscard]] constexpr float& y() noexcept
83 {
84 return _v.y();
85 }
86
90 [[nodiscard]] constexpr float& z() noexcept
91 {
92 return _v.z();
93 }
94
98 [[nodiscard]] constexpr float x() const noexcept
99 {
100 return _v.x();
101 }
102
106 [[nodiscard]] constexpr float y() const noexcept
107 {
108 return _v.y();
109 }
110
114 [[nodiscard]] constexpr float z() const noexcept
115 {
116 return _v.z();
117 }
118
122 [[nodiscard]] constexpr vector3 operator-() const noexcept
123 {
124 return vector3{-_v};
125 }
126
127 constexpr vector3& operator+=(vector3 const& rhs) noexcept
128 {
129 return *this = *this + rhs;
130 }
131
132 constexpr vector3& operator-=(vector3 const& rhs) noexcept
133 {
134 return *this = *this - rhs;
135 }
136
137 constexpr vector3& operator*=(float const& rhs) noexcept
138 {
139 return *this = *this * rhs;
140 }
141
147 [[nodiscard]] constexpr friend vector3 operator+(vector3 const& lhs, vector3 const& rhs) noexcept
148 {
149 return vector3{lhs._v + rhs._v};
150 }
151
157 [[nodiscard]] constexpr friend vector3 operator-(vector3 const& lhs, vector3 const& rhs) noexcept
158 {
159 return vector3{lhs._v - rhs._v};
160 }
161
167 [[nodiscard]] constexpr friend vector3 operator*(vector3 const& lhs, float const& rhs) noexcept
168 {
169 return vector3{lhs._v * rhs};
170 }
171
177 [[nodiscard]] constexpr friend vector3 operator*(float const& lhs, vector3 const& rhs) noexcept
178 {
179 return vector3{array_type::broadcast(lhs) * rhs._v};
180 }
181
187 [[nodiscard]] constexpr friend bool operator==(vector3 const& lhs, vector3 const& rhs) noexcept
188 {
189 return equal(lhs._v, rhs._v);
190 }
191
196 [[nodiscard]] constexpr friend float squared_hypot(vector3 const& rhs) noexcept
197 {
198 return squared_hypot<0b0111>(rhs._v);
199 }
200
205 [[nodiscard]] friend float hypot(vector3 const& rhs) noexcept
206 {
207 return hypot<0b0111>(rhs._v);
208 }
209
214 [[nodiscard]] constexpr friend float rcp_hypot(vector3 const& rhs) noexcept
215 {
216 return rcp_hypot<0b0111>(rhs._v);
217 }
218
223 [[nodiscard]] constexpr friend vector3 normalize(vector3 const& rhs) noexcept
224 {
225 return vector3{normalize<0b0111>(rhs._v)};
226 }
227
233 [[nodiscard]] constexpr friend float dot(vector3 const& lhs, vector3 const& rhs) noexcept
234 {
235 return dot<0b0111>(lhs._v, rhs._v);
236 }
237
243 [[nodiscard]] constexpr friend vector3 normal(vector3 const& rhs, float angle) noexcept
244 {
245 if (angle != float{0}) {
247 }
248 return normal(vector2{array_type{rhs}.xy00()});
249 }
250
256 [[nodiscard]] constexpr friend vector3 cross(vector3 const& lhs, vector3 const& rhs) noexcept
257 {
258 return vector3{cross_3D(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
259 }
260
266 [[nodiscard]] friend constexpr vector3 min(vector3 const& lhs, vector3 const& rhs) noexcept
267 {
268 return vector3{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
269 }
270
276 [[nodiscard]] friend constexpr vector3 max(vector3 const& lhs, vector3 const& rhs) noexcept
277 {
278 return vector3{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
279 }
280
283 [[nodiscard]] friend constexpr vector3 round(vector3 const& rhs) noexcept
284 {
285 return vector3{round(static_cast<array_type>(rhs))};
286 }
287
290 [[nodiscard]] friend constexpr vector3 ceil(vector3 const& rhs) noexcept
291 {
292 return vector3{ceil(static_cast<array_type>(rhs))};
293 }
294
297 [[nodiscard]] friend constexpr vector3 floor(vector3 const& rhs) noexcept
298 {
299 return vector3{floor(static_cast<array_type>(rhs))};
300 }
301
305 [[nodiscard]] constexpr bool holds_invariant() const noexcept
306 {
307 return _v.w() == 0.0f;
308 }
309
310 [[nodiscard]] friend std::string to_string(vector3 const& rhs) noexcept
311 {
312 return std::format("({}, {}, {})", rhs._v.x(), rhs._v.y(), rhs._v.z());
313 }
314
315 friend std::ostream& operator<<(std::ostream& lhs, vector3 const& rhs) noexcept
316 {
317 return lhs << to_string(rhs);
318 }
319
320private:
321 array_type _v;
322};
323
324
325
326}} // namespace hi::v1
327
328template<typename CharT>
329struct std::formatter<hi::vector3, CharT> {
330 auto parse(auto& pc)
331 {
332 return pc.end();
333 }
334
335 auto format(hi::vector3 const& t, auto& fc)
336 {
337 return std::vformat_to(fc.out(), "({}, {}, {})", std::make_format_args(t.x(), t.y(), t.z()));
338 }
339};
#define hi_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:335
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
@ 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 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
constexpr vector3 operator-() const noexcept
Mirror this vector.
Definition vector3.hpp:122
constexpr friend float rcp_hypot(vector3 const &rhs) noexcept
Get the length of the vector.
Definition vector3.hpp:214
constexpr friend vector3 operator+(vector3 const &lhs, vector3 const &rhs) noexcept
Add two vectors from each other.
Definition vector3.hpp:147
constexpr friend vector3 operator*(vector3 const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector3.hpp:167
constexpr friend vector3 operator*(float const &lhs, vector3 const &rhs) noexcept
Scale the vector by a scaler.
Definition vector3.hpp:177
constexpr friend bool operator==(vector3 const &lhs, vector3 const &rhs) noexcept
Compare if two vectors are equal.
Definition vector3.hpp:187
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:276
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:266
constexpr vector3(float x, float y, float z=0.0f) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector3.hpp:69
constexpr friend vector3 operator-(vector3 const &lhs, vector3 const &rhs) noexcept
Subtract two vectors from each other.
Definition vector3.hpp:157
constexpr float z() const noexcept
Access the z element from the vector.
Definition vector3.hpp:114
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector3.hpp:74
constexpr friend float dot(vector3 const &lhs, vector3 const &rhs) noexcept
Get the dot product between two vectors.
Definition vector3.hpp:233
constexpr float x() const noexcept
Access the x element from the vector.
Definition vector3.hpp:98
constexpr vector3(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector3.hpp:55
constexpr friend float squared_hypot(vector3 const &rhs) noexcept
Get the squared length of the vector.
Definition vector3.hpp:196
friend constexpr vector3 round(vector3 const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector3.hpp:283
constexpr friend vector3 normalize(vector3 const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector3.hpp:223
friend float hypot(vector3 const &rhs) noexcept
Get the length of the vector.
Definition vector3.hpp:205
constexpr friend vector3 cross(vector3 const &lhs, vector3 const &rhs) noexcept
Get the cross product between two 3D vectors.
Definition vector3.hpp:256
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector3.hpp:305
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector3.hpp:82
constexpr vector3() noexcept
Construct a empty vector / zero length.
Definition vector3.hpp:62
constexpr vector3(vector2 const &other) noexcept
Construct a vector from a lower dimension vector.
Definition vector3.hpp:31
constexpr float y() const noexcept
Access the y element from the vector.
Definition vector3.hpp:106
friend constexpr vector3 ceil(vector3 const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector3.hpp:290
friend constexpr vector3 floor(vector3 const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector3.hpp:297
constexpr friend vector3 normal(vector3 const &rhs, float angle) noexcept
Get the normal on a 3D vector.
Definition vector3.hpp:243
constexpr float & z() noexcept
Access the z element from the vector.
Definition vector3.hpp:90