Blender V5.3
BLI_math_vector_types.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <array>
12#include <ostream>
13#include <type_traits>
14
15#include "BLI_build_config.hh"
16#include "BLI_hash.hh"
19#include "BLI_unique_hash.hh"
20#include "BLI_utildefines.hh"
21
22namespace blender {
23
24template<typename T, int Size, bool is_trivial_type> struct vec_struct_base {
25 std::array<T, Size> values;
26};
27
28template<typename T> struct vec_struct_base<T, 1, false> {
30};
31
32template<typename T> struct vec_struct_base<T, 2, false> : VecSwizzleFunc<T, 2> {
33 T x, y;
34};
35
36template<typename T> struct vec_struct_base<T, 3, false> : VecSwizzleFunc<T, 3> {
37 T x, y, z;
38};
39
40template<typename T> struct vec_struct_base<T, 4, false> : VecSwizzleFunc<T, 4> {
41 T x, y, z, w;
42};
43
47#if COMPILER_GCC
48# pragma GCC diagnostic push
49# pragma GCC diagnostic ignored "-Wpedantic"
50#elif COMPILER_CLANG
51# pragma clang diagnostic push
52# pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
53# pragma clang diagnostic ignored "-Wnested-anon-types"
54#elif COMPILER_MSVC
55# pragma warning(push)
56# pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
57#endif
58
59template<typename T> struct vec_struct_base<T, 1, true> {
60 union {
61#ifndef NDEBUG
62 /* Easier to read inside a debugger. */
63 std::array<T, 1> debug_values_;
64#endif
65 T x;
66 /* Nesting to avoid readability issues inside a debugger. */
68 };
69};
70
71template<typename T> struct vec_struct_base<T, 2, true> {
72 union {
73#ifndef NDEBUG
74 /* Easier to read inside a debugger. */
75 std::array<T, 2> debug_values_;
76#endif
77 struct {
78 T x;
79 union {
80 struct {
81 /* This still needs to be wrapped in a struct to avoid a MSVC 16.5+ bug. */
82 T y;
83 };
85 };
86 };
87 /* Nesting to avoid readability issues inside a debugger. */
88 union {
91 };
92 };
93};
94
95template<typename T> struct vec_struct_base<T, 3, true> {
96 union {
97#ifndef NDEBUG
98 /* Easier to read inside a debugger. */
99 std::array<T, 3> debug_values_;
100#endif
101 struct {
102 T x;
103 union {
104 struct {
105 T y;
106 union {
107 struct {
108 /* This still needs to be wrapped in a struct to avoid a MSVC 16.5+ bug. */
109 T z;
110 };
112 };
113 };
116 };
117 };
118 /* Nesting to avoid readability issues inside a debugger. */
119 union {
123 };
124 };
125};
126
127template<typename T> struct vec_struct_base<T, 4, true> {
128 union {
129#ifndef NDEBUG
130 /* Useful for debugging. */
131 std::array<T, 4> debug_values_;
132#endif
133 struct {
134 T x;
135 union {
136 struct {
137 T y;
138 union {
139 struct {
140 T z;
141 union {
142 struct {
143 /* This still needs to be wrapped in a struct to avoid a MSVC 16.5+ bug. */
144 T w;
145 };
147 };
148 };
151 };
152 };
156 };
157 };
158 /* Nesting to avoid readability issues inside a debugger. */
159 union {
164 };
165 };
166};
167
168#if COMPILER_GCC
169# pragma GCC diagnostic pop
170#elif COMPILER_CLANG
171# pragma clang diagnostic pop
172#elif COMPILER_MSVC
173# pragma warning(pop)
174#endif
175
176namespace math {
177
178template<typename T> constexpr uint64_t vector_hash(const T &vec)
179{
180 BLI_STATIC_ASSERT(T::type_length <= 4, "Longer types need to implement vector_hash themself.");
181 if constexpr (T::type_length == 1) {
182 return get_default_hash(vec.x);
183 }
184 else if constexpr (T::type_length == 2) {
185 return get_default_hash(vec.x, vec.y);
186 }
187 else if constexpr (T::type_length == 3) {
188 return get_default_hash(vec.x, vec.y, vec.z);
189 }
190 else if constexpr (T::type_length == 4) {
191 return get_default_hash(vec.x, vec.y, vec.z, vec.w);
192 }
193 else {
195 }
196}
197
198} // namespace math
199
200template<typename T, int Size>
201struct VecBase : public vec_struct_base<T, Size, std::is_trivial_v<T>> {
202
203 BLI_STATIC_ASSERT(alignof(T) <= sizeof(T),
204 "VecBase is not compatible with aligned type for now.");
205
206 static constexpr int type_length = Size;
207
208 using base_type = T;
209
210 VecBase() = default;
211
212#if defined(__GNUC__)
213# pragma GCC diagnostic push
214# pragma GCC diagnostic ignored "-Wdeprecated-copy"
215#endif
216
218 VecBase &operator=(const VecBase &) & = default;
219
220#if defined(__GNUC__)
221# pragma GCC diagnostic pop
222#endif
223
224 explicit VecBase(T value)
225 requires(Size > 1)
226 {
227 for (int i = 0; i < Size; i++) {
228 (*this)[i] = value;
229 }
230 }
231
232 template<typename U>
233 explicit VecBase(U value)
234 requires(std::is_convertible_v<U, T>)
235 : VecBase(T(value))
236 {
237 }
238
239 constexpr VecBase(T _x)
240 requires(Size == 1)
241 {
242 this->x = _x;
243 }
244
245 constexpr VecBase(T _x, T _y)
246 requires(Size == 2)
247 {
248 this->x = _x;
249 this->y = _y;
250 }
251
252 constexpr VecBase(T _x, T _y, T _z)
253 requires(Size == 3)
254 {
255 this->x = _x;
256 this->y = _y;
257 this->z = _z;
258 }
259
260 constexpr VecBase(T _x, T _y, T _z, T _w)
261 requires(Size == 4)
262 {
263 this->x = _x;
264 this->y = _y;
265 this->z = _z;
266 this->w = _w;
267 }
268
270
271 template<typename U>
272 constexpr VecBase(const VecBase<U, 2> &xy, T z)
273 requires(Size == 3)
274 : VecBase(T(xy.x), T(xy.y), z)
275 {
276 }
277
278 template<typename U>
279 constexpr VecBase(T x, const VecBase<U, 2> &yz)
280 requires(Size == 3)
281 : VecBase(x, T(yz.x), T(yz.y))
282 {
283 }
284
285 template<typename U>
287 requires(Size == 4)
288 : VecBase(T(xyz.x), T(xyz.y), T(xyz.z), T(w))
289 {
290 }
291
292 template<typename U>
294 requires(Size == 4)
295 : VecBase(T(x), T(yzw.x), T(yzw.y), T(yzw.z))
296 {
297 }
298
299 template<typename U, typename V>
301 requires(Size == 4)
302 : VecBase(T(xy.x), T(xy.y), T(zw.x), T(zw.y))
303 {
304 }
305
306 template<typename U>
308 requires(Size == 4)
309 : VecBase(T(xy.x), T(xy.y), T(z), T(w))
310 {
311 }
312
313 template<typename U>
315 requires(Size == 4)
316 : VecBase(T(x), T(yz.x), T(yz.y), T(w))
317 {
318 }
319
320 template<typename U>
322 requires(Size == 4)
323 : VecBase(T(x), T(y), T(zw.x), T(zw.y))
324 {
325 }
326
332 template<typename U, int OtherSize>
334 requires(OtherSize < Size)
335 = delete;
336
338
339 template<typename U, int OtherSize>
340 explicit VecBase(const VecBase<U, OtherSize> &other)
341 requires(OtherSize > Size)
342 {
343 for (int i = 0; i < Size; i++) {
344 (*this)[i] = T(other[i]);
345 }
346 }
347
349
350 /* False positive warning with GCC: it sees array access like [3] but
351 * input is only a 3-element array. But it fails to realize that the
352 * [3] access is within "if constexpr (Size == 4)" check already. */
353#ifdef __GNUC__
354# pragma GCC diagnostic push
355# pragma GCC diagnostic ignored "-Warray-bounds"
356#endif
357
358 VecBase(const T *ptr)
359 {
361 }
362
363 template<typename U>
364 explicit VecBase(const U *ptr)
365 requires(std::is_convertible_v<U, T>)
366 {
368 }
369
370 VecBase(const T (*ptr)[Size]) : VecBase(static_cast<const T *>(ptr[0])) {}
371
372#ifdef __GNUC__
373# pragma GCC diagnostic pop
374#endif
375
377
378 template<typename U> explicit VecBase(const VecBase<U, Size> &vec)
379 {
381 }
382
384
385 operator const T *() const
386 {
387 return reinterpret_cast<const T *>(this);
388 }
389
390 operator T *()
391 {
392 return reinterpret_cast<T *>(this);
393 }
394
396
397 const T &operator[](int index) const
398 {
399 BLI_assert(index >= 0);
400 BLI_assert(index < Size);
401 return reinterpret_cast<const T *>(this)[index];
402 }
403
405 {
406 BLI_assert(index >= 0);
407 BLI_assert(index < Size);
408 return reinterpret_cast<T *>(this)[index];
409 }
410
412
413 friend VecBase operator+(const VecBase &a, const VecBase &b)
414 {
416 }
417
418 friend VecBase operator+(const VecBase &a, const T &b)
419 {
421 }
422
423 friend VecBase operator+(const T &a, const VecBase &b)
424 {
426 }
427
432
434 {
436 }
437
438 friend VecBase operator-(const VecBase &a)
439 {
441 }
442
443 friend VecBase operator-(const VecBase &a, const VecBase &b)
444 {
446 }
447
448 friend VecBase operator-(const VecBase &a, const T &b)
449 {
451 }
452
453 friend VecBase operator-(const T &a, const VecBase &b)
454 {
456 }
457
462
464 {
466 }
467
468 friend VecBase operator*(const VecBase &a, const VecBase &b)
469 {
471 }
472
473 template<typename FactorT> friend VecBase operator*(const VecBase &a, FactorT b)
474 {
476 }
477
478 friend VecBase operator*(T a, const VecBase &b)
479 {
481 }
482
487
492
493 friend VecBase operator/(const VecBase &a, const VecBase &b)
494 {
495 for (int i = 0; i < Size; i++) {
496 BLI_assert(b[i] != T(0));
497 }
499 }
500
501 friend VecBase operator/(const VecBase &a, T b)
502 {
503 BLI_assert(b != T(0));
505 }
506
507 friend VecBase operator/(T a, const VecBase &b)
508 {
509 for (int i = 0; i < Size; i++) {
510 BLI_assert(b[i] != T(0));
511 }
513 }
514
516 {
517 BLI_assert(b != T(0));
519 }
520
522 {
523 for (int i = 0; i < Size; i++) {
524 BLI_assert(b[i] != T(0));
525 }
527 }
528
530
531 friend VecBase operator&(const VecBase &a, const VecBase &b)
532 requires(std::is_integral_v<T>)
533 {
535 }
536
537 friend VecBase operator&(const VecBase &a, T b)
538 requires(std::is_integral_v<T>)
539 {
541 }
542
543 friend VecBase operator&(T a, const VecBase &b)
544 requires(std::is_integral_v<T>)
545 {
547 }
548
550 requires(std::is_integral_v<T>)
551 {
553 }
554
556 requires(std::is_integral_v<T>)
557 {
559 }
560
561 friend VecBase operator|(const VecBase &a, const VecBase &b)
562 requires(std::is_integral_v<T>)
563 {
565 }
566
567 friend VecBase operator|(const VecBase &a, T b)
568 requires(std::is_integral_v<T>)
569 {
571 }
572
573 friend VecBase operator|(T a, const VecBase &b)
574 requires(std::is_integral_v<T>)
575 {
577 }
578
580 requires(std::is_integral_v<T>)
581 {
583 }
584
586 requires(std::is_integral_v<T>)
587 {
589 }
590
591 friend VecBase operator^(const VecBase &a, const VecBase &b)
592 requires(std::is_integral_v<T>)
593 {
595 }
596
597 friend VecBase operator^(const VecBase &a, T b)
598 requires(std::is_integral_v<T>)
599 {
601 }
602
603 friend VecBase operator^(T a, const VecBase &b)
604 requires(std::is_integral_v<T>)
605 {
607 }
608
610 requires(std::is_integral_v<T>)
611 {
613 }
614
616 requires(std::is_integral_v<T>)
617 {
619 }
620
621 friend VecBase operator~(const VecBase &a)
622 requires(std::is_integral_v<T>)
623 {
625 }
626
628
629 friend VecBase operator<<(const VecBase &a, const VecBase &b)
630 requires(std::is_integral_v<T>)
631 {
633 }
634
635 friend VecBase operator<<(const VecBase &a, T b)
636 requires(std::is_integral_v<T>)
637 {
639 }
640
642 requires(std::is_integral_v<T>)
643 {
645 }
646
648 requires(std::is_integral_v<T>)
649 {
651 }
652
653 friend VecBase operator>>(const VecBase &a, const VecBase &b)
654 requires(std::is_integral_v<T>)
655 {
657 }
658
659 friend VecBase operator>>(const VecBase &a, T b)
660 requires(std::is_integral_v<T>)
661 {
663 }
664
666 requires(std::is_integral_v<T>)
667 {
669 }
670
672 requires(std::is_integral_v<T>)
673 {
675 }
676
678
679 friend VecBase operator%(const VecBase &a, const VecBase &b)
680 requires(std::is_integral_v<T>)
681 {
682 for (int i = 0; i < Size; i++) {
683 BLI_assert(b[i] != T(0));
684 }
686 }
687
688 friend VecBase operator%(const VecBase &a, T b)
689 requires(std::is_integral_v<T>)
690 {
691 BLI_assert(b != 0);
693 }
694
695 friend VecBase operator%(T a, const VecBase &b)
696 requires(std::is_integral_v<T>)
697 {
698 for (int i = 0; i < Size; i++) {
699 BLI_assert(b[i] != T(0));
700 }
702 }
703
705
706 friend bool operator==(const VecBase &a, const VecBase &b)
707 {
708 for (int i = 0; i < Size; i++) {
709 if (a[i] != b[i]) {
710 return false;
711 }
712 }
713 return true;
714 }
715
716 friend bool operator!=(const VecBase &a, const VecBase &b)
717 {
718 return !(a == b);
719 }
720
722
723 constexpr uint64_t hash() const
724 {
725 return math::vector_hash(*this);
726 }
727
729 {
730 unroll<Size>([&](auto i) {
731 const T &value = (*this)[i];
733 });
734 }
735
736 friend std::ostream &operator<<(std::ostream &stream, const VecBase &v)
737 {
738 stream << "(";
739 for (int i = 0; i < Size; i++) {
740 stream << v[i];
741 if (i != Size - 1) {
742 stream << ", ";
743 }
744 }
745 stream << ")";
746 return stream;
747 }
748};
749
754template<typename T, int Size> constexpr bool value_is_zero_memory(const VecBase<T, Size> &v)
755{
756 for (int i = 0; i < Size; i++) {
757 if (!value_is_zero_memory(v[i])) {
758 return false;
759 }
760 }
761 return true;
762}
763
764namespace math {
765
766template<typename T> struct AssertUnitEpsilon {
768 static constexpr T value = T(0.0002);
769};
770
771} // namespace math
772
776
780
784
788
792
796
801
805
806} // namespace blender
#define BLI_UNROLL_MATH_VEC_OP_INIT_INDEX(a)
#define BLI_UNROLL_MATH_VEC_OP_VEC_SCALAR(op, a, b)
#define BLI_UNROLL_MATH_VEC_OP_VEC(op, a)
#define BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(op, a, b)
#define BLI_UNROLL_MATH_VEC_OP_VEC_VEC(op, a, b)
#define BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(op, b)
#define BLI_UNROLL_MATH_VEC_OP_INIT_VECTOR(a)
#define BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(op, b)
#define BLI_assert_unreachable_static()
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define T
constexpr uint64_t vector_hash(const T &vec)
BMesh Iterators.
VecBase< uint32_t, 2 > uint2
UserDef U
Definition blender.cc:58
VecBase< int16_t, 3 > short3
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< uint8_t, 4 > uchar4
unsigned __int64 uint64_t
Definition BKE_text.h:91
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
VecBase< float, 1 > float1
VecBase< int32_t, 4 > int4
VecBase< uint16_t, 3 > ushort3
VecBase< int8_t, 2 > char2
VecBase< uint32_t, 4 > uint4
VecBase< double, 2 > double2
constexpr bool value_is_zero_memory(const VecBase< T, Size > &v)
VecBase< uint32_t, 3 > uint3
ATTR_WARN_UNUSED_RESULT const BMVert * v
VecBase< int8_t, 4 > char4
VecBase< float, 4 > float4
VecBase< int16_t, 4 > short4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
VecBase< uint16_t, 2 > ushort2
VecBase< double, 3 > double3
VecBase< uint8_t, 2 > uchar2
VecBase< int16_t, 2 > short2
VecBase< int8_t, 3 > char3
VecBase< uint8_t, 3 > uchar3
VecBase< float, 3 > float3
VecBase< uint16_t, 4 > ushort4
void hash_unique_default(const T &value, UniqueHashBytes &hash)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
VecBase< double, 4 > double4
constexpr void unroll(Fn fn)
Definition BLI_unroll.hh:27
VecBase(const VecBase< U, OtherSize > &other)=delete
constexpr VecBase(T _x, T _y, T _z)
VecBase & operator&=(T b) &
constexpr VecBase(T _x, T _y, T _z, T _w)
friend VecBase operator|(T a, const VecBase &b)
friend VecBase operator&(T a, const VecBase &b)
friend VecBase operator<<(const VecBase &a, const VecBase &b)
VecBase(VecBase< U, 2 > xy, VecBase< V, 2 > zw)
friend std::ostream & operator<<(std::ostream &stream, const VecBase &v)
VecBase(VecBase< U, 2 > xy, T z, T w)
VecBase()=default
friend VecBase operator>>(const VecBase &a, T b)
constexpr uint64_t hash() const
VecBase & operator|=(const VecBase &b) &
friend VecBase operator>>(const VecBase &a, const VecBase &b)
VecBase(T x, VecBase< U, 2 > yz, T w)
friend VecBase operator^(const VecBase &a, const VecBase &b)
VecBase(const VecBase< U, OtherSize > &other)
VecBase & operator<<=(T b) &
friend VecBase operator<<(const VecBase &a, T b)
const T & operator[](int index) const
void hash_unique(UniqueHashBytes &hash) const
friend VecBase operator%(const VecBase &a, T b)
friend VecBase operator-(const T &a, const VecBase &b)
friend VecBase operator-(const VecBase &a, const VecBase &b)
BLI_STATIC_ASSERT(alignof(T)<=sizeof(T), "VecBase is not compatible with aligned type for now.")
friend VecBase operator/(const VecBase &a, T b)
friend VecBase operator^(T a, const VecBase &b)
friend VecBase operator|(const VecBase &a, T b)
constexpr VecBase(T _x, T _y)
VecBase & operator>>=(T b) &
friend VecBase operator*(const VecBase &a, FactorT b)
VecBase(T x, T y, VecBase< U, 2 > zw)
friend bool operator==(const VecBase &a, const VecBase &b)
VecBase & operator^=(T b) &
friend VecBase operator&(const VecBase &a, T b)
VecBase & operator-=(const T &b) &
VecBase & operator&=(const VecBase &b) &
friend VecBase operator*(const VecBase &a, const VecBase &b)
friend VecBase operator/(T a, const VecBase &b)
friend VecBase operator|(const VecBase &a, const VecBase &b)
friend VecBase operator/(const VecBase &a, const VecBase &b)
VecBase & operator=(const VecBase &) &=default
VecBase & operator^=(const VecBase &b) &
friend VecBase operator^(const VecBase &a, T b)
friend bool operator!=(const VecBase &a, const VecBase &b)
friend VecBase operator+(const VecBase &a, const VecBase &b)
constexpr VecBase(T x, const VecBase< U, 2 > &yz)
VecBase & operator<<=(const VecBase &b) &
VecBase & operator+=(const VecBase &b) &
VecBase & operator|=(T b) &
VecBase(const VecBase< U, Size > &vec)
VecBase(const T(*ptr)[Size])
friend VecBase operator~(const VecBase &a)
friend VecBase operator-(const VecBase &a)
friend VecBase operator%(const VecBase &a, const VecBase &b)
VecBase & operator/=(const VecBase &b) &
constexpr VecBase(const VecBase< U, 2 > &xy, T z)
friend VecBase operator+(const T &a, const VecBase &b)
VecBase & operator/=(T b) &
VecBase & operator>>=(const VecBase &b) &
friend VecBase operator+(const VecBase &a, const T &b)
friend VecBase operator&(const VecBase &a, const VecBase &b)
friend VecBase operator*(T a, const VecBase &b)
VecBase & operator-=(const VecBase &b) &
VecBase(T x, VecBase< U, 3 > yzw)
VecBase & operator+=(const T &b) &
VecBase(VecBase< U, 3 > xyz, T w)
friend VecBase operator%(T a, const VecBase &b)
friend VecBase operator-(const VecBase &a, const T &b)
VecBase & operator*=(const VecBase &b) &
VecBase & operator*=(T b) &