MeshLib Documentation
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 template <typename U = T> // Adding a template parameter to allow instantiating the rest of the class with `T == void`.
31 static constexpr U diagonal( U v ) { return v; }
32};
33
34template <typename T>
36{
37 using BaseType = T;
38 static constexpr int size = 2;
39 static constexpr bool supportNoInit = true;
40
41 template <typename U>
43
44 template <typename U>
45 [[nodiscard]] static auto&& getElem( int i, U&& value )
46 {
47 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
48 // GCC and Clang optimize both in the same manner.
49 return ( &value.x )[i];
50 }
51
52 static constexpr auto diagonal( T v ) { return Vector2<T>::diagonal( v ); }
53};
54
55template <typename T>
57{
58 using BaseType = T;
59 static constexpr int size = 3;
60 static constexpr bool supportNoInit = true;
61
62 template <typename U>
64
65 template <typename U>
66 [[nodiscard]] static auto&& getElem( int i, U&& value )
67 {
68 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
69 // GCC and Clang optimize both in the same manner.
70 return ( &value.x )[i];
71 }
72
73 static constexpr auto diagonal( T v ) { return Vector3<T>::diagonal( v ); }
74};
75
76template <typename T>
78{
79 using BaseType = T;
80 static constexpr int size = 4;
81 static constexpr bool supportNoInit = true;
82
83 template <typename U>
85
86 template <typename U>
87 [[nodiscard]] static auto&& getElem( int i, U&& value )
88 {
89 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
90 // GCC and Clang optimize both in the same manner.
91 return ( &value.x )[i];
92 }
93
94 static constexpr auto diagonal( T v ) { return Vector4<T>::diagonal( v ); }
95};
96
97}
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:45
static constexpr auto diagonal(T v)
Definition MRMesh/MRVectorTraits.h:52
T BaseType
Definition MRMesh/MRVectorTraits.h:37
T BaseType
Definition MRMesh/MRVectorTraits.h:58
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:66
static constexpr auto diagonal(T v)
Definition MRMesh/MRVectorTraits.h:73
T BaseType
Definition MRMesh/MRVectorTraits.h:79
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:87
static constexpr auto diagonal(T v)
Definition MRMesh/MRVectorTraits.h:94
Definition MRMesh/MRVectorTraits.h:14
static constexpr U diagonal(U v)
Definition MRMesh/MRVectorTraits.h:31
U ChangeBaseType
Definition MRMesh/MRVectorTraits.h:23
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