MeshLib C++ Docs
Loading...
Searching...
No Matches
MRPalette.h
Go to the documentation of this file.
1#pragma once
2#include "MRViewerFwd.h"
3#include "MRMesh/MRMeshFwd.h"
4#include "MRMesh/MRVector4.h"
6#include "MRMesh/MRColor.h"
7#include "MRMesh/MRExpected.h"
8#include "MRViewer/MRImGui.h"
9#include "MRMesh/MRBox.h"
10#include <algorithm>
11#include <filesystem>
12
13namespace Json{ class Value; }
14
15namespace MR
16{
17
26{
27public:
29 MRVIEWER_API static const std::vector<Color> DefaultColors;
30 [[nodiscard]] static const std::vector<Color> & BlueGreenRedColors() { return DefaultColors; }
31
33 [[nodiscard]] MRVIEWER_API static const std::vector<Color> & GreenRedColors();
34
35 MRVIEWER_API Palette( const std::vector<Color>& colors );
42 MRVIEWER_API void setBaseColors( const std::vector<Color>& colors );
47 MRVIEWER_API void setRangeMinMax( float min, float max );
53 MRVIEWER_API void setRangeMinMaxNegPos( float minNeg, float maxNeg, float minPos, float maxPos );
54
55 // set number of different colors for discrete palette
56 MRVIEWER_API void setDiscretizationNumber( int discretization );
57 // set palette type (linear / discrete)
58 MRVIEWER_API void setFilterType( FilterType type );
59
64 MRVIEWER_API void draw( const std::string& windowName, const ImVec2& pose, const ImVec2& size, bool onlyTopHalf = false );
65
66 // structure for label
67 struct MRVIEWER_CLASS Label
68 {
69 float value = 0.f; // label position according normal scale (from Min to Max)
70 std::string text; // label text
71
72 Label() = default;
73 MRVIEWER_API Label( float val, std::string text );
74 };
75 // reset labels to standard view
76 MRVIEWER_API void resetLabels();
77 // set labels manually
78 MRVIEWER_API void setCustomLabels( const std::vector<Label>& labels );
79 // set labels visible
80 MRVIEWER_API void setLabelsVisible( bool visible );
81
82
83 // Setup palette data from JsonValue
84 // @return return true if loading successful
85 MRVIEWER_API bool loadFromJson( const Json::Value& root );
86 // Serialize this palette data to JsonValue
87 MRVIEWER_API void saveCurrentToJson( Json::Value& root ) const;
88
89
94 MRVIEWER_API Color getColor( float relativeValue ) const;
96 Color getInvalidColor() const { return Color::gray(); }
97
101 MRVIEWER_API VertColors getVertColors( const VertScalars& values, const VertBitSet& region, const VertBitSet* valids ) const;
102
103 const MeshTexture& getTexture() const { return texture_; };
104
105 // get relative position in [0,1], where 0 is for minimum and 1 is for maximum
106 MRVIEWER_API float getRelativePos( float val ) const;
107
110 UVCoord getUVcoord( float val, bool valid = true ) const
111 {
112 return {
113 ( texEnd_ - texStart_ ) * getRelativePos( val ) + texStart_,
114 valid ? 0.25f : 0.75f
115 };
116 }
117
121 MRVIEWER_API VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertPredicate & valids = {} ) const;
122 MRVIEWER_API VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertBitSet * valids ) const;
123
124 // base parameters of palette
126 {
127 std::vector<float> ranges = { 0.f, 1.f }; // range limits for palette
128 std::vector<Color> baseColors; // palette base colors (need for calculate real colors according discretization)
129 MinMaxf legendLimits; // if valid - limit legend range
130 int discretization = 7; // number of different colors for discrete palette
131 };
132
133 [[nodiscard]] const Parameters& getParameters() const { return parameters_; }
134
136 [[nodiscard]] float getRangeMin() const { return parameters_.ranges.front(); }
137
139 [[nodiscard]] float getRangeMax() const { return parameters_.ranges.back(); }
140
142 [[nodiscard]] float getRangeSq() const { return std::max( sqr( getRangeMin() ), sqr( getRangeMax() ) ); }
143
144 // returns formated string for this value of palette
145 MRVIEWER_API std::string getStringValue( float value );
146 // returns maximal label count
147 MRVIEWER_API int getMaxLabelCount();
148 // sets maximal label count
149 MRVIEWER_API void setMaxLabelCount( int val );
150
152 MRVIEWER_API void setLegendLimits( const MinMaxf& limits );
153
154private:
155 void setRangeLimits_( const std::vector<float>& ranges );
156
157 void updateDiscretizatedColors_();
158 Color getBaseColor_( float val );
159
160
161 // fill labels with equal distance between
162 void setUniformLabels_();
163 // first label is equal to min value, last - to the max val
164 // don't use with MinMaxNegPos mode
165 void setZeroCentredLabels_();
166
167 void updateCustomLabels_();
168
169 void sortLabels_();
170
171 void updateLegendLimits_( const MinMaxf& limits );
172 void updateLegendLimitIndexes_();
173
174 std::vector<Label> customLabels_;
175 std::vector<Label> labels_;
176 bool showLabels_ = false;
177
178 // stores OpenGL textures. Change useDiscrete_ to switch between them
179 MeshTexture texture_;
180
181 // texture positions of min and max values
182 float texStart_ = 0, texEnd_ = 1;
183
184 Parameters parameters_;
185
186 bool isWindowOpen_ = false;
187
188 bool useCustomLabels_ = false;
189
190 int maxLabelCount_ = 0;
191
192 float prevMaxLabelWidth_ = 0.0f;
193
194 MinMaxi legendLimitIndexes_ = { 0, 7 };
195 MinMaxf relativeLimits_ = { 0.f, 1.f };
196
197 static void resizeCallback_( ImGuiSizeCallbackData* data );
198};
199
202{
203public:
205 MRVIEWER_API static const std::vector<std::string>& getPresetNames();
208 MRVIEWER_API static bool loadPreset( const std::string& name, Palette& palette );
210 MRVIEWER_API static Expected<void> savePreset( const std::string& name, const Palette& palette );
212 MRVIEWER_API static std::filesystem::path getPalettePresetsFolder();
213private:
215 ~PalettePresets() = default;
216
217 std::vector<std::string> names_;
218
219 void update_();
220
221 static PalettePresets& instance_();
222};
223
224}
Class to save and load user palette presets.
Definition MRPalette.h:202
static MRVIEWER_API Expected< void > savePreset(const std::string &name, const Palette &palette)
saves given palette to preset with given name
static MRVIEWER_API std::filesystem::path getPalettePresetsFolder()
returns path to presets folder
static MRVIEWER_API bool loadPreset(const std::string &name, Palette &palette)
static MRVIEWER_API const std::vector< std::string > & getPresetNames()
gets names of existing presets
Class to hold one dimension texture with value to UV mapping.
Definition MRPalette.h:26
MRVIEWER_API float getRelativePos(float val) const
MRVIEWER_API VertColors getVertColors(const VertScalars &values, const VertBitSet &region, const VertBitSet *valids) const
MRVIEWER_API void draw(const std::string &windowName, const ImVec2 &pose, const ImVec2 &size, bool onlyTopHalf=false)
float getRangeSq() const
returns minimum squared value, not smaller than all squared values of palette's range
Definition MRPalette.h:142
MRVIEWER_API int getMaxLabelCount()
const MeshTexture & getTexture() const
Definition MRPalette.h:103
MRVIEWER_API Palette(const std::vector< Color > &colors)
MRVIEWER_API bool loadFromJson(const Json::Value &root)
MRVIEWER_API void setFilterType(FilterType type)
const Parameters & getParameters() const
Definition MRPalette.h:133
MRVIEWER_API std::string getStringValue(float value)
static MRVIEWER_API const std::vector< Color > & GreenRedColors()
simpler palette colors: from green to red
MRVIEWER_API void setCustomLabels(const std::vector< Label > &labels)
MRVIEWER_API void setLegendLimits(const MinMaxf &limits)
set legend limits. if min > max - limits are disabled
float getRangeMin() const
returns minimum value in the palette's range
Definition MRPalette.h:136
MRVIEWER_API void setMaxLabelCount(int val)
MRVIEWER_API VertUVCoords getUVcoords(const VertScalars &values, const VertBitSet &region, const VertBitSet *valids) const
MRVIEWER_API void saveCurrentToJson(Json::Value &root) const
MRVIEWER_API void setRangeMinMax(float min, float max)
set range limits for palette (need for find color by value) all palette colors are evenly distributed...
MRVIEWER_API VertUVCoords getUVcoords(const VertScalars &values, const VertBitSet &region, const VertPredicate &valids={}) const
MRVIEWER_API void setDiscretizationNumber(int discretization)
MRVIEWER_API Color getColor(float relativeValue) const
MRVIEWER_API void setRangeMinMaxNegPos(float minNeg, float maxNeg, float minPos, float maxPos)
set range limits for palette (need for find color by value) two half palette colors are evenly distri...
static MRVIEWER_API const std::vector< Color > DefaultColors
preset palette colors: from blue via green to red
Definition MRPalette.h:29
MRVIEWER_API void resetLabels()
float getRangeMax() const
returns maximum value in the palette's range
Definition MRPalette.h:139
static const std::vector< Color > & BlueGreenRedColors()
Definition MRPalette.h:30
UVCoord getUVcoord(float val, bool valid=true) const
Definition MRPalette.h:110
MRVIEWER_API void setLabelsVisible(bool visible)
Color getInvalidColor() const
return invalid color
Definition MRPalette.h:96
MRVIEWER_API void setBaseColors(const std::vector< Color > &colors)
Set base palette colors colors.size() should be more or equal 2 for discrete palette using vector of ...
Definition MRCameraOrientationPlugin.h:8
constexpr T sqr(T x) noexcept
squared value
Definition MRMesh/MRMeshFwd.h:688
MinMax< float > MinMaxf
Definition MRMesh/MRMeshFwd.h:353
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:29
FilterType
Definition MRMesh/MRMeshFwd.h:666
tl::expected< T, E > Expected
Definition MRExpected.h:25
Vector2f UVCoord
Definition MRMesh/MRMeshFwd.h:447
MinMax< int > MinMaxi
Definition MRMesh/MRMeshFwd.h:355
Definition MRMesh/MRColor.h:9
static constexpr Color gray() noexcept
Definition MRMesh/MRColor.h:29
Definition MRMeshTexture.h:13
Definition MRPalette.h:68
MRVIEWER_API Label(float val, std::string text)
std::string text
Definition MRPalette.h:70
Definition MRPalette.h:126
std::vector< Color > baseColors
Definition MRPalette.h:128
int discretization
Definition MRPalette.h:130
std::vector< float > ranges
Definition MRPalette.h:127
MinMaxf legendLimits
Definition MRPalette.h:129