Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
conversions.cpp
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
9
10#include "momentum/common/log.h"
12
13namespace momentum {
14
15namespace {
16
17[[maybe_unused]] [[nodiscard]] Unit toUnit(const std::string& unitStr) {
18 if (unitStr == "m" || unitStr == "M") {
19 return Unit::M;
20 } else if (unitStr == "dm" || unitStr == "DM") {
21 return Unit::DM;
22 } else if (unitStr == "cm" || unitStr == "CM") {
23 return Unit::CM;
24 } else if (unitStr == "mm" || unitStr == "MM") {
25 return Unit::MM;
26 } else {
27 return Unit::Unknown;
28 }
29}
30
31[[maybe_unused]] [[nodiscard]] std::string_view toString(const Unit& unit) {
32 switch (unit) {
33 case Unit::M:
34 return "m";
35 case Unit::DM:
36 return "dm";
37 case Unit::CM:
38 return "cm";
39 case Unit::MM:
40 return "mm";
41 case Unit::Unknown:
42 default:
43 return "unknown";
44 }
45}
46
47// Maps the legacy Unit enum to LengthUnit for use with the coordinate system utility.
48[[nodiscard]] LengthUnit toLengthUnit(Unit unit) {
49 switch (unit) {
50 case Unit::M:
51 return LengthUnit::Meter;
52 case Unit::DM:
54 case Unit::CM:
56 case Unit::MM:
58 case Unit::Unknown:
59 default:
60 return LengthUnit::Centimeter; // Fallback: treat as cm
61 }
62}
63
64} // namespace
65
66template <typename T>
68 if (unit == Unit::Unknown) {
69 MT_LOGE(
70 "{}: Unknown unit '{}' found in the file. Use centimeters instead.",
71 __func__,
72 toString(unit));
73 }
74
75 // Use the coordinate system utility for unit conversion
76 const LengthUnit srcUnit = toLengthUnit(unit);
77 const T s = scaleFactor<T>(
80 Vector3<T> vec_in_cm = vec * s;
81
82 // Preserve the existing axis remapping exactly as-is.
83 // The marker I/O converts to Z-up internally (Z is identity).
84 // TODO: Migrate to use changeVector() once marker convention is unified to Y-up.
85 switch (up) {
86 case UpVector::X:
87 return {vec_in_cm.y(), vec_in_cm.z(), vec_in_cm.x()};
88 case UpVector::Y:
89 return {vec_in_cm.x(), vec_in_cm.z(), -vec_in_cm.y()};
90 case UpVector::Z:
91 return vec_in_cm;
92 case UpVector::YNeg:
93 return {vec_in_cm.x(), -vec_in_cm.z(), vec_in_cm.y()};
94 default:
95 return vec_in_cm;
96 }
97}
98
99template <typename T>
100Vector3<T> toMomentumVector3(const Vector3<T>& vec, UpVector up, const std::string& unitStr) {
101 return toMomentumVector3(vec, up, toUnit(unitStr));
102}
103
106
107template Vector3<float>
108toMomentumVector3(const Vector3<float>& vec, UpVector up, const std::string& unitStr);
109template Vector3<double>
110toMomentumVector3(const Vector3<double>& vec, UpVector up, const std::string& unitStr);
111
112} // namespace momentum
#define MT_LOGE(...)
Definition log.h:181
UpVector
This enumeration is used to define the primary up vector directions for various coordinate systems an...
@ X
X-axis up vector.
@ Z
Z-axis up vector.
@ Y
Y-axis up vector.
@ YNeg
Negative Y-axis up vector.
Unit
An enumeration representing the units of measurement used in marker data files.
@ CM
Centimeters.
@ Unknown
Unknown unit of measurement.
@ MM
Millimeters.
LengthUnit
Specifies the unit of length measurement.
Vector3< T > toMomentumVector3(const Vector3< T > &vec, UpVector up, Unit unit)
This function converts a 3D vector in the mocap marker coordinate system to the Momentum coordinate s...
Vector< T, 3 > Vector3
Definition types.h:236
std::string_view toString(const LimitType type)
T scaleFactor(const CoordinateSystem &from, const CoordinateSystem &to)
Returns the length scale factor when converting a length from from's unit to to's unit.
A 3D coordinate system defined by its up axis, handedness, and length unit.