File: itkDeformationFieldGradientTensorImageFilter.hxx

package info (click to toggle)
ants 2.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,656 kB
  • sloc: cpp: 84,137; sh: 11,419; perl: 694; xml: 115; makefile: 74; python: 48
file content (267 lines) | stat: -rwxr-xr-x 9,162 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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkDeformationFieldGradientTensorImageFilter.hxx,v $
  Language:  C++
  Date:      $Date: 2008/10/18 00:16:52 $
  Version:   $Revision: 1.1.1.1 $

  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 _itkDeformationFieldGradientTensorImageFilter_hxx
#define _itkDeformationFieldGradientTensorImageFilter_hxx

#include "itkDeformationFieldGradientTensorImageFilter.h"

#include "itkImageRegionIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkProgressReporter.h"
#include "itkVectorCastImageFilter.h"
#include "itkZeroFluxNeumannBoundaryCondition.h"

#include "vnl/vnl_det.h"

namespace itk
{

template <typename TInputImage, typename TRealType, typename TOutputImage>
DeformationFieldGradientTensorImageFilter<TInputImage, TRealType, TOutputImage>
::DeformationFieldGradientTensorImageFilter()
{
  this->m_UseImageSpacing = true;
  this->m_UseCenteredDifference = true;
  this->m_CalculateJacobian = false;
  this->m_Order = 1;
  this->m_DerivativeWeights.Fill( 1.0 );
}

template <typename TInputImage, typename TRealType, typename TOutputImage>
void
DeformationFieldGradientTensorImageFilter<TInputImage, TRealType, TOutputImage>
::GenerateInputRequestedRegion() throw( InvalidRequestedRegionError )
{
  // call the superclass' implementation of this method
  Superclass::GenerateInputRequestedRegion();

  // get pointers to the input and output
  InputImagePointer  inputPtr =
    const_cast< InputImageType * >( this->GetInput() );
  OutputImagePointer outputPtr = this->GetOutput();

  if ( !inputPtr || !outputPtr )
    {
    return;
    }

  // get a copy of the input requested region (should equal the output
  // requested region)
  typename TInputImage::RegionType inputRequestedRegion;
  inputRequestedRegion = inputPtr->GetRequestedRegion();

  this->m_NeighborhoodRadius.Fill( this->m_Order );

  // pad the input requested region by the operator radius
  inputRequestedRegion.PadByRadius( this->m_NeighborhoodRadius );

  // crop the input requested region at the input's largest possible region
  if ( inputRequestedRegion.Crop( inputPtr->GetLargestPossibleRegion() ) )
    {
    inputPtr->SetRequestedRegion( inputRequestedRegion );
    return;
    }
  else
    {
    // Couldn't crop the region (requested region is outside the largest
    // possible region).  Throw an exception.

    // store what we tried to request (prior to trying to crop)
    inputPtr->SetRequestedRegion( inputRequestedRegion );

    // build an exception
    InvalidRequestedRegionError e( __FILE__, __LINE__ );
    e.SetLocation( ITK_LOCATION );
    e.SetDescription( "Requested region is outside the largest possible region." );
    e.SetDataObject( inputPtr );
    throw e;
    }
}

template< typename TInputImage, typename TRealType, typename TOutputImage >
void
DeformationFieldGradientTensorImageFilter<TInputImage, TRealType, TOutputImage>
::BeforeThreadedGenerateData()
{
  if ( this->m_UseImageSpacing )
    {
    for ( unsigned int i = 0; i < ImageDimension; i++ )
      {
      if ( this->GetInput()->GetSpacing()[i] == 0.0 )
        {
        itkExceptionMacro(<< "Image spacing in dimension " << i << " is zero.");
        }
      this->m_DerivativeWeights[i] = 1.0 /
        static_cast<RealType>( this->GetInput()->GetSpacing()[i] );
      }
    }

  /** If the input needs casting to a real-valued vector type, create the
      appropriate image and set the m_RealValuedInputImage pointer to this
      image.  Otherwise just point to the input image. */
  if ( typeid( typename InputImageType::PixelType ) != typeid( RealVectorType ) )
    {
    typename VectorCastImageFilter<TInputImage, RealVectorImageType>::Pointer
      caster = VectorCastImageFilter<TInputImage, RealVectorImageType>::New();
    caster->SetInput( this->GetInput() );
    caster->Update();
    this->m_RealValuedInputImage = caster->GetOutput();
    }
  else
    {
    this->m_RealValuedInputImage
      = dynamic_cast<const RealVectorImageType *>( this->GetInput() );
    }
}

template< typename TInputImage, typename TRealType, typename TOutputImage >
void
DeformationFieldGradientTensorImageFilter< TInputImage, TRealType, TOutputImage >
::ThreadedGenerateData( const OutputImageRegionType& outputRegionForThread,
                        ThreadIdType threadId )
{
  ZeroFluxNeumannBoundaryCondition<RealVectorImageType> nbc;
  ConstNeighborhoodIteratorType bit;
  ImageRegionIterator<TOutputImage> it;

  // Find the data-set boundary "faces"
  typename NeighborhoodAlgorithm::
    ImageBoundaryFacesCalculator<RealVectorImageType>::FaceListType faceList;
  NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<RealVectorImageType> bC;
  faceList = bC( dynamic_cast<const RealVectorImageType *>
                   ( this->m_RealValuedInputImage.GetPointer() ),
                 outputRegionForThread, this->m_NeighborhoodRadius );

  typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<RealVectorImageType>::
    FaceListType::iterator fit;

  // Support progress methods/callbacks
  ProgressReporter progress( this, threadId, outputRegionForThread.GetNumberOfPixels() );

  const typename InputImageType::DirectionType R = this->m_RealValuedInputImage->GetDirection();
  const typename InputImageType::DirectionType RI = this->m_RealValuedInputImage->GetInverseDirection();

  // Process each of the data set faces.  The iterator is reinitialized on each
  // face so that it can determine whether or not to check for boundary
  // conditions.
  for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
    {
    bit = ConstNeighborhoodIteratorType( this->m_NeighborhoodRadius,
                                         dynamic_cast<const RealVectorImageType *>
                                           ( m_RealValuedInputImage.GetPointer() ),
                                         *fit );
    it = ImageRegionIterator<TOutputImage>( this->GetOutput(), *fit );
    bit.OverrideBoundaryCondition( &nbc );
    bit.GoToBegin();

    while ( ! bit.IsAtEnd() )
      {
      RealMatrixType F = this->EvaluateAtNeighborhood( bit );

      it.Set( R * F * RI );
      ++bit;
      ++it;
      progress.CompletedPixel();
      }
    }
}

template< typename TInputImage, typename TRealType, typename TOutputImage >
typename
DeformationFieldGradientTensorImageFilter< TInputImage, TRealType, TOutputImage >::RealMatrixType
DeformationFieldGradientTensorImageFilter< TInputImage, TRealType, TOutputImage >
::EvaluateAtNeighborhood( const ConstNeighborhoodIteratorType &it ) const
{
  unsigned i, j;
  RealMatrixType F;

  for ( i = 0; i < ImageDimension; ++i )
    {
    RealType weight = this->m_DerivativeWeights[i];
    for ( j = 0; j < VectorDimension; ++j )
      {
      if ( this->m_UseCenteredDifference )
        {
        switch( this->m_Order )
          {
          case 1: default:
            F[i][j] = weight * 0.5 * ( it.GetNext( i, 1 )[j] - it.GetPrevious( i, 1 )[j] );
            break;
          case 2:
            F[i][j] = weight * ( -it.GetNext( i, 2 )[j] + 8.0*it.GetNext( i, 1 )[j]
                       - 8.0*it.GetPrevious( i, 1 )[j] + it.GetPrevious(i, 2)[j] ) / 12.0;
            break;
          }
        }
      else  // Forward difference schema
        {
        switch( this->m_Order )
          {
          case 1: default:
            F[i][j] = weight*( it.GetNext( i, 1 )[j] - it.GetCenterPixel()[j] );
            break;
          }
        }
      }
    }

  if ( this->m_CalculateJacobian )
    {
    unsigned int minDimension = ImageDimension;
    if ( static_cast<unsigned int>( VectorDimension ) <
         static_cast<unsigned int>( ImageDimension ) )
      {
      minDimension = VectorDimension;
      }
    for ( i = 0; i < minDimension; i++ )
      {
      F[i][i] += 1.0;
      }
    }
  return F;
}

template <typename TInputImage, typename TRealType, typename TOutputImage>
void
DeformationFieldGradientTensorImageFilter<TInputImage, TRealType, TOutputImage>
::PrintSelf( std::ostream& os, Indent indent ) const
{
  Superclass::PrintSelf(os,indent);

  os << indent << "m_CalculateJacobian = "
     << this->m_CalculateJacobian
     << std::endl;
  os << indent << "m_UseImageSpacing = "
     << this->m_UseImageSpacing
     << std::endl;
  os << indent << "m_UseCenteredDifference = "
     << this->m_UseCenteredDifference
     << std::endl;
  os << indent << "m_Order = "
     << this->m_Order
     << std::endl;
  os << indent << "m_NeighborhoodRadius = "
     << this->m_NeighborhoodRadius
     << std::endl;
  os << indent << "m_RealValuedInputImage = "
     << m_RealValuedInputImage.GetPointer()
     << std::endl;
}

} // end namespace itk

#endif