MeshLib C++ Docs
Loading...
Searching...
No Matches
MRUnitInfo.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVectorTraits.h"
4#include "MRMacros.h"
5
6#include <cassert>
7#include <optional>
8
9namespace MR
10{
13
14
16enum class NoUnit
17{
18 _count [[maybe_unused]]
19};
20
22enum class LengthUnit
23{
24 microns,
27 meters,
28 inches,
29 feet,
30 _count [[maybe_unused]],
31};
32
34enum class AngleUnit
35{
36 radians,
37 degrees,
38 _count [[maybe_unused]],
39};
40
42enum class PixelSizeUnit
43{
44 pixels,
45 _count [[maybe_unused]],
46};
47
49enum class RatioUnit
50{
51 factor,
52 percents,
53 _count [[maybe_unused]],
54};
55
57enum class TimeUnit
58{
59 seconds,
61 _count [[maybe_unused]],
62};
63
75
77enum class AreaUnit
78{
82 meters2,
83 inches2,
84 feet2,
85 _count [[maybe_unused]],
86};
87
89enum class VolumeUnit
90{
94 meters3,
95 inches3,
96 feet3,
97 _count [[maybe_unused]],
98};
99
102{
108 inv_feet,
109 _count [[maybe_unused]],
110};
111
113#define DETAIL_MR_UNIT_ENUMS(X) X(NoUnit) X(LengthUnit) X(AngleUnit) X(PixelSizeUnit) X(RatioUnit) X(TimeUnit) X(MovementSpeedUnit) X(AreaUnit) X(VolumeUnit) X(InvLengthUnit)
114
117#define DETAIL_MR_UNIT_VALUE_TYPES(X, ...) \
118 X(float ,__VA_ARGS__) X(double ,__VA_ARGS__) X(long double ,__VA_ARGS__) \
119 X(signed char ,__VA_ARGS__) X(unsigned char ,__VA_ARGS__) \
120 X(short ,__VA_ARGS__) X(unsigned short ,__VA_ARGS__) \
121 X(int ,__VA_ARGS__) X(unsigned int ,__VA_ARGS__) \
122 X(long ,__VA_ARGS__) X(unsigned long ,__VA_ARGS__) \
123 X(long long ,__VA_ARGS__) X(unsigned long long ,__VA_ARGS__)
124
126template <typename T>
127concept UnitEnum =
128 #define MR_X(E) || std::same_as<T, E>
130 #undef MR_X
131
133
136{
140
141 std::string_view prettyName;
142
145 std::string_view unitSuffix;
146};
147
149template <UnitEnum E>
150[[nodiscard]] const UnitInfo& getUnitInfo( E unit ) = delete;
151
153#define MR_X(E) template <> [[nodiscard]] MRMESH_API MR_BIND_IGNORE const UnitInfo& getUnitInfo( E unit );
155#undef MR_X
156
158template <UnitEnum E>
159[[nodiscard]] bool unitsAreEquivalent( E a, E b )
160{
161 return a == b || getUnitInfo( a ).conversionFactor == getUnitInfo( b ).conversionFactor;
162}
164template <UnitEnum E>
165[[nodiscard]] bool unitsAreEquivalent( const std::optional<E> &a, const std::optional<E> &b )
166{
167 return !a || !b || unitsAreEquivalent( *a, *b );
168}
169
170namespace detail::Units
171{
172 template <typename T>
173 concept Scalar = std::is_arithmetic_v<T> && !std::is_same_v<T, bool>;
174
175 template <typename T>
176 using MakeFloatingPoint = std::conditional_t<std::is_integral_v<typename VectorTraits<T>::BaseType>, typename VectorTraits<T>::template ChangeBaseType<float>, T>;
177}
178
182template <UnitEnum E, typename T>
183[[nodiscard]] detail::Units::MakeFloatingPoint<T> convertUnits( E from, E to, const T& value )
184{
185 using ReturnType = detail::Units::MakeFloatingPoint<T>;
186
187 bool needConversion = !unitsAreEquivalent( from, to );
188
189 if constexpr ( std::is_same_v<T, ReturnType> )
190 {
191 if ( !needConversion )
192 return value;
193 }
194
195 ReturnType ret{};
196
197 for ( int i = 0; i < VectorTraits<T>::size; i++ )
198 {
200
202 bool needElemConversion = needConversion;
203 if constexpr ( std::is_floating_point_v<typename VectorTraits<T>::BaseType> )
204 {
205 if ( needElemConversion &&
206 (
207 target <= std::numeric_limits<typename VectorTraits<T>::BaseType>::lowest() ||
208 target >= std::numeric_limits<typename VectorTraits<T>::BaseType>::max()
209 )
210 )
211 needElemConversion = false;
212 }
213
214 if ( needElemConversion )
215 target = target * getUnitInfo( from ).conversionFactor / getUnitInfo( to ).conversionFactor;
216 }
217
218 return ret;
219}
220
222template <UnitEnum E, typename T>
223[[nodiscard]] detail::Units::MakeFloatingPoint<T> convertUnits( const std::optional<E> &from, const std::optional<E> &to, const T& value )
224{
225 if ( from && to )
226 return convertUnits( *from, *to, value );
227 else
229}
230
231}
Whether E is one of the unit enums: NoUnit, LengthUnit, AngleUnit, ...
Definition MRUnitInfo.h:127
Definition MRUnitInfo.h:173
PixelSizeUnit
Measurement units of screen sizes.
Definition MRUnitInfo.h:43
#define MR_X(E)
ignore for bindings to prevent GCC14 error: undefined symbol: ZN2MR11getUnitInfoITkNS_8UnitEnumENS_8T...
Definition MRUnitInfo.h:153
std::string_view unitSuffix
Definition MRUnitInfo.h:145
#define DETAIL_MR_UNIT_ENUMS(X)
A list of all unit enums, for internal use.
Definition MRUnitInfo.h:113
std::string_view prettyName
Definition MRUnitInfo.h:141
const UnitInfo & getUnitInfo(E unit)=delete
Returns information about a single measurement unit.
NoUnit
A stub measurement unit representing no unit.
Definition MRUnitInfo.h:17
bool unitsAreEquivalent(E a, E b)
Returns true if converting a value between units a and b doesn't change its value.
Definition MRUnitInfo.h:159
LengthUnit
Measurement units of length.
Definition MRUnitInfo.h:23
float conversionFactor
Definition MRUnitInfo.h:139
static constexpr auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:32
RatioUnit
Measurement units for factors / ratios.
Definition MRUnitInfo.h:50
MovementSpeedUnit
Measurement units for movement speed.
Definition MRUnitInfo.h:66
detail::Units::MakeFloatingPoint< T > convertUnits(E from, E to, const T &value)
Definition MRUnitInfo.h:183
VolumeUnit
Measurement units for body volume.
Definition MRUnitInfo.h:90
T BaseType
The base template handles scalars (or just non-vectors).
Definition MRMesh/MRVectorTraits.h:21
AreaUnit
Measurement units for surface area.
Definition MRUnitInfo.h:78
std::conditional_t< std::is_integral_v< typename VectorTraits< T >::BaseType >, typename VectorTraits< T >::template ChangeBaseType< float >, T > MakeFloatingPoint
Definition MRUnitInfo.h:176
TimeUnit
Measurement units for time.
Definition MRUnitInfo.h:58
InvLengthUnit
Measurement units for 1/length.
Definition MRUnitInfo.h:102
AngleUnit
Measurement units of angle.
Definition MRUnitInfo.h:35
@ percents
0..1 x
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Information about a single measurement unit.
Definition MRUnitInfo.h:136
Common traits for (mathematical) vectors.
Definition MRMesh/MRVectorTraits.h:18