MeshLib C++ Docs
Loading...
Searching...
No Matches
MRUnits.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRViewer/exports.h"
5
6#include <cassert>
7#include <optional>
8#include <string>
9#include <variant>
10
11// Read the manual at: docs/measurement_units.md
12
13namespace MR
14{
15
16// A stub measurement unit representing no unit.
17enum class NoUnit
18{
19 _count [[maybe_unused]]
20};
21
22// Measurement units of length.
23enum class LengthUnit
24{
25 mm,
26 meters,
27 inches,
28 _count [[maybe_unused]],
29};
30
31// Measurement units of angle.
32enum class AngleUnit
33{
34 radians,
35 degrees,
36 _count [[maybe_unused]],
37};
38
39// Measurement units of screen sizes.
40enum class PixelSizeUnit
41{
42 pixels,
43 _count [[maybe_unused]],
44};
45
46// Measurement units for factors / ratios.
47enum class RatioUnit
48{
49 factor, // 0..1 x
50 percents, // 0..100 %
51 _count [[maybe_unused]],
52};
53
54// Measurement units for time.
55enum class TimeUnit
56{
57 seconds,
59 _count [[maybe_unused]],
60};
61
62// Measurement units for movement speed.
64{
68 _count [[maybe_unused]],
69};
70
71// Measurement units for surface area.
72enum class AreaUnit
73{
74 mm2,
75 meters2,
76 inches2,
77 _count [[maybe_unused]],
78};
79
80// Measurement units for body volume.
81enum class VolumeUnit
82{
83 mm3,
84 meters3,
85 inches3,
86 _count [[maybe_unused]],
87};
88
89// Measurement units for 1/length.
90enum class InvLengthUnit
91{
92 inv_mm, // mm^-1
93 inv_meters, // meters^-1
94 inv_inches, // inches^-1
95 _count [[maybe_unused]],
96};
97
98// A list of all unit enums, for internal use.
99#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)
100
101// All supported value types for `valueToString()`.
102// Not using `__VA_OPT__(,)` here to support legacy MSVC preprocessor.
103#ifdef __clang__
104#pragma clang diagnostic push
105#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
106#endif
107#define DETAIL_MR_UNIT_VALUE_TYPES(X, ...) \
108 X(float ,##__VA_ARGS__) X(double ,##__VA_ARGS__) X(long double ,##__VA_ARGS__) \
109 X(signed char ,##__VA_ARGS__) X(unsigned char ,##__VA_ARGS__) \
110 X(short ,##__VA_ARGS__) X(unsigned short ,##__VA_ARGS__) \
111 X(int ,##__VA_ARGS__) X(unsigned int ,##__VA_ARGS__) \
112 X(long ,##__VA_ARGS__) X(unsigned long ,##__VA_ARGS__) \
113 X(long long ,##__VA_ARGS__) X(unsigned long long ,##__VA_ARGS__)
114#ifdef __clang__
115#pragma clang diagnostic pop
116#endif
117
118// Whether `E` is one of the unit enums: NoUnit, LengthUnit, AngleUnit, ...
119template <typename T>
120concept UnitEnum =
121 #define MR_X(E) || std::same_as<T, E>
123 #undef MR_X
124
125// ---
126
127// Information about a single measurement unit.
129{
130 // This is used to convert between units.
131 // To convert from A to B, multiply by A's factor and divide by B's.
133
134 std::string_view prettyName;
135
136 // The short unit name that's placed after values.
137 // This may or may not start with a space.
138 std::string_view unitSuffix;
139};
140
141// Returns information about a single measurement unit.
142template <UnitEnum E>
143[[nodiscard]] const UnitInfo& getUnitInfo( E unit ) = delete;
144
145#define MR_X(E) template <> [[nodiscard]] MRVIEWER_API const UnitInfo& getUnitInfo( E unit );
147#undef MR_X
148
149// Returns true if converting a value between units `a` and `b` doesn't change its value.
150template <UnitEnum E>
151[[nodiscard]] bool unitsAreEquivalent( E a, E b )
152{
153 return a == b || getUnitInfo( a ).conversionFactor == getUnitInfo( b ).conversionFactor;
154}
155// This version also returns true if `a` or `b` is null.
156template <UnitEnum E>
157[[nodiscard]] bool unitsAreEquivalent( const std::optional<E> &a, const std::optional<E> &b )
158{
159 return !a || !b || unitsAreEquivalent( *a, *b );
160}
161
162namespace detail::Units
163{
164 struct Empty {};
165
166 template <typename T>
167 concept Scalar = std::is_arithmetic_v<T> && !std::is_same_v<T, bool>;
168
169 template <typename T>
170 using MakeFloatingPoint = std::conditional_t<std::is_integral_v<typename VectorTraits<T>::BaseType>, typename VectorTraits<T>::template ChangeBaseType<float>, T>;
171}
172
173// Converts `value` from unit `from` to unit `to`. `value` is a scalar of a Vector2/3/4 or ImVec2/4 of them.
174// The return type matches `T` if it's not integral. If it's integral, its element type type is changed to `float`.
175// Returns min/max floating-point values unchanged.
176template <UnitEnum E, typename T>
177[[nodiscard]] detail::Units::MakeFloatingPoint<T> convertUnits( E from, E to, const T& value )
178{
179 using ReturnType = detail::Units::MakeFloatingPoint<T>;
180
181 bool needConversion = !unitsAreEquivalent( from, to );
182
183 if constexpr ( std::is_same_v<T, ReturnType> )
184 {
185 if ( !needConversion )
186 return value;
187 }
188
189 ReturnType ret{};
190
191 for ( int i = 0; i < VectorTraits<T>::size; i++ )
192 {
194
195 // Don't touch min/max floating-point values.
196 bool needElemConversion = needConversion;
197 if constexpr ( std::is_floating_point_v<typename VectorTraits<T>::BaseType> )
198 {
199 if ( needElemConversion &&
200 (
201 target <= std::numeric_limits<typename VectorTraits<T>::BaseType>::lowest() ||
202 target >= std::numeric_limits<typename VectorTraits<T>::BaseType>::max()
203 )
204 )
205 needElemConversion = false;
206 }
207
208 if ( needElemConversion )
209 target = target * getUnitInfo( from ).conversionFactor / getUnitInfo( to ).conversionFactor;
210 }
211
212 return ret;
213}
214
215// This version is a no-op if `from` or `to` is null.
216template <UnitEnum E, typename T>
217[[nodiscard]] detail::Units::MakeFloatingPoint<T> convertUnits( const std::optional<E> &from, const std::optional<E> &to, const T& value )
218{
219 if ( from && to )
220 return convertUnits( *from, *to, value );
221 else
223}
224
225// ---
226
227template <UnitEnum E>
228struct UnitToStringParams;
229
230// Returns the default parameters for converting a specific unit type to a string.
231// You can modify those with `setDefaultUnitParams()`.
232template <UnitEnum E>
234
235#define MR_X(E) extern template MRVIEWER_API const UnitToStringParams<E>& getDefaultUnitParams();
237#undef MR_X
238
239// Modifies the default parameters for converting a specific unit type to a string.
240template <UnitEnum E>
242
243#define MR_X(E) extern template MRVIEWER_API void setDefaultUnitParams( const UnitToStringParams<E>& newParams );
245#undef MR_X
246
247enum class NumberStyle
248{
249 normal, // Like %f.
250 distributePrecision, // Like %f, but the precision digits are spread across both decimal and integral parts.
251 exponential, // Like %e.
252 maybeExponential, // Like %g.
253};
254
255// This controls how the degrees are printed.
256enum class DegreesMode
257{
258 degrees, // Fractional degrees.
259 degreesMinutes, // Integral degrees, fractional arcminutes.
260 degreesMinutesSeconds, // Integral degrees and minutes, fractional arcseconds.
261 _count [[maybe_unused]],
262};
263[[nodiscard]] MRVIEWER_API std::string_view toString( DegreesMode mode );
264
265// Controls how a value with a unit is converted to a string.
266template <UnitEnum E>
268{
269 // The resulting string is wrapped in this.
270 // Do NOT use this for custom unit suffixes! Add them as actual units instead.
271 std::string_view decorationFormatString = "{}";
272
273 // --- Units:
274
275 // The measurement unit of the input value. If null, no conversion is performed.
277 // The measurement unit of the resulting string. If null, no conversion is performed, and the unit name is taken from `sourceUnit` if any.
278 std::optional<E> targetUnit = getDefaultUnitParams<E>().targetUnit;
279
280 // Whether to show the unit suffix.
282
283 // --- Precision:
284
285 // The output style. (Scientific notation or not, fixed-precision or not.)
287
288 // How many digits of precision.
290
291 // --- Other:
292
293 // If false, silently remove the minus sign before negative zeroes
294 // (including numbers that get rounded into negative zeroes with the current `precision` setting).
295 bool allowNegativeZero = getDefaultUnitParams<E>().allowNegativeZero;
296
297 // Use a pretty Unicode minus sign instead of the ASCII `-`.
298 bool unicodeMinusSign = getDefaultUnitParams<E>().unicodeMinusSign;
299
300 // If non-zero, this character is inserted between every three digits to the left of the decimal point.
301 char thousandsSeparator = getDefaultUnitParams<E>().thousandsSeparator;
302 // If non-zero, this character is inserted between every three digits to the right of the decimal point.
303 char thousandsSeparatorFrac = getDefaultUnitParams<E>().thousandsSeparatorFrac;
304
305 // If false, remove zero before the fractional point (`.5` instead of `0.5`).
307
308 // Remove trailing zeroes after the fractional point. If the point becomes the last symbol, remove the point too.
309 bool stripTrailingZeroes = getDefaultUnitParams<E>().stripTrailingZeroes;
310
311 // When printing degrees, this lets you display arcminutes and possibly arcseconds. Ignored for everything else.
312 std::conditional_t<std::is_same_v<E, AngleUnit>, DegreesMode, detail::Units::Empty> degreesMode = getDefaultUnitParams<E>().degreesMode;
313
314 // If you add new fields there, update the initializer for `defaultUnitToStringParams` in `MRUnits.cpp`.
315
316 friend bool operator==( const UnitToStringParams&, const UnitToStringParams& ) = default;
317};
318
319// The `std::variant` of `UnitToStringParams<E>` for all known `E`s (unit kinds).
320using VarUnitToStringParams = std::variant<
321 #define MR_TRIM_LEADING_COMMA(...) MR_TRIM_LEADING_COMMA_(__VA_ARGS__)
322 #define MR_TRIM_LEADING_COMMA_(x, ...) __VA_ARGS__
323 #define MR_X(E) , UnitToStringParams<E>
325 #undef MR_TRIM_LEADING_COMMA
326 #undef MR_TRIM_LEADING_COMMA_
327 #undef MR_X
328>;
329
330// Converts value to a string, possibly converting it to a different unit.
331// By default, length is kept as is, while angles are converted from radians to the current UI unit.
332template <UnitEnum E, detail::Units::Scalar T>
333[[nodiscard]] MRVIEWER_API std::string valueToString( T value, const UnitToStringParams<E>& params = getDefaultUnitParams<E>() );
334
335#define MR_Y(T, E) extern template MRVIEWER_API std::string valueToString<E, T>( T value, const UnitToStringParams<E>& params );
336#define MR_X(E) DETAIL_MR_UNIT_VALUE_TYPES(MR_Y, E)
338#undef MR_X
339#undef MR_Y
340
341// This overload lets you select the unit kind at runtime.
342template <detail::Units::Scalar T>
343[[nodiscard]] MRVIEWER_API std::string valueToString( T value, const VarUnitToStringParams& params );
344
345#define MR_X(T) extern template MRVIEWER_API std::string valueToString( T value, const VarUnitToStringParams& params );
347#undef MR_X
348
349// Guesses the number of digits of precision for fixed-point formatting of `value`.
350// Mostly for internal use.
351template <detail::Units::Scalar T>
352[[nodiscard]] MRVIEWER_API int guessPrecision( T value );
353
354// Guesses the number of digits of precision for fixed-point formatting of the min-max range.
355// If `min >= max`, always returns zero. Ignores min and/or max if they are the smallest of the largest representable value respectively.
356// Mostly for internal use.
357template <detail::Units::Scalar T>
358[[nodiscard]] MRVIEWER_API int guessPrecision( T min, T max );
359
360// Same but for vectors.
361template <typename T>
363[[nodiscard]] int guessPrecision( T value )
364{
365 int ret = 0;
366 for ( int i = 0; i < VectorTraits<T>::size; i++ )
367 ret = std::max( ret, guessPrecision( VectorTraits<T>::getElem( i, value ) ) );
368 return ret;
369}
370template <typename T>
371requires (VectorTraits<T>::size > 1 && detail::Units::Scalar<typename VectorTraits<T>::BaseType>)
372[[nodiscard]] int guessPrecision( T min, T max )
373{
374 int ret = 0;
375 for ( int i = 0; i < VectorTraits<T>::size; i++ )
376 ret = std::max( ret, guessPrecision( VectorTraits<T>::getElem( i, min ), VectorTraits<T>::getElem( i, max ) ) );
377 return ret;
378}
379
380#define MR_X(T) \
381 extern template MRVIEWER_API int guessPrecision( T value ); \
382 extern template MRVIEWER_API int guessPrecision( T min, T max );
384#undef MR_X
385
386// Generates a printf-style format string for `value`, for use with ImGui widgets.
387// It has form "123.45 mm##%.6f" (the baked number, then `##` and some format string).
388// The `##...` part isn't printed, but we need it when ctrl+clicking the number, to show the correct number of digits.
389template <UnitEnum E, detail::Units::Scalar T>
390[[nodiscard]] MRVIEWER_API std::string valueToImGuiFormatString( T value, const UnitToStringParams<E>& params = getDefaultUnitParams<E>() );
391
392#define MR_Y(T, E) extern template MRVIEWER_API std::string valueToImGuiFormatString( T value, const UnitToStringParams<E>& params );
393#define MR_X(E) DETAIL_MR_UNIT_VALUE_TYPES(MR_Y, E)
395#undef MR_X
396#undef MR_Y
397
398// This overload lets you select the unit kind at runtime.
399template <detail::Units::Scalar T>
400[[nodiscard]] MRVIEWER_API std::string valueToImGuiFormatString( T value, const VarUnitToStringParams& params );
401
402#define MR_X(T) extern template MRVIEWER_API std::string valueToImGuiFormatString( T value, const VarUnitToStringParams& params );
404#undef MR_X
405
406}
#define MR_X(E)
Definition MRUnits.h:145
#define MR_TRIM_LEADING_COMMA(...)
#define DETAIL_MR_UNIT_VALUE_TYPES(X,...)
Definition MRUnits.h:107
#define DETAIL_MR_UNIT_ENUMS(X)
Definition MRUnits.h:99
Definition MRUnits.h:120
Definition MRUnits.h:167
std::conditional_t< std::is_integral_v< typename VectorTraits< T >::BaseType >, typename VectorTraits< T >::template ChangeBaseType< float >, T > MakeFloatingPoint
Definition MRUnits.h:170
PixelSizeUnit
Definition MRUnits.h:41
NumberStyle
Definition MRUnits.h:248
MRVIEWER_API int guessPrecision(T value)
Definition MRUnits.h:363
MRVIEWER_API std::string valueToString(T value, const UnitToStringParams< E > &params=getDefaultUnitParams< E >())
const UnitInfo & getUnitInfo(E unit)=delete
NoUnit
Definition MRUnits.h:18
bool unitsAreEquivalent(E a, E b)
Definition MRUnits.h:151
LengthUnit
Definition MRUnits.h:24
const UnitToStringParams< E > & getDefaultUnitParams()
void setDefaultUnitParams(const UnitToStringParams< E > &newParams)
MRVIEWER_API std::string valueToImGuiFormatString(T value, const UnitToStringParams< E > &params=getDefaultUnitParams< E >())
RatioUnit
Definition MRUnits.h:48
DegreesMode
Definition MRUnits.h:257
MovementSpeedUnit
Definition MRUnits.h:64
detail::Units::MakeFloatingPoint< T > convertUnits(E from, E to, const T &value)
Definition MRUnits.h:177
VolumeUnit
Definition MRUnits.h:82
AreaUnit
Definition MRUnits.h:73
MRMESH_API std::string_view toString(DimensionsVisualizePropertyType value)
TimeUnit
Definition MRUnits.h:56
InvLengthUnit
Definition MRUnits.h:91
AngleUnit
Definition MRUnits.h:33
std::variant< > VarUnitToStringParams
Definition MRUnits.h:320
Definition MRUnits.h:129
std::string_view unitSuffix
Definition MRUnits.h:138
std::string_view prettyName
Definition MRUnits.h:134
float conversionFactor
Definition MRUnits.h:132
Definition MRUnits.h:268
bool leadingZero
Definition MRUnits.h:306
int precision
Definition MRUnits.h:289
std::conditional_t< std::is_same_v< E, AngleUnit >, DegreesMode, detail::Units::Empty > degreesMode
Definition MRUnits.h:312
NumberStyle style
Definition MRUnits.h:286
std::optional< E > sourceUnit
Definition MRUnits.h:276
std::string_view decorationFormatString
Definition MRUnits.h:271
char thousandsSeparator
Definition MRUnits.h:301
bool unitSuffix
Definition MRUnits.h:281
bool allowNegativeZero
Definition MRUnits.h:295
char thousandsSeparatorFrac
Definition MRUnits.h:303
std::optional< E > targetUnit
Definition MRUnits.h:278
friend bool operator==(const UnitToStringParams &, const UnitToStringParams &)=default
bool stripTrailingZeroes
Definition MRUnits.h:309
bool unicodeMinusSign
Definition MRUnits.h:298
Definition MRMesh/MRVectorTraits.h:14
static constexpr int size
Definition MRMesh/MRVectorTraits.h:18
static constexpr auto && getElem(int i, U &&value)
Definition MRMesh/MRVectorTraits.h:28
T BaseType
Definition MRMesh/MRVectorTraits.h:17
Definition MRUnits.h:164