MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/MRVectorTraits.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMesh/MRMeshFwd.h"
4#include "MRPch/MRBindingMacros.h"
5
6#include <cassert>
7
8namespace MR
9{
10
11// Common traits for (mathematical) vectors.
12
13template <typename T>
14struct MR_BIND_IGNORE VectorTraits
15{
16 // The base template handles scalars (or just non-vectors).
17
18 using BaseType = T;
19 static constexpr int size = 1;
20 static constexpr bool supportNoInit = false;
21
22 // Changes the vector element type. For scalars, replaces the whole type.
23 template <typename U>
24 using ChangeBaseType = U;
25
26 // Currently this doesn't forward the value, for simplicity. (In all specializations.)
27 // For scalars this intentionally doesn't check the index.
28 template <typename U>
29 [[nodiscard]] static constexpr auto&& getElem( int i, U&& value ) { (void)i; return value; }
30
31 template <typename U = T> // Adding a template parameter to allow instantiating the rest of the class with `T == void`.
32 static constexpr U diagonal( U v ) { return v; }
33};
34
35template <typename T>
37{
38 using BaseType = T;
39 static constexpr int size = 2;
40 static constexpr bool supportNoInit = true;
41
42 template <typename U>
44
45 template <typename U>
46 [[nodiscard]] static auto&& getElem( int i, U&& value )
47 {
48 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
49 // GCC and Clang optimize both in the same manner.
50 return ( &value.x )[i];
51 }
52
53 static constexpr auto diagonal( T v ) { return Vector2<T>::diagonal( v ); }
54};
55
56template <typename T>
58{
59 using BaseType = T;
60 static constexpr int size = 3;
61 static constexpr bool supportNoInit = true;
62
63 template <typename U>
65
66 template <typename U>
67 [[nodiscard]] static auto&& getElem( int i, U&& value )
68 {
69 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
70 // GCC and Clang optimize both in the same manner.
71 return ( &value.x )[i];
72 }
73
74 static constexpr auto diagonal( T v ) { return Vector3<T>::diagonal( v ); }
75};
76
77template <typename T>
79{
80 using BaseType = T;
81 static constexpr int size = 4;
82 static constexpr bool supportNoInit = true;
83
84 template <typename U>
86
87 template <typename U>
88 [[nodiscard]] static auto&& getElem( int i, U&& value )
89 {
90 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
91 // GCC and Clang optimize both in the same manner.
92 return ( &value.x )[i];
93 }
94
95 static constexpr auto diagonal( T v ) { return Vector4<T>::diagonal( v ); }
96};
97
98}
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:29
Definition MRVector2.h:25
static constexpr Vector2 diagonal(T a) noexcept
Definition MRVector2.h:38
Definition MRMesh/MRVector3.h:26
static constexpr Vector3 diagonal(T a) noexcept
Definition MRMesh/MRVector3.h:39
Definition MRVector4.h:20
static constexpr Vector4 diagonal(T a) noexcept
Definition MRVector4.h:31
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:46
static constexpr auto diagonal(T v)
Definition MRMesh/MRVectorTraits.h:53
T BaseType
Definition MRMesh/MRVectorTraits.h:38
T BaseType
Definition MRMesh/MRVectorTraits.h:59
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:67
static constexpr auto diagonal(T v)
Definition MRMesh/MRVectorTraits.h:74
T BaseType
Definition MRMesh/MRVectorTraits.h:80
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:88
static constexpr auto diagonal(T v)
Definition MRMesh/MRVectorTraits.h:95
Definition MRMesh/MRVectorTraits.h:15
static constexpr U diagonal(U v)
Definition MRMesh/MRVectorTraits.h:32
U ChangeBaseType
Definition MRMesh/MRVectorTraits.h:24
static constexpr int size
Definition MRMesh/MRVectorTraits.h:19
static constexpr auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:29
static constexpr bool supportNoInit
Definition MRMesh/MRVectorTraits.h:20
T BaseType
Definition MRMesh/MRVectorTraits.h:18