File: itkBloxCoreAtomImage.txx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (307 lines) | stat: -rw-r--r-- 10,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkBloxCoreAtomImage.txx,v $
  Language:  C++
  Date:      $Date: 2006-03-19 16:30:14 $
  Version:   $Revision: 1.41 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkBloxCoreAtomImage_txx
#define __itkBloxCoreAtomImage_txx

#include "itkBloxCoreAtomImage.h"

#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "itkConicShellInteriorExteriorSpatialFunction.h"
#include "itkEllipsoidInteriorExteriorSpatialFunction.h"
#include "itkFloodFilledSpatialFunctionConditionalIterator.h"

#include "vnl/vnl_matrix.h"

namespace itk
{

template <unsigned int NDimension>
BloxCoreAtomImage<NDimension>
::BloxCoreAtomImage()
{
  m_MedialNodeCount = 0;
  m_NodePointerList = new std::vector<BloxCoreAtomPixel<NDimension>*>();
}

template <unsigned int NDimension>
BloxCoreAtomImage<NDimension>
::~BloxCoreAtomImage()
{
  delete m_NodePointerList;
}

template <unsigned int NDimension>
void
BloxCoreAtomImage<NDimension>
::DoEigenanalysis()
{
  itk::ImageRegionIterator<Self> bloxIt = 
    itk::ImageRegionIterator<Self>(this, this->GetLargestPossibleRegion() );

  for(bloxIt.GoToBegin(); !bloxIt.IsAtEnd(); ++bloxIt)
    {
    ( &bloxIt.Value() )->DoCoreAtomEigenanalysis();
    }
}

template <unsigned int NDimension>
void
BloxCoreAtomImage<NDimension>
::DoCoreAtomVoting()
{
  //cerr << "BloxCoreAtomImage::DoCoreAtomVoting()" << endl;
  
  // Iterator to access all pixels in the image
  ImageRegionIterator<Self> bloxIt = 
    ImageRegionIterator<Self>(this, this->GetLargestPossibleRegion() );

  // Pointer for accessing pixels
  BloxCoreAtomPixel<NDimension>* pPixel = 0;

  // Results of eigenanalysis from each pixel
  typename BloxCoreAtomPixel<NDimension>::EigenvalueType eigenvalues;
  typename BloxCoreAtomPixel<NDimension>::EigenvectorType eigenvectors;

  // Results of eigenanalysis from each pixel
  typename BloxCoreAtomPixel<NDimension>::EigenvalueType sf_eigenvalues;
  typename BloxCoreAtomPixel<NDimension>::EigenvectorType sf_eigenvectors;

  unsigned int voterCount = 0;
  for(bloxIt.GoToBegin(); !bloxIt.IsAtEnd(); ++bloxIt)
    {
    // Get a pointer to the pixel
    pPixel = &bloxIt.Value();

    // If there are no core atoms in this pixel, it doesn't get to vote
    if( pPixel->empty() )
      {
      continue;
      }
    //populate the NodePointerList
    m_NodePointerList->push_back(pPixel);

    voterCount++;

    // Get eigenanalysis results
    eigenvalues = pPixel->GetEigenvalues();
    eigenvectors = pPixel->GetEigenvectors();

    // Ellipsoid axis length array
    Point<double, NDimension> axisLengthArray;

    // Compute first length
    axisLengthArray[0] = 0.5 * pPixel->GetMeanCoreAtomDiameter();
 
    // Precompute alphaOne
    double alphaOne = 1 - eigenvalues[0];

    // Watch out for /0 problems
    if(alphaOne==0)
      {
      alphaOne = 0.001;
      }
    // Now compute the rest of the lengths
    for(unsigned int i = 1; i < NDimension; i++)
      {
      axisLengthArray[i] = ( (1 - eigenvalues[i]) / alphaOne) 
                                                        * axisLengthArray[0];
      }

    // Build the ellipsoid voting region
    typedef EllipsoidInteriorExteriorSpatialFunction<NDimension, PositionType> 
                                                              VoteFunctionType;
    typename VoteFunctionType::Pointer ellipsoid = VoteFunctionType::New();

    // Create an iterator to traverse the ellipsoid region
    typedef FloodFilledSpatialFunctionConditionalIterator
      <Self, VoteFunctionType> ItType;

    // The seed position for the ellipsoid is the current pixel's index 
    // in data space since this is always at the center of the voting ellipsoid
    typename Self::IndexType seedPos = bloxIt.GetIndex();
    
    // Figure out the center of the ellipsoid, which is the center
    // of the voting pixel
    typename VoteFunctionType::InputType centerPosition;

    ContinuousIndex<double, NDimension> contIndex;

    for(unsigned int i = 0; i < NDimension; i ++ )
      {
      contIndex[i] = (double)seedPos[i] + 0.5;
      }

    // Get the physical location of this center index
    this->TransformContinuousIndexToPhysicalPoint(contIndex, centerPosition);

    ellipsoid->SetCenter(centerPosition);
    ellipsoid->SetOrientations(eigenvectors);
    ellipsoid->SetAxes(axisLengthArray);
    
    // Instantiate the iterator
    ItType sfi = ItType(this, ellipsoid, seedPos);

    // Get the position of the voting blox
    typedef Point<double, NDimension> TPosition;
    TPosition voterPosition;
    typename Self::IndexType voterIndex = bloxIt.GetIndex();
    this->TransformIndexToPhysicalPoint(voterIndex, voterPosition);

    int voteeCount = 0;

    sfi.SetCenterInclusionStrategy();

    // Iterate through the ellipsoid and cast votes
    for( sfi.GoToBegin(); !( sfi.IsAtEnd() ); ++sfi)
      {
      TPosition voteePosition;
      typename Self::IndexType voteeIndex = sfi.GetIndex();
      //std::cerr << "voteeIndex "<< voteeIndex << std::endl ;
      
      this->TransformIndexToPhysicalPoint(voteeIndex, voteePosition);

      // vector from voting blox to current votee
      typename TPosition::VectorType dbar = voterPosition - voteePosition;
      voteeCount ++;

      // The voting process and variables are explained in
      // IEEE TRANSACTIONS ON MEDICAL IMAGING, VOL. 18, NO. 10, OCTOBER 1999
      // page 1029 

      // The votee does not get voted for if it's empty
      if( sfi.Get().GetSize() == 0 )
        {
        continue;
        }
      // form the ellipsoidal distance de
      double de = 0;
      double sf_de_sqr = 0;

      for (unsigned int i = 0; i < NDimension; i++)
        {
        de += vcl_pow((dot_product(eigenvectors.get_column(i),
                  dbar.GetVnlVector() ) / axisLengthArray[i] ), 2);
        }

      de = vcl_sqrt(de);

      //printf("De = %f\n", de);

      double weight_factor = vcl_exp(-1.0*de*de);

      // vote strength
      double voteStrength = pPixel->size()*weight_factor;
      //printf("Vote strength = %f\n", voteStrength);
      //printf("weight_factor = %f\n", weight_factor);

      // Get eigenanalysis results
      sf_eigenvalues = sfi.Get().GetEigenvalues();
      sf_eigenvectors = sfi.Get().GetEigenvectors();

      for (unsigned int i = 0; i < NDimension; i++)
        {
        sf_de_sqr += vcl_pow((dot_product(sf_eigenvectors.get_column(i), 
                          dbar.GetVnlVector() ) / axisLengthArray[i] ), 2);
        }

      //printf("sf_de = %f\n", vcl_sqrt(sf_de_sqr));

      //CALCULATE WEIGHT FACTOR FOR INDEX OF SPATIAL FUNCTION ITERATION
      double sf_weight_factor = vcl_exp(-1.0*sf_de_sqr);

      //HERE WE CALL CalcWeightedCoreAtomLocation using de to keep track 
      //of the weighted location of each voted medial node based on 
      // constituent core atom locations/
      // Reminder: sfi.Get() is the pixel being voted for
      // and pPixel is doing the voting
      sfi.Get().CalcWeightedCoreAtomLocation(sf_weight_factor, pPixel);

      // cast the vote
      sfi.Get().CollectVote(pPixel->GetRawCMatrixPointer(), 
                            voteStrength, pPixel->size() );
      
      } // end cast votes from this pixel

    //printf("Blox voted for %i other pixels\n", voteeCount);
    } // end cast votes from all pixels

  // The final task is to normalize all of the voted blox
  // and recompute the eigenanalysis on the new matrix
  for(bloxIt.GoToBegin(); !bloxIt.IsAtEnd(); ++bloxIt)
    {
    (&bloxIt.Value())->NormalizeVotedCMatrix();
    (&bloxIt.Value())->DoVotedEigenanalysis();
    }

  m_MedialNodeCount = voterCount;
  //cerr << "MedialNodeCount = " << m_MedialNodeCount << endl;
}

template <unsigned int NDimension>
void
BloxCoreAtomImage<NDimension>
::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os,indent);

  // Iterator to access all pixels in the image
  ImageRegionConstIterator<Self> bloxIt = 
    ImageRegionConstIterator<Self>(this, this->GetLargestPossibleRegion() );

  // Pointer for accessing pixels
  BloxCoreAtomPixel<NDimension> pPixel;

  // Results of eigenanalysis from each pixel
  typename BloxCoreAtomPixel<NDimension>::EigenvalueType eigenvalues;
  typename BloxCoreAtomPixel<NDimension>::EigenvalueType veigenvalues;

  os << indent << "Index\t# Core Atoms\tEigen Values\t\t\tMean CA Length\t\
                  Voted Eigen Values\n" 
            << "-----\t------------\t------------\t\t\t--------------\t----\
               --------------\n" << std::endl;

  int counter = 0;

  for(bloxIt.GoToBegin(); !bloxIt.IsAtEnd(); ++bloxIt)
    {
    // Get a pointer to the pixel
    pPixel = bloxIt.Value();

    eigenvalues = pPixel.GetEigenvalues();
    veigenvalues = pPixel.GetVotedEigenvalues();

    if(!pPixel.empty())
      {
      os << indent << bloxIt.GetIndex() << "\t";
      os << indent << pPixel.GetSize() << "\t";
      os << indent << eigenvalues[0] << " " << eigenvalues[1] << " " 
         << eigenvalues[2] << "\t";
      os << indent << pPixel.GetMeanCoreAtomDiameter() << "\t\t";
      os << indent << veigenvalues[0] << " " << veigenvalues[1] << " " 
         << veigenvalues[2] << "\t" << std::endl;
      os << std::endl << indent << "Node Pointer List: " 
         << (*m_NodePointerList)[counter]->GetVotedLocation() << std::endl;
      counter++;
      }
    }  
  os << "Number of Medial Nodes: " << m_MedialNodeCount << std::endl;
}

} // end namespace itk

#endif