INSTINCT Code Coverage Report


Directory: src/
File: util/Logger/CommonLog.cpp
Date: 2025-06-02 15:19:59
Exec Total Coverage
Lines: 48 69 69.6%
Functions: 7 9 77.8%
Branches: 39 112 34.8%

Line Branch Exec Source
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 "CommonLog.hpp"
10
11 #include <imgui.h>
12
13 #include "Navigation/Ellipsoid/Ellipsoid.hpp"
14 #include "Navigation/Transformations/Units.hpp"
15
16 #include "util/Logger.hpp"
17
18 namespace NAV
19 {
20
21 json CommonLog::save()
22 {
23 json j;
24 j["useGuiInputs"] = _useGuiInputs;
25 if (_originPosition) { j["originPosition"] = *_originPosition; }
26
27 return j;
28 }
29
30 112 void CommonLog::restore(const json& j)
31 {
32
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 92 times.
112 if (j.contains("useGuiInputs")) { j.at("useGuiInputs").get_to(_useGuiInputs); }
33 92 else { _useGuiInputs = false; }
34
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
112 if (j.contains("originPosition")) { _originPosition = j.at("originPosition").get<gui::widgets::PositionWithFrame>(); }
35 112 else { _originPosition.reset(); }
36 112 }
37
38 708 CommonLog::CommonLog()
39 {
40
1/2
✓ Branch 1 taken 708 times.
✗ Branch 2 not taken.
708 std::scoped_lock lk(_mutex);
41 708 _index = _wantsInit.size(); // NOLINT(cppcoreguidelines-prefer-member-initializer)
42
1/2
✓ Branch 1 taken 708 times.
✗ Branch 2 not taken.
708 _wantsInit.push_back(false);
43 708 }
44
45 1416 CommonLog::~CommonLog()
46 {
47 1416 std::scoped_lock lk(_mutex);
48 1416 _wantsInit.erase(_wantsInit.begin() + static_cast<int64_t>(_index));
49 1416 }
50
51 108 void CommonLog::initialize() const
52 {
53
1/2
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
108 std::scoped_lock lk(_mutex);
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (_useGuiInputs) { return; }
55
1/2
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
108 _wantsInit.at(_index) = true;
56
57
3/4
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 86 times.
✓ Branch 4 taken 22 times.
250 if (std::ranges::all_of(_wantsInit, [](bool val) { return val; }))
58 {
59
2/4
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 86 times.
✗ Branch 7 not taken.
172 LOG_DEBUG("Resetting common log variables.");
60 86 _startTime.reset();
61
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 if (!_useGuiInputs)
62 {
63 86 _originPosition.reset();
64 }
65
66
1/2
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
86 std::ranges::fill(_wantsInit, false);
67 }
68 108 }
69
70 339125 double CommonLog::calcTimeIntoRun(const InsTime& insTime)
71 {
72
3/4
✓ Branch 1 taken 339125 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 339123 times.
678250 if (std::scoped_lock lk(_mutex);
73 339125 _startTime.empty())
74 {
75 2 _startTime = insTime;
76
4/8
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
2 LOG_DEBUG("Common log setting start time to {} ({}) GPST.", _startTime.toYMDHMS(GPST), _startTime.toGPSweekTow(GPST));
77 339125 }
78
1/2
✓ Branch 1 taken 339125 times.
✗ Branch 2 not taken.
339125 return static_cast<double>((insTime - _startTime).count());
79 }
80
81 96886 CommonLog::LocalPosition CommonLog::calcLocalPosition(const Eigen::Vector3d& lla_position)
82 {
83 {
84
1/2
✓ Branch 1 taken 96886 times.
✗ Branch 2 not taken.
96886 std::scoped_lock lk(_mutex);
85
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 96885 times.
96886 if (!_originPosition.has_value())
86 {
87
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (lla_position.hasNaN())
88 {
89 LOG_WARN("Not setting common log origin to {}, {}, {} [deg, deg, m]",
90 rad2deg(lla_position.x()), rad2deg(lla_position.y()), lla_position.z());
91 }
92 else
93 {
94
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 _originPosition = gui::widgets::PositionWithFrame();
95
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 _originPosition->e_position = trafo::lla2ecef_WGS84(lla_position);
96
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
1 LOG_DEBUG("Common log setting position of origin to {}, {}, {} [deg, deg, m]",
97 rad2deg(lla_position.x()), rad2deg(lla_position.y()), lla_position.z());
98 }
99 }
100 96886 }
101
102 // North/South deviation [m]
103 96886 double northSouth = calcGeographicalDistance(lla_position.x(), lla_position.y(),
104 96886 _originPosition->latitude(), lla_position.y())
105
2/2
✓ Branch 3 taken 48436 times.
✓ Branch 4 taken 48450 times.
96886 * (lla_position.x() > _originPosition->latitude() ? 1 : -1);
106
107 // East/West deviation [m]
108 193772 double eastWest = calcGeographicalDistance(lla_position.x(), lla_position.y(),
109 96886 lla_position.x(), _originPosition->longitude())
110
2/2
✓ Branch 3 taken 144 times.
✓ Branch 4 taken 96742 times.
96886 * (lla_position.y() > _originPosition->longitude() ? 1 : -1);
111
112 96886 return { .northSouth = northSouth, .eastWest = eastWest };
113 }
114
115 bool CommonLog::ShowOriginInput(const char* id)
116 {
117 bool changed = false;
118 if (ImGui::Checkbox(fmt::format("Override position origin (for all common logging)##{}", id).c_str(), &_useGuiInputs))
119 {
120 LOG_DEBUG("{}: useGuiInputs changed to {}", id, _useGuiInputs);
121 changed = true;
122 }
123 if (_useGuiInputs)
124 {
125 if (!_originPosition) { _originPosition = gui::widgets::PositionWithFrame(); }
126 ImGui::Indent();
127 std::scoped_lock lk(_mutex);
128 if (gui::widgets::PositionInput(fmt::format("Origin##{}", id).c_str(), _originPosition.value(), gui::widgets::PositionInputLayout::SINGLE_ROW))
129 {
130 changed = true;
131 }
132 ImGui::Unindent();
133 }
134
135 return changed;
136 }
137
138 } // namespace NAV
139