OGS
VtkColorLookupTable Class Reference

Detailed Description

Calculates and stores a colour lookup table.

Based on a start colour and an end colour, RGB-values are interpolated and stored in vector of GeoLib::Color. If no colours are set, default values are used for start (blue) and end (red). The number of entries of the colour table can be set in the constructor, the default value is 256. If additional colours are inserted into the table using setColor() the interpolation will be calculated iteratively between set colour values. Interpolation can be linear (default) or exponential. Based on the set range of values, colour values can be retrieved using getColor().

Definition at line 23 of file VtkColorLookupTable.h.

#include <VtkColorLookupTable.h>

Inheritance diagram for VtkColorLookupTable:
[legend]
Collaboration diagram for VtkColorLookupTable:
[legend]

Public Member Functions

 vtkTypeMacro (VtkColorLookupTable, vtkLookupTable)
void Build () override
 Builds the colour table based on the previously set parameters. This method should only be called after all options have been set.
void setColor (double pos, DataHolderLib::Color const &color)
void getColor (vtkIdType index, unsigned char rgba[4]) const
DataHolderLib::LUTType getInterpolationType () const
 Returns the type of interpolation used.
void setInterpolationType (DataHolderLib::LUTType type)
 Sets the type of interpolation.
void setLookupTable (DataHolderLib::ColorLookupTable const &lut)
 Imports settings of OGS lookup table class.
void writeToFile (const std::string &filename)
 Exports a color table to a file.
void SetTableValueRGBA (vtkIdType idx, unsigned char rgba[4])
 Set a value within the LUT.
void GetTableValue (vtkIdType idx, unsigned char rgba[4])
 Get a value from the LUT.

Static Public Member Functions

static VtkColorLookupTableNew ()
 Create new objects with New() because of VTKs object reference counting.

Static Public Attributes

static const int DEFAULTMINVALUE = -9999
static const int DEFAULTMAXVALUE = 9999

Protected Member Functions

 VtkColorLookupTable ()
 Constructor.
 ~VtkColorLookupTable () override
 Destructor.

Private Member Functions

unsigned char linInterpolation (unsigned char a, unsigned char b, double p) const
 Interpolates values linearly.
unsigned char expInterpolation (unsigned char a, unsigned char b, double gamma, double p) const
 Interpolates values exponentially. gamma should roughly be in [0,4), for gamma=1 interpolation is linear.

Private Attributes

std::map< double, unsigned char * > _dict
DataHolderLib::LUTType _type {DataHolderLib::LUTType::LINEAR}

Constructor & Destructor Documentation

◆ VtkColorLookupTable()

VtkColorLookupTable::VtkColorLookupTable ( )
protecteddefault

Constructor.

Referenced by New(), and vtkTypeMacro().

◆ ~VtkColorLookupTable()

VtkColorLookupTable::~VtkColorLookupTable ( )
overrideprotected

Destructor.

Definition at line 18 of file VtkColorLookupTable.cpp.

19{
20 for (auto& it : _dict)
21 {
22 delete it.second;
23 }
24}
std::map< double, unsigned char * > _dict

References _dict.

Member Function Documentation

◆ Build()

void VtkColorLookupTable::Build ( )
override

Builds the colour table based on the previously set parameters. This method should only be called after all options have been set.

Definition at line 42 of file VtkColorLookupTable.cpp.

