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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkBSplineDeformableTransform.txx,v $
Language: C++
Date: $Date: 2008-04-11 16:28:11 $
Version: $Revision: 1.40 $
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 __itkBSplineDeformableTransform_txx
#define __itkBSplineDeformableTransform_txx
#include "itkBSplineDeformableTransform.h"
#include "itkContinuousIndex.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkIdentityTransform.h"
namespace itk
{
// Constructor with default arguments
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::BSplineDeformableTransform():Superclass(SpaceDimension,0)
{
// Instantiate a weights function
m_WeightsFunction = WeightsFunctionType::New();
m_SupportSize = m_WeightsFunction->GetSupportSize();
// Instantiate an identity transform
typedef IdentityTransform<ScalarType, SpaceDimension> IdentityTransformType;
typename IdentityTransformType::Pointer id = IdentityTransformType::New();
m_BulkTransform = id;
// Default grid size is zero
typename RegionType::SizeType size;
typename RegionType::IndexType index;
size.Fill( 0 );
index.Fill( 0 );
m_GridRegion.SetSize( size );
m_GridRegion.SetIndex( index );
m_GridOrigin.Fill( 0.0 ); // default origin is all zeros
m_GridSpacing.Fill( 1.0 ); // default spacing is all ones
m_GridDirection.SetIdentity(); // default spacing is all ones
m_InternalParametersBuffer = ParametersType(0);
// Make sure the parameters pointer is not NULL after construction.
m_InputParametersPointer = &m_InternalParametersBuffer;
// Initialize coeffient images
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_WrappedImage[j] = ImageType::New();
m_WrappedImage[j]->SetRegions( m_GridRegion );
m_WrappedImage[j]->SetOrigin( m_GridOrigin.GetDataPointer() );
m_WrappedImage[j]->SetSpacing( m_GridSpacing.GetDataPointer() );
m_WrappedImage[j]->SetDirection( m_GridDirection );
m_CoefficientImage[j] = NULL;
}
// Setup variables for computing interpolation
m_Offset = SplineOrder / 2;
if ( SplineOrder % 2 )
{
m_SplineOrderOdd = true;
}
else
{
m_SplineOrderOdd = false;
}
m_ValidRegion = m_GridRegion;
// Initialize jacobian images
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_JacobianImage[j] = ImageType::New();
m_JacobianImage[j]->SetRegions( m_GridRegion );
m_JacobianImage[j]->SetOrigin( m_GridOrigin.GetDataPointer() );
m_JacobianImage[j]->SetSpacing( m_GridSpacing.GetDataPointer() );
m_JacobianImage[j]->SetDirection( m_GridDirection );
}
/** Fixed Parameters store the following information:
* Grid Size
* Grid Origin
* Grid Spacing
* Grid Direction
* The size of these is equal to the NInputDimensions
*/
this->m_FixedParameters.SetSize ( NDimensions * (NDimensions + 3) );
this->m_FixedParameters.Fill ( 0.0 );
for (unsigned int i=0; i<NDimensions; i++)
{
this->m_FixedParameters[2*NDimensions+i] = m_GridSpacing[i];
}
for (unsigned int di=0; di<NDimensions; di++)
{
for (unsigned int dj=0; dj<NDimensions; dj++)
{
this->m_FixedParameters[3*NDimensions+(di*NDimensions+dj)] = m_GridDirection[di][dj];
}
}
DirectionType scale;
for( unsigned int i=0; i<SpaceDimension; i++)
{
scale[i][i] = m_GridSpacing[i];
}
m_IndexToPoint = m_GridDirection * scale;
m_PointToIndex = m_IndexToPoint.GetInverse();
m_LastJacobianIndex = m_ValidRegion.GetIndex();
}
// Destructor
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::~BSplineDeformableTransform()
{
}
// Get the number of parameters
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
unsigned int
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::GetNumberOfParameters(void) const
{
// The number of parameters equal SpaceDimension * number of
// of pixels in the grid region.
return ( static_cast<unsigned int>( SpaceDimension ) *
static_cast<unsigned int>( m_GridRegion.GetNumberOfPixels() ) );
}
// Get the number of parameters per dimension
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
unsigned int
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::GetNumberOfParametersPerDimension(void) const
{
// The number of parameters per dimension equal number of
// of pixels in the grid region.
return ( static_cast<unsigned int>( m_GridRegion.GetNumberOfPixels() ) );
}
// Set the grid region
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetGridRegion( const RegionType& region )
{
if ( m_GridRegion != region )
{
m_GridRegion = region;
// set regions for each coefficient and jacobian image
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_WrappedImage[j]->SetRegions( m_GridRegion );
m_JacobianImage[j]->SetRegions( m_GridRegion );
}
// Set the valid region
// If the grid spans the interval [start, last].
// The valid interval for evaluation is [start+offset, last-offset]
// when spline order is even.
// The valid interval for evaluation is [start+offset, last-offset)
// when spline order is odd.
// Where offset = vcl_floor(spline / 2 ).
// Note that the last pixel is not included in the valid region
// with odd spline orders.
typename RegionType::SizeType size = m_GridRegion.GetSize();
typename RegionType::IndexType index = m_GridRegion.GetIndex();
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
index[j] +=
static_cast< typename RegionType::IndexValueType >( m_Offset );
size[j] -=
static_cast< typename RegionType::SizeValueType> ( 2 * m_Offset );
m_ValidRegionLast[j] = index[j] +
static_cast< typename RegionType::IndexValueType >( size[j] ) - 1;
}
m_ValidRegion.SetSize( size );
m_ValidRegion.SetIndex( index );
//
// If we are using the default parameters, update their size and set to identity.
//
// Input parameters point to internal buffer => using default parameters.
if (m_InputParametersPointer == &m_InternalParametersBuffer)
{
// Check if we need to resize the default parameter buffer.
if ( m_InternalParametersBuffer.GetSize() != this->GetNumberOfParameters() )
{
m_InternalParametersBuffer.SetSize( this->GetNumberOfParameters() );
// Fill with zeros for identity.
m_InternalParametersBuffer.Fill( 0 );
}
}
this->Modified();
}
}
// Set the grid spacing
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetGridSpacing( const SpacingType& spacing )
{
if ( m_GridSpacing != spacing )
{
m_GridSpacing = spacing;
// set spacing for each coefficient and jacobian image
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_WrappedImage[j]->SetSpacing( m_GridSpacing.GetDataPointer() );
m_JacobianImage[j]->SetSpacing( m_GridSpacing.GetDataPointer() );
}
DirectionType scale;
for( unsigned int i=0; i<SpaceDimension; i++)
{
scale[i][i] = m_GridSpacing[i];
}
m_IndexToPoint = m_GridDirection * scale;
m_PointToIndex = m_IndexToPoint.GetInverse();
this->Modified();
}
}
// Set the grid direction
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetGridDirection( const DirectionType & direction )
{
if ( m_GridDirection != direction )
{
m_GridDirection = direction;
// set direction for each coefficient and jacobian image
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_WrappedImage[j]->SetDirection( m_GridDirection );
m_JacobianImage[j]->SetDirection( m_GridDirection );
}
DirectionType scale;
for( unsigned int i=0; i<SpaceDimension; i++)
{
scale[i][i] = m_GridSpacing[i];
}
m_IndexToPoint = m_GridDirection * scale;
m_PointToIndex = m_IndexToPoint.GetInverse();
this->Modified();
}
}
// Set the grid origin
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetGridOrigin( const OriginType& origin )
{
if ( m_GridOrigin != origin )
{
m_GridOrigin = origin;
// set spacing for each coefficient and jacobianimage
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_WrappedImage[j]->SetOrigin( m_GridOrigin.GetDataPointer() );
m_JacobianImage[j]->SetOrigin( m_GridOrigin.GetDataPointer() );
}
this->Modified();
}
}
// Set the parameters
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetIdentity()
{
if( m_InputParametersPointer )
{
ParametersType * parameters =
const_cast<ParametersType *>( m_InputParametersPointer );
parameters->Fill( 0.0 );
this->Modified();
}
else
{
itkExceptionMacro( << "Input parameters for the spline haven't been set ! "
<< "Set them using the SetParameters or SetCoefficientImage method first." );
}
}
// Set the parameters
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetParameters( const ParametersType & parameters )
{
// check if the number of parameters match the
// expected number of parameters
if ( parameters.Size() != this->GetNumberOfParameters() )
{
itkExceptionMacro(<<"Mismatched between parameters size "
<< parameters.size()
<< " and region size "
<< m_GridRegion.GetNumberOfPixels() );
}
// Clean up buffered parameters
m_InternalParametersBuffer = ParametersType( 0 );
// Keep a reference to the input parameters
m_InputParametersPointer = ¶meters;
// Wrap flat array as images of coefficients
this->WrapAsImages();
// Modified is always called since we just have a pointer to the
// parameters and cannot know if the parameters have changed.
this->Modified();
}
// Set the Fixed Parameters
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetFixedParameters( const ParametersType & passedParameters )
{
ParametersType parameters( NDimensions * (3 + NDimensions) );
// check if the number of parameters match the
// expected number of parameters
if ( passedParameters.Size() == NDimensions * 3 )
{
parameters.Fill( 0.0 );
for(unsigned int i=0; i<3 * NDimensions; i++)
{
parameters[i] = passedParameters[i];
}
for (unsigned int di=0; di<NDimensions; di++)
{
parameters[3*NDimensions+(di*NDimensions+di)] = 1;
}
}
else if ( passedParameters.Size() != NDimensions * (3 + NDimensions) )
{
itkExceptionMacro(<< "Mismatched between parameters size "
<< passedParameters.size()
<< " and number of fixed parameters "
<< NDimensions * (3 + NDimensions) );
}
else
{
for(unsigned int i=0; i<NDimensions * (3 + NDimensions); i++)
{
parameters[i] = passedParameters[i];
}
}
/*********************************************************
Fixed Parameters store the following information:
Grid Size
Grid Origin
Grid Spacing
Grid Direction
The size of these is equal to the NInputDimensions
*********************************************************/
/** Set the Grid Parameters */
SizeType gridSize;
for (unsigned int i=0; i<NDimensions; i++)
{
gridSize[i] = static_cast<int> (parameters[i]);
}
RegionType bsplineRegion;
bsplineRegion.SetSize( gridSize );
/** Set the Origin Parameters */
OriginType origin;
for (unsigned int i=0; i<NDimensions; i++)
{
origin[i] = parameters[NDimensions+i];
}
/** Set the Spacing Parameters */
SpacingType spacing;
for (unsigned int i=0; i<NDimensions; i++)
{
spacing[i] = parameters[2*NDimensions+i];
}
/** Set the Direction Parameters */
DirectionType direction;
for (unsigned int di=0; di<NDimensions; di++)
{
for (unsigned int dj=0; dj<NDimensions; dj++)
{
direction[di][dj] = parameters[3*NDimensions+(di*NDimensions+dj)];
}
}
this->SetGridSpacing( spacing );
this->SetGridDirection( direction );
this->SetGridOrigin( origin );
this->SetGridRegion( bsplineRegion );
this->Modified();
}
// Wrap flat parameters as images
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::WrapAsImages()
{
/**
* Wrap flat parameters array into SpaceDimension number of ITK images
* NOTE: For efficiency, parameters are not copied locally. The parameters
* are assumed to be maintained by the caller.
*/
PixelType * dataPointer =
const_cast<PixelType *>(( m_InputParametersPointer->data_block() ));
unsigned int numberOfPixels = m_GridRegion.GetNumberOfPixels();
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_WrappedImage[j]->GetPixelContainer()->
SetImportPointer( dataPointer, numberOfPixels );
dataPointer += numberOfPixels;
m_CoefficientImage[j] = m_WrappedImage[j];
}
/**
* Allocate memory for Jacobian and wrap into SpaceDimension number
* of ITK images
*/
this->m_Jacobian.set_size( SpaceDimension, this->GetNumberOfParameters() );
this->m_Jacobian.Fill( NumericTraits<JacobianPixelType>::Zero );
m_LastJacobianIndex = m_ValidRegion.GetIndex();
JacobianPixelType * jacobianDataPointer = this->m_Jacobian.data_block();
for ( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_JacobianImage[j]->GetPixelContainer()->
SetImportPointer( jacobianDataPointer, numberOfPixels );
jacobianDataPointer += this->GetNumberOfParameters() + numberOfPixels;
}
}
// Set the parameters by value
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetParametersByValue( const ParametersType & parameters )
{
// check if the number of parameters match the
// expected number of parameters
if ( parameters.Size() != this->GetNumberOfParameters() )
{
itkExceptionMacro(<<"Mismatched between parameters size "
<< parameters.size()
<< " and region size "
<< m_GridRegion.GetNumberOfPixels() );
}
// copy it
m_InternalParametersBuffer = parameters;
m_InputParametersPointer = &m_InternalParametersBuffer;
// wrap flat array as images of coefficients
this->WrapAsImages();
// Modified is always called since we just have a pointer to the
// parameters and cannot know if the parameters have changed.
this->Modified();
}
// Get the parameters
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
const
typename BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::ParametersType &
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::GetParameters( void ) const
{
/** NOTE: For efficiency, this class does not keep a copy of the parameters -
* it just keeps pointer to input parameters.
*/
if (NULL == m_InputParametersPointer)
{
itkExceptionMacro( <<"Cannot GetParameters() because m_InputParametersPointer is NULL. Perhaps SetCoefficientImage() has been called causing the NULL pointer." );
}
return (*m_InputParametersPointer);
}
// Get the parameters
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
const
typename BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::ParametersType &
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::GetFixedParameters( void ) const
{
RegionType resRegion = this->GetGridRegion( );
for (unsigned int i=0; i<NDimensions; i++)
{
this->m_FixedParameters[i] = (resRegion.GetSize())[i];
}
for (unsigned int i=0; i<NDimensions; i++)
{
this->m_FixedParameters[NDimensions+i] = (this->GetGridOrigin())[i];
}
for (unsigned int i=0; i<NDimensions; i++)
{
this->m_FixedParameters[2*NDimensions+i] = (this->GetGridSpacing())[i];
}
for (unsigned int di=0; di<NDimensions; di++)
{
for (unsigned int dj=0; dj<NDimensions; dj++)
{
this->m_FixedParameters[3*NDimensions+(di*NDimensions+dj)] = (this->GetGridDirection())[di][dj];
}
}
return (this->m_FixedParameters);
}
// Set the B-Spline coefficients using input images
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::SetCoefficientImage( ImagePointer images[] )
{
if ( images[0] )
{
this->SetGridRegion( images[0]->GetBufferedRegion() );
this->SetGridSpacing( images[0]->GetSpacing() );
this->SetGridDirection( images[0]->GetDirection() );
this->SetGridOrigin( images[0]->GetOrigin() );
for( unsigned int j = 0; j < SpaceDimension; j++ )
{
m_CoefficientImage[j] = images[j];
}
// Clean up buffered parameters
m_InternalParametersBuffer = ParametersType( 0 );
m_InputParametersPointer = NULL;
}
}
// Print self
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::PrintSelf(std::ostream &os, Indent indent) const
{
unsigned int j;
this->Superclass::PrintSelf(os, indent);
os << indent << "GridRegion: " << m_GridRegion << std::endl;
os << indent << "GridOrigin: " << m_GridOrigin << std::endl;
os << indent << "GridSpacing: " << m_GridSpacing << std::endl;
os << indent << "GridDirection: " << m_GridDirection << std::endl;
os << indent << "IndexToPoint: " << m_IndexToPoint << std::endl;
os << indent << "PointToIndex: " << m_PointToIndex << std::endl;
os << indent << "CoefficientImage: [ ";
for ( j = 0; j < SpaceDimension - 1; j++ )
{
os << m_CoefficientImage[j].GetPointer() << ", ";
}
os << m_CoefficientImage[j].GetPointer() << " ]" << std::endl;
os << indent << "WrappedImage: [ ";
for ( j = 0; j < SpaceDimension - 1; j++ )
{
os << m_WrappedImage[j].GetPointer() << ", ";
}
os << m_WrappedImage[j].GetPointer() << " ]" << std::endl;
os << indent << "InputParametersPointer: "
<< m_InputParametersPointer << std::endl;
os << indent << "ValidRegion: " << m_ValidRegion << std::endl;
os << indent << "LastJacobianIndex: " << m_LastJacobianIndex << std::endl;
os << indent << "BulkTransform: ";
os << m_BulkTransform.GetPointer() << std::endl;
os << indent << "WeightsFunction: ";
os << m_WeightsFunction.GetPointer() << std::endl;
if ( m_BulkTransform )
{
os << indent << "BulkTransformType: "
<< m_BulkTransform->GetNameOfClass() << std::endl;
}
}
// Transform a point
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
bool
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::InsideValidRegion(
const ContinuousIndexType& index ) const
{
bool inside = true;
if ( !m_ValidRegion.IsInside( index ) )
{
inside = false;
}
if ( inside && m_SplineOrderOdd )
{
typedef typename ContinuousIndexType::ValueType ValueType;
for( unsigned int j = 0; j < SpaceDimension; j++ )
{
if ( index[j] >= static_cast<ValueType>( m_ValidRegionLast[j] ) )
{
inside = false;
break;
}
}
}
return inside;
}
// Transform a point
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::TransformPoint(
const InputPointType & point,
OutputPointType & outputPoint,
WeightsType & weights,
ParameterIndexArrayType & indices,
bool& inside ) const
{
unsigned int j;
IndexType supportIndex;
InputPointType transformedPoint;
if ( m_BulkTransform )
{
transformedPoint = m_BulkTransform->TransformPoint( point );
}
else
{
transformedPoint = point;
}
if ( m_CoefficientImage[0] )
{
ContinuousIndexType index;
this->TransformPointToContinuousIndex( point, index );
// NOTE: if the support region does not lie totally within the grid
// we assume zero displacement and return the input point
inside = this->InsideValidRegion( index );
if ( !inside )
{
outputPoint = transformedPoint;
return;
}
// Compute interpolation weights
m_WeightsFunction->Evaluate( index, weights, supportIndex );
// For each dimension, correlate coefficient with weights
RegionType supportRegion;
supportRegion.SetSize( m_SupportSize );
supportRegion.SetIndex( supportIndex );
outputPoint.Fill( NumericTraits<ScalarType>::Zero );
typedef ImageRegionConstIterator<ImageType> IteratorType;
IteratorType m_Iterator[ SpaceDimension ];
unsigned long counter = 0;
const PixelType * basePointer = m_CoefficientImage[0]->GetBufferPointer();
for ( j = 0; j < SpaceDimension; j++ )
{
m_Iterator[j] = IteratorType( m_CoefficientImage[j], supportRegion );
}
while ( ! m_Iterator[0].IsAtEnd() )
{
// multiply weigth with coefficient
for ( j = 0; j < SpaceDimension; j++ )
{
outputPoint[j] += static_cast<ScalarType>(
weights[counter] * m_Iterator[j].Get());
}
// populate the indices array
indices[counter] = &(m_Iterator[0].Value()) - basePointer;
// go to next coefficient in the support region
++ counter;
for ( j = 0; j < SpaceDimension; j++ )
{
++( m_Iterator[j] );
}
}
// return results
for ( j = 0; j < SpaceDimension; j++ )
{
outputPoint[j] += transformedPoint[j];
}
}
else
{
itkWarningMacro( << "B-spline coefficients have not been set" );
for ( j = 0; j < SpaceDimension; j++ )
{
outputPoint[j] = transformedPoint[j];
}
}
}
// Transform a point
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
typename BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::OutputPointType
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::TransformPoint(const InputPointType &point) const
{
WeightsType weights( m_WeightsFunction->GetNumberOfWeights() );
ParameterIndexArrayType indices( m_WeightsFunction->GetNumberOfWeights() );
OutputPointType outputPoint;
bool inside;
this->TransformPoint( point, outputPoint, weights, indices, inside );
return outputPoint;
}
// Compute the Jacobian in one position
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
const
typename BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::JacobianType &
BSplineDeformableTransform<TScalarType, NDimensions, VSplineOrder>
::GetJacobian( const InputPointType & point ) const
{
// Can only compute Jacobian if parameters are set via
// SetParameters or SetParametersByValue
if( m_InputParametersPointer == NULL )
{
itkExceptionMacro( <<"Cannot compute Jacobian: parameters not set" );
}
// Zero all components of jacobian
// NOTE: for efficiency, we only need to zero out the coefficients
// that got fill last time this method was called.
RegionType supportRegion;
supportRegion.SetSize( m_SupportSize );
supportRegion.SetIndex( m_LastJacobianIndex );
typedef ImageRegionIterator<JacobianImageType> IteratorType;
IteratorType m_Iterator[ SpaceDimension ];
unsigned int j;
for ( j = 0; j < SpaceDimension; j++ )
{
m_Iterator[j] = IteratorType( m_JacobianImage[j], supportRegion );
}
while ( ! m_Iterator[0].IsAtEnd() )
{
// zero out jacobian elements
for ( j = 0; j < SpaceDimension; j++ )
{
m_Iterator[j].Set( NumericTraits<JacobianPixelType>::Zero );
}
for ( j = 0; j < SpaceDimension; j++ )
{
++( m_Iterator[j] );
}
}
ContinuousIndexType index;
this->TransformPointToContinuousIndex( point, index );
// NOTE: if the support region does not lie totally within the grid
// we assume zero displacement and return the input point
if ( !this->InsideValidRegion( index ) )
{
return this->m_Jacobian;
}
// Compute interpolation weights
WeightsType weights( m_WeightsFunction->GetNumberOfWeights() );
IndexType supportIndex;
m_WeightsFunction->Evaluate( index, weights, supportIndex );
m_LastJacobianIndex = supportIndex;
// For each dimension, copy the weight to the support region
supportRegion.SetIndex( supportIndex );
unsigned long counter = 0;
for ( j = 0; j < SpaceDimension; j++ )
{
m_Iterator[j] = IteratorType( m_JacobianImage[j], supportRegion );
}
while ( ! m_Iterator[0].IsAtEnd() )
{
// copy weight to jacobian image
for ( j = 0; j < SpaceDimension; j++ )
{
m_Iterator[j].Set( static_cast<JacobianPixelType>( weights[counter] ) );
}
// go to next coefficient in the support region
++ counter;
for ( j = 0; j < SpaceDimension; j++ )
{
++( m_Iterator[j] );
}
}
// Return the results
return this->m_Jacobian;
}
// Compute the Jacobian in one position
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions,VSplineOrder>
::GetJacobian( const InputPointType & point, WeightsType& weights, ParameterIndexArrayType& indexes) const
{
RegionType supportRegion;
supportRegion.SetSize( m_SupportSize );
const PixelType * basePointer = m_CoefficientImage[0]->GetBufferPointer();
ContinuousIndexType index;
this->TransformPointToContinuousIndex( point, index );
// NOTE: if the support region does not lie totally within the grid
// we assume zero displacement and return the input point
if ( !this->InsideValidRegion( index ) )
{
weights.Fill(0.0);
indexes.Fill(0);
return;
}
// Compute interpolation weights
IndexType supportIndex;
m_WeightsFunction->Evaluate( index, weights, supportIndex );
// For each dimension, copy the weight to the support region
supportRegion.SetIndex( supportIndex );
unsigned long counter = 0;
typedef ImageRegionIterator<JacobianImageType> IteratorType;
IteratorType m_Iterator = IteratorType( m_CoefficientImage[0], supportRegion );
while ( ! m_Iterator.IsAtEnd() )
{
indexes[counter] = &(m_Iterator.Value()) - basePointer;
// go to next coefficient in the support region
++ counter;
++m_Iterator;
}
}
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
void
BSplineDeformableTransform<TScalarType, NDimensions,VSplineOrder>
::TransformPointToContinuousIndex( const InputPointType & point, ContinuousIndexType & index ) const
{
unsigned int j;
Vector<double, SpaceDimension> tvector;
for ( j = 0; j < SpaceDimension; j++ )
{
tvector[j] = point[j] - this->m_GridOrigin[j];
}
Vector<double, SpaceDimension> cvector;
cvector = m_PointToIndex * tvector;
for ( j = 0; j < SpaceDimension; j++ )
{
index[j] = static_cast< typename ContinuousIndexType::CoordRepType >( cvector[j] );
}
}
template<class TScalarType, unsigned int NDimensions, unsigned int VSplineOrder>
unsigned int
BSplineDeformableTransform<TScalarType, NDimensions,VSplineOrder>
::GetNumberOfAffectedWeights() const
{
return m_WeightsFunction->GetNumberOfWeights();
}
} // namespace
#endif
|