Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
QmitkColorTransferFunctionCanvas.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef QmitkColorTransferFunctionCanvas_h
14 #define QmitkColorTransferFunctionCanvas_h
15 
18 
19 #include <vtkColorTransferFunction.h>
20 
31 {
32  Q_OBJECT
33 
34 public:
40  QmitkColorTransferFunctionCanvas(QWidget *parent = nullptr, Qt::WindowFlags f = {});
41 
43  void paintEvent(QPaintEvent *e) override;
44 
52  int GetNearHandle(int x, int y, unsigned int maxSquaredDistance = 100) override;
53 
58  void SetTitle(const QString &title);
59 
64  void SetColorTransferFunction(vtkColorTransferFunction *colorTransferFunction)
65  {
66  this->m_ColorTransferFunction = colorTransferFunction;
67  this->SetMin(colorTransferFunction->GetRange()[0]);
68  this->SetMax(colorTransferFunction->GetRange()[1]);
69  setEnabled(true);
70  update();
71  }
72 
79  int AddFunctionPoint(double x, double val) override
80  {
81  (void)val;
82  return m_ColorTransferFunction->AddRGBPoint(x,
83  m_ColorTransferFunction->GetRedValue(x),
84  m_ColorTransferFunction->GetGreenValue(x),
85  m_ColorTransferFunction->GetBlueValue(x));
86  }
87 
92  void RemoveFunctionPoint(double x) override
93  {
94  int old_size = GetFunctionSize();
95  m_ColorTransferFunction->RemovePoint(x);
96  if (GetFunctionSize() + 1 != old_size)
97  {
98  std::cout << "old/new size" << old_size << "/" << GetFunctionSize() << std::endl;
99  std::cout << "called with x=" << x << std::endl;
100  }
101  }
102 
108  double GetFunctionX(int index) override { return m_ColorTransferFunction->GetDataPointer()[index * 4]; }
109 
114  int GetFunctionSize() override { return m_ColorTransferFunction->GetSize(); }
115 
120  void DoubleClickOnHandle(int handle) override;
121 
127  void MoveFunctionPoint(int index, std::pair<double, double> pos) override;
128 
136  void AddRGB(double x, double r, double g, double b);
137 
142  double GetFunctionMax() { return m_ColorTransferFunction->GetRange()[1]; }
143 
148  double GetFunctionMin() { return m_ColorTransferFunction->GetRange()[0]; }
149 
155  {
156  double range;
157  if ((m_ColorTransferFunction->GetRange()[0]) == 0)
158  {
159  range = m_ColorTransferFunction->GetRange()[1];
160  return range;
161  }
162  else
163  {
164  range = (m_ColorTransferFunction->GetRange()[1]) - (m_ColorTransferFunction->GetRange()[0]);
165  return range;
166  }
167  }
168 
171  {
172  m_ColorTransferFunction->AddRGBSegment(this->GetFunctionMin(), 1, 0, 0, this->GetFunctionMax(), 1, 1, 0);
173  }
174 
179  double GetFunctionY(int /*index*/) override { return 0.0; }
180 protected:
181  vtkColorTransferFunction *m_ColorTransferFunction;
182  QString m_Title;
183 };
184 
185 #endif
#define MITKQTWIDGETSEXT_EXPORT
Canvas widget for editing a vtkColorTransferFunction.
QmitkColorTransferFunctionCanvas(QWidget *parent=nullptr, Qt::WindowFlags f={})
Construct the color transfer function canvas.
int GetFunctionSize() override
Get the number of points in the color transfer function.
int GetNearHandle(int x, int y, unsigned int maxSquaredDistance=100) override
Find a handle near the given canvas position.
void SetTitle(const QString &title)
Set the title text displayed on the canvas.
void DoubleClickOnHandle(int handle) override
Open a QColorDialog to change the color at the given handle.
void RemoveFunctionPoint(double x) override
Remove the color function point at position x.
double GetFunctionY(int) override
Always returns 0.0 since color transfer functions have no y component.
void MoveFunctionPoint(int index, std::pair< double, double > pos) override
Move a function point to a new position, preserving its color.
double GetFunctionX(int index) override
Get the x (scalar) value of a function point by index.
int AddFunctionPoint(double x, double val) override
Add a color function point at position x, preserving the existing color at x.
double GetFunctionMin()
Get the minimum scalar value of the function range.
double GetFunctionRange()
Get the span of the function range (max - min).
void AddRGB(double x, double r, double g, double b)
Add an RGB color point to the transfer function.
void RemoveAllFunctionPoints()
Reset the function to a default red-to-yellow gradient.
double GetFunctionMax()
Get the maximum scalar value of the function range.
void paintEvent(QPaintEvent *e) override
Paint the color gradient, handles, title, and min/max labels.
void SetColorTransferFunction(vtkColorTransferFunction *colorTransferFunction)
Set the vtkColorTransferFunction to edit.
Abstract base class for transfer function editing canvases.
void SetMin(double min)
Set the minimum scalar value and update the lower display bound.
virtual int GetFunctionSize()=0
Get the number of function points.
void SetMax(double max)
Set the maximum scalar value and update the upper display bound.