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