MeshLib
 
Loading...
Searching...
No Matches
MRPalette.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRViewerFwd.h"
4#include "MRMesh/MRVector4.h"
6#include "MRMesh/MRColor.h"
7#include "MRMesh/MRExpected.h"
8#include <imgui.h>
9#include <algorithm>
10#include <filesystem>
11
12namespace Json{ class Value; }
13
14namespace MR
15{
16
25{
26public:
28 MRVIEWER_API static const std::vector<Color> DefaultColors;
29 [[nodiscard]] static const std::vector<Color> & BlueGreenRedColors() { return DefaultColors; }
30
32 [[nodiscard]] MRVIEWER_API static const std::vector<Color> & GreenRedColors();
33
34 MRVIEWER_API Palette( const std::vector<Color>& colors );
41 MRVIEWER_API void setBaseColors( const std::vector<Color>& colors );
46 MRVIEWER_API void setRangeMinMax( float min, float max );
52 MRVIEWER_API void setRangeMinMaxNegPos( float minNeg, float maxNeg, float minPos, float maxPos );
53
54 // set number of different colors for discrete palette
55 MRVIEWER_API void setDiscretizationNumber( int discretization );
56 // set palette type (linear / discrete)
57 MRVIEWER_API void setFilterType( FilterType type );
58
63 MRVIEWER_API void draw( const std::string& windowName, const ImVec2& pose, const ImVec2& size, bool onlyTopHalf = false );
64
65 // structure for label
66 struct MRVIEWER_CLASS Label
67 {
68 float value = 0.f; // label position according normal scale (from Min to Max)
69 std::string text; // label text
70
71 Label() = default;
72 MRVIEWER_API Label( float val, std::string text );
73 };
74 // reset labels to standard view
75 MRVIEWER_API void resetLabels();
76 // set labels manually
77 MRVIEWER_API void setCustomLabels( const std::vector<Label>& labels );
78 // set labels visible
79 MRVIEWER_API void setLabelsVisible( bool visible );
80
81
82 // Setup palette data from JsonValue
83 // @return return true if loading successful
84 MRVIEWER_API bool loadFromJson( const Json::Value& root );
85 // Serialize this palette data to JsonValue
86 MRVIEWER_API void saveCurrentToJson( Json::Value& root ) const;
87
88
89 // return color according val and setting filter type
90 // Discrete: returns color for given value
91 // Linear: get interpolated color from the init vector
92 MRVIEWER_API Color getColor( float val );
93
94 const MeshTexture& getTexture() const
95 {
96 return texture_;
97 };
98
99 // get relative position in [0,1], where 0 is for minimum and 1 is for maximum
100 MRVIEWER_API float getRelativePos( float val ) const;
101
104 UVCoord getUVcoord( float val, bool valid = true ) const
105 {
106 return {
107 ( texEnd_ - texStart_ ) * getRelativePos( val ) + texStart_,
108 valid ? 0.25f : 0.75f
109 };
110 }
111
115 MRVIEWER_API VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertPredicate & valids = {} ) const;
116 MRVIEWER_API VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertBitSet * valids ) const;
117
118 // base parameters of palette
120 {
121 std::vector<float> ranges = { 0.f, 1.f }; // range limits for palette
122 std::vector<Color> baseColors; // palette base colors (need for calculate real colors according discretization)
123 int discretization = 7; // number of different colors for discrete palette
124 };
125
126 [[nodiscard]] const Parameters& getParameters() const { return parameters_; }
127
129 [[nodiscard]] float getRangeMin() const { return parameters_.ranges.front(); }
130
132 [[nodiscard]] float getRangeMax() const { return parameters_.ranges.back(); }
133
135 [[nodiscard]] float getRangeSq() const { return std::max( sqr( getRangeMin() ), sqr( getRangeMax() ) ); }
136
137 // returns formated string for this value of palette
138 MRVIEWER_API std::string getStringValue( float value );
139 // returns maximal label count
140 MRVIEWER_API int getMaxLabelCount();
141 // sets maximal label count
142 MRVIEWER_API void setMaxLabelCount( int val );
143
144private:
145 void setRangeLimits_( const std::vector<float>& ranges );
146
147 void updateDiscretizatedColors_();
148 Color getBaseColor_( float val );
149
150
151 // fill labels with equal distance between
152 void setUniformLabels_();
153 // first label is equal to min value, last - to the max val
154 // don't use with MinMaxNegPos mode
155 void setZeroCentredLabels_();
156
157 void updateCustomLabels_();
158
159 void sortLabels_();
160
161 std::vector<Label> customLabels_;
162 std::vector<Label> labels_;
163 bool showLabels_ = false;
164
165 // stores OpenGL textures. Change useDiscrete_ to switch between them
166 MeshTexture texture_;
167
168 // texture positions of min and max values
169 float texStart_ = 0, texEnd_ = 1;
170
171 Parameters parameters_;
172
173 bool isWindowOpen_ = false;
174
175 bool useCustomLabels_ = false;
176
177 int maxLabelCount_ = 0;
178
179 float prevMaxLabelWidth_ = 0.0f;
180
181 static void resizeCallback_( ImGuiSizeCallbackData* data );
182};
183
186{
187public:
189 MRVIEWER_API static const std::vector<std::string>& getPresetNames();
192 MRVIEWER_API static bool loadPreset( const std::string& name, Palette& palette );
194 MRVIEWER_API static VoidOrErrStr savePreset( const std::string& name, const Palette& palette );
196 MRVIEWER_API static std::filesystem::path getPalettePresetsFolder();
197private:
199 ~PalettePresets() = default;
200
201 std::vector<std::string> names_;
202
203 void update_();
204
205 static PalettePresets& instance_();
206};
207
208}
Definition MRDotNet/MRBitSet.h:39
Class to save and load user palette presets.
Definition MRPalette.h:186
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 VoidOrErrStr savePreset(const std::string &name, const Palette &palette)
saves given palette to preset with given name
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:25
MRVIEWER_API float getRelativePos(float val) 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:135
MRVIEWER_API int getMaxLabelCount()
const MeshTexture & getTexture() const
Definition MRPalette.h:94
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:126
MRVIEWER_API std::string getStringValue(float value)
MRVIEWER_API Color getColor(float val)
static MRVIEWER_API const std::vector< Color > & GreenRedColors()
simpler palette colors: from green to red
MRVIEWER_API void setCustomLabels(const std::vector< Label > &labels)
float getRangeMin() const
returns minimum value in the palette's range
Definition MRPalette.h:129
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 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:28
MRVIEWER_API void resetLabels()
float getRangeMax() const
returns maximum value in the palette's range
Definition MRPalette.h:132
static const std::vector< Color > & BlueGreenRedColors()
Definition MRPalette.h:29
UVCoord getUVcoord(float val, bool valid=true) const
Definition MRPalette.h:104
MRVIEWER_API void setLabelsVisible(bool visible)
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:7
constexpr T sqr(T x) noexcept
Definition MRMesh/MRMeshFwd.h:613
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
FilterType
Definition MRMesh/MRMeshFwd.h:592
Vector2f UVCoord
Definition MRMesh/MRMeshFwd.h:370
Expected< void > VoidOrErrStr
return type for a void function that can produce an error string
Definition MRExpected.h:60
Definition MRColor.h:9
Definition MRMeshTexture.h:13
Definition MRPalette.h:67
MRVIEWER_API Label(float val, std::string text)
std::string text
Definition MRPalette.h:69
Definition MRPalette.h:120
std::vector< Color > baseColors
Definition MRPalette.h:122
int discretization
Definition MRPalette.h:123
std::vector< float > ranges
Definition MRPalette.h:121