Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkEnergyComputer.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 "mitkEnergyComputer.h"
18 #include <vnl/vnl_copy.h>
19 #include <itkNumericTraits.h>
20 
21 using namespace mitk;
22 
24  : m_UseTrilinearInterpolation(true)
25 {
26  m_ParticleGrid = particleGrid;
27  m_RandGen = randGen;
28  //m_Image = qballImage;
29  m_SphereInterpolator = interpolator;
30  m_Mask = mask;
31 
34 
35  m_Size[0] = mask->GetLargestPossibleRegion().GetSize()[0];
36  m_Size[1] = mask->GetLargestPossibleRegion().GetSize()[1];
37  m_Size[2] = mask->GetLargestPossibleRegion().GetSize()[2];
38 
39  if (m_Size[0]<3 || m_Size[1]<3 || m_Size[2]<3)
41 
42  m_Spacing[0] = mask->GetSpacing()[0];
43  m_Spacing[1] = mask->GetSpacing()[1];
44  m_Spacing[2] = mask->GetSpacing()[2];
45 
46  // get rotation matrix
47  vnl_matrix<double> temp = mask->GetDirection().GetVnlMatrix();
48  vnl_matrix<float> directionMatrix; directionMatrix.set_size(3,3);
49  vnl_copy(temp, directionMatrix);
50  vnl_matrix_fixed<float, 3, 3> I = directionMatrix*directionMatrix.transpose();
51  for (int i = 0; i < 3; i++)
52  for (int j = 0; j < 3; j++)
53  I[i][j] = fabs(I[i][j]);
54  if(!I.is_identity(0.001))
55  {
56  fprintf(stderr,"EnergyComputer: image direction is not a rotation matrix. Tracking not possible!\n");
57  std::cout << directionMatrix << std::endl;
58  }
59  m_RotationMatrix = directionMatrix;
60 
62  fprintf(stderr,"EnergyComputer: error during init: data does not match with interpolation scheme\n");
63 
64  int totsz = m_Size[0]*m_Size[1]*m_Size[2];
65  m_CumulatedSpatialProbability.resize(totsz + 1, 0.0);
66  m_ActiveIndices.resize(totsz, 0);
67 
68  // calculate active voxels and cumulate probabilities
71  for (int x = 0; x < m_Size[0];x++)
72  for (int y = 0; y < m_Size[1];y++)
73  for (int z = 0; z < m_Size[2];z++)
74  {
75  int idx = x+(y+z*m_Size[1])*m_Size[0];
76  ItkFloatImageType::IndexType index;
77  index[0] = x; index[1] = y; index[2] = z;
78  if (m_Mask->GetPixel(index) > 0.5)
79  {
83  }
84  }
85  for (int k = 0; k < m_NumActiveVoxels; k++)
87 
88  std::cout << "EnergyComputer: " << m_NumActiveVoxels << " active voxels found" << std::endl;
89 }
90 
91 void EnergyComputer::SetParameters(float particleWeight, float particleWidth, float connectionPotential, float curvThres, float inexBalance, float particlePotential)
92 {
93  m_ParticleChemicalPotential = particlePotential;
94  m_ConnectionPotential = connectionPotential;
95  m_ParticleWeight = particleWeight;
96 
97  float bal = 1/(1+exp(-inexBalance));
98  m_ExtStrength = 2*bal;
100 
101  m_CurvatureThreshold = curvThres;
102 
103  float sigma_s = particleWidth;
104  gamma_s = 1/(sigma_s*sigma_s);
106 }
107 
108 // draw random position from active voxels
109 void EnergyComputer::DrawRandomPosition(vnl_vector_fixed<float, 3>& R)
110 {
111  float r = m_RandGen->GetVariate();//m_RandGen->frand();
112  int j;
113  int rl = 1;
114  int rh = m_NumActiveVoxels;
115  while(rh != rl)
116  {
117  j = rl + (rh-rl)/2;
119  {
120  rh = j;
121  continue;
122  }
124  {
125  rl = j+1;
126  continue;
127  }
128  break;
129  }
130  R[0] = m_Spacing[0]*((float)(m_ActiveIndices[rh-1] % m_Size[0]) + m_RandGen->GetVariate());
131  R[1] = m_Spacing[1]*((float)((m_ActiveIndices[rh-1]/m_Size[0]) % m_Size[1]) + m_RandGen->GetVariate());
132  R[2] = m_Spacing[2]*((float)(m_ActiveIndices[rh-1]/(m_Size[0]*m_Size[1])) + m_RandGen->GetVariate());
133 }
134 
135 // return spatial probability of position
136 float EnergyComputer::SpatProb(vnl_vector_fixed<float, 3> pos)
137 {
138  ItkFloatImageType::IndexType index;
139  index[0] = floor(pos[0]/m_Spacing[0]);
140  index[1] = floor(pos[1]/m_Spacing[1]);
141  index[2] = floor(pos[2]/m_Spacing[2]);
142 
143  if (m_Mask->GetLargestPossibleRegion().IsInside(index)) // is inside image?
144  return m_Mask->GetPixel(index);
145  else
146  return 0;
147 }
148 
150 {
151  // BESSEL_APPROXCOEFF[0] = -0.1714;
152  // BESSEL_APPROXCOEFF[1] = 0.5332;
153  // BESSEL_APPROXCOEFF[2] = -1.4889;
154  // BESSEL_APPROXCOEFF[3] = 2.0389;
155  float y = x*x;
156  float erg = -0.1714;
157  erg += y*0.5332;
158  erg += y*y*-1.4889;
159  erg += y*y*y*2.0389;
160  return erg;
161 }
162 
163 float EnergyComputer::mexp(float x)
164 {
165  return((x>=7.0) ? 0 : ((x>=5.0) ? (-0.0029*x+0.0213) : ((x>=3.0) ? (-0.0215*x+0.1144) : ((x>=2.0) ? (-0.0855*x+0.3064) : ((x>=1.0) ? (-0.2325*x+0.6004) : ((x>=0.5) ? (-0.4773*x+0.8452) : ((x>=0.0) ? (-0.7869*x+1.0000) : 1 )))))));
166  // return exp(-x);
167 }
168 
170 {
171  return m_NumActiveVoxels;
172 }
std::vector< int > m_ActiveIndices
vnl_vector_fixed< float, 3 > m_Spacing
std::vector< float > m_CumulatedSpatialProbability
Contains and manages particles.
vnl_matrix_fixed< float, 3, 3 > m_RotationMatrix
DataCollection - Class to facilitate loading/accessing structured data.
SphereInterpolator * m_SphereInterpolator
Lookuptable based trilinear interpolation of spherically arranged scalar values.
itk::Image< float, 3 > ItkFloatImageType
vnl_vector_fixed< int, 3 > m_Size
float mexp(float x)
float mbesseli0(float x)
ItkRandGenType * m_RandGen
void DrawRandomPosition(vnl_vector_fixed< float, 3 > &R)
void SetParameters(float particleWeight, float particleWidth, float connectionPotential, float curvThres, float inexBalance, float particlePotential)
#define QBALL_ODFSIZE
EnergyComputer(ItkFloatImageType *mask, ParticleGrid *particleGrid, SphereInterpolator *interpolator, ItkRandGenType *randGen)
ParticleGrid * m_ParticleGrid
bool m_UseTrilinearInterpolation
itk::Statistics::MersenneTwisterRandomVariateGenerator ItkRandGenType
float m_ParticleChemicalPotential
ItkFloatImageType * m_Mask
float SpatProb(vnl_vector_fixed< float, 3 > pos)