17 #ifndef __itkResampleImageFilter_txx
18 #define __itkResampleImageFilter_txx
23 #include "itkConfigure.h"
25 #ifdef ITK_USE_OPTIMIZED_REGISTRATION_METHODS
45 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
49 m_OutputSpacing.Fill(1.0);
50 m_OutputOrigin.Fill(0.0);
51 m_OutputDirection.SetIdentity();
53 m_UseReferenceImage =
false;
56 m_OutputStartIndex.Fill( 0 );
61 m_DefaultPixelValue = 0;
70 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
75 Superclass::PrintSelf(os,indent);
77 os << indent <<
"DefaultPixelValue: "
78 <<
static_cast<typename NumericTraits<PixelType>::PrintType
>(m_DefaultPixelValue)
80 os << indent <<
"Size: " << m_Size << std::endl;
81 os << indent <<
"OutputStartIndex: " << m_OutputStartIndex << std::endl;
82 os << indent <<
"OutputOrigin: " << m_OutputOrigin << std::endl;
83 os << indent <<
"OutputSpacing: " << m_OutputSpacing << std::endl;
84 os << indent <<
"OutputDirection: " << m_OutputDirection << std::endl;
85 os << indent <<
"Transform: " << m_Transform.GetPointer() << std::endl;
86 os << indent <<
"Interpolator: " << m_Interpolator.GetPointer() << std::endl;
87 os << indent <<
"UseReferenceImage: " << (m_UseReferenceImage ?
"On" :
"Off") << std::endl;
94 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
98 const double* spacing)
101 this->SetOutputSpacing( s );
108 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
112 const double* origin)
115 this->SetOutputOrigin( p );
124 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
131 itkExceptionMacro(<<
"Transform not set");
134 if( !m_Interpolator )
136 itkExceptionMacro(<<
"Interpolator not set");
140 m_Interpolator->SetInputImage( this->GetInput() );
147 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
153 m_Interpolator->SetInputImage(
NULL );
160 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
176 InputSpecialCoordinatesImageType::Pointer foo =
177 InputSpecialCoordinatesImageType::New();
178 OutputSpecialCoordinatesImageType::Pointer bar =
179 OutputSpecialCoordinatesImageType::New();
181 if (dynamic_cast<const InputSpecialCoordinatesImageType *>(this->GetInput())
182 || dynamic_cast<const OutputSpecialCoordinatesImageType *>(this->GetOutput()))
184 this->NonlinearThreadedGenerateData(outputRegionForThread, threadId);
191 if( m_Transform->IsLinear() )
193 this->LinearThreadedGenerateData(outputRegionForThread, threadId);
199 this->NonlinearThreadedGenerateData(outputRegionForThread, threadId);
204 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
220 OutputIterator outIt(outputPtr, outputRegionForThread);
229 ContinuousIndexType inputIndex;
232 ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
238 const PixelType minValue = NumericTraits<PixelType >::NonpositiveMin();
239 const PixelType maxValue = NumericTraits<PixelType >::max();
241 const OutputType minOutputValue =
static_cast<OutputType
>(minValue);
242 const OutputType maxOutputValue =
static_cast<OutputType
>(maxValue);
251 double precisionConstant = 1<<(NumericTraits<double>::digits>>1);
253 while ( !outIt.IsAtEnd() )
256 outputPtr->TransformIndexToPhysicalPoint( outIt.GetIndex(), outputPoint );
259 inputPoint = m_Transform->TransformPoint(outputPoint);
260 inputPtr->TransformPhysicalPointToContinuousIndex(inputPoint, inputIndex);
272 for (
int i=0; i < ImageDimension; ++i)
274 double roundedInputIndex = vcl_floor(inputIndex[i]);
275 double inputIndexFrac = inputIndex[i] - roundedInputIndex;
276 double newInputIndexFrac = vcl_floor(precisionConstant * inputIndexFrac)/precisionConstant;
277 inputIndex[i] = roundedInputIndex + newInputIndexFrac;
281 if( m_Interpolator->IsInsideBuffer(inputIndex) )
284 const OutputType value
285 = m_Interpolator->EvaluateAtContinuousIndex(inputIndex);
287 NumericTraits<OutputType>::Clamp(value,minOutputValue,maxOutputValue)
293 outIt.Set(m_DefaultPixelValue);
296 progress.CompletedPixel();
303 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
319 OutputIterator outIt(outputPtr, outputRegionForThread);
320 outIt.SetDirection( 0 );
331 ContinuousIndexType inputIndex;
332 ContinuousIndexType tmpInputIndex;
339 ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
344 PixelType defaultValue = this->GetDefaultPixelValue();
348 const PixelType minValue = NumericTraits<PixelType >::NonpositiveMin();
349 const PixelType maxValue = NumericTraits<PixelType >::max();
351 const OutputType minOutputValue =
static_cast<OutputType
>(minValue);
352 const OutputType maxOutputValue =
static_cast<OutputType
>(maxValue);
356 index = outIt.GetIndex();
357 outputPtr->TransformIndexToPhysicalPoint( index, outputPoint );
361 inputPoint = m_Transform->TransformPoint(outputPoint);
362 inputPtr->TransformPhysicalPointToContinuousIndex(inputPoint, inputIndex);
382 outputPtr->TransformIndexToPhysicalPoint( index, tmpOutputPoint );
383 tmpInputPoint = m_Transform->TransformPoint( tmpOutputPoint );
384 inputPtr->TransformPhysicalPointToContinuousIndex(tmpInputPoint,
386 delta = tmpInputIndex - inputIndex;
392 double precisionConstant = 1<<(NumericTraits<double>::digits>>1);
406 for (
int i=0; i < ImageDimension; ++i)
408 double roundedDelta = vcl_floor(delta[i]);
409 double deltaFrac = delta[i] - roundedDelta;
410 double newDeltaFrac = vcl_floor(precisionConstant * deltaFrac)/precisionConstant;
411 delta[i] = roundedDelta + newDeltaFrac;
415 while ( !outIt.IsAtEnd() )
422 index = outIt.GetIndex();
423 outputPtr->TransformIndexToPhysicalPoint( index, outputPoint );
427 inputPoint = m_Transform->TransformPoint(outputPoint);
428 inputPtr->TransformPhysicalPointToContinuousIndex(inputPoint, inputIndex);
440 for (
int i=0; i < ImageDimension; ++i)
442 double roundedInputIndex = vcl_floor(inputIndex[i]);
443 double inputIndexFrac = inputIndex[i] - roundedInputIndex;
444 double newInputIndexFrac = vcl_floor(precisionConstant * inputIndexFrac)/precisionConstant;
445 inputIndex[i] = roundedInputIndex + newInputIndexFrac;
449 while( !outIt.IsAtEndOfLine() )
452 if( m_Interpolator->IsInsideBuffer(inputIndex) )
455 const OutputType value
456 = m_Interpolator->EvaluateAtContinuousIndex(inputIndex);
459 NumericTraits<OutputType>::Clamp(value,minOutputValue,maxOutputValue)
465 outIt.Set(defaultValue);
468 progress.CompletedPixel();
486 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
492 Superclass::GenerateInputRequestedRegion();
494 if ( !this->GetInput() )
501 const_cast< TInputImage *
>( this->GetInput() );
505 inputRegion = inputPtr->GetLargestPossibleRegion();
506 inputPtr->SetRequestedRegion(inputRegion);
516 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
521 Self * surrogate =
const_cast< Self *
>( this );
523 static_cast<const OutputImageType *
>(surrogate->ProcessObject::GetInput(1));
524 return referenceImage;
532 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
537 itkDebugMacro(
"setting input ReferenceImage to " << image);
546 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
551 this->SetOutputOrigin ( image->
GetOrigin() );
552 this->SetOutputSpacing ( image->
GetSpacing() );
558 #if !defined(ITK_LEGACY_REMOVE)
559 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
564 itkGenericLegacyReplaceBodyMacro(itk::ResampleImageFilter::SetOutputParametersFromConstImage,
567 this->SetOutputParametersFromImage(image);
574 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
580 Superclass::GenerateOutputInformation();
592 if( m_UseReferenceImage && referenceImage )
594 outputPtr->SetLargestPossibleRegion( referenceImage->GetLargestPossibleRegion() );
598 typename TOutputImage::RegionType outputLargestPossibleRegion;
599 outputLargestPossibleRegion.SetSize( m_Size );
600 outputLargestPossibleRegion.SetIndex( m_OutputStartIndex );
601 outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
605 if (m_UseReferenceImage && referenceImage)
607 outputPtr->SetOrigin( referenceImage->GetOrigin() );
608 outputPtr->SetSpacing( referenceImage->GetSpacing() );
609 outputPtr->SetDirection( referenceImage->GetDirection() );
613 outputPtr->SetOrigin( m_OutputOrigin );
614 outputPtr->SetSpacing( m_OutputSpacing );
615 outputPtr->SetDirection( m_OutputDirection );
623 template <
class TInputImage,
class TOutputImage,
class TInterpolatorPrecisionType>
632 if( latestTime < m_Transform->GetMTime() )
634 latestTime = m_Transform->GetMTime();
640 if( latestTime < m_Interpolator->GetMTime() )
642 latestTime = m_Interpolator->GetMTime();