ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ccColorScale.cpp
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 #include <pybind11/pybind11.h>
9 #include <pybind11/stl.h>
10 #include <pybind11/stl_bind.h>
11 
12 #include <ecvColorScale.h>
13 
14 #include <QColor>
15 
16 #include "../casters.h"
17 
18 namespace py = pybind11;
19 using namespace pybind11::literals;
20 
21 void define_ccColorScale(py::module &m)
22 {
23  py::class_<ccColorScaleElement>(m, "ccColorScaleElement")
24  .def(py::init<>())
25  .def(py::init<double, const QColor &>(), "relativePos"_a, "color"_a)
26  .def("setRelativePos", &ccColorScaleElement::setRelativePos, "pos"_a)
27  .def("getRelativePos", &ccColorScaleElement::getRelativePos)
28  .def("setColor", &ccColorScaleElement::setColor, "color"_a)
29  .def("getColor", &ccColorScaleElement::getColor)
30  .def_static("IsSmaller", &ccColorScaleElement::IsSmaller, "e1"_a, "e2"_a);
31 
32  py::class_<ccColorScale> pyColorScale(m, "ccColorScale");
33  pyColorScale.def(py::init<const QString &, const QString &>(), "name"_a, "uuid"_a = QString())
34  .def_property_readonly_static("MIN_STEPS", [] { return ccColorScale::MIN_STEPS; })
35  .def_property_readonly_static("DEFAULT_STEPS", [] { return ccColorScale::DEFAULT_STEPS; })
36  .def_property_readonly_static("MAX_STEPS", [] { return ccColorScale::MAX_STEPS; })
37  .def("getName", &ccColorScale::getName)
38  .def("setName", &ccColorScale::setName, "name"_a)
39  .def("getUuid", &ccColorScale::getUuid)
40  .def("setUuid", &ccColorScale::setUuid, "uuid"_a)
41  .def("generateNewUuid", &ccColorScale::generateNewUuid)
42  .def("isRelative", &ccColorScale::isRelative)
43  .def("setRelative", &ccColorScale::setRelative)
44  .def("setAbsolute", &ccColorScale::setAbsolute, "minVal"_a, "maxVal"_a)
45  .def("setAbsolute", &ccColorScale::setAbsolute, "minVal"_a, "maxVal"_a)
46  .def("getAbsoluteBoundaries",
47  [](const ccColorScale &self)
48  {
49  double minVal, maxVal;
50  self.getAbsoluteBoundaries(minVal, maxVal);
51  return py::make_tuple(minVal, maxVal);
52  })
53  .def("isLocked", &ccColorScale::isLocked)
54  .def("setLocked", &ccColorScale::setLocked, "state"_a)
55  .def("setCustomLabels", &ccColorScale::setCustomLabels, "labels"_a)
56  .def("stepCount", &ccColorScale::stepCount)
57  .def("step",
58  static_cast<ccColorScaleElement &(ccColorScale::*)(int)>(&ccColorScale::step),
59  "index"_a)
60  .def("insert", &ccColorScale::insert, "step"_a, "autoUpdate"_a = true)
61  .def("remove", &ccColorScale::remove, "index"_a, "autoUpdate"_a = true)
62  .def("clear", &ccColorScale::clear)
63  .def("update", &ccColorScale::update)
64  .def("getRelativePosition", &ccColorScale::getRelativePosition, "value"_a)
65  .def("getColorByValue",
67  "value"_a,
68  "outOfRangeColor"_a = nullptr)
69  .def("getColorByRelativePos",
70  static_cast<const ecvColor::Rgb *(ccColorScale::*)(double, const ecvColor::Rgb *)
72  "relativePos"_a,
73  "outOfRangeColor"_a = nullptr)
74  .def("getColorByRelativePos",
75  static_cast<const ecvColor::Rgb *(
76  ccColorScale::*)(double, unsigned, const ecvColor::Rgb *) const>(
78  "relativePos"_a,
79  "steps"_a,
80  "outOfRangeColor"_a = nullptr)
81  .def("getColorByIndex", &ccColorScale::getColorByIndex, "index"_a)
82  .def_static("LoadFromXML", &ccColorScale::LoadFromXML, "filename"_a)
83  .def("saveAsXML", &ccColorScale::saveAsXML, "filename"_a);
84 
85  py::class_<ccColorScale::Label>(pyColorScale, "Label")
86  .def_readwrite("value", &ccColorScale::Label::value)
87  .def_readwrite("text", &ccColorScale::Label::text, py::return_value_policy::reference)
88  .def(py::init<double>(), "value"_a)
89  .def(py::init<double, const QString &>(), "value"_a, "text"_a);
90 
91  pyColorScale
92  .def("customLabels",
93  static_cast<const std::set<ccColorScale::Label> &(ccColorScale::*)() const>(
95  .def("customLabels",
96  static_cast<std::set<ccColorScale::Label> &(ccColorScale::*)()>(
98 
99  // Thanks https://github.com/pybind/pybind11/issues/820#issuecomment-297894565
100  py::class_<ccColorScale::Shared>(pyColorScale, "Shared");
101 }
void define_ccColorScale(py::module &m)
Color scale element: one value + one color.
Definition: ecvColorScale.h:22
const QColor & getColor() const
Returns color.
Definition: ecvColorScale.h:43
double getRelativePos() const
Returns step position (relative to scale boundaries)
Definition: ecvColorScale.h:38
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
bool saveAsXML(QString filename) const
Saves this color scale as an XML file.
static const unsigned DEFAULT_STEPS
Default number of steps for display.
ccColorScaleElement & step(int index)
Access to a given step.
static Shared LoadFromXML(QString filename)
Loads a color scale from an XML file.
QString getUuid() const
Returns unique ID.
void setRelative()
Sets scale as relative.
LabelSet & customLabels()
Returns the list of custom labels (if any)
void setAbsolute(double minVal, double maxVal)
Sets scale as absolute.
void update()
Updates internal representation.
static const unsigned MIN_STEPS
Minimum number of steps.
Definition: ecvColorScale.h:99
void setLocked(bool state)
Sets whether scale is locked or not.
const QString & getName() const
Returns name.
const ecvColor::Rgb & getColorByIndex(unsigned index) const
Returns color by index.
bool isRelative() const
Returns whether scale is relative or absoute.
void remove(int index, bool autoUpdate=true)
Deletes a given step.
double getRelativePosition(double value) const
void insert(const ccColorScaleElement &step, bool autoUpdate=true)
Adds a step.
const ecvColor::Rgb * getColorByRelativePos(double relativePos, const ecvColor::Rgb *outOfRangeColor=nullptr) const
Returns color by relative position in scale.
void generateNewUuid()
Generates a new unique ID.
int stepCount() const
Returns the current number of steps.
void clear()
Clears all steps.
const ecvColor::Rgb * getColorByValue(double value, const ecvColor::Rgb *outOfRangeColor=nullptr) const
Returns color by value.
void setUuid(QString uuid)
Sets unique ID.
void setName(const QString &name)
Sets name.
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)
static const unsigned MAX_STEPS
Maximum number of steps (internal representation)
RGB color structure.
Definition: ecvColorTypes.h:49