Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkSimpleUnstructuredGridHistogram.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 (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 
14 
15 #include <mitkUnstructuredGrid.h>
16 
17 #include <itkSampleToHistogramFilter.h>
18 #include <vtkDataArray.h>
19 #include <vtkPointData.h>
20 #include <vtkUnstructuredGrid.h>
21 
22 namespace mitk
23 {
25  : m_UGHistogram(HistogramType::New()), m_InvMaxFrequency(1)
26  {
27  // MITK_INFO << "####### Created a SimpleUnstructuredGridHistogram";
28  }
29 
30  double SimpleUnstructuredGridHistogram::GetMin() const { return m_UGHistogram->GetBinMin(0, 0); }
32  {
33  return m_UGHistogram->GetBinMax(0, m_UGHistogram->GetSize(0) - 1);
34  }
35 
37  {
38  auto *grid = dynamic_cast<UnstructuredGrid *>(source);
39  // m_UGHistogram->Initialize(grid);
40 
41  vtkUnstructuredGrid *vtkUGrid = grid->GetVtkUnstructuredGrid();
42 
43  ListSampleType::Pointer listSample = ListSampleType::New();
44  listSample->SetMeasurementVectorSize(1);
45 
46  MeasurementVectorType v;
47 
48  MeasurementVectorType lowerBound;
49  MeasurementVectorType upperBound;
50 
51  int numberOfBins = 1;
52  HistogramType::SizeType size(1);
53 
54  vtkDataArray *data;
55  /*if (m_UsePointData)*/ data = vtkUGrid->GetPointData()->GetScalars();
56  // else data = vtkUGrid->GetCellData()->GetScalars();
57 
58  if (data == nullptr)
59  {
60  listSample->Resize(1);
61  v[0] = 0;
62  listSample->PushBack(v);
63  lowerBound[0] = 0;
64  upperBound[0] = 0;
65  size.Fill(numberOfBins);
66  }
67  else
68  {
69  listSample->Resize(data->GetNumberOfTuples());
70  for (vtkIdType i = 0; i < data->GetNumberOfTuples(); ++i)
71  {
72  v[0] = data->GetComponent(i, 0);
73  // if (v[0] != 0) MITK_INFO << "ug scalar: " << v[0];
74  listSample->PushBack(v);
75  }
76 
77  vtkIdType numberOfTuples = data->GetNumberOfTuples();
78 
79  if (numberOfTuples < 1000)
80  numberOfBins = 250;
81  else if (numberOfTuples < 30000)
82  numberOfBins = 100;
83  else if (numberOfTuples < 100000)
84  numberOfBins = 50;
85  else
86  numberOfBins = 20;
87 
88  size.Fill(numberOfBins);
89 
90  double range[2];
91  data->GetRange(range);
92  lowerBound[0] = range[0];
93  upperBound[0] = range[1];
94  }
95 
96  typedef itk::Statistics::SampleToHistogramFilter<ListSampleType, HistogramType> FilterType;
97  FilterType::Pointer histoFilter = FilterType::New();
98 
99  FilterType::HistogramMeasurementVectorType binMin(1);
100  FilterType::HistogramMeasurementVectorType binMax(1);
101 
102  binMin[0] = lowerBound[0];
103  binMax[0] = upperBound[0];
104 
105  histoFilter->SetInput(listSample);
106  histoFilter->SetHistogramSize(size);
107  histoFilter->SetHistogramBinMinimum(binMin);
108  histoFilter->SetHistogramBinMaximum(binMax);
109  histoFilter->Update();
110 
111  m_UGHistogram = histoFilter->GetOutput();
112 
113  m_BinSize = (GetMax() - GetMin()) / (double)numberOfBins;
114 
115  m_Mins = m_UGHistogram->GetMins();
116  m_Maxs = m_UGHistogram->GetMaxs();
117 
118  HistogramType::AbsoluteFrequencyType maxFrequency = 0;
119  HistogramType::SizeValueType histoSize = m_UGHistogram->GetSize(0);
120  for (HistogramType::SizeValueType i = 0; i < histoSize; ++i)
121  {
122  HistogramType::AbsoluteFrequencyType f = m_UGHistogram->GetFrequency(i);
123  if (f > maxFrequency)
124  {
125  maxFrequency = f;
126  }
127  // MITK_INFO << "Bin #" << i << ": " << m_UGHistogram->GetMeasurement(i,0);
128  }
129 
130  if (maxFrequency)
131  {
132  m_InvMaxFrequency = 1.0 / log((double)maxFrequency);
133  }
134 
135  // MITK_INFO << "UGHistogramm size: " << m_UGHistogram->GetSize(0) << ", maxF: " << maxFrequency
136  // << " min count: " << m_Mins.size() << " max count: " << m_Maxs.size();
137  }
138 
139  float SimpleUnstructuredGridHistogram::GetRelativeBin(double start, double end) const
140  {
141  // MITK_INFO << "GetRelativeBin start: " << start << ", end: " << end;
142  HistogramType::AbsoluteFrequencyType maxf = 0;
143  for (double pos = start; pos < end; pos += m_BinSize)
144  {
145  HistogramType::AbsoluteFrequencyType f = m_UGHistogram->GetFrequency(m_UGHistogram->GetIndex(pos));
146  if (f > maxf)
147  maxf = f;
148  }
149 
150  return log(static_cast<double>(maxf)) * m_InvMaxFrequency;
151  }
152 }
double GetMax() const override
Returns the maximum value of the histogram.
Base of all data objects.
Definition: mitkBaseData.h:37
DataCollection - Class to facilitate loading/accessing structured data.
virtual vtkUnstructuredGrid * GetVtkUnstructuredGrid(unsigned int t=0)
void ComputeFromBaseData(BaseData *source) override
Creates a new histogram out the source.
double GetMin() const override
Returns the minimal value of the histogram.
Class for storing unstructured grids (vtkUnstructuredGrid)
float GetRelativeBin(double start, double end) const override
TODO: (What should this method do?)