| 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 | /// @file Matrix.hpp | ||
| 10 | /// @brief Widgets related to Matrices | ||
| 11 | /// @author T. Topp (topp@ins.uni-stuttgart.de) | ||
| 12 | /// @date 2021-10-04 | ||
| 13 | |||
| 14 | #pragma once | ||
| 15 | |||
| 16 | #include <Eigen/Core> | ||
| 17 | #include <cstdint> | ||
| 18 | #include <imgui.h> | ||
| 19 | |||
| 20 | /// @brief Flags for MatrixView GUI elements @ref GuiMatrixViewFlags_ | ||
| 21 | using GuiMatrixViewFlags = int; | ||
| 22 | |||
| 23 | /// @brief Flags to select the MatrixView behaviour | ||
| 24 | enum GuiMatrixViewFlags_ : uint8_t | ||
| 25 | { | ||
| 26 | GuiMatrixViewFlags_None = 0, ///< None | ||
| 27 | GuiMatrixViewFlags_RowHeader = 1 << 0, ///< Print the Row Header | ||
| 28 | GuiMatrixViewFlags_ColumnHeader = 1 << 1, ///< Print the Col Header | ||
| 29 | GuiMatrixViewFlags_Header = GuiMatrixViewFlags_RowHeader | GuiMatrixViewFlags_ColumnHeader, ///< Print all Header | ||
| 30 | }; | ||
| 31 | |||
| 32 | namespace NAV::gui::widgets | ||
| 33 | { | ||
| 34 | #if defined(__GNUC__) || defined(__clang__) | ||
| 35 | #pragma GCC diagnostic push | ||
| 36 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" | ||
| 37 | #endif | ||
| 38 | |||
| 39 | /// @brief Shows GUI elements to display the coefficients of a matrix | ||
| 40 | /// @tparam _Scalar Data Type of the matrix | ||
| 41 | /// @tparam _Rows Amount of rows of the matrix | ||
| 42 | /// @tparam _Cols Amount of cols of the matrix | ||
| 43 | /// @param[in] label Label to display beside. Has to be unique (use # to hide text afterwards to append an uid) | ||
| 44 | /// @param[in] matrix Pointer to the matrix to display | ||
| 45 | /// @param[in] flags Flags to modify the behavior of the whole element | ||
| 46 | /// @param[in] tableFlags Flags to modify the Table behaviour | ||
| 47 | /// @param[in] format Printf format to display the value with | ||
| 48 | template<typename _Scalar, int _Rows, int _Cols> | ||
| 49 | ✗ | void MatrixView(const char* label, const Eigen::Matrix<_Scalar, _Rows, _Cols>* matrix, GuiMatrixViewFlags flags = GuiMatrixViewFlags_None, ImGuiTableFlags tableFlags = ImGuiTableFlags_None, | |
| 50 | const char* format = "%.6f") | ||
| 51 | { | ||
| 52 | ✗ | if (ImGui::BeginTable(label, static_cast<int>(matrix->cols()) + ((flags & GuiMatrixViewFlags_RowHeader) == 1), tableFlags)) | |
| 53 | { | ||
| 54 | ✗ | if (flags & GuiMatrixViewFlags_ColumnHeader) | |
| 55 | { | ||
| 56 | ✗ | if (flags & GuiMatrixViewFlags_RowHeader) | |
| 57 | { | ||
| 58 | ✗ | ImGui::TableSetupColumn(""); | |
| 59 | } | ||
| 60 | ✗ | for (int64_t col = 0; col < matrix->cols(); col++) | |
| 61 | { | ||
| 62 | ✗ | ImGui::TableSetupColumn(std::to_string(col).c_str()); | |
| 63 | } | ||
| 64 | ✗ | ImGui::TableHeadersRow(); | |
| 65 | } | ||
| 66 | |||
| 67 | ✗ | for (int64_t row = 0; row < matrix->rows(); row++) | |
| 68 | { | ||
| 69 | ✗ | if (flags & GuiMatrixViewFlags_RowHeader) | |
| 70 | { | ||
| 71 | ✗ | ImGui::TableNextColumn(); | |
| 72 | ✗ | ImGui::TextUnformatted(std::to_string(row).c_str()); | |
| 73 | ✗ | ImU32 cell_bg_color = ImGui::GetColorU32(ImGui::GetStyle().Colors[ImGuiCol_TableHeaderBg]); | |
| 74 | ✗ | ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color); | |
| 75 | } | ||
| 76 | |||
| 77 | ✗ | for (int64_t col = 0; col < matrix->cols(); col++) | |
| 78 | { | ||
| 79 | ✗ | ImGui::TableNextColumn(); | |
| 80 | ✗ | ImGui::Text(format, (*matrix)(row, col)); | |
| 81 | } | ||
| 82 | } | ||
| 83 | ✗ | ImGui::EndTable(); | |
| 84 | } | ||
| 85 | ✗ | } | |
| 86 | #if defined(__GNUC__) || defined(__clang__) | ||
| 87 | #pragma GCC diagnostic pop | ||
| 88 | #endif | ||
| 89 | |||
| 90 | /// @brief Shows GUI elements to modify the coefficients of a matrix with | ||
| 91 | /// @tparam _Scalar Data Type of the matrix | ||
| 92 | /// @tparam _Rows Amount of rows of the matrix | ||
| 93 | /// @tparam _Cols Amount of cols of the matrix | ||
| 94 | /// @param[in] label Label to display beside. Has to be unique (use # to hide text afterwards to append an uid) | ||
| 95 | /// @param[in, out] matrix Pointer to the matrix to modify | ||
| 96 | /// @param[in] flags Flags to modify the behavior of the whole element | ||
| 97 | /// @param[in] tableFlags Flags to modify the Table behaviour | ||
| 98 | /// @param[in] inputTextWidth With in px of each InputText | ||
| 99 | /// @param[in] step Step size of the InputText | ||
| 100 | /// @param[in] step_fast Fast step size of the InputText | ||
| 101 | /// @param[in] format Printf format to display the value with | ||
| 102 | /// @param[in] inputTextFlags InputText flags to modify the behavior of the input fields | ||
| 103 | /// @return True if the value was changed | ||
| 104 | template<typename _Scalar, int _Rows, int _Cols> | ||
| 105 | ✗ | bool InputMatrix(const char* label, Eigen::Matrix<_Scalar, _Rows, _Cols>* matrix, GuiMatrixViewFlags flags = GuiMatrixViewFlags_None, ImGuiTableFlags tableFlags = ImGuiTableFlags_None, | |
| 106 | float inputTextWidth = 50.0F, double step = 0.0, double step_fast = 0.0, const char* format = "%.6f", ImGuiInputTextFlags inputTextFlags = ImGuiInputTextFlags_None) | ||
| 107 | { | ||
| 108 | ✗ | bool changed = false; | |
| 109 | ✗ | if (ImGui::BeginTable(label, static_cast<int>(matrix->cols()) + ((flags & GuiMatrixViewFlags_RowHeader) == 1), tableFlags)) | |
| 110 | { | ||
| 111 | ✗ | if (flags & GuiMatrixViewFlags_ColumnHeader) | |
| 112 | { | ||
| 113 | ✗ | if (flags & GuiMatrixViewFlags_RowHeader) | |
| 114 | { | ||
| 115 | ✗ | ImGui::TableSetupColumn(""); | |
| 116 | } | ||
| 117 | ✗ | for (int64_t col = 0; col < matrix->cols(); col++) | |
| 118 | { | ||
| 119 | ✗ | ImGui::TableSetupColumn(std::to_string(col).c_str()); | |
| 120 | } | ||
| 121 | ✗ | ImGui::TableHeadersRow(); | |
| 122 | } | ||
| 123 | |||
| 124 | ✗ | for (int64_t row = 0; row < matrix->rows(); row++) | |
| 125 | { | ||
| 126 | ✗ | if (flags & GuiMatrixViewFlags_RowHeader) | |
| 127 | { | ||
| 128 | ✗ | ImGui::TableNextColumn(); | |
| 129 | ✗ | ImGui::TextUnformatted(std::to_string(row).c_str()); | |
| 130 | ✗ | ImU32 cell_bg_color = ImGui::GetColorU32(ImGui::GetStyle().Colors[ImGuiCol_TableHeaderBg]); | |
| 131 | ✗ | ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color); | |
| 132 | } | ||
| 133 | |||
| 134 | ✗ | for (int64_t col = 0; col < matrix->cols(); col++) | |
| 135 | { | ||
| 136 | ✗ | ImGui::TableNextColumn(); | |
| 137 | ✗ | ImGui::SetNextItemWidth(inputTextWidth); | |
| 138 | ✗ | if (ImGui::InputDouble(fmt::format("##{} ({}, {})", label, row, col).c_str(), &(*matrix)(row, col), step, step_fast, format, inputTextFlags)) | |
| 139 | { | ||
| 140 | ✗ | changed = true; | |
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | ✗ | ImGui::EndTable(); | |
| 145 | } | ||
| 146 | |||
| 147 | ✗ | return changed; | |
| 148 | } | ||
| 149 | |||
| 150 | } // namespace NAV::gui::widgets | ||
| 151 |