0.4.1
Loading...
Searching...
No Matches
Colormap.hpp
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/// @file Colormap.hpp
10/// @brief Colormap
11/// @author T. Topp (topp@ins.uni-stuttgart.de)
12/// @date 2023-09-22
13
14#pragma once
15
16#include <imgui.h>
17#include <imgui_internal.h>
18#include <optional>
19#include <functional>
20#include <string>
21#include <vector>
22#include "util/Json.hpp"
23
24namespace NAV
25{
26
27class Colormap;
28
29/// @brief Display a colormap button
30/// @param[in] label Label to display on the button (unique id for ImGui)
31/// @param[in, out] cmap Colormap to show on the button and edit in the popup
32/// @param[in] size_arg Size of the button
33/// @return True if clicked
34/// @note Code from Implot library
35bool ColormapButton(const char* label, Colormap& cmap, const ImVec2& size_arg);
36
37/// @brief Converts the provided object into a json object
38/// @param[out] j Return Json object
39/// @param[in] cmap Colormap to convert
40void to_json(json& j, const Colormap& cmap);
41/// @brief Converts the provided json object into a struct
42/// @param[in] j Json object with the vector values
43/// @param[out] cmap Struct to return
44void from_json(const json& j, Colormap& cmap);
45
46/// @brief Colormap class
48{
49 public:
50 /// @brief Constructor
51 Colormap();
52
53 /// @brief Add a color with value
54 /// @param value Value
55 /// @param color Color
56 void addColor(double value, ImColor color);
57
58 /// @brief Remove the entry at the index (if past the last index or empty, NoOp)
59 /// @param idx Index to remove
60 void removeColor(size_t idx);
61
62 /// @brief Gets the color for the given value
63 /// @param[in] value Value to look for
64 /// @param[in] defaultColor Default color to display if no enty in the colormap matches
65 /// @return The color for the value or the given defaultColor if nothing matches
66 [[nodiscard]] ImColor getColor(double value, const ImColor& defaultColor) const;
67
68 /// Return the id of the colormap
69 [[nodiscard]] int64_t getId() const;
70
71 /// @brief Return the map
72 [[nodiscard]] const std::vector<std::pair<double, ImColor>>& getColormap() const;
73
74 std::string name = "Colormap"; ///< Name of the Colormap
75 bool discrete = false; ///< Whether to have discrete changes of the colors or continuous
76 size_t version = 0; ///< Version, to tell nodes that the colormap was updated
77
78 private:
79 /// Unique id of the colormap
80 int64_t id;
81 /// Sorted list of value/color combinations (value is active if lookup is greater or equal)
82 std::vector<std::pair<double, ImColor>> colormap = { { 0.0, ImColor(1.0F, 1.0F, 1.0F, 1.0F) } };
83
84 /// @brief Renders the colormap
85 /// @note Code from Implot library
86 void render() const;
87
88 /// @brief Renders the colormap
89 /// @param[in] bounds Bounds where to draw the rect
90 /// @note Code from Implot library
91 void render(const ImRect& bounds) const;
92
93 /// @brief Display a colormap button
94 /// @param[in] label Label to display on the button (unique id for ImGui)
95 /// @param[in, out] cmap Colormap to show on the button and edit in the popup
96 /// @param[in] size_arg Size of the button
97 /// @return True if clicked
98 /// @note Code from Implot library
99 friend bool NAV::ColormapButton(const char* label, Colormap& cmap, const ImVec2& size_arg);
100
101 /// @brief Converts the provided object into a json object
102 /// @param[out] j Return Json object
103 /// @param[in] cmap Colormap to convert
104 friend void to_json(json& j, const Colormap& cmap);
105 /// @brief Converts the provided json object into a struct
106 /// @param[in] j Json object with the vector values
107 /// @param[out] cmap Struct to return
108 friend void from_json(const json& j, Colormap& cmap);
109};
110
111/// Global colormaps
112extern std::vector<Colormap> ColormapsGlobal;
113/// Flow colormaps
114extern std::vector<Colormap> ColormapsFlow;
115
116/// @brief Type of the Colormap mask
117enum class ColormapMaskType : uint8_t
118{
119 None, ///< Do not use a colormap mask
120 Global, ///< Use the global colormap
121 Flow, ///< Use the flow colormap
122};
123
124/// @brief Shows a combobox to select a colormap
125/// @param[in, out] type Type of the selected colormap
126/// @param[in, out] id Id of the selected colormap
127/// @param[in] label Unique ImGui Id
128/// @return True if a change was made
129bool ShowColormapSelector(ColormapMaskType& type, int64_t& id, const char* label = "");
130
131/// @brief Searches for the colormap in the Global and Flow colormaps
132/// @param type Type of the colormap (global or flow)
133/// @param id Id of the colormap
134/// @return The colormap if one could be found
135std::optional<std::reference_wrapper<const Colormap>> ColormapSearch(const ColormapMaskType& type, const int64_t& id);
136
137} // namespace NAV
nlohmann::json json
json namespace
Defines how to save certain datatypes to json.
Colormap class.
Definition Colormap.hpp:48
friend void to_json(json &j, const Colormap &cmap)
Converts the provided object into a json object.
Definition Colormap.cpp:215
const std::vector< std::pair< double, ImColor > > & getColormap() const
Return the map.
Definition Colormap.cpp:78
ImColor getColor(double value, const ImColor &defaultColor) const
Gets the color for the given value.
Definition Colormap.cpp:51
void removeColor(size_t idx)
Remove the entry at the index (if past the last index or empty, NoOp)
Definition Colormap.cpp:44
int64_t id
Unique id of the colormap.
Definition Colormap.hpp:80
bool discrete
Whether to have discrete changes of the colors or continuous.
Definition Colormap.hpp:75
std::string name
Name of the Colormap.
Definition Colormap.hpp:74
int64_t getId() const
Return the id of the colormap.
Definition Colormap.cpp:73
std::vector< std::pair< double, ImColor > > colormap
Sorted list of value/color combinations (value is active if lookup is greater or equal)
Definition Colormap.hpp:82
size_t version
Version, to tell nodes that the colormap was updated.
Definition Colormap.hpp:76
friend void from_json(const json &j, Colormap &cmap)
Converts the provided json object into a struct.
Definition Colormap.cpp:225
void render() const
Renders the colormap.
Definition Colormap.cpp:83
Colormap()
Constructor.
Definition Colormap.cpp:29
void addColor(double value, ImColor color)
Add a color with value.
Definition Colormap.cpp:36
void to_json(json &j, const Node &node)
Converts the provided node into a json object.
Definition Node.cpp:990
ColormapMaskType
Type of the Colormap mask.
Definition Colormap.hpp:118
@ Global
Use the global colormap.
Definition Colormap.hpp:120
@ Flow
Use the flow colormap.
Definition Colormap.hpp:121
@ None
Ionosphere model turned off.
std::vector< Colormap > ColormapsGlobal
Global colormaps.
Definition Colormap.cpp:26
bool ShowColormapSelector(ColormapMaskType &type, int64_t &id, const char *label)
Shows a combobox to select a colormap.
Definition Colormap.cpp:245
void from_json(const json &j, Node &node)
Converts the provided json object into a node object.
Definition Node.cpp:1007
std::vector< Colormap > ColormapsFlow
Flow colormaps.
Definition Colormap.cpp:27
bool ColormapButton(const char *label, Colormap &cmap, const ImVec2 &size_arg)
Display a colormap button.
Definition Colormap.cpp:118
std::optional< std::reference_wrapper< const Colormap > > ColormapSearch(const ColormapMaskType &type, const int64_t &id)
Searches for the colormap in the Global and Flow colormaps.
Definition Colormap.cpp:301