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
20 // Changes the vector element type. For scalars, replaces the whole type.
21 template <typename U>
22 using ChangeBaseType = U;
23
24 // Currently this doesn't forward the value, for simplicity. (In all specializations.)
25 // For scalars this intentionally doesn't check the index.
26 template <typename U>
27 [[nodiscard]] static constexpr auto&& getElem( int i, U&& value ) { (void)i; return value; }
28
29};
30
31template <typename T>
33{
34 using BaseType = T;
35 static constexpr int size = 2;
36
37 template <typename U>
39
40 template <typename U>
41 [[nodiscard]] static auto&& getElem( int i, U&& value )
42 {
43 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
44 // GCC and Clang optimize both in the same manner.
45 return ( &value.x )[i];
46 }
47};
48template <typename T>
50{
51 using BaseType = T;
52 static constexpr int size = 3;
53
54 template <typename U>
56
57 template <typename U>
58 [[nodiscard]] static auto&& getElem( int i, U&& value )
59 {
60 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
61 // GCC and Clang optimize both in the same manner.
62 return ( &value.x )[i];
63 }
64};
65template <typename T>
67{
68 using BaseType = T;
69 static constexpr int size = 4;
70
71 template <typename U>
73
74 template <typename U>
75 [[nodiscard]] static auto&& getElem( int i, U&& value )
76 {
77 // Technically UB, but helps with optimizations on MSVC for some reason, compared to an if-else chain.
78 // GCC and Clang optimize both in the same manner.
79 return ( &value.x )[i];
80 }
81};
82
83}
Definition MRCameraOrientationPlugin.h:7
Definition MRVector2.h:18
Definition MRMesh/MRVector3.h:19
Definition MRVector4.h:13
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:41
T BaseType
Definition MRMesh/MRVectorTraits.h:34
T BaseType
Definition MRMesh/MRVectorTraits.h:51
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:58
T BaseType
Definition MRMesh/MRVectorTraits.h:68
static auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:75
Definition MRMesh/MRVectorTraits.h:14
U ChangeBaseType
Definition MRMesh/MRVectorTraits.h:22
static constexpr int size
Definition MRMesh/MRVectorTraits.h:18
static constexpr auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:27
T BaseType
Definition MRMesh/MRVectorTraits.h:17