0.3.0
Loading...
Searching...
No Matches
Units.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "Units.hpp"
10
11#include <cmath>
13#include "util/Logger.hpp"
14
15namespace NAV::Units
16{
17
19{
20 j = to_string(data);
21}
23{
24 if (!j.is_string())
25 {
26 LOG_WARN("Could not parse '{}' into PositionUncertaintyUnits. Consider resaving the flow", j.dump());
27 return;
28 }
29 std::string str = j.get<std::string>();
30 for (size_t i = 0; i < static_cast<size_t>(PositionUncertaintyUnits::COUNT); i++)
31 {
32 auto enumItem = static_cast<PositionUncertaintyUnits>(i);
33 if (str == to_string(enumItem))
34 {
35 data = enumItem;
36 return;
37 }
38 }
39}
40
42{
43 j = to_string(data);
44}
46{
47 if (!j.is_string())
48 {
49 LOG_WARN("Could not parse '{}' into VelocityUncertaintyUnits. Consider resaving the flow", j.dump());
50 return;
51 }
52 std::string str = j.get<std::string>();
53 for (size_t i = 0; i < static_cast<size_t>(VelocityUncertaintyUnits::COUNT); i++)
54 {
55 auto enumItem = static_cast<VelocityUncertaintyUnits>(i);
56 if (str == to_string(enumItem))
57 {
58 data = enumItem;
59 return;
60 }
61 }
62}
63
65{
66 j = to_string(data);
67}
69{
70 if (!j.is_string())
71 {
72 LOG_WARN("Could not parse '{}' into AttitudeUncertaintyUnits. Consider resaving the flow", j.dump());
73 return;
74 }
75 std::string str = j.get<std::string>();
76 for (size_t i = 0; i < static_cast<size_t>(AttitudeUncertaintyUnits::COUNT); i++)
77 {
78 auto enumItem = static_cast<AttitudeUncertaintyUnits>(i);
79 if (str == to_string(enumItem))
80 {
81 data = enumItem;
82 return;
83 }
84 }
85}
86
87void to_json(json& j, const VelocityUnits& data)
88{
89 j = to_string(data);
90}
91void from_json(const json& j, VelocityUnits& data)
92{
93 if (!j.is_string())
94 {
95 LOG_WARN("Could not parse '{}' into VelocityUnits. Consider resaving the flow", j.dump());
96 return;
97 }
98 std::string str = j.get<std::string>();
99 for (size_t i = 0; i < static_cast<size_t>(VelocityUnits::COUNT); i++)
100 {
101 auto enumItem = static_cast<VelocityUnits>(i);
102 if (str == to_string(enumItem))
103 {
104 data = enumItem;
105 return;
106 }
107 }
108}
109
110void to_json(json& j, const AttitudeUnits& data)
111{
112 j = to_string(data);
113}
114void from_json(const json& j, AttitudeUnits& data)
115{
116 if (!j.is_string())
117 {
118 LOG_WARN("Could not parse '{}' into AttitudeUnits. Consider resaving the flow", j.dump());
119 return;
120 }
121 std::string str = j.get<std::string>();
122 for (size_t i = 0; i < static_cast<size_t>(AttitudeUnits::COUNT); i++)
123 {
124 auto enumItem = static_cast<AttitudeUnits>(i);
125 if (str == to_string(enumItem))
126 {
127 data = enumItem;
128 return;
129 }
130 }
131}
132
133} // namespace NAV::Units
134
135double NAV::convertUnit(const double& value, Units::PositionUncertaintyUnits unit)
136{
137 switch (unit)
138 {
140 return value;
142 return std::sqrt(value);
144 break;
145 }
146 return value; // Position standard deviation [m]
147}
148Eigen::Vector3d NAV::convertUnit(const Eigen::Vector3d& value, Units::PositionUncertaintyUnits unit)
149{
150 switch (unit)
151 {
153 return value;
155 return value.array().sqrt();
157 break;
158 }
159 return value; // Position standard deviation [m]
160}
161
162double NAV::convertUnit(const double& value, Units::VelocityUncertaintyUnits unit)
163{
164 switch (unit)
165 {
167 return value;
169 return std::sqrt(value);
171 break;
172 }
173 return value; // Velocity standard deviation [m/s]
174}
175Eigen::Vector3d NAV::convertUnit(const Eigen::Vector3d& value, Units::VelocityUncertaintyUnits unit)
176{
177 switch (unit)
178 {
180 return value;
182 return value.array().sqrt();
184 break;
185 }
186 return value; // Velocity standard deviation [m/s]
187}
188
189double NAV::convertUnit(const double& value, Units::AttitudeUncertaintyUnits unit)
190{
191 switch (unit)
192 {
194 return value;
196 return deg2rad(value);
198 return std::sqrt(value);
200 return deg2rad(std::sqrt(value));
202 break;
203 }
204 return value; // Attitude standard deviation [rad]
205}
206Eigen::Vector3d NAV::convertUnit(const Eigen::Vector3d& value, Units::AttitudeUncertaintyUnits unit)
207{
208 switch (unit)
209 {
211 return value;
213 return deg2rad(value);
215 return value.array().sqrt();
217 return deg2rad(value.array().sqrt());
219 break;
220 }
221 return value; // Attitude standard deviation [rad]
222}
223
224double NAV::convertUnit(const double& value, Units::VelocityUnits unit)
225{
226 switch (unit)
227 {
229 return value;
231 break;
232 }
233 return value; // [m/s]
234}
235Eigen::Vector3d NAV::convertUnit(const Eigen::Vector3d& value, Units::VelocityUnits unit)
236{
237 switch (unit)
238 {
240 return value;
242 break;
243 }
244 return value; // [m/s]
245}
246
247double NAV::convertUnit(const double& value, Units::AttitudeUnits unit)
248{
249 switch (unit)
250 {
252 return value;
254 return deg2rad(value);
256 break;
257 }
258 return value; // [rad]
259}
260Eigen::Vector3d NAV::convertUnit(const Eigen::Vector3d& value, Units::AttitudeUnits unit)
261{
262 switch (unit)
263 {
265 return value;
267 return deg2rad(value);
269 break;
270 }
271 return value; // [rad]
272}
273
275{
276 switch (unit)
277 {
279 return "m^2";
281 return "m";
283 break;
284 }
285 return "";
286}
287
289{
290 switch (unit)
291 {
293 return "m^2/s^2";
295 return "m/s";
297 break;
298 }
299 return "";
300}
301
303{
304 switch (unit)
305 {
307 return "rad";
309 return "deg";
311 return "rad^2";
313 return "deg^2";
315 break;
316 }
317 return "";
318}
319
321{
322 switch (unit)
323 {
325 return "m/s";
327 break;
328 }
329 return "";
330}
331
333{
334 switch (unit)
335 {
337 return "rad";
339 return "deg";
341 break;
342 }
343 return "";
344}
nlohmann::json json
json namespace
Units used by GNSS positioning.
Utility class for logging to console and file.
#define LOG_WARN
Error occurred, but a fallback option exists and program continues to work normally.
Definition Logger.hpp:71
AttitudeUncertaintyUnits
Possible units for the uncertainty of the attitude.
Definition Units.hpp:46
@ rad
Standard deviation [radian].
Definition Units.hpp:47
@ deg
Standard deviation [degree].
Definition Units.hpp:48
@ COUNT
Amount of items in the enum.
Definition Units.hpp:51
@ rad2
Variance [radian^2].
Definition Units.hpp:49
@ deg2
Variance [degree^2].
Definition Units.hpp:50
void to_json(json &j, const PositionUncertaintyUnits &data)
Converts the provided data into a json object.
Definition Units.cpp:18
PositionUncertaintyUnits
Possible units for the uncertainty of the position.
Definition Units.hpp:30
@ meter2
Variance [m^2, m^2, m^2].
Definition Units.hpp:32
@ COUNT
Amount of items in the enum.
Definition Units.hpp:33
@ meter
Standard deviation [m, m, m].
Definition Units.hpp:31
VelocityUncertaintyUnits
Possible units for the uncertainty of the velocity.
Definition Units.hpp:38
@ m_s
Standard deviation [m/s].
Definition Units.hpp:39
@ COUNT
Amount of items in the enum.
Definition Units.hpp:41
@ m2_s2
Variance [m^2/s^2].
Definition Units.hpp:40
void from_json(const json &j, PositionUncertaintyUnits &data)
Converts the provided json object into the data object.
Definition Units.cpp:22
AttitudeUnits
Possible units for the attitude.
Definition Units.hpp:63
@ COUNT
Amount of items in the enum.
Definition Units.hpp:66
VelocityUnits
Possible units for the velocity.
Definition Units.hpp:56
@ COUNT
Amount of items in the enum.
Definition Units.hpp:58
const char * to_string(gui::widgets::PositionWithFrame::ReferenceFrame refFrame)
Converts the enum to a string.
double convertUnit(const double &value, Units::PositionUncertaintyUnits unit)
Converts the value depending on the unit provided.
Definition Units.cpp:135
constexpr auto deg2rad(const T &deg)
Convert Degree to Radians.
Definition Units.hpp:21