17 #ifndef __itkWarpImageFilter_txx
18 #define __itkWarpImageFilter_txx
23 #include "itkNumericTraits.h"
26 #include "vnl/vnl_math.h"
37 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
42 this->SetNumberOfRequiredInputs( 2 );
45 m_OutputSpacing.Fill( 1.0 );
46 m_OutputOrigin.Fill( 0.0 );
47 m_OutputDirection.SetIdentity();
50 m_OutputStartIndex.Fill(0);
53 DefaultInterpolatorType::New();
62 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
68 Superclass::PrintSelf(os, indent);
70 os << indent <<
"OutputSpacing: " << m_OutputSpacing << std::endl;
71 os << indent <<
"OutputOrigin: " << m_OutputOrigin << std::endl;
72 os << indent <<
"OutputDirection: " << m_OutputDirection << std::endl;
73 os << indent <<
"OutputSize: " << m_OutputSize << std::endl;
74 os << indent <<
"OutputStartIndex: " << m_OutputStartIndex << std::endl;
75 os << indent <<
"EdgePaddingValue: "
76 <<
static_cast<typename NumericTraits<PixelType>::PrintType
>(m_EdgePaddingValue)
78 os << indent <<
"Interpolator: " << m_Interpolator.GetPointer() << std::endl;
87 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
91 const double* spacing)
94 this->SetOutputSpacing( s );
102 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
106 const double* origin)
109 this->SetOutputOrigin(p);
113 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
118 this->SetOutputOrigin ( image->
GetOrigin() );
119 this->SetOutputSpacing ( image->
GetSpacing() );
129 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
145 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
147 ::DeformationFieldType *
161 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
167 if( !m_Interpolator )
169 itkExceptionMacro(<<
"Interpolator not set");
174 m_Interpolator->SetInputImage( this->GetInput() );
175 typename DeformationFieldType::RegionType defRegion =
176 fieldPtr->GetLargestPossibleRegion();
177 typename OutputImageType::RegionType outRegion =
178 this->GetOutput()->GetLargestPossibleRegion();
179 m_DefFieldSizeSame = outRegion == defRegion;
180 if(!m_DefFieldSizeSame)
182 m_StartIndex = fieldPtr->GetBufferedRegion().GetIndex();
183 for(
unsigned i = 0; i < ImageDimension; i++)
185 m_EndIndex[i] = m_StartIndex[i] +
186 fieldPtr->GetBufferedRegion().GetSize()[i] - 1;
194 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
200 m_Interpolator->SetInputImage(
NULL );
204 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
207 TDeformationField>::DisplacementType
213 fieldPtr->TransformPhysicalPointToContinuousIndex(point,index);
221 double distance[ImageDimension];
223 for( dim = 0; dim < ImageDimension; dim++ )
225 baseIndex[dim] = Math::Floor< IndexValueType >( index[dim] );
227 if( baseIndex[dim] >= m_StartIndex[dim] )
229 if( baseIndex[dim] < m_EndIndex[dim] )
231 distance[dim] = index[dim] -
static_cast<double>( baseIndex[dim] );
235 baseIndex[dim] = m_EndIndex[dim];
241 baseIndex[dim] = m_StartIndex[dim];
254 double totalOverlap = 0.0;
255 unsigned int numNeighbors(1 << TInputImage::ImageDimension);
257 for(
unsigned int counter = 0; counter < numNeighbors; counter++ )
259 double overlap = 1.0;
260 unsigned int upper = counter;
263 for( dim = 0; dim < ImageDimension; dim++ )
268 neighIndex[dim] = baseIndex[dim] + 1;
269 overlap *= distance[dim];
273 neighIndex[dim] = baseIndex[dim];
274 overlap *= 1.0 - distance[dim];
285 fieldPtr->GetPixel( neighIndex );
288 output[k] += overlap *
static_cast<double>( input[k] );
290 totalOverlap += overlap;
293 if( totalOverlap == 1.0 )
309 return PixelType::Dimension;
315 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
328 ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
332 outputPtr, outputRegionForThread );
336 if(this->m_DefFieldSizeSame)
340 fieldIt(fieldPtr, outputRegionForThread );
342 while( !outputIt.IsAtEnd() )
345 index = outputIt.GetIndex();
346 outputPtr->TransformIndexToPhysicalPoint( index, point );
349 displacement = fieldIt.
Get();
352 for(
unsigned int j = 0; j < ImageDimension; j++ )
354 point[j] += displacement[j];
358 if( m_Interpolator->IsInsideBuffer( point ) )
361 static_cast<PixelType>(m_Interpolator->Evaluate( point ) );
362 outputIt.Set( value );
366 outputIt.Set( m_EdgePaddingValue );
370 progress.CompletedPixel();
375 while( !outputIt.IsAtEnd() )
379 outputPtr->TransformIndexToPhysicalPoint( index, point );
381 displacement = this->EvaluateDeformationAtPhysicalPoint(point);
383 for(
unsigned int j = 0; j < ImageDimension; j++ )
385 point[j] += displacement[j];
389 if( m_Interpolator->IsInsideBuffer( point ) )
392 static_cast<PixelType>(m_Interpolator->Evaluate( point ) );
393 outputIt.Set( value );
397 outputIt.Set( m_EdgePaddingValue );
400 progress.CompletedPixel();
406 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
413 Superclass::GenerateInputRequestedRegion();
421 inputPtr->SetRequestedRegionToLargestPossibleRegion();
428 if(fieldPtr.IsNotNull() )
430 fieldPtr->SetRequestedRegion( outputPtr->GetRequestedRegion() );
431 if(!fieldPtr->VerifyRequestedRegion())
433 fieldPtr->SetRequestedRegion(fieldPtr->GetLargestPossibleRegion());
439 template <
class TInputImage,
class TOutputImage,
class TDeformationField>
445 Superclass::GenerateOutputInformation();
449 outputPtr->SetSpacing( m_OutputSpacing );
450 outputPtr->SetOrigin( m_OutputOrigin );
451 outputPtr->SetDirection( m_OutputDirection );
454 if( this->m_OutputSize[0] == 0 &&
455 fieldPtr.IsNotNull())
457 outputPtr->SetLargestPossibleRegion( fieldPtr->
458 GetLargestPossibleRegion() );
463 region.SetSize(this->m_OutputSize);
464 region.SetIndex(this->m_OutputStartIndex);
465 outputPtr->SetLargestPossibleRegion(region);