43{
44 double range[2];
45 this->GetTableRange(range);
46 const double interval = range[1] - range[0];
47 this->SetNumberOfTableValues(static_cast<vtkIdType>(ceil(interval)) + 1);
48 // const vtkIdType nColours = this->GetNumberOfTableValues();
49 if (!_dict.empty())
50 {
51 // make sure that color map starts with the first color in the
52 // dictionary
53 unsigned char startcolor[4] = {0, 0, 0, 0};
54 std::pair<std::size_t, unsigned char*> lastValue(0, startcolor);
55
56 for (auto& it : _dict)
57 {
58 double val = (it.first < range[0])
59 ? range[0]
60 : ((it.first > range[1]) ? range[1] : it.first);
61 auto const nextIndex =
62 static_cast<std::size_t>(std::floor(val - range[0]));
63
64 this->SetTableValueRGBA(nextIndex, it.second);
65
66 if (nextIndex - lastValue.first > 0)
67 {
68 for (std::size_t i = lastValue.first + 1; i < nextIndex; i++)
69 {
70 unsigned char int_rgba[4];
71 double pos =
72 (i - lastValue.first) /
73 (static_cast<double>(nextIndex - lastValue.first));
74
76 {
77 for (std::size_t j = 0; j < 4; j++)
78 {
79 int_rgba[j] = linInterpolation(
80 (lastValue.second)[j], (it.second)[j], pos);
81 }
82 }
84 {
85 for (std::size_t j = 0; j < 4; j++)
86 {
87 int_rgba[j] =
88 expInterpolation((lastValue.second)[j],
89 (it.second)[j], 0.2, pos);
90 }
91 }
92 else
93 { // no interpolation
94 for (std::size_t j = 0; j < 4; j++)
95 {
96 int_rgba[j] = (lastValue.second)[j];
97 }
98 }
99
100 this->SetTableValueRGBA(i, int_rgba);
101 }
102 }
103
104 lastValue.first = nextIndex;
105 lastValue.second = it.second;
106 }
107 }
108 else
109 {
110 vtkLookupTable::Build();
111 }
112}
DataHolderLib::LUTType _type
unsigned char expInterpolation(unsigned char a, unsigned char b, double gamma, double p) const
Interpolates values exponentially. gamma should roughly be in [0,4), for gamma=1 interpolation is lin...
void SetTableValueRGBA(vtkIdType idx, unsigned char rgba[4])
Set a value within the LUT.
unsigned char linInterpolation(unsigned char a, unsigned char b, double p) const
Interpolates values linearly.

References _dict, _type, expInterpolation(), DataHolderLib::EXPONENTIAL, DataHolderLib::LINEAR, linInterpolation(), and SetTableValueRGBA().

Referenced by VtkCompositeElementSelectionFilter::GetLookupTable(), VtkCompositeColorByHeightFilter::init(), and setLookupTable().

◆ expInterpolation()

unsigned char VtkColorLookupTable::expInterpolation ( unsigned char a,
unsigned char b,
double gamma,
double p ) const
private

Interpolates values exponentially. gamma should roughly be in [0,4), for gamma=1 interpolation is linear.

Definition at line 33 of file VtkColorLookupTable.cpp.

37{
38 assert(gamma > 0 && gamma < 4);
39 return static_cast<unsigned char>((b - a) * pow(p, gamma) + a);
40}

Referenced by Build().

◆ getColor()

void VtkColorLookupTable::getColor ( vtkIdType index,
unsigned char rgba[4] ) const

Definition at line 182 of file VtkColorLookupTable.cpp.

183{
184 index = ((index < this->TableRange[0])
185 ? static_cast<vtkIdType>(this->TableRange[0])
186 : (index >= this->TableRange[1]
187 ? static_cast<vtkIdType>(this->TableRange[1]) - 1
188 : index));
189 index = static_cast<std::size_t>(std::floor(
190 (index - this->TableRange[0]) *
191 (this->NumberOfColors / (this->TableRange[1] - this->TableRange[0]))));
192
193 unsigned char* _rgba;
194 _rgba = this->Table->GetPointer(index * 4);
195 for (std::size_t i = 0; i < 4; i++)
196 {
197 rgba[i] = _rgba[i];
198 }
199}

◆ getInterpolationType()

DataHolderLib::LUTType VtkColorLookupTable::getInterpolationType ( ) const
inline

Returns the type of interpolation used.

Definition at line 52 of file VtkColorLookupTable.h.

52{ return _type; }

References _type.

◆ GetTableValue()

void VtkColorLookupTable::GetTableValue ( vtkIdType idx,
unsigned char rgba[4] )

Get a value from the LUT.

Definition at line 160 of file VtkColorLookupTable.cpp.

161{
162 double value[4];
163 vtkLookupTable::GetTableValue(idx, value);
164
165 for (unsigned i = 0; i < 4; ++i)
166 {
167 rgba[i] = static_cast<unsigned char>(value[i] * 255.0);
168 }
169}

Referenced by writeToFile().

◆ linInterpolation()

unsigned char VtkColorLookupTable::linInterpolation ( unsigned char a,
unsigned char b,
double p ) const
private

Interpolates values linearly.

Definition at line 26 of file VtkColorLookupTable.cpp.

29{
30 return static_cast<unsigned char>(a * (1 - p) + b * p);
31}

Referenced by Build().

◆ New()

VtkColorLookupTable * VtkColorLookupTable::New ( )
static

Create new objects with New() because of VTKs object reference counting.

References VtkColorLookupTable().

Referenced by VtkCompositeElementSelectionFilter::GetLookupTable(), and VtkAlgorithmProperties::SetLookUpTable().

◆ setColor()

void VtkColorLookupTable::setColor ( double pos,
DataHolderLib::Color const & color )

Definition at line 171 of file VtkColorLookupTable.cpp.

173{
174 auto* dict_rgba = new unsigned char[4];
175 for (std::size_t i = 0; i < 4; i++)
176 {
177 dict_rgba[i] = color[i];
178 }
179 _dict.insert(std::pair<double, unsigned char*>(pos, dict_rgba));
180}

References _dict.

Referenced by VtkCompositeElementSelectionFilter::GetLookupTable(), VtkCompositeColorByHeightFilter::init(), and setLookupTable().

◆ setInterpolationType()

void VtkColorLookupTable::setInterpolationType ( DataHolderLib::LUTType type)
inline

Sets the type of interpolation.

Definition at line 55 of file VtkColorLookupTable.h.

55{ _type = type; }

References _type.

Referenced by VtkCompositeColorByHeightFilter::init(), and setLookupTable().

◆ setLookupTable()

void VtkColorLookupTable::setLookupTable ( DataHolderLib::ColorLookupTable const & lut)

Imports settings of OGS lookup table class.

Definition at line 114 of file VtkColorLookupTable.cpp.

116{
117 std::size_t const n_colors(lut.size());
118 for (std::size_t i = 0; i < n_colors; ++i)
119 {
120 setColor(std::get<0>(lut[i]), std::get<1>(lut[i]));
121 }
122 setInterpolationType(lut.getInterpolationType());
123 auto const range(lut.getTableRange());
124 SetTableRange(range.first, range.second);
125 Build();
126}
void setColor(double pos, DataHolderLib::Color const &color)
void Build() override
Builds the colour table based on the previously set parameters. This method should only be called aft...
void setInterpolationType(DataHolderLib::LUTType type)
Sets the type of interpolation.

References Build(), DataHolderLib::ColorLookupTable::getInterpolationType(), DataHolderLib::ColorLookupTable::getTableRange(), setColor(), setInterpolationType(), and DataHolderLib::ColorLookupTable::size().

Referenced by VtkAlgorithmProperties::SetLookUpTable().

◆ SetTableValueRGBA()

void VtkColorLookupTable::SetTableValueRGBA ( vtkIdType idx,
unsigned char rgba[4] )

Set a value within the LUT.

Definition at line 148 of file VtkColorLookupTable.cpp.

150{
151 double value[4];
152
153 for (unsigned i = 0; i < 4; ++i)
154 {
155 value[i] = rgba[i] / 255.0;
156 }
157 vtkLookupTable::SetTableValue(idx, value);
158}

Referenced by Build().

◆ vtkTypeMacro()

VtkColorLookupTable::vtkTypeMacro ( VtkColorLookupTable ,
vtkLookupTable  )

References VtkColorLookupTable().

◆ writeToFile()

void VtkColorLookupTable::writeToFile ( const std::string & filename)

Exports a color table to a file.

Definition at line 128 of file VtkColorLookupTable.cpp.

129{
130 std::stringstream strout;
131 strout << "Writing color table to " << filename << " ... ";
132 std::ofstream out(filename.c_str(), std::ios::out);
133
134 std::size_t nColors = this->GetNumberOfTableValues();
135 for (std::size_t i = 0; i < nColors; i++)
136 {
137 unsigned char rgba[4];
138 this->GetTableValue(i, rgba);
139 out << i << "\t" << rgba[0] << "\t" << rgba[1] << "\t" << rgba[2]
140 << "\n";
141 }
142
143 strout << " done." << std::endl;
144 INFO("{:s}", strout.str());
145 out.close();
146}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void GetTableValue(vtkIdType idx, unsigned char rgba[4])
Get a value from the LUT.

References GetTableValue(), and INFO().

Member Data Documentation

◆ _dict

std::map<double, unsigned char*> VtkColorLookupTable::_dict
private

Definition at line 83 of file VtkColorLookupTable.h.

Referenced by ~VtkColorLookupTable(), Build(), and setColor().

◆ _type

◆ DEFAULTMAXVALUE

const int VtkColorLookupTable::DEFAULTMAXVALUE = 9999
static

Definition at line 27 of file VtkColorLookupTable.h.

◆ DEFAULTMINVALUE

const int VtkColorLookupTable::DEFAULTMINVALUE = -9999
static

Definition at line 26 of file VtkColorLookupTable.h.


The documentation for this class was generated from the following files: