Ocean
Loading...
Searching...
No Matches
MetricSize.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_CV_CALIBRATION_METRIC_SIZE_H
9#define META_OCEAN_CV_CALIBRATION_METRIC_SIZE_H
10
12
13#include "ocean/base/String.h"
14
15#include "ocean/math/Numeric.h"
16
17namespace Ocean
18{
19
20namespace CV
21{
22
23namespace Calibration
24{
25
26/**
27 * This class implements an object holding a metric size like width, height, length, or thickness.
28 * Internally, the size is stored in millimeter but can be converted to and from any other unit.
29 * @ingroup cvcalibration
30 */
32{
33 public:
34
35 /**
36 * Definition of different unit types.
37 */
38 enum UnitType : uint32_t
39 {
40 /// An invalid unit type.
42 /// Millimeter as unit.
44 /// Inch as unit.
45 UT_INCH // 1 inch == 25.4 mm
46 };
47
48 /**
49 * Definition of different paper types.
50 */
51 enum PaperType : uint32_t
52 {
53 /// An invalid paper type.
55 /// The paper is a DIN A3.
57 /// The paper is a DIN A4.
59 /// The paper is a letter.
61 /// The paper is a tabloid aka ledger.
63 };
64
65 public:
66
67 /**
68 * Creates a new object creating an invalid metric size.
69 */
70 MetricSize() = default;
71
72 /**
73 * Creates a new metric size object.
74 * @param value The size value, with range (-infinity, infinity)
75 * @param unitType The unit type of the given value, must be valid
76 */
77 MetricSize(const double value, const UnitType unitType);
78
79 /**
80 * Returns the size value in a specific unit.
81 * @param unitType The unit type in which the value will be returned, must be valid
82 * @return The size value in the specified unit
83 */
84 double value(const UnitType unitType) const;
85
86 /**
87 * Returns the size value in a specific unit as string.
88 * @param unitType The unit type in which the value will be returned, must be valid
89 * @param precision The number of decimal places to use, with range [0, infinity)
90 * @return The size value in the specified unit as string
91 */
92 inline std::string value(const UnitType unitType, const unsigned int precision) const;
93
94 /**
95 * Returns whether the size value is zero.
96 * @return True, if so
97 */
98 inline bool isNull() const;
99
100 /**
101 * Returns whether this size object holds a valid value.
102 * @return True, if so
103 */
104 inline bool isValid() const;
105
106 /**
107 * Adds two metric size objects.
108 * @param value The second value to add
109 * @return The resulting sum of both sizes
110 */
111 inline MetricSize operator+(const MetricSize& value) const;
112
113 /**
114 * Adds and assigns two metric size objects.
115 * @param value The second value to add
116 * @return The resulting sum of both sizes
117 */
118 inline MetricSize& operator+=(const MetricSize& value);
119
120 /**
121 * Subtracts two metric size objects.
122 * @param value The second value to subtract
123 * @return The resulting difference of both sizes
124 */
125 inline MetricSize operator-(const MetricSize& value) const;
126
127 /**
128 * Subtracts and assigns two metric size objects.
129 * @param value The second value to subtract
130 * @return The resulting difference of both sizes
131 */
132 inline MetricSize& operator-=(const MetricSize& value);
133
134 /**
135 * Multiplies this metric size object with a scalar factor.
136 * @param value The multiplication factor, with range (-infinity, infinity)
137 * @return The resulting product
138 */
139 inline MetricSize operator*(const double factor) const;
140
141 /**
142 * Multiplies and assigns this metric object with a scalar factor.
143 * @param value The multiplication factor, with range (-infinity, infinity)
144 * @return The resulting product
145 */
146 inline MetricSize& operator*=(const double factor);
147
148 /**
149 * Divides this metric size object by a scalar factor.
150 * @param factor The division factor, must not be zero
151 * @return The resulting quotient
152 */
153 inline MetricSize operator/(const double factor) const;
154
155 /**
156 * Divides and assigns this metric size object by a scalar factor.
157 * @param factor The division factor, must not be zero
158 * @return The resulting quotient
159 */
160 inline MetricSize& operator/=(const double factor);
161
162 /**
163 * Divides two metric size objects and returns the ratio.
164 * @param value The second value to divide, must not be zero
165 * @return The resulting ratio as scalar value
166 */
167 inline double operator/(const MetricSize& value) const;
168
169 /**
170 * Returns whether this metric size object is smaller than a second one.
171 * @param value The second metric size object to compare
172 * @return True, if so
173 */
174 inline bool operator<(const MetricSize& value) const;
175
176 /**
177 * Returns whether this metric size object is smaller or equal than a second one.
178 * @param value The second metric size object to compare
179 * @return True, if so
180 */
181 inline bool operator<=(const MetricSize& value) const;
182
183 /**
184 * Returns whether this metric size object is greater than a second one.
185 * @param value The second metric size object to compare
186 * @return True, if so
187 */
188 inline bool operator>(const MetricSize& value) const;
189
190 /**
191 * Returns whether this metric size object is greater or equal than a second one.
192 * @param value The second metric size object to compare
193 * @return True, if so
194 */
195 inline bool operator>=(const MetricSize& value) const;
196
197 /**
198 * Returns whether two metric size objects hold the same size values.
199 * @param value The second metric size object to compare
200 * @return True, if so
201 */
202 inline bool operator==(const MetricSize& value) const;
203
204 /**
205 * Returns whether two metric size objects do not hold the same size values.
206 * @param value The second metric size object to compare
207 * @return True, if so
208 */
209 inline bool operator!=(const MetricSize& value) const;
210
211 /**
212 * Returns the metric width and height of a paper.
213 * @param paperType The type of the paper, must be valid
214 * @return True, if succeeded
215 */
216 static bool determinePaperSize(const PaperType paperType, MetricSize& width, MetricSize& height);
217
218 /**
219 * Returns the string of a unit type.
220 * @param unitType The unit type to return as string
221 * @return The unit type as string, 'Invalid' if invalid or unknown
222 */
223 static std::string translateUnitType(const UnitType unitType);
224
225 /**
226 * Translates the string of a paper type to a paper type value.
227 * @param paperType The string of the paper type
228 * @return The resulting paper type, PT_INVALID if invalid or unknown
229 */
230 static PaperType translatePaperType(const std::string& paperType);
231
232 /**
233 * Translates the paper type value to a readable string.
234 * @param paperType The paper type to translate
235 * @return The readable paper type as string, 'invalid' if invalid or unknown
236 */
237 static std::string translatePaperType(const PaperType paperType);
238
239 protected:
240
241 /**
242 * Converts the size value from one unit to another unit.
243 * @param sourceValue The source value in the specified source unit to convert, with range (-infinity, infinity)
244 * @return The resulting value in the specified target unit, with range (-infinity, infinity)
245 * @tparam TSourceUnit The source unit in which the source value is defined
246 * @tparam TTargetUnit The target unit in which the target value will be returned
247 */
248 template <UnitType TSourceUnit, UnitType TTargetUnit>
249 static double convert(const double sourceValue);
250
251 /**
252 * Converts the size value from one unit to another unit.
253 * @param sourceValue The source value in the specified source unit to convert, with range (-infinity, infinity)
254 * @param targetUnit The unit in which the target value will be returned, must be valid
255 * @return The resulting value in the specified target unit, with range (-infinity, infinity)
256 * @tparam TSourceUnit The source unit in which the source value is defined
257 */
258 template <UnitType TSourceUnit>
259 static double convert(const double sourceValue, const UnitType targetUnit);
260
261 /**
262 * Converts the metric size defined in one unit to another unit.
263 * @param sourceUnit The unit in which the source value is defined, must be valid
264 * @param sourceValue The source value in the specified unit, with range (-infinity, infinity)
265 * @param targetUnit The unit in which the target value will be returned, must be valid
266 * @return The converted value in the specified target unit, with range (-infinity, infinity)
267 */
268 static double convert(const UnitType sourceUnit, const double sourceValue, const UnitType targetUnit);
269
270 protected:
271
272 /// The metric size in millimeter, with range (-infinity, infinity).
274};
275
276inline std::string MetricSize::value(const UnitType unitType, const unsigned int precision) const
277{
278 return "\"" + String::toAString(value(unitType), precision) + translateUnitType(unitType) + "\"";
279}
280
281inline bool MetricSize::isNull() const
282{
284}
285
286inline bool MetricSize::isValid() const
287{
289}
290
292{
293 return MetricSize(valueMillimeter_ + value.valueMillimeter_, UT_MILLIMETER);
294}
295
297{
299
300 return *this;
301}
302
304{
305 return MetricSize(valueMillimeter_ - value.valueMillimeter_, UT_MILLIMETER);
306}
307
309{
311
312 return *this;
313}
314
315inline MetricSize MetricSize::operator*(const double factor) const
316{
317 return MetricSize(valueMillimeter_ * factor, UT_MILLIMETER);
318}
319
320inline MetricSize& MetricSize::operator*=(const double factor)
321{
322 valueMillimeter_ *= factor;
323
324 return *this;
325}
326
327inline MetricSize MetricSize::operator/(const double factor) const
328{
329 ocean_assert(NumericD::isNotEqualEps(factor));
330
331 return MetricSize(valueMillimeter_ / factor, UT_MILLIMETER);
332}
333
334inline MetricSize& MetricSize::operator/=(const double factor)
335{
336 ocean_assert(NumericD::isNotEqualEps(factor));
337
338 valueMillimeter_ /= factor;
339
340 return *this;
341}
342
343inline double MetricSize::operator/(const MetricSize& value) const
344{
345 ocean_assert(value.isValid());
346
347 ocean_assert(NumericD::isNotEqualEps(value.valueMillimeter_));
348
349 return valueMillimeter_ / value.valueMillimeter_;
350}
351
352inline bool MetricSize::operator<(const MetricSize& value) const
353{
354 return valueMillimeter_ < value.valueMillimeter_;
355}
356
357inline bool MetricSize::operator<=(const MetricSize& value) const
358{
359 return valueMillimeter_ <= value.valueMillimeter_;
360}
361
362inline bool MetricSize::operator>(const MetricSize& value) const
363{
364 return valueMillimeter_ > value.valueMillimeter_;
365}
366
367inline bool MetricSize::operator>=(const MetricSize& value) const
368{
369 return valueMillimeter_ >= value.valueMillimeter_;
370}
371
372inline bool MetricSize::operator==(const MetricSize& value) const
373{
374 return NumericD::isEqual(valueMillimeter_, value.valueMillimeter_);
375}
376
377inline bool MetricSize::operator!=(const MetricSize& value) const
378{
379 return NumericD::isNotEqual(valueMillimeter_, value.valueMillimeter_);
380}
381
382template <>
383inline double MetricSize::convert<MetricSize::UT_MILLIMETER, MetricSize::UT_INCH>(const double sourceValue)
384{
385 return sourceValue / 25.4;
386}
387
388template <>
389inline double MetricSize::convert<MetricSize::UT_INCH, MetricSize::UT_MILLIMETER>(const double sourceValue)
390{
391 return sourceValue * 25.4;
392}
393
394template <MetricSize::UnitType TSourceUnit, MetricSize::UnitType TTargetUnit>
395inline double MetricSize::convert(const double sourceValue)
396{
397 static_assert(TSourceUnit == TTargetUnit, "Missing specialization for either TSourceUnit or TTargetUnit");
398 static_assert(TSourceUnit != UT_INVALID && TTargetUnit != UT_INVALID, "Invalid unit type!");
399
400 return sourceValue;
401}
402
403template <>
404inline double MetricSize::convert<MetricSize::UT_MILLIMETER>(const double sourceValue, const UnitType targetUnit)
405{
406 ocean_assert(targetUnit != UT_INVALID);
407
408 switch (targetUnit)
409 {
410 case UT_INVALID:
411 break;
412
413 case UT_MILLIMETER:
414 return convert<UT_MILLIMETER, UT_MILLIMETER>(sourceValue);
415
416 case UT_INCH:
417 return convert<UT_MILLIMETER, UT_INCH>(sourceValue);
418 }
419
420 ocean_assert(false && "Invalid unit type!");
421 return sourceValue;
422}
423
424template <>
425inline double MetricSize::convert<MetricSize::UT_INCH>(const double sourceValue, const UnitType targetUnit)
426{
427 ocean_assert(targetUnit != UT_INVALID);
428
429 switch (targetUnit)
430 {
431 case UT_INVALID:
432 break;
433
434 case UT_MILLIMETER:
435 return convert<UT_INCH, UT_MILLIMETER>(sourceValue);
436
437 case UT_INCH:
438 return convert<UT_INCH, UT_INCH>(sourceValue);
439 }
440
441 ocean_assert(false && "Invalid unit type!");
442 return sourceValue;
443}
444
445template <MetricSize::UnitType TSourceUnit>
446inline double MetricSize::convert(const double sourceValue, const UnitType /*targetUnit*/)
447{
448 static_assert(oceanFalse<TSourceUnit>(), "Missing specialization");
449
450 return sourceValue;
451}
452
453}
454
455}
456
457}
458
459#endif // META_OCEAN_CV_CALIBRATION_METRIC_SIZE_H
This class implements an object holding a metric size like width, height, length, or thickness.
Definition MetricSize.h:32
MetricSize & operator*=(const double factor)
Multiplies and assigns this metric object with a scalar factor.
Definition MetricSize.h:320
bool operator<(const MetricSize &value) const
Returns whether this metric size object is smaller than a second one.
Definition MetricSize.h:352
MetricSize operator-(const MetricSize &value) const
Subtracts two metric size objects.
Definition MetricSize.h:303
bool operator>(const MetricSize &value) const
Returns whether this metric size object is greater than a second one.
Definition MetricSize.h:362
PaperType
Definition of different paper types.
Definition MetricSize.h:52
@ PT_INVALID
An invalid paper type.
Definition MetricSize.h:54
@ PT_LETTER
The paper is a letter.
Definition MetricSize.h:60
@ PT_TABLOID
The paper is a tabloid aka ledger.
Definition MetricSize.h:62
@ PT_DIN_A3
The paper is a DIN A3.
Definition MetricSize.h:56
@ PT_DIN_A4
The paper is a DIN A4.
Definition MetricSize.h:58
static std::string translateUnitType(const UnitType unitType)
Returns the string of a unit type.
bool isNull() const
Returns whether the size value is zero.
Definition MetricSize.h:281
MetricSize & operator-=(const MetricSize &value)
Subtracts and assigns two metric size objects.
Definition MetricSize.h:308
bool isValid() const
Returns whether this size object holds a valid value.
Definition MetricSize.h:286
MetricSize(const double value, const UnitType unitType)
Creates a new metric size object.
double valueMillimeter_
The metric size in millimeter, with range (-infinity, infinity).
Definition MetricSize.h:273
static std::string translatePaperType(const PaperType paperType)
Translates the paper type value to a readable string.
double value(const UnitType unitType) const
Returns the size value in a specific unit.
MetricSize & operator+=(const MetricSize &value)
Adds and assigns two metric size objects.
Definition MetricSize.h:296
MetricSize operator/(const double factor) const
Divides this metric size object by a scalar factor.
Definition MetricSize.h:327
bool operator<=(const MetricSize &value) const
Returns whether this metric size object is smaller or equal than a second one.
Definition MetricSize.h:357
bool operator!=(const MetricSize &value) const
Returns whether two metric size objects do not hold the same size values.
Definition MetricSize.h:377
static bool determinePaperSize(const PaperType paperType, MetricSize &width, MetricSize &height)
Returns the metric width and height of a paper.
MetricSize operator+(const MetricSize &value) const
Adds two metric size objects.
Definition MetricSize.h:291
MetricSize operator*(const double factor) const
Multiplies this metric size object with a scalar factor.
Definition MetricSize.h:315
bool operator==(const MetricSize &value) const
Returns whether two metric size objects hold the same size values.
Definition MetricSize.h:372
static double convert(const double sourceValue)
Converts the size value from one unit to another unit.
Definition MetricSize.h:395
bool operator>=(const MetricSize &value) const
Returns whether this metric size object is greater or equal than a second one.
Definition MetricSize.h:367
static double convert(const UnitType sourceUnit, const double sourceValue, const UnitType targetUnit)
Converts the metric size defined in one unit to another unit.
MetricSize()=default
Creates a new object creating an invalid metric size.
static PaperType translatePaperType(const std::string &paperType)
Translates the string of a paper type to a paper type value.
MetricSize & operator/=(const double factor)
Divides and assigns this metric size object by a scalar factor.
Definition MetricSize.h:334
UnitType
Definition of different unit types.
Definition MetricSize.h:39
@ UT_MILLIMETER
Millimeter as unit.
Definition MetricSize.h:43
@ UT_INVALID
An invalid unit type.
Definition MetricSize.h:41
@ UT_INCH
Inch as unit.
Definition MetricSize.h:45
static constexpr T minValue()
Returns the min scalar value.
Definition Numeric.h:3253
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition Numeric.h:2389
static constexpr bool isEqualEps(const T value)
Returns whether a value is smaller than or equal to a small epsilon.
Definition Numeric.h:2090
static constexpr bool isNotEqualEps(const T value)
Returns whether a value is not smaller than or equal to a small epsilon.
Definition Numeric.h:2240
static bool isNotEqual(const T first, const T second)
Returns whether two values are not equal up to a small epsilon.
Definition Numeric.h:2616
static std::string toAString(const char value)
Converts a value to a string with 8bit character.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15