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