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#include "../macros.hpp"
10
11namespace hi { inline namespace v1 {
12
20class vector3 {
21public:
22 using array_type = simd<float, 4>;
23 using value_type = array_type::value_type;
24
25 constexpr vector3(vector3 const&) noexcept = default;
26 constexpr vector3(vector3&&) noexcept = default;
27 constexpr vector3& operator=(vector3 const&) noexcept = default;
28 constexpr vector3& operator=(vector3&&) noexcept = default;
29
32 [[nodiscard]] constexpr vector3(vector2 const& other) noexcept : _v(static_cast<array_type>(other))
33 {
34 hi_axiom(holds_invariant());
35 }
36
40 [[nodiscard]] constexpr explicit operator vector2() noexcept
41 {
42 auto tmp = _v;
43 tmp.z() = 0.0f;
44 return vector2{tmp};
45 }
46
49 [[nodiscard]] constexpr explicit operator array_type() const noexcept
50 {
51 return _v;
52 }
53
56 [[nodiscard]] constexpr explicit vector3(array_type const& other) noexcept : _v(other)
57 {
58 hi_axiom(holds_invariant());
59 }
60
63 [[nodiscard]] constexpr vector3() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
64
70 [[nodiscard]] constexpr vector3(float x, float y, float z = 0.0f) noexcept : _v(x, y, z, 0.0f) {}
71
75 [[nodiscard]] constexpr float& x() noexcept
76 {
77 return _v.x();
78 }
79
83 [[nodiscard]] constexpr float& y() noexcept
84 {
85 return _v.y();
86 }
87
91 [[nodiscard]] constexpr float& z() noexcept
92 {
93 return _v.z();
94 }
95
99 [[nodiscard]] constexpr float x() const noexcept
100 {
101 return _v.x();
102 }
103
107 [[nodiscard]] constexpr float y() const noexcept
108 {
109 return _v.y();
110 }
111
115 [[nodiscard]] constexpr float z() const noexcept
116 {
117 return _v.z();
118 }
119
124 {
125 return vector3{-_v};
126 }
127
128 constexpr vector3& operator+=(vector3 const& rhs) noexcept
129 {
130 return *this = *this + rhs;
131 }
132
133 constexpr vector3& operator-=(vector3 const& rhs) noexcept
134 {
135 return *this = *this - rhs;
136 }
137
138 constexpr vector3& operator*=(float const& rhs) noexcept
139 {
140 return *this = *this * rhs;
141 }
142
148 [[nodiscard]] constexpr friend vector3 operator+(vector3 const& lhs, vector3 const& rhs) noexcept
149 {
150 return vector3{lhs._v + rhs._v};
151 }
152
158 [[nodiscard]] constexpr friend vector3 operator-(vector3 const& lhs, vector3 const& rhs) noexcept
159 {
160 return vector3{lhs._v - rhs._v};
161 }
162
168 [[nodiscard]] constexpr friend vector3 operator*(vector3 const& lhs, float const& rhs) noexcept
169 {
170 return vector3{lhs._v * rhs};
171 }
172
178 [[nodiscard]] constexpr friend vector3 operator*(float const& lhs, vector3 const& rhs) noexcept
179 {
180 return vector3{array_type::broadcast(lhs) * rhs._v};
181 }
182
188 [[nodiscard]] constexpr friend bool operator==(vector3 const& lhs, vector3 const& rhs) noexcept
189 {
190 return equal(lhs._v, rhs._v);
191 }
192
197 [[nodiscard]] constexpr friend float squared_hypot(vector3 const& rhs) noexcept
198 {
199 return squared_hypot<0b0111>(rhs._v);
200 }
201
206 [[nodiscard]] friend float hypot(vector3 const& rhs) noexcept
207 {
208 return hypot<0b0111>(rhs._v);
209 }
210
215 [[nodiscard]] constexpr friend float rcp_hypot(vector3 const& rhs) noexcept
216 {
217 return rcp_hypot<0b0111>(rhs._v);
218 }
219
224 [[nodiscard]] constexpr friend vector3 normalize(vector3 const& rhs) noexcept
225 {
226 return vector3{normalize<0b0111>(rhs._v)};
227 }
228
234 [[nodiscard]] constexpr friend float dot(vector3 const& lhs, vector3 const& rhs) noexcept
235 {
236 return dot<0b0111>(lhs._v, rhs._v);
237 }
238
244 [[nodiscard]] constexpr friend vector3 normal(vector3 const& rhs, float angle) noexcept
245 {
246 if (angle != float{0}) {
247 hi_not_implemented();
248 }
249 return normal(vector2{array_type{rhs}.xy00()});
250 }
251
257 [[nodiscard]] constexpr friend vector3 cross(vector3 const& lhs, vector3 const& rhs) noexcept
258 {
259 return vector3{cross_3D(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
260 }
261
267 [[nodiscard]] friend constexpr vector3 min(vector3 const& lhs, vector3 const& rhs) noexcept
268 {
269 return vector3{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
270 }
271
277 [[nodiscard]] friend constexpr vector3 max(vector3 const& lhs, vector3 const& rhs) noexcept
278 {
279 return vector3{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
280 }
281
284 [[nodiscard]] friend constexpr vector3 round(vector3 const& rhs) noexcept
285 {
286 return vector3{round(static_cast<array_type>(rhs))};
287 }
288
291 [[nodiscard]] friend constexpr vector3 ceil(vector3 const& rhs) noexcept
292 {
293 return vector3{ceil(static_cast<array_type>(rhs))};
294 }
295
298 [[nodiscard]] friend constexpr vector3 floor(vector3 const& rhs) noexcept
299 {
300 return vector3{floor(static_cast<array_type>(rhs))};
301 }
302
307 {
308 return _v.w() == 0.0f;
309 }
310
311 [[nodiscard]] friend std::string to_string(vector3 const& rhs) noexcept
312 {
313 return std::format("({}, {}, {})", rhs._v.x(), rhs._v.y(), rhs._v.z());
314 }
315
316 friend std::ostream& operator<<(std::ostream& lhs, vector3 const& rhs) noexcept
317 {
318 return lhs << to_string(rhs);
319 }
320
321private:
322 array_type _v;
323};
324
325
326
327}} // namespace hi::v1
328
329template<typename CharT>
330struct std::formatter<hi::vector3, CharT> {
331 auto parse(auto& pc)
332 {
333 return pc.end();
334 }
335
336 auto format(hi::vector3 const& t, auto& fc) const
337 {
338 return std::vformat_to(fc.out(), "({}, {}, {})", std::make_format_args(t.x(), t.y(), t.z()));
339 }
340};
@ 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 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
constexpr vector3 operator-() const noexcept
Mirror this vector.
Definition vector3.hpp:123
constexpr friend float rcp_hypot(vector3 const &rhs) noexcept
Get the length of the vector.
Definition vector3.hpp:215
constexpr friend vector3 operator+(vector3 const &lhs, vector3 const &rhs) noexcept
Add two vectors from each other.
Definition vector3.hpp:148
constexpr friend vector3 operator*(vector3 const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector3.hpp:168
constexpr friend vector3 operator*(float const &lhs, vector3 const &rhs) noexcept
Scale the vector by a scaler.
Definition vector3.hpp:178
constexpr friend bool operator==(vector3 const &lhs, vector3 const &rhs) noexcept
Compare if two vectors are equal.
Definition vector3.hpp:188
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:277
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:267
constexpr vector3(float x, float y, float z=0.0f) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector3.hpp:70
constexpr friend vector3 operator-(vector3 const &lhs, vector3 const &rhs) noexcept
Subtract two vectors from each other.
Definition vector3.hpp:158
constexpr float z() const noexcept
Access the z element from the vector.
Definition vector3.hpp:115
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector3.hpp:75
constexpr friend float dot(vector3 const &lhs, vector3 const &rhs) noexcept
Get the dot product between two vectors.
Definition vector3.hpp:234
constexpr float x() const noexcept
Access the x element from the vector.
Definition vector3.hpp:99
constexpr vector3(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector3.hpp:56
constexpr friend float squared_hypot(vector3 const &rhs) noexcept
Get the squared length of the vector.
Definition vector3.hpp:197
friend constexpr vector3 round(vector3 const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector3.hpp:284
constexpr friend vector3 normalize(vector3 const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector3.hpp:224
friend float hypot(vector3 const &rhs) noexcept
Get the length of the vector.
Definition vector3.hpp:206
constexpr friend vector3 cross(vector3 const &lhs, vector3 const &rhs) noexcept
Get the cross product between two 3D vectors.
Definition vector3.hpp:257
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector3.hpp:306
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector3.hpp:83
constexpr vector3() noexcept
Construct a empty vector / zero length.
Definition vector3.hpp:63
constexpr vector3(vector2 const &other) noexcept
Construct a vector from a lower dimension vector.
Definition vector3.hpp:32
constexpr float y() const noexcept
Access the y element from the vector.
Definition vector3.hpp:107
friend constexpr vector3 ceil(vector3 const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector3.hpp:291
friend constexpr vector3 floor(vector3 const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector3.hpp:298
constexpr friend vector3 normal(vector3 const &rhs, float angle) noexcept
Get the normal on a 3D vector.
Definition vector3.hpp:244
constexpr float & z() noexcept
Access the z element from the vector.
Definition vector3.hpp:91