Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsHistogramBase.cpp
Go to the documentation of this file.
1 
2 
3 /*===================================================================
4 
5 The Medical Imaging Interaction Toolkit (MITK)
6 
7 Copyright (c) German Cancer Research Center,
8 Division of Medical and Biological Informatics.
9 All rights reserved.
10 
11 This software is distributed WITHOUT ANY WARRANTY; without
12 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE.
14 
15 See LICENSE.txt or http://www.mitk.org for details.
16 
17 ===================================================================*/
18 
21 
22 #include <boost/numeric/conversion/converter.hpp>
23 
25 : m_Valid( false )
26 , m_BaselineValue( 0 )
27 , m_TopValue( 1 )
28 , m_StartValue( 0 )
29 , m_Subject( "" )
30 {
31 }
32 
34 {
35 }
36 
37 
39 {
40  return m_BaselineValue;
41 }
42 
44 {
45  return m_TopValue;
46 }
47 
49 {
50  return this->GetXMin();
51 }
52 
54 {
55  return this->GetXMax();
56 }
57 
59 {
60  return m_StartValue;
61 }
62 
64 {
65  return ( m_StartValue + this->GetRange() );
66 }
67 
69 {
70  return m_HistogramVector.size();
71 }
72 
74 {
75  return m_Valid;
76 }
77 
79 {
80  MITK_INFO << "Histogram - Maximum " << this->GetYMax() << " Minimum " << this->GetYMin() << " Range " << this->GetRange();
81 
82  for(unsigned int index( 0 ); index < m_HistogramVector.size(); index++ )
83  {
84  MITK_INFO << " Bin: " << index << " Value: " << m_HistogramVector[ index ];
85  }
86 }
87 
89 {
90  return m_Subject;
91 }
92 
94 {
95  m_Subject = subject;
96 }
97 
99 {
100  m_Valid = false;
101  m_HistogramVector.clear();
102 
103  //check if input is valid
104  if (source==nullptr)
105  { // empty base data
106  return;
107  }
108 
109  mitk::ConnectomicsNetwork* networkSource = dynamic_cast<mitk::ConnectomicsNetwork*>(source);
110 
111  if (networkSource==nullptr)
112  { // base data but no network
113  return;
114  }
115 
116  ComputeFromConnectomicsNetwork( networkSource );
117 }
118 
119 float mitk::ConnectomicsHistogramBase::GetRelativeBin( double start, double end ) const
120 {
121  // use the boost double to int converter
122  // it defaults to trunc
123  typedef boost::numeric::converter<int,double> Double2Int ;
124 
125  float result( 0.0 );
126 
127  if( !m_Valid )
128  {
130  return result;
131  }
132 
133  if( ( start < 0.0 ) ||
134  ( end < 0.0 ) )
135  {
137  return result;
138  }
139 
140  // calculate result
141  if( std::abs( end - start ) <= 1.0 )
142  { // if the bin size is one or less, we can do not need to interpolate
143 
144  unsigned int index( 0 );
145  try
146  {
147  // show the value for n between n - .5 and n + .5
148  double temp = ( start + end ) / 2.0;
149  index = Double2Int::convert( temp ); // By default throws positive_overflow()
150  }
151  catch ( boost::numeric::positive_overflow const& )
152  {
154  }
155 
156  if( index < m_HistogramVector.size() )
157  {
158  result = m_HistogramVector[ index ];
159  }
160  else
161  {
163  index << " on vector sized: " << m_HistogramVector.size();
164  }
165  }
166  else
167  { // if the bin size is more than one we need to interpolate
168 
169  unsigned int indexStart( 0 ), indexEnd( 0 );
170 
171  try
172  {
173  indexStart = Double2Int::convert( start );
174  indexEnd = Double2Int::convert( end );
175  }
176  catch ( boost::numeric::positive_overflow const& )
177  {
179  }
180 
181  if( ( indexStart < m_HistogramVector.size() ) &&
182  ( indexEnd < m_HistogramVector.size() ) )
183  {
184  // add up weighted values and divide by range
185 
186  // add partial start and end bin
187 
188  double startPercentage = 1.0 - start + indexStart;
189  double endPercentage = end - indexEnd;
190 
191  result += startPercentage * m_HistogramVector[ indexStart ];
192  result += endPercentage * m_HistogramVector[ indexEnd ];
193 
194  // add whole inbetween bins
195  for( unsigned int tempIndex = indexStart + 1; tempIndex < indexEnd; tempIndex++ )
196  {
197  result += m_HistogramVector[ tempIndex ];
198  }
199  }
200  else
201  {
203  indexEnd << " on vector sized: " << m_HistogramVector.size();
204  }
205  }
206 
207  // normalizeresult by dividing through maximum degree
208  result = result / GetYMax();
209  return result;
210 }
211 
213 {
214  for ( unsigned int index( 0 ); index < m_HistogramVector.size(); index++ )
215  {
216  if( m_HistogramVector[ index ] > m_TopValue )
217  {
218  m_TopValue = m_HistogramVector[ index ];
219  }
220  }
221 }
222 
224 {
225  return m_HistogramVector;
226 }
virtual double GetYMin() const
Returns the minimal y=f(x) value of the histogram.
#define MITK_INFO
Definition: mitkLogMacros.h:22
Base of all data objects.
Definition: mitkBaseData.h:39
#define MITK_ERROR
Definition: mitkLogMacros.h:24
virtual bool IsValid() const
Returns whether the histogram can be considered valid.
virtual double GetYMax() const
Returns the maximum y=f(x) value of the histogram.
virtual int GetRange() const
Returns the range of the histogram.
virtual std::vector< double > GetHistogramVector()
Get the double vector.
virtual void ComputeFromBaseData(BaseData *source) override
Creates a new histogram from the source.
virtual double GetXMax() const
Returns the maximum x value of the histogram.
virtual std::string GetSubject() const
Returns the subject of the histogram as a string.
virtual double GetXMin() const
Returns the minimal x value of the histogram.
virtual double GetMin() const override
Legacy method, do no use.
virtual double GetMax() const override
Legacy method, do no use.
virtual float GetRelativeBin(double start, double end) const override
Get bin height for the bin between start and end.
static const char * CONNECTOMICS_ERROR_PASSED_NEGATIVE_INDEX_TO_HISTOGRAM
static const char * CONNECTOMICS_ERROR_TRIED_TO_ACCESS_INVALID_HISTOGRAM
virtual void PrintToConsole() const
Print values to console.
virtual void SetSubject(std::string)
Set the subject of the histogram as a string.
Connectomics Network Class.
virtual void UpdateYMax()
Update the Y maximum to the maximal value in the histogram.
#define MBI_WARN
Definition: mbilog.h:222