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 "MRMesh/MRVector.h"
11#include <algorithm>
12#include <filesystem>
13
14namespace Json{ class Value; }
15
16namespace MR
17{
18
27{
28public:
30 MRVIEWER_API static const std::vector<Color> DefaultColors;
31 [[nodiscard]] static const std::vector<Color> & BlueGreenRedColors() { return DefaultColors; }
32
34 [[nodiscard]] MRVIEWER_API static const std::vector<Color> & GreenRedColors();
35
36 MRVIEWER_API Palette( const std::vector<Color>& colors );
43 MRVIEWER_API void setBaseColors( const std::vector<Color>& colors );
48 MRVIEWER_API void setRangeMinMax( float min, float max );
54 MRVIEWER_API void setRangeMinMaxNegPos( float minNeg, float maxNeg, float minPos, float maxPos );
55
56 // set number of different colors for discrete palette
57 MRVIEWER_API void setDiscretizationNumber( int discretization );
58 // set palette type (linear / discrete)
59 MRVIEWER_API void setFilterType( FilterType type );
60
65 MRVIEWER_API void draw( const std::string& windowName, const ImVec2& pose, const ImVec2& size, bool onlyTopHalf = false );
66
72 MRVIEWER_API void draw( ImDrawList* drawList, float scaling, const ImVec2& pos, const ImVec2& size, const Color& labelBgColor, bool onlyTopHalf = false ) const;
73 MRVIEWER_API void draw( ImDrawList* drawList, float scaling, const ImVec2& pos, const ImVec2& size, bool onlyTopHalf = false ) const;
74
75 // structure for label
76 struct MRVIEWER_CLASS Label
77 {
78 float value = 0.f; // label position according normal scale (from Min to Max)
79 std::string text; // label text
80
81 // The special zero label.
82 // The flag is there so we can hide it if we want.
83 bool isZero = false;
84
85 Label() = default;
86 MRVIEWER_API Label( float val, std::string text );
87 };
88 // reset labels to standard view
89 MRVIEWER_API void resetLabels();
90 // set labels manually
91 MRVIEWER_API void setCustomLabels( const std::vector<Label>& labels );
92 // set labels visible
93 MRVIEWER_API void setLabelsVisible( bool visible );
94
95
96 // Setup palette data from JsonValue
97 // @return return true if loading successful
98 MRVIEWER_API bool loadFromJson( const Json::Value& root );
99 // Serialize this palette data to JsonValue
100 MRVIEWER_API void saveCurrentToJson( Json::Value& root ) const;
101
102
107 MRVIEWER_API Color getColor( float relativeValue ) const;
109 Color getInvalidColor() const { return Color::gray(); }
110
114 MRVIEWER_API VertColors getVertColors( const VertScalars& values, const VertBitSet& region, const VertBitSet* valids, const VertBitSet* validsForStats );
115
116 const MeshTexture& getTexture() const { return texture_; };
117
118 // get relative position in [0,1], where 0 is for minimum and 1 is for maximum
119 MRVIEWER_API float getRelativePos( float val ) const;
120
123 UVCoord getUVcoord( float val, bool valid = true ) const
124 {
125 return {
126 ( texEnd_ - texStart_ ) * getRelativePos( val ) + texStart_,
127 valid ? 0.25f : 0.75f
128 };
129 }
130
131 // If `bits` is non-zero, captures the pointer and returns a predicate that checks against this bitset.
132 // Otherwise returns null.
133 [[nodiscard]] MRVIEWER_API static VertPredicate predFromBitSet( const VertBitSet* bits );
134
139 MRVIEWER_API VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertPredicate & valids = {}, const VertPredicate & validsForStats = {} );
140
141 VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertBitSet * valids, const VertBitSet * validsForStats = nullptr )
142 {
143 return getUVcoords( values, region, predFromBitSet( valids ), predFromBitSet( validsForStats ) );
144 }
145
146 // base parameters of palette
148 {
149 std::vector<float> ranges = { 0.f, 1.f }; // range limits for palette
150 std::vector<Color> baseColors; // palette base colors (need for calculate real colors according discretization)
151 MinMaxf legendLimits; // if valid - limit legend range
152 int discretization = 7; // number of different colors for discrete palette
153 };
154
155 [[nodiscard]] const Parameters& getParameters() const { return parameters_; }
156
158 [[nodiscard]] float getRangeMin() const { return parameters_.ranges.front(); }
159
161 [[nodiscard]] float getRangeMax() const { return parameters_.ranges.back(); }
162
164 [[nodiscard]] float getRangeSq() const { return std::max( sqr( getRangeMin() ), sqr( getRangeMax() ) ); }
165
167 MRVIEWER_API std::string getStringValue( float value ) const;
169 MRVIEWER_API int getMaxLabelCount();
171 MRVIEWER_API void setMaxLabelCount( int val );
172
174 MRVIEWER_API void setLegendLimits( const MinMaxf& limits );
175
176
177 // Histogram:
178
179 [[nodiscard]] bool isHistogramEnabled() const { return getNumHistogramBuckets() != 0; }
180 [[nodiscard]] MRVIEWER_API int getNumHistogramBuckets() const;
181 // Pass zero to disable the histogram. Pass `getDefaultNumHistogramBuckets()` or any other number to enable it.
182 // This should probably be odd, to have a bucket for zero in the middle.
183 MRVIEWER_API void setNumHistogramBuckets( int n );
184 // Returns the recommended argument for `setNumHistogramBuckets()`.
185 [[nodiscard]] MRVIEWER_API int getDefaultNumHistogramBuckets() const;
186
187
188 // Should we maintain the percentages of distances in each discretization step?
189 [[nodiscard]] bool isDiscretizationPercentagesEnabled() const { return enableHistogramDiscr_; }
190 void enableDiscretizationPercentages( bool enable ) { histogramDiscr_.reset(); enableHistogramDiscr_ = enable; }
191
192
193 // This is called automatically by `getValidVerts()` and `getUVcoords(), so usually you don't need to call this manually.
194 // Call this after `setNumHistogramBuckets()`.
195 MRVIEWER_API void updateStats( const VertScalars& values, const VertBitSet& region, const VertPredicate& vertPredicate );
196
199 MRVIEWER_API std::vector<Label> createUniformLabels() const;
200
202 {
203 // If this is empty, the histogram is disabled.
204 std::vector<int> buckets;
205 // The buckets for out-of-range elements.
207 int afterBucket = 0;
208 // The sum of all values in `buckets` and `{low,high}Bucket`.
209 int numEntries = 0;
210 // The max value in `buckets` (but ignoring `{low,high}Bucket`).
211 int maxEntry = 0;
212
213 // `reset()` sets this to true, and `finalize()` sets this to false.
214 bool needsUpdate = true;
215
216 MRVIEWER_API void reset();
217 MRVIEWER_API void addValue( float value );
218 // Call once after all `addValue()` calls.
219 MRVIEWER_API void finalize();
220 };
221
222 // The normal histogram, if enabled (check with `isHistogramEnabled()`).
223 [[nodiscard]] const Histogram &getHistogramValues() { return histogram_; }
224 // This one has the size matching `getParameters().discretization`. Only has meaningful values if enabled, check with `isDiscretizationPercentagesEnabled()`.
225 [[nodiscard]] const Histogram &getDiscrHistogramValues() const { return histogramDiscr_; }
226
227private:
228 void setRangeLimits_( const std::vector<float>& ranges );
229
230 void updateDiscretizatedColors_();
231 Color getBaseColor_( float val );
232
233 // What color we assume the pallete is drawn on top of. Typically should be the viewport background color.
234 const Color& getBackgroundColor_() const;
235
236
237 // set labels with equal distance between
238 void setUniformLabels_();
239 // make labels with equal distance between
240 void makeUniformLabels_( std::vector<Label>& labels ) const;
241 // first label is equal to min value, last - to the max val
242 // don't use with MinMaxNegPos mode
243 void setZeroCentredLabels_();
244
245 void updateCustomLabels_();
246
247 void sortLabels_( std::vector<Label>& labels ) const;
248
249 void updateLegendLimits_( const MinMaxf& limits );
250 void updateLegendLimitIndexes_();
251
252 std::vector<Label> customLabels_;
253 std::vector<Label> labels_;
254 bool showLabels_ = false;
255
259 const char* getAdjustedLabelText_( std::size_t labelIndex, bool onlyTopHalf, std::string& storage ) const;
260
262 float getMaxLabelWidth_( bool onlyTopHalf = false ) const;
263
264 struct StyleVariables
265 {
266 // Top-left window padding.
267 ImVec2 windowPaddingA;
268 // Bottom-right window padding.
269 ImVec2 windowPaddingB;
270 // Spacing between the labels and the colored rect.
271 float labelToColoredRectSpacing {};
272 // The min width of the colored rect.
273 float minColoredRectWidth {};
274 };
275 StyleVariables getStyleVariables_( float scaling ) const;
276
277 // stores OpenGL textures. Change useDiscrete_ to switch between them
278 MeshTexture texture_;
279
280 // texture positions of min and max values
281 float texStart_ = 0, texEnd_ = 1;
282
283 Parameters parameters_;
284
285 bool isWindowOpen_ = false;
286
287 bool useCustomLabels_ = false;
288
289 int maxLabelCount_ = 0;
290
291 float prevMaxLabelWidth_ = 0.0f;
292
293 MinMaxi legendLimitIndexes_ = { 0, 7 };
294 MinMaxf relativeLimits_ = { 0.f, 1.f };
295
296 // This one is of a user-defined size.
297 Histogram histogram_;
298
299 // This one has size matching `parameters_.discretization`.
300 Histogram histogramDiscr_;
301
302 // Whether we should actually update `histogramDiscr_`.
303 bool enableHistogramDiscr_ = false;
304
305 static void resizeCallback_( ImGuiSizeCallbackData* data );
306};
307
310{
311public:
313 MRVIEWER_API static const std::vector<std::string>& getPresetNames();
316 MRVIEWER_API static bool loadPreset( const std::string& name, Palette& palette );
318 MRVIEWER_API static Expected<void> savePreset( const std::string& name, const Palette& palette );
320 MRVIEWER_API static std::filesystem::path getPalettePresetsFolder();
321private:
323 ~PalettePresets() = default;
324
325 std::vector<std::string> names_;
326
327 void update_();
328
329 static PalettePresets& instance_();
330};
331
332}
Class to save and load user palette presets.
Definition MRPalette.h:310
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:27
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:164
MRVIEWER_API int getMaxLabelCount()
returns maximal label count
const MeshTexture & getTexture() const
Definition MRPalette.h:116
MRVIEWER_API Palette(const std::vector< Color > &colors)
MRVIEWER_API int getNumHistogramBuckets() const
MRVIEWER_API bool loadFromJson(const Json::Value &root)
MRVIEWER_API void setFilterType(FilterType type)
MRVIEWER_API void updateStats(const VertScalars &values, const VertBitSet &region, const VertPredicate &vertPredicate)
MRVIEWER_API void draw(ImDrawList *drawList, float scaling, const ImVec2 &pos, const ImVec2 &size, const Color &labelBgColor, bool onlyTopHalf=false) const
const Parameters & getParameters() const
Definition MRPalette.h:155
MRVIEWER_API std::vector< Label > createUniformLabels() const
MRVIEWER_API void draw(ImDrawList *drawList, float scaling, const ImVec2 &pos, const ImVec2 &size, bool onlyTopHalf=false) const
bool isDiscretizationPercentagesEnabled() const
Definition MRPalette.h:189
MRVIEWER_API VertUVCoords getUVcoords(const VertScalars &values, const VertBitSet &region, const VertPredicate &valids={}, const VertPredicate &validsForStats={})
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:158
bool isHistogramEnabled() const
Definition MRPalette.h:179
void enableDiscretizationPercentages(bool enable)
Definition MRPalette.h:190
MRVIEWER_API void setMaxLabelCount(int val)
sets maximal label count
MRVIEWER_API void setNumHistogramBuckets(int n)
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 int getDefaultNumHistogramBuckets() 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:30
VertUVCoords getUVcoords(const VertScalars &values, const VertBitSet &region, const VertBitSet *valids, const VertBitSet *validsForStats=nullptr)
Definition MRPalette.h:141
const Histogram & getHistogramValues()
Definition MRPalette.h:223
MRVIEWER_API void resetLabels()
float getRangeMax() const
returns maximum value in the palette's range
Definition MRPalette.h:161
const Histogram & getDiscrHistogramValues() const
Definition MRPalette.h:225
static const std::vector< Color > & BlueGreenRedColors()
Definition MRPalette.h:31
static MRVIEWER_API VertPredicate predFromBitSet(const VertBitSet *bits)
UVCoord getUVcoord(float val, bool valid=true) const
Definition MRPalette.h:123
MRVIEWER_API void setLabelsVisible(bool visible)
Color getInvalidColor() const
return invalid color
Definition MRPalette.h:109
MRVIEWER_API VertColors getVertColors(const VertScalars &values, const VertBitSet &region, const VertBitSet *valids, const VertBitSet *validsForStats)
MRVIEWER_API std::string getStringValue(float value) const
returns formated string for this value of palette
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:753
MinMax< float > MinMaxf
Definition MRMesh/MRMeshFwd.h:411
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:29
FilterType
Definition MRMesh/MRMeshFwd.h:731
tl::expected< T, E > Expected
Definition MRExpected.h:25
Vector2f UVCoord
Definition MRMesh/MRMeshFwd.h:505
MinMax< int > MinMaxi
Definition MRMesh/MRMeshFwd.h:413
Box given by its min- and max- corners.
Definition MRMesh/MRBox.h:26
Definition MRMesh/MRColor.h:9
static constexpr Color gray() noexcept
Definition MRMesh/MRColor.h:27
Definition MRMeshTexture.h:13
Definition MRPalette.h:202
int numEntries
Definition MRPalette.h:209
bool needsUpdate
Definition MRPalette.h:214
std::vector< int > buckets
Definition MRPalette.h:204
int afterBucket
Definition MRPalette.h:207
MRVIEWER_API void reset()
MRVIEWER_API void finalize()
int maxEntry
Definition MRPalette.h:211
int beforeBucket
Definition MRPalette.h:206
MRVIEWER_API void addValue(float value)
Definition MRPalette.h:77
MRVIEWER_API Label(float val, std::string text)
std::string text
Definition MRPalette.h:79
Definition MRPalette.h:148
std::vector< Color > baseColors
Definition MRPalette.h:150
int discretization
Definition MRPalette.h:152
std::vector< float > ranges
Definition MRPalette.h:149
MinMaxf legendLimits
Definition MRPalette.h:151