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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkTransformToDeformationFieldSource.txx
Language: C++
Date: $Date$
Version: $Revision$
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.
=========================================================================*/
/*======================================================================
This file is part of the elastix software.
Copyright (c) University Medical Center Utrecht. All rights reserved.
See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php 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 __itkTransformToDeformationFieldSource_txx
#define __itkTransformToDeformationFieldSource_txx
#include "itkTransformToDeformationFieldSource.h"
#include "itkIdentityTransform.h"
#include "itkProgressReporter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageLinearIteratorWithIndex.h"
namespace itk
{
/**
* Constructor
*/
template <class TOutputImage, class TTransformPrecisionType>
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::TransformToDeformationFieldSource()
{
this->m_OutputSpacing.Fill(1.0);
this->m_OutputOrigin.Fill(0.0);
this->m_OutputDirection.SetIdentity();
SizeType size;
size.Fill( 0 );
this->m_OutputRegion.SetSize( size );
IndexType index;
index.Fill( 0 );
this->m_OutputRegion.SetIndex( index );
this->m_Transform
= IdentityTransform<TTransformPrecisionType, ImageDimension>::New();
} // end Constructor
/**
* Print out a description of self
*
* \todo Add details about this class
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::PrintSelf( std::ostream & os, Indent indent ) const
{
Superclass::PrintSelf( os, indent );
os << indent << "OutputRegion: " << this->m_OutputRegion << std::endl;
os << indent << "OutputSpacing: " << this->m_OutputSpacing << std::endl;
os << indent << "OutputOrigin: " << this->m_OutputOrigin << std::endl;
os << indent << "OutputDirection: " << this->m_OutputDirection << std::endl;
os << indent << "Transform: " << this->m_Transform.GetPointer() << std::endl;
} // end PrintSelf()
/**
* Set the output image size.
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::SetOutputSize( const SizeType & size )
{
this->m_OutputRegion.SetSize( size );
}
/**
* Get the output image size.
*/
template <class TOutputImage, class TTransformPrecisionType>
const typename TransformToDeformationFieldSource<TOutputImage,
TTransformPrecisionType>
::SizeType &
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::GetOutputSize()
{
return this->m_OutputRegion.GetSize();
}
/**
* Set the output image index.
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::SetOutputIndex( const IndexType & index )
{
this->m_OutputRegion.SetIndex( index );
}
/**
* Get the output image index.
*/
template <class TOutputImage, class TTransformPrecisionType>
const typename TransformToDeformationFieldSource<TOutputImage,
TTransformPrecisionType>
::IndexType &
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::GetOutputIndex()
{
return this->m_OutputRegion.GetIndex();
}
/**
* Set the output image spacing.
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::SetOutputSpacing( const double *spacing )
{
SpacingType s( spacing );
this->SetOutputSpacing( s );
} // end SetOutputSpacing()
/**
* Set the output image origin.
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::SetOutputOrigin( const double *origin )
{
OriginType p( origin );
this->SetOutputOrigin( p );
}
/** Helper method to set the output parameters based on this image */
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::SetOutputParametersFromImage ( const ImageBaseType *image )
{
if ( !image )
{
itkExceptionMacro(<< "Cannot use a null image reference");
}
this->SetOutputOrigin( image->GetOrigin() );
this->SetOutputSpacing( image->GetSpacing() );
this->SetOutputDirection( image->GetDirection() );
this->SetOutputRegion( image->GetLargestPossibleRegion() );
} // end SetOutputParametersFromImage()
/**
* Set up state of filter before multi-threading.
* InterpolatorType::SetInputImage is not thread-safe and hence
* has to be set up before ThreadedGenerateData
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::BeforeThreadedGenerateData( void )
{
if ( !this->m_Transform )
{
itkExceptionMacro(<< "Transform not set");
}
} // end BeforeThreadedGenerateData()
/**
* ThreadedGenerateData
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::ThreadedGenerateData(
const OutputImageRegionType & outputRegionForThread,
int threadId )
{
// Check whether we can use a fast path for resampling. Fast path
// can be used if the transformation is linear. Transform respond
// to the IsLinear() call.
if ( this->m_Transform->IsLinear() )
{
this->LinearThreadedGenerateData( outputRegionForThread, threadId );
return;
}
// Otherwise, we use the normal method where the transform is called
// for computing the transformation of every point.
this->NonlinearThreadedGenerateData( outputRegionForThread, threadId );
} // end ThreadedGenerateData()
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::NonlinearThreadedGenerateData(
const OutputImageRegionType & outputRegionForThread,
int threadId )
{
// Get the output pointer
OutputImagePointer outputPtr = this->GetOutput();
// Create an iterator that will walk the output region for this thread.
typedef ImageRegionIteratorWithIndex<TOutputImage> OutputIteratorType;
OutputIteratorType outIt( outputPtr, outputRegionForThread );
// Define a few variables that will be used to translate from an input pixel
// to an output pixel
PointType outputPoint; // Coordinates of output pixel
PointType transformedPoint; // Coordinates of transformed pixel
PixelType deformation; // the difference
// Support for progress methods/callbacks
ProgressReporter progress( this, threadId,
outputRegionForThread.GetNumberOfPixels() );
// Walk the output region
outIt.GoToBegin();
while ( !outIt.IsAtEnd() )
{
// Determine the index of the current output pixel
outputPtr->TransformIndexToPhysicalPoint( outIt.GetIndex(), outputPoint );
// Compute corresponding input pixel position
transformedPoint = this->m_Transform->TransformPoint( outputPoint );
// Compute the deformation
for ( unsigned int i = 0; i < ImageDimension; ++i )
{
deformation[i] = static_cast<PixelValueType>(
transformedPoint[i] - outputPoint[i] );
}
// Set it
outIt.Set( deformation );
// Update progress and iterator
progress.CompletedPixel();
++outIt;
}
} // end NonlinearThreadedGenerateData()
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::LinearThreadedGenerateData(
const OutputImageRegionType & outputRegionForThread,
int threadId )
{
// Get the output pointer
OutputImagePointer outputPtr = this->GetOutput();
// Create an iterator that will walk the output region for this thread.
typedef ImageLinearIteratorWithIndex<TOutputImage> OutputIteratorType;
OutputIteratorType outIt( outputPtr, outputRegionForThread );
outIt.SetDirection( 0 );
// Define a few indices that will be used to translate from an input pixel
// to an output pixel
PointType outputPoint; // Coordinates of current output pixel
PointType transformedPoint; // Coordinates of transformed pixel
PixelType deformation; // the difference
IndexType index;
// Support for progress methods/callbacks
ProgressReporter progress( this, threadId,
outputRegionForThread.GetNumberOfPixels() );
// Determine the position of the first pixel in the scanline
outIt.GoToBegin();
index = outIt.GetIndex();
outputPtr->TransformIndexToPhysicalPoint( index, outputPoint );
// Compute corresponding transformed pixel position
transformedPoint = this->m_Transform->TransformPoint( outputPoint );
// Compare with the ResampleImageFilter
// Compute delta
PointType outputPointNeighbour;
PointType transformedPointNeighbour;
typedef typename PointType::VectorType VectorType;
VectorType delta;
++index[0];
outputPtr->TransformIndexToPhysicalPoint( index, outputPointNeighbour );
transformedPointNeighbour = this->m_Transform->TransformPoint(
outputPointNeighbour );
delta = transformedPointNeighbour - transformedPoint
- ( outputPointNeighbour - outputPoint );
// loop over the vector image
while ( !outIt.IsAtEnd() )
{
// Get current point
index = outIt.GetIndex();
outputPtr->TransformIndexToPhysicalPoint( index, outputPoint );
// Compute transformed point
transformedPoint = this->m_Transform->TransformPoint( outputPoint );
while ( !outIt.IsAtEndOfLine() )
{
// Compute the deformation
for ( unsigned int i = 0; i < ImageDimension; ++i )
{
deformation[i] = static_cast<PixelValueType>(
transformedPoint[i] - outputPoint[i] );
}
// Set it
outIt.Set( deformation );
// Update stuff
progress.CompletedPixel();
++outIt;
transformedPoint += delta;
}
outIt.NextLine();
}
} // end LinearThreadedGenerateData()
/**
* Inform pipeline of required output region
*/
template <class TOutputImage, class TTransformPrecisionType>
void
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::GenerateOutputInformation( void )
{
// call the superclass' implementation of this method
Superclass::GenerateOutputInformation();
// get pointer to the output
OutputImagePointer outputPtr = this->GetOutput();
if ( !outputPtr )
{
return;
}
outputPtr->SetLargestPossibleRegion( m_OutputRegion );
outputPtr->SetSpacing( m_OutputSpacing );
outputPtr->SetOrigin( m_OutputOrigin );
outputPtr->SetDirection( m_OutputDirection );
} // end GenerateOutputInformation()
/**
* Verify if any of the components has been modified.
*/
template <class TOutputImage, class TTransformPrecisionType>
unsigned long
TransformToDeformationFieldSource<TOutputImage, TTransformPrecisionType>
::GetMTime( void ) const
{
unsigned long latestTime = Object::GetMTime();
if ( this->m_Transform )
{
if ( latestTime < this->m_Transform->GetMTime() )
{
latestTime = this->m_Transform->GetMTime();
}
}
return latestTime;
} // end GetMTime()
} // end namespace itk
#endif // end #ifndef _itkTransformToDeformationFieldSource_txx
|