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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkWarpImageFilter.txx,v $
Language: C++
Date: $Date: 2005-07-27 15:21:12 $
Version: $Revision: 1.25 $
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 __itkWarpImageFilter_txx
#define __itkWarpImageFilter_txx
#include "itkWarpImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkNumericTraits.h"
#include "itkProgressReporter.h"
namespace itk
{
/**
* Default constructor.
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::WarpImageFilter()
{
// Setup the number of required inputs
this->SetNumberOfRequiredInputs( 2 );
// Setup default values
m_OutputSpacing.Fill( 1.0 );
m_OutputOrigin.Fill( 0.0 );
m_EdgePaddingValue = NumericTraits<PixelType>::Zero;
// Setup default interpolator
typename DefaultInterpolatorType::Pointer interp =
DefaultInterpolatorType::New();
m_Interpolator =
static_cast<InterpolatorType*>( interp.GetPointer() );
}
/**
* Standard PrintSelf method.
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "OutputSpacing: " << m_OutputSpacing << std::endl;;
os << indent << "OutputOrigin: " << m_OutputOrigin << std::endl;
os << indent << "EdgePaddingValue: "
<< static_cast<typename NumericTraits<PixelType>::PrintType>(m_EdgePaddingValue)
<< std::endl;
os << indent << "Interpolator: " << m_Interpolator.GetPointer() << std::endl;
}
/**
* Set the output image spacing.
*
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::SetOutputSpacing(
const double* spacing)
{
SpacingType s(spacing);
this->SetOutputSpacing( s );
}
/**
* Set the output image origin.
*
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::SetOutputOrigin(
const double* origin)
{
PointType p(origin);
this->SetOutputOrigin(p);
}
/**
* Set deformation field as Inputs[1] for this ProcessObject.
*
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::SetDeformationField(
const DeformationFieldType * field )
{
// const cast is needed because the pipeline is not const-correct.
DeformationFieldType * input =
const_cast< DeformationFieldType * >( field );
this->ProcessObject::SetNthInput( 1, input );
}
/**
* Return a pointer to the deformation field.
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
typename WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::DeformationFieldType *
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::GetDeformationField(void)
{
return static_cast<DeformationFieldType *>
( this->ProcessObject::GetInput( 1 ));
}
/**
* Setup state of filter before multi-threading.
* InterpolatorType::SetInputImage is not thread-safe and hence
* has to be setup before ThreadedGenerateData
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::BeforeThreadedGenerateData()
{
if( !m_Interpolator )
{
itkExceptionMacro(<< "Interpolator not set");
}
// Connect input image to interpolator
m_Interpolator->SetInputImage( this->GetInput() );
}
/**
* Setup state of filter after multi-threading.
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::AfterThreadedGenerateData()
{
// Disconnect input image from interpolator
m_Interpolator->SetInputImage( NULL );
}
/**
* Compute the output for the region specified by outputRegionForThread.
*/
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::ThreadedGenerateData(
const OutputImageRegionType& outputRegionForThread,
int threadId )
{
InputImageConstPointer inputPtr = this->GetInput();
OutputImagePointer outputPtr = this->GetOutput();
DeformationFieldPointer fieldPtr = this->GetDeformationField();
// support progress methods/callbacks
ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// iterator for the output image
ImageRegionIteratorWithIndex<OutputImageType> outputIt(
outputPtr, outputRegionForThread );
// iterator for the deformation field
ImageRegionIterator<DeformationFieldType> fieldIt(
fieldPtr, outputRegionForThread );
IndexType index;
PointType point;
DisplacementType displacement;
while( !outputIt.IsAtEnd() )
{
// get the output image index
index = outputIt.GetIndex();
outputPtr->TransformIndexToPhysicalPoint( index, point );
// get the required displacement
displacement = fieldIt.Get();
// compute the required input image point
for(unsigned int j = 0; j < ImageDimension; j++ )
{
point[j] += displacement[j];
}
// get the interpolated value
if( m_Interpolator->IsInsideBuffer( point ) )
{
PixelType value = static_cast<PixelType>(
m_Interpolator->Evaluate( point ) );
outputIt.Set( value );
}
else
{
outputIt.Set( m_EdgePaddingValue );
}
++outputIt;
++fieldIt;
progress.CompletedPixel();
}
}
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::GenerateInputRequestedRegion()
{
// call the superclass's implementation
Superclass::GenerateInputRequestedRegion();
// request the largest possible region for the input image
InputImagePointer inputPtr =
const_cast< InputImageType * >( this->GetInput() );
if( inputPtr )
{
inputPtr->SetRequestedRegionToLargestPossibleRegion();
}
// just propagate up the output requested region for the
// deformation field.
DeformationFieldPointer fieldPtr = this->GetDeformationField();
OutputImagePointer outputPtr = this->GetOutput();
if( fieldPtr )
{
fieldPtr->SetRequestedRegion( outputPtr->GetRequestedRegion() );
}
}
template <class TInputImage,class TOutputImage,class TDeformationField>
void
WarpImageFilter<TInputImage,TOutputImage,TDeformationField>
::GenerateOutputInformation()
{
// call the superclass's implementation of this method
Superclass::GenerateOutputInformation();
OutputImagePointer outputPtr = this->GetOutput();
outputPtr->SetSpacing( m_OutputSpacing );
outputPtr->SetOrigin( m_OutputOrigin );
DeformationFieldPointer fieldPtr = this->GetDeformationField();
if( fieldPtr )
{
outputPtr->SetLargestPossibleRegion( fieldPtr->
GetLargestPossibleRegion() );
}
}
} // end namespace itk
#endif
|