Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTransferFunction.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkTransferFunction.h"
18 #include "mitkHistogramGenerator.h"
19 #include "mitkImageToItk.h"
20 
21 #include <itkRGBPixel.h>
22 
23 #include <vector>
24 
25 namespace mitk
26 {
27  TransferFunction::TransferFunction() : m_Min(0), m_Max(0)
28  {
32 
33  m_ScalarOpacityFunction->Initialize();
34  m_ScalarOpacityFunction->AddPoint(0, 1);
35 
36  m_GradientOpacityFunction->Initialize();
37  m_GradientOpacityFunction->AddPoint(0, 1);
38 
39  m_ColorTransferFunction->RemoveAllPoints();
40  m_ColorTransferFunction->SetColorSpaceToHSV();
41  m_ColorTransferFunction->AddRGBPoint(0, 1, 1, 1);
42  }
43 
45  : itk::Object(),
46  m_ScalarOpacityFunction(other.m_ScalarOpacityFunction.New()),
47  m_GradientOpacityFunction(other.m_GradientOpacityFunction.New()),
48  m_ColorTransferFunction(other.m_ColorTransferFunction.New()),
49  m_Min(other.m_Min),
50  m_Max(other.m_Max),
51  m_Histogram(other.m_Histogram),
52  m_ScalarOpacityPoints(other.m_ScalarOpacityPoints),
53  m_GradientOpacityPoints(other.m_GradientOpacityPoints),
54  m_RGBPoints(other.m_RGBPoints)
55  {
59  }
60 
63  {
64  if ((m_Min != other.m_Min) || (m_Max != other.m_Max))
65  return false;
66 
67  bool sizes = (m_ScalarOpacityFunction->GetSize() == other.m_ScalarOpacityFunction->GetSize()) &&
68  (m_GradientOpacityFunction->GetSize() == other.m_GradientOpacityFunction->GetSize()) &&
69  (m_ColorTransferFunction->GetSize() == other.m_ColorTransferFunction->GetSize());
70  if (sizes == false)
71  return false;
72 
73  for (int i = 0; i < m_ScalarOpacityFunction->GetSize(); i++)
74  {
75  double myVal[4];
76  double otherVal[4];
77  m_ScalarOpacityFunction->GetNodeValue(i, myVal);
78  other.m_ScalarOpacityFunction->GetNodeValue(i, otherVal);
79  bool equal = (myVal[0] == otherVal[0]) && (myVal[1] == otherVal[1]) && (myVal[2] == otherVal[2]) &&
80  (myVal[3] == otherVal[3]);
81  if (equal == false)
82  return false;
83  }
84  for (int i = 0; i < m_GradientOpacityFunction->GetSize(); i++)
85  {
86  double myVal[4];
87  double otherVal[4];
88  m_GradientOpacityFunction->GetNodeValue(i, myVal);
89  other.m_GradientOpacityFunction->GetNodeValue(i, otherVal);
90  bool equal = (myVal[0] == otherVal[0]) && (myVal[1] == otherVal[1]) && (myVal[2] == otherVal[2]) &&
91  (myVal[3] == otherVal[3]);
92  if (equal == false)
93  return false;
94  }
95  for (int i = 0; i < m_ColorTransferFunction->GetSize(); i++)
96  {
97  double myVal[6];
98  double otherVal[6];
99  m_ColorTransferFunction->GetNodeValue(i, myVal);
100  other.m_ColorTransferFunction->GetNodeValue(i, otherVal);
101  bool equal = (myVal[0] == otherVal[0]) // X
102  && (myVal[1] == otherVal[1]) // R
103  && (myVal[2] == otherVal[2]) // G
104  && (myVal[3] == otherVal[3]) // B
105  && (myVal[4] == otherVal[4]) // midpoint
106  && (myVal[5] == otherVal[5]); // sharpness
107  if (equal == false)
108  return false;
109  }
110  return true;
111  }
112 
114  {
115  m_ScalarOpacityFunction->RemoveAllPoints();
116  for (unsigned int i = 0; i <= points.size() - 1; i++)
117  {
118  this->AddScalarOpacityPoint(points[i].first, points[i].second);
119  }
120  }
121 
123  {
124  m_GradientOpacityFunction->RemoveAllPoints();
125  for (unsigned int i = 0; i <= points.size() - 1; i++)
126  {
127  this->AddGradientOpacityPoint(points[i].first, points[i].second);
128  }
129  }
130 
132  {
133  m_ColorTransferFunction->RemoveAllPoints();
134  for (unsigned int i = 0; i <= rgbpoints.size() - 1; i++)
135  {
136  this->AddRGBPoint(rgbpoints[i].first, rgbpoints[i].second[0], rgbpoints[i].second[1], rgbpoints[i].second[2]);
137  }
138  }
139 
140  void TransferFunction::AddScalarOpacityPoint(double x, double value) { m_ScalarOpacityFunction->AddPoint(x, value); }
141  void TransferFunction::AddGradientOpacityPoint(double x, double value)
142  {
143  m_GradientOpacityFunction->AddPoint(x, value);
144  }
145 
146  void TransferFunction::AddRGBPoint(double x, double r, double g, double b)
147  {
148  m_ColorTransferFunction->AddRGBPoint(x, r, g, b);
149  }
150 
152  {
153  // Retrieve data points from VTK transfer function and store them in a vector
154  m_ScalarOpacityPoints.clear();
155  double *data = m_ScalarOpacityFunction->GetDataPointer();
156  for (int i = 0; i < m_ScalarOpacityFunction->GetSize(); ++i)
157  {
158  m_ScalarOpacityPoints.push_back(std::make_pair(data[i * 2], data[i * 2 + 1]));
159  }
160 
161  return m_ScalarOpacityPoints;
162  }
163 
165  {
166  // Retrieve data points from VTK transfer function and store them in a vector
167  m_GradientOpacityPoints.clear();
168  double *data = m_GradientOpacityFunction->GetDataPointer();
169  for (int i = 0; i < m_GradientOpacityFunction->GetSize(); ++i)
170  {
171  m_GradientOpacityPoints.push_back(std::make_pair(data[i * 2], data[i * 2 + 1]));
172  }
173 
174  return m_GradientOpacityPoints;
175  }
176 
178  {
179  // Retrieve data points from VTK transfer function and store them in a vector
180  m_RGBPoints.clear();
181  double *data = m_ColorTransferFunction->GetDataPointer();
182  for (int i = 0; i < m_ColorTransferFunction->GetSize(); ++i)
183  {
184  double rgb[] = {data[i * 4 + 1], data[i * 4 + 2], data[i * 4 + 3]};
185  m_RGBPoints.push_back(std::make_pair(data[i * 4], rgb));
186  }
187 
188  return m_RGBPoints;
189  }
190 
191  int TransferFunction::RemoveScalarOpacityPoint(double x) { return m_ScalarOpacityFunction->RemovePoint(x); }
193  int TransferFunction::RemoveRGBPoint(double x) { return m_ColorTransferFunction->RemovePoint(x); }
197  void TransferFunction::InitializeByItkHistogram(const itk::Statistics::Histogram<double> *histogram)
198  {
199  m_Histogram = histogram;
200  m_Min = (int)GetHistogram()->GetBinMin(0, 0);
201  m_Max = (int)GetHistogram()->GetBinMax(0, GetHistogram()->Size() - 1);
202 
203  /*
204  m_ScalarOpacityFunction->Initialize();
205  m_ScalarOpacityFunction->AddPoint(m_Min,0.0);
206  m_ScalarOpacityFunction->AddPoint(0.0,0.0);
207  m_ScalarOpacityFunction->AddPoint(m_Max,1.0);
208  m_GradientOpacityFunction->Initialize();
209  m_GradientOpacityFunction->AddPoint(m_Min,0.0);
210  m_GradientOpacityFunction->AddPoint(0.0,1.0);
211  m_GradientOpacityFunction->AddPoint((m_Max*0.125),1.0);
212  m_GradientOpacityFunction->AddPoint((m_Max*0.2),1.0);
213  m_GradientOpacityFunction->AddPoint((m_Max*0.25),1.0);
214  m_GradientOpacityFunction->AddPoint(m_Max,1.0);
215  m_ColorTransferFunction->RemoveAllPoints();
216  m_ColorTransferFunction->AddRGBPoint(m_Min,1,0,0);
217  m_ColorTransferFunction->AddRGBPoint(m_Max,1,1,0);
218  m_ColorTransferFunction->SetColorSpaceToHSV();
219  MITK_INFO << "min/max in tf-c'tor:" << m_Min << "/" << m_Max << std::endl;
220  */
221  }
222 
224  {
226  histGen->SetImage(image);
227  histGen->SetSize(256);
228  histGen->ComputeHistogram();
229  m_Histogram = histGen->GetHistogram();
230  m_Min = (int)GetHistogram()->GetBinMin(0, 0);
231  m_Max = (int)GetHistogram()->GetBinMax(0, GetHistogram()->Size() - 1);
232  m_ScalarOpacityFunction->Initialize();
233  m_ScalarOpacityFunction->AddPoint(m_Min, 0.0);
234  m_ScalarOpacityFunction->AddPoint(0.0, 0.0);
235  m_ScalarOpacityFunction->AddPoint(m_Max, 1.0);
236  m_GradientOpacityFunction->Initialize();
237  m_GradientOpacityFunction->AddPoint(m_Min, 0.0);
238  m_GradientOpacityFunction->AddPoint(0.0, 1.0);
239  m_GradientOpacityFunction->AddPoint((m_Max * 0.125), 1.0);
240  m_GradientOpacityFunction->AddPoint((m_Max * 0.2), 1.0);
241  m_GradientOpacityFunction->AddPoint((m_Max * 0.25), 1.0);
242  m_GradientOpacityFunction->AddPoint(m_Max, 1.0);
243  m_ColorTransferFunction->RemoveAllPoints();
244  m_ColorTransferFunction->AddRGBPoint(m_Min, 1, 0, 0);
245  m_ColorTransferFunction->AddRGBPoint(m_Max, 1, 1, 0);
246  m_ColorTransferFunction->SetColorSpaceToHSV();
247  // MITK_INFO << "min/max in tf-c'tor:" << m_Min << "/" << m_Max << std::endl;
248  }
249 
251  {
253  histGen->SetImage(image);
254  histGen->SetSize(256);
255  histGen->ComputeHistogram();
256  m_Histogram = histGen->GetHistogram();
257  m_Min = (int)GetHistogram()->GetBinMin(0, 0);
258  m_Max = (int)GetHistogram()->GetBinMax(0, GetHistogram()->Size() - 1);
259  }
260 
261  void TransferFunction::PrintSelf(std::ostream &os, itk::Indent indent) const
262  {
263  os << indent << "ScalarOpacity: ";
264  m_ScalarOpacityFunction->PrintHeader(os, vtkIndent());
265  os << indent << "GradientOpacity: ";
266  m_GradientOpacityFunction->PrintHeader(os, vtkIndent());
267  os << indent << "ColorTransfer: ";
268  m_ColorTransferFunction->PrintHeader(os, vtkIndent());
269  os << indent << "Min: " << m_Min << ", Max: " << m_Max << std::endl;
270  }
271 
273  {
274  itk::LightObject::Pointer result(new Self(*this));
275  result->UnRegister();
276  return result;
277  }
278 
279 } // namespace
void InitializeByMitkImage(const mitk::Image *image)
Initialize transfer function based on the histogram of an mitk::Image.
void ClearGradientOpacityPoints()
Removes all control points from the gradient opacity transfer function.
vtkSmartPointer< vtkColorTransferFunction > m_ColorTransferFunction
void SetGradientOpacityPoints(TransferFunction::ControlPoints points)
Insert control points and values into the gradient opacity transfer function.
itk::SmartPointer< Self > Pointer
virtual itk::LightObject::Pointer InternalClone() const override
TransferFunction::RGBControlPoints & GetRGBPoints()
Get a copy of the color transfer function control-points.
vtkSmartPointer< vtkPiecewiseFunction > m_GradientOpacityFunction
void InitializeByItkHistogram(const itk::Statistics::Histogram< double > *histogram)
Initialize transfer function based on the specified histogram.
The TransferFunction class A wrapper class for VTK scalar opacity, gradient opacity, and color transfer functions.Holds a copy of each of the three standard VTK transfer functions (scalar opacity, gradient opacity, color) and provides an interface for manipulating their control points. Each original function can be retrieved by a Get() method.
DataCollection - Class to facilitate loading/accessing structured data.
void ClearScalarOpacityPoints()
Removes all control points from the scalar opacity transfer function.
void SetScalarOpacityPoints(TransferFunction::ControlPoints points)
Insert control points and values into the scalar opacity transfer function.
std::vector< std::pair< double, itk::RGBPixel< double > > > RGBControlPoints
void AddRGBPoint(double x, double r, double g, double b)
Add a single control point to the color opacity transfer function.
int RemoveScalarOpacityPoint(double x)
Remove the specified control point from the scalar opacity transfer function.
void SetRGBPoints(TransferFunction::RGBControlPoints rgbpoints)
Insert control points and RGB values into the color transfer function.
mitk::HistogramGenerator::HistogramType::ConstPointer m_Histogram
TransferFunction::ControlPoints & GetScalarOpacityPoints()
Get a copy of the scalar opacity transfer function control-points.
void ClearRGBPoints()
Removes all control points from the color transfer function.
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:76
vtkSmartPointer< vtkPiecewiseFunction > m_ScalarOpacityFunction
int RemoveRGBPoint(double x)
Remove the specified control point from the color transfer function.
void InitializeHistogram(const mitk::Image *image)
Initialize the internal histogram and min/max range based on the specified mitk::Image.
std::vector< std::pair< double, double > > ControlPoints
TransferFunction::ControlPoints & GetGradientOpacityPoints()
Get a copy of the gradient opacity transfer function control-points.
void AddScalarOpacityPoint(double x, double value)
Add a single control point to the scalar opacity transfer function.
virtual const HistogramGenerator::HistogramType * GetHistogram()
Get histogram used for transfer function initialization.
void AddGradientOpacityPoint(double x, double value)
Add a single control point to the gradient opacity transfer function.
void PrintSelf(std::ostream &os, itk::Indent indent) const override
int RemoveGradientOpacityPoint(double x)
Remove the specified control point from the gradient opacity transfer function.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.