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{
20
21
30{
31public:
33 MRVIEWER_API static const std::vector<Color> DefaultColors;
34 [[nodiscard]] static const std::vector<Color> & BlueGreenRedColors() { return DefaultColors; }
35
37 [[nodiscard]] MRVIEWER_API static const std::vector<Color> & GreenRedColors();
38
39 MRVIEWER_API Palette( const std::vector<Color>& colors );
46 MRVIEWER_API void setBaseColors( const std::vector<Color>& colors );
51 MRVIEWER_API void setRangeMinMax( float min, float max );
57 MRVIEWER_API void setRangeMinMaxNegPos( float minNeg, float maxNeg, float minPos, float maxPos );
58
60 MRVIEWER_API void setDiscretizationNumber( int discretization );
62 MRVIEWER_API void setFilterType( FilterType type );
63
68 MRVIEWER_API void draw( const std::string& windowName, const ImVec2& pose, const ImVec2& size, bool onlyTopHalf = false );
69
75 MRVIEWER_API void draw( ImDrawList* drawList, float scaling, const ImVec2& pos, const ImVec2& size, const Color& labelBgColor, bool onlyTopHalf = false ) const;
76 MRVIEWER_API void draw( ImDrawList* drawList, float scaling, const ImVec2& pos, const ImVec2& size, bool onlyTopHalf = false ) const;
77
79 struct MRVIEWER_CLASS Label
80 {
81 float value = 0.f;
82 std::string text;
83
86 bool isZero = false;
87
88 Label() = default;
89 MRVIEWER_API Label( float val, std::string text );
90 };
92 MRVIEWER_API void resetLabels();
94 MRVIEWER_API void setCustomLabels( const std::vector<Label>& labels );
96 MRVIEWER_API void setLabelsVisible( bool visible );
97
98
101 MRVIEWER_API bool loadFromJson( const Json::Value& root );
103 MRVIEWER_API void saveCurrentToJson( Json::Value& root ) const;
104
105
110 MRVIEWER_API Color getColor( float relativeValue ) const;
112 Color getInvalidColor() const { return Color::gray(); }
113
117 MRVIEWER_API VertColors getVertColors( const VertScalars& values, const VertBitSet& region, const VertBitSet* valids, const VertBitSet* validsForStats );
118
119 const MeshTexture& getTexture() const { return texture_; };
120
122 MRVIEWER_API float getRelativePos( float val ) const;
123
126 UVCoord getUVcoord( float val, bool valid = true ) const
127 {
128 return {
129 ( texEnd_ - texStart_ ) * getRelativePos( val ) + texStart_,
130 valid ? 0.25f : 0.75f
131 };
132 }
133
136 [[nodiscard]] MRVIEWER_API static VertPredicate predFromBitSet( const VertBitSet* bits );
137
142 MRVIEWER_API VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertPredicate & valids = {}, const VertPredicate & validsForStats = {} );
143
144 VertUVCoords getUVcoords( const VertScalars & values, const VertBitSet & region, const VertBitSet * valids, const VertBitSet * validsForStats = nullptr )
145 {
146 return getUVcoords( values, region, predFromBitSet( valids ), predFromBitSet( validsForStats ) );
147 }
148
151 {
152 std::vector<float> ranges = { 0.f, 1.f };
153 std::vector<Color> baseColors;
154 MinMaxf legendLimits;
156 };
157
158 [[nodiscard]] const Parameters& getParameters() const { return parameters_; }
159
161 [[nodiscard]] float getRangeMin() const { return parameters_.ranges.front(); }
162
164 [[nodiscard]] float getRangeMax() const { return parameters_.ranges.back(); }
165
167 [[nodiscard]] float getRangeSq() const { return std::max( sqr( getRangeMin() ), sqr( getRangeMax() ) ); }
168
170 MRVIEWER_API std::string getStringValue( float value ) const;
172 MRVIEWER_API int getMaxLabelCount();
174 MRVIEWER_API void setMaxLabelCount( int val );
175
177 MRVIEWER_API void setLegendLimits( const MinMaxf& limits );
178
179
181
182 [[nodiscard]] bool isHistogramEnabled() const { return getNumHistogramBuckets() != 0; }
183 [[nodiscard]] MRVIEWER_API int getNumHistogramBuckets() const;
186 MRVIEWER_API void setNumHistogramBuckets( int n );
188 [[nodiscard]] MRVIEWER_API int getDefaultNumHistogramBuckets() const;
189
190
192 [[nodiscard]] bool isDiscretizationPercentagesEnabled() const { return enableHistogramDiscr_; }
193 void enableDiscretizationPercentages( bool enable ) { histogramDiscr_.reset(); enableHistogramDiscr_ = enable; }
194
195
198 MRVIEWER_API void updateStats( const VertScalars& values, const VertBitSet& region, const VertPredicate& vertPredicate );
199
202 MRVIEWER_API std::vector<Label> createUniformLabels() const;
203
204 struct Histogram
205 {
207 std::vector<int> buckets;
209 int beforeBucket = 0;
210 int afterBucket = 0;
212 int numEntries = 0;
214 int maxEntry = 0;
215
217 bool needsUpdate = true;
218
219 MRVIEWER_API void reset();
220 MRVIEWER_API void addValue( float value );
222 MRVIEWER_API void finalize();
223 };
224
226 [[nodiscard]] const Histogram &getHistogramValues() { return histogram_; }
228 [[nodiscard]] const Histogram &getDiscrHistogramValues() const { return histogramDiscr_; }
229
232 MRVIEWER_API void setDrawDelayFrames( int numFrames );
233
234private:
235 void setRangeLimits_( const std::vector<float>& ranges );
236
237 void updateDiscretizatedColors_();
238 Color getBaseColor_( float val );
239
241 const Color& getBackgroundColor_() const;
242
243
245 void setUniformLabels_();
247 void makeUniformLabels_( std::vector<Label>& labels ) const;
250 void setZeroCentredLabels_();
251
252 void updateCustomLabels_();
253
254 void sortLabels_( std::vector<Label>& labels ) const;
255
256 void updateLegendLimits_( const MinMaxf& limits );
257 void updateLegendLimitIndexes_();
258
259 std::vector<Label> customLabels_;
260 std::vector<Label> labels_;
261 bool showLabels_ = false;
262
266 const char* getAdjustedLabelText_( std::size_t labelIndex, bool onlyTopHalf, std::string& storage ) const;
267
269 float getMaxLabelWidth_( bool onlyTopHalf = false ) const;
270
271 struct StyleVariables
272 {
274 ImVec2 windowPaddingA;
276 ImVec2 windowPaddingB;
278 float labelToColoredRectSpacing {};
280 float minColoredRectWidth {};
281 };
282 StyleVariables getStyleVariables_( float scaling ) const;
283
285 MeshTexture texture_;
286
288 float texStart_ = 0, texEnd_ = 1;
289
290 Parameters parameters_;
291
292 bool isWindowOpen_ = false;
293
294 bool useCustomLabels_ = false;
295
296 int maxLabelCount_ = 0;
297
298 float prevMaxLabelWidth_ = 0.0f;
299
300 MinMaxi legendLimitIndexes_ = { 0, 7 };
301 MinMaxf relativeLimits_ = { 0.f, 1.f };
302
304 Histogram histogram_;
305
307 Histogram histogramDiscr_;
308
310 bool enableHistogramDiscr_ = false;
311
314
315 static void resizeCallback_( ImGuiSizeCallbackData* data );
316};
317
320{
321public:
323 MRVIEWER_API static const std::vector<std::string>& getPresetNames();
326 MRVIEWER_API static bool loadPreset( const std::string& name, Palette& palette );
328 MRVIEWER_API static Expected<void> savePreset( const std::string& name, const Palette& palette );
330 MRVIEWER_API static std::filesystem::path getPalettePresetsFolder();
331private:
333 ~PalettePresets() = default;
334
335 std::vector<std::string> names_;
336
337 void update_();
338
339 static PalettePresets& instance_();
340};
341
342}
Definition MRHistogram.h:13
Class to save and load user palette presets.
Definition MRPalette.h:320
Class to hold one dimension texture with value to UV mapping.
Definition MRPalette.h:30
static MRVIEWER_API Expected< void > savePreset(const std::string &name, const Palette &palette)
saves given palette to preset with given name
MRVIEWER_API float getRelativePos(float val) const
get relative position in [0,1], where 0 is for minimum and 1 is for maximum
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:167
std::vector< Color > baseColors
range limits for palette
Definition MRPalette.h:153
MRVIEWER_API int getMaxLabelCount()
returns maximal label count
const MeshTexture & getTexture() const
Definition MRPalette.h:119
MRVIEWER_API Palette(const std::vector< Color > &colors)
MRVIEWER_API int getNumHistogramBuckets() const
static MRVIEWER_API std::filesystem::path getPalettePresetsFolder()
returns path to presets folder
MRVIEWER_API bool loadFromJson(const Json::Value &root)
MRVIEWER_API void setFilterType(FilterType type)
set palette type (linear / discrete)
static void resizeCallback_(ImGuiSizeCallbackData *data)
int discretization
if valid - limit legend range
Definition MRPalette.h:155
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
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:158
MRVIEWER_API void draw(ImDrawList *drawList, float scaling, const ImVec2 &pos, const ImVec2 &size, bool onlyTopHalf=false) const
bool isDiscretizationPercentagesEnabled() const
Should we maintain the percentages of distances in each discretization step?
Definition MRPalette.h:192
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)
set labels manually
int drawDelayFrames_
Definition MRPalette.h:313
MRVIEWER_API void setLegendLimits(const MinMaxf &limits)
set legend limits. if min > max - limits are disabled
FilterType
Definition MREnums.h:12
float getRangeMin() const
returns minimum value in the palette's range
Definition MRPalette.h:161
tl::expected< T, E > Expected
Definition MRExpected.h:31
bool isHistogramEnabled() const
Histogram:
Definition MRPalette.h:182
void enableDiscretizationPercentages(bool enable)
Definition MRPalette.h:193
MRVIEWER_API void setMaxLabelCount(int val)
sets maximal label count
MRVIEWER_API void setNumHistogramBuckets(int n)
MRVIEWER_API void saveCurrentToJson(Json::Value &root) const
Serialize this palette data to JsonValue.
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
Returns the recommended argument for setNumHistogramBuckets().
MRVIEWER_API void setDiscretizationNumber(int discretization)
set number of different colors for discrete palette
MRVIEWER_API Color getColor(float relativeValue) const
std::vector< float > ranges
Definition MRPalette.h:152
MRVIEWER_API Label(float val, std::string text)
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:33
VertUVCoords getUVcoords(const VertScalars &values, const VertBitSet &region, const VertBitSet *valids, const VertBitSet *validsForStats=nullptr)
Definition MRPalette.h:144
static constexpr Color gray() noexcept
Definition MRColor.h:30
static MRVIEWER_API bool loadPreset(const std::string &name, Palette &palette)
MRVIEWER_API void resetLabels()
reset labels to standard view
float getRangeMax() const
returns maximum value in the palette's range
Definition MRPalette.h:164
static const std::vector< Color > & BlueGreenRedColors()
Definition MRPalette.h:34
static MRVIEWER_API VertPredicate predFromBitSet(const VertBitSet *bits)
UVCoord getUVcoord(float val, bool valid=true) const
Definition MRPalette.h:126
MRVIEWER_API void setLabelsVisible(bool visible)
set labels visible
std::string text
label position according normal scale (from Min to Max)
Definition MRPalette.h:82
Color getInvalidColor() const
return invalid color
Definition MRPalette.h:112
MRVIEWER_API VertColors getVertColors(const VertScalars &values, const VertBitSet &region, const VertBitSet *valids, const VertBitSet *validsForStats)
MinMaxf legendLimits
palette base colors (need for calculate real colors according discretization)
Definition MRPalette.h:154
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 ...
static MRVIEWER_API const std::vector< std::string > & getPresetNames()
gets names of existing presets
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRColor.h:12
Definition MRMeshTexture.h:13
structure for label
Definition MRPalette.h:80
base parameters of palette
Definition MRPalette.h:151