Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
QmitkPiecewiseFunctionCanvas.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 QmitkPiecewiseFunctionCanvas_h
14 #define QmitkPiecewiseFunctionCanvas_h
15 
18 
19 #include <vtkPiecewiseFunction.h>
20 
31 {
32  Q_OBJECT
33 
34 public:
40  QmitkPiecewiseFunctionCanvas(QWidget *parent = nullptr, Qt::WindowFlags f = {});
41 
43  void paintEvent(QPaintEvent *e) override;
44 
49  void SetTitle(const QString &title);
50 
58  int GetNearHandle(int x, int y, unsigned int maxSquaredDistance = 100) override;
59 
64  void SetPiecewiseFunction(vtkPiecewiseFunction *piecewiseFunction)
65  {
66  this->m_PiecewiseFunction = piecewiseFunction;
67  this->SetMin(m_PiecewiseFunction->GetRange()[0]);
68  this->SetMax(m_PiecewiseFunction->GetRange()[1]);
69  setEnabled(true);
70 
71  update();
72  }
73 
80  int AddFunctionPoint(double x, double val) override { return m_PiecewiseFunction->AddPoint(x, val); }
81 
86  void RemoveFunctionPoint(double x) override
87  {
88  int old_size = GetFunctionSize();
89  m_PiecewiseFunction->RemovePoint(x);
90  if (GetFunctionSize() + 1 != old_size)
91  {
92  std::cout << "old/new size" << old_size << "/" << GetFunctionSize() << std::endl;
93  std::cout << "called with x=" << x << std::endl;
94  }
95  }
96 
102  double GetFunctionX(int index) override { return m_PiecewiseFunction->GetDataPointer()[index * 2]; }
103 
109  double GetFunctionY(int index) override
110  {
111  return m_PiecewiseFunction->GetValue(m_PiecewiseFunction->GetDataPointer()[index * 2]);
112  }
113 
118  int GetFunctionSize() override { return m_PiecewiseFunction->GetSize(); }
119 
121  void DoubleClickOnHandle(int) override {}
122 
128  void MoveFunctionPoint(int index, std::pair<double, double> pos) override;
129 
134  double GetFunctionMax() { return m_PiecewiseFunction->GetRange()[1]; }
135 
140  double GetFunctionMin() { return m_PiecewiseFunction->GetRange()[0]; }
141 
147  {
148  double range;
149  if ((m_PiecewiseFunction->GetRange()[0]) < 0)
150  {
151  range = (m_PiecewiseFunction->GetRange()[1]) - (m_PiecewiseFunction->GetRange()[0]);
152  return range;
153  }
154  else
155  {
156  range = m_PiecewiseFunction->GetRange()[1];
157  return range;
158  }
159  }
160 
163  {
164  m_PiecewiseFunction->AddSegment(this->GetFunctionMin(), 0, this->GetFunctionMax(), 1);
165  m_PiecewiseFunction->AddPoint(0.0, 0.0);
166  }
167 
169  void ResetGO()
170  { // Gradient Opacity
171  m_PiecewiseFunction->AddSegment(this->GetFunctionMin(), 0, 0, 1);
172  m_PiecewiseFunction->AddSegment(0, 1, ((this->GetFunctionRange()) * 0.125), 1);
173  m_PiecewiseFunction->AddSegment(((this->GetFunctionRange()) * 0.125), 1, ((this->GetFunctionRange()) * 0.2), 1);
174  m_PiecewiseFunction->AddSegment(((this->GetFunctionRange()) * 0.2), 1, ((this->GetFunctionRange()) * 0.25), 1);
175  }
176 
177 protected:
178  vtkPiecewiseFunction *m_PiecewiseFunction;
179  QString m_Title;
180 };
181 
182 #endif
#define MITKQTWIDGETSEXT_EXPORT
Canvas widget for editing a vtkPiecewiseFunction (opacity transfer function).
void SetTitle(const QString &title)
Set the title text displayed on the canvas.
void DoubleClickOnHandle(int) override
No-op for piecewise functions (double-click does not change values).
int GetFunctionSize() override
Get the number of points in the piecewise function.
double GetFunctionY(int index) override
Get the y (opacity) value of a function point by index.
double GetFunctionMax()
Get the maximum scalar value of the function range.
QmitkPiecewiseFunctionCanvas(QWidget *parent=nullptr, Qt::WindowFlags f={})
Construct the piecewise function canvas.
int GetNearHandle(int x, int y, unsigned int maxSquaredDistance=100) override
Find a handle near the given canvas position.
void RemoveFunctionPoint(double x) override
Remove the function point at position x.
void SetPiecewiseFunction(vtkPiecewiseFunction *piecewiseFunction)
Set the vtkPiecewiseFunction to edit.
double GetFunctionRange()
Get the span of the function range.
void RemoveAllFunctionPoints()
Reset the function to a default ramp from 0 to 1 with a zero-point at origin.
double GetFunctionX(int index) override
Get the x (scalar) value of a function point by index.
void MoveFunctionPoint(int index, std::pair< double, double > pos) override
Move a function point to a new position.
void paintEvent(QPaintEvent *e) override
Paint the histogram, function lines, handles, title, and range labels.
int AddFunctionPoint(double x, double val) override
Add a function point at position (x, val).
double GetFunctionMin()
Get the minimum scalar value of the function range.
void ResetGO()
Reset the function to a default gradient opacity configuration.
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.