ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvColorScale.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Local
11 #include "ecvColorTypes.h"
12 #include "ecvSerializableObject.h"
13 
14 // Qt
15 #include <QList>
16 #include <QSharedPointer>
17 
18 // System
19 #include <set>
20 
23 public:
26 
28  ccColorScaleElement(double relativePos, QColor color)
29  : m_relativePos(relativePos), m_color(color) {}
30 
32 
34  inline void setRelativePos(double pos) { m_relativePos = pos; }
36 
38  inline double getRelativePos() const { return m_relativePos; }
39 
41  inline void setColor(QColor color) { m_color = color; }
43  inline const QColor& getColor() const { return m_color; }
44 
46  inline static bool IsSmaller(const ccColorScaleElement& e1,
47  const ccColorScaleElement& e2) {
48  return e1.getRelativePos() < e2.getRelativePos();
49  }
50 
51 protected:
53 
55  double m_relativePos;
57  QColor m_color;
58 };
59 
61 
72 public:
74  typedef QSharedPointer<ccColorScale> Shared;
75 
77 
79  static ccColorScale::Shared Create(const QString& name);
80 
82 
88  ccColorScale(const QString& name, const QString& uuid = QString());
89 
91  virtual ~ccColorScale();
92 
94  ccColorScale::Shared copy(const QString& uuid = QString()) const;
95 
97 
99  static const unsigned MIN_STEPS = 2;
100 
102 
104  static const unsigned DEFAULT_STEPS = 256;
105 
107 
109  static const unsigned MAX_STEPS = 1024;
110 
112  inline const QString& getName() const { return m_name; }
114  void setName(const QString& name) { m_name = name; }
115 
117  QString getUuid() const { return m_uuid; }
119  void setUuid(QString uuid) { m_uuid = uuid; }
121  void generateNewUuid();
122 
124 
126  inline bool isRelative() const { return m_relative; }
127 
129  inline void setRelative() { m_relative = true; }
130 
132  void setAbsolute(double minVal, double maxVal);
133 
135 
137  void getAbsoluteBoundaries(double& minVal, double& maxVal) const;
138 
140  inline bool isLocked() const { return m_locked; }
141 
143  inline void setLocked(bool state) { m_locked = state; }
144 
146  struct Label {
147  Label(double v) : value(v) {}
148 
149  Label(double v, const QString& t) : value(v), text(t) {}
150 
151  double value = 0.0;
152  QString text;
153 
154  bool operator<(const Label& otherLabel) const {
155  return value < otherLabel.value;
156  }
157  };
158 
160  using LabelSet = std::set<Label>;
161 
163  inline LabelSet& customLabels() { return m_customLabels; }
165  inline const LabelSet& customLabels() const { return m_customLabels; }
166 
168 
170  inline void setCustomLabels(const LabelSet& labels) {
171  m_customLabels = labels;
172  }
173 
175 
177  inline int stepCount() const { return m_steps.size(); }
178 
180  inline ccColorScaleElement& step(int index) { return m_steps[index]; }
181 
183  inline const ccColorScaleElement& step(int index) const {
184  return m_steps[index];
185  }
186 
188 
190  void insert(const ccColorScaleElement& step, bool autoUpdate = true);
191 
193 
196  void remove(int index, bool autoUpdate = true);
197 
199 
202  void clear();
203 
205 
208  void update();
209 
212 
215  inline double getRelativePosition(double value) const {
216  assert(m_updated && !m_relative);
217  return (value - m_absoluteMinValue) / m_absoluteRange;
218  }
219 
221 
227  double value,
228  const ecvColor::Rgb* outOfRangeColor = nullptr) const {
229  assert(m_updated && !m_relative);
230  double relativePos = getRelativePosition(value);
231  return (relativePos >= 0.0 && relativePos <= 1.0
232  ? getColorByRelativePos(relativePos)
233  : outOfRangeColor);
234  }
235 
237 
242  double relativePos,
243  const ecvColor::Rgb* outOfRangeColor = nullptr) const {
244  assert(m_updated);
245  if (relativePos >= 0.0 && relativePos <= 1.0)
246  return &getColorByIndex(
247  static_cast<unsigned>(relativePos * (MAX_STEPS - 1)));
248  else
249  return outOfRangeColor;
250  }
251 
253 
259  double relativePos,
260  unsigned steps,
261  const ecvColor::Rgb* outOfRangeColor = nullptr) const {
262  assert(m_updated);
263  if (relativePos >= 0.0 && relativePos <= 1.0) {
264  // quantized (16 bits) version --> much faster than floor!
265  unsigned index =
266  (static_cast<unsigned>((relativePos * steps) * 65535.0)) >>
267  16;
268  return &getColorByIndex((index * (MAX_STEPS - 1)) / steps);
269  } else {
270  return outOfRangeColor;
271  }
272  }
273 
275 
278  inline const ecvColor::Rgb& getColorByIndex(unsigned index) const {
279  assert(m_updated && index < MAX_STEPS);
280  return m_rgbaScale[index];
281  }
282 
284  bool saveAsXML(QString filename) const;
286  static Shared LoadFromXML(QString filename);
287 
288  // inherited from ccSerializableObject
289  bool isSerializable() const override { return true; }
290  bool toFile(QFile& out, short dataVersion) const override;
291  short minimumFileVersion() const override;
292  bool fromFile(QFile& in,
293  short dataVersion,
294  int flags,
295  LoadedIDMap& oldToNewIDMap) override;
296 
297 protected:
299  void sort();
300 
302  QString m_name;
303 
305  QString m_uuid;
306 
308  QList<ccColorScaleElement> m_steps;
309 
311  ecvColor::Rgb m_rgbaScale[MAX_STEPS];
312 
314  bool m_updated;
315 
318 
320  bool m_locked;
321 
323 
327 
329 
333 
336 };
std::string filename
#define CV_DB_LIB_API
Definition: CV_db.h:15
std::string name
math::float4 color
bool copy
Definition: VtkUtils.cpp:74
Color scale element: one value + one color.
Definition: ecvColorScale.h:22
const QColor & getColor() const
Returns color.
Definition: ecvColorScale.h:43
ccColorScaleElement()
Default constructor.
Definition: ecvColorScale.h:25
double m_relativePos
Step (relative) position.
Definition: ecvColorScale.h:55
ccColorScaleElement(double relativePos, QColor color)
Constructor from a (relative) position and a color.
Definition: ecvColorScale.h:28
double getRelativePos() const
Returns step position (relative to scale boundaries)
Definition: ecvColorScale.h:38
QColor m_color
Color.
Definition: ecvColorScale.h:57
static bool IsSmaller(const ccColorScaleElement &e1, const ccColorScaleElement &e2)
Comparison operator between two color scale elements.
Definition: ecvColorScale.h:46
void setRelativePos(double pos)
Sets associated value (relative to scale boundaries)
Definition: ecvColorScale.h:34
void setColor(QColor color)
Sets color.
Definition: ecvColorScale.h:41
Color scale.
Definition: ecvColorScale.h:71
ccColorScaleElement & step(int index)
Access to a given step.
QString getUuid() const
Returns unique ID.
void setRelative()
Sets scale as relative.
LabelSet & customLabels()
Returns the list of custom labels (if any)
void setLocked(bool state)
Sets whether scale is locked or not.
QList< ccColorScaleElement > m_steps
Elements.
const QString & getName() const
Returns name.
bool m_relative
Whether scale is relative or not.
const ecvColor::Rgb & getColorByIndex(unsigned index) const
Returns color by index.
bool isRelative() const
Returns whether scale is relative or absoute.
bool m_locked
Whether scale is locked or not.
double getRelativePosition(double value) const
bool isSerializable() const override
Returns whether object is serializable of not.
const LabelSet & customLabels() const
Returns the list of custom labels (if any - const version)
const ecvColor::Rgb * getColorByRelativePos(double relativePos, const ecvColor::Rgb *outOfRangeColor=nullptr) const
Returns color by relative position in scale.
double m_absoluteMinValue
'Absolute' minimum value
double m_absoluteRange
'Absolute' range
int stepCount() const
Returns the current number of steps.
const ccColorScaleElement & step(int index) const
Access to a given step (const)
const ecvColor::Rgb * getColorByRelativePos(double relativePos, unsigned steps, const ecvColor::Rgb *outOfRangeColor=nullptr) const
Returns color by relative position in scale with a given 'resolution'.
QSharedPointer< ccColorScale > Shared
Shared pointer type.
Definition: ecvColorScale.h:74
QString m_uuid
Unique ID.
QString m_name
Name.
const ecvColor::Rgb * getColorByValue(double value, const ecvColor::Rgb *outOfRangeColor=nullptr) const
Returns color by value.
void setUuid(QString uuid)
Sets unique ID.
LabelSet m_customLabels
List of custom labels.
void setName(const QString &name)
Sets name.
std::set< Label > LabelSet
Type of a list of custom labels.
bool m_updated
Internal representation validity.
bool isLocked() const
Returns whether scale is locked or not.
void setCustomLabels(const LabelSet &labels)
Sets the list of custom labels (only if the scale is absolute)
Serializable object interface.
virtual short minimumFileVersion() const =0
Returns the minimum file version required to save this instance.
virtual bool toFile(QFile &out, short dataVersion) const
Saves data to binary stream.
virtual bool fromFile(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap)
Loads data from binary stream.
RGB color structure.
Definition: ecvColorTypes.h:49
double e2[36]
constexpr Rgb black(0, 0, 0)
Color scale label (value + optional text)
Label(double v, const QString &t)
bool operator<(const Label &otherLabel) const