1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
17 /*===================================================================
19 This file is based heavily on a corresponding ITK filter.
21 ===================================================================*/
23 #ifndef __itkBSplineDeformableTransformInitializer_txx
24 #define __itkBSplineDeformableTransformInitializer_txx
26 #include "itkBSplineDeformableTransformInitializer.h"
30 template <class TTransform, class TImage>
31 BSplineDeformableTransformInitializer<TTransform, TImage>::BSplineDeformableTransformInitializer()
33 this->m_GridSizeInsideTheImage.Fill(5);
36 template <class TTransform, class TImage>
37 void BSplineDeformableTransformInitializer<TTransform, TImage>::InitializeTransform() const
42 itkExceptionMacro("Reference Image has not been set");
46 if (!this->m_Transform)
48 itkExceptionMacro("Transform has not been set");
52 // If the image come from a filter, then update that filter.
53 if (this->m_Image->GetSource())
55 this->m_Image->GetSource()->Update();
58 typedef typename TransformType::RegionType RegionType;
60 typename RegionType::SizeType numberOfGridNodesOutsideTheImageSupport;
61 typename RegionType::SizeType totalGridSize;
63 numberOfGridNodesOutsideTheImageSupport.Fill(TransformType::SplineOrder);
65 totalGridSize = this->m_GridSizeInsideTheImage;
66 totalGridSize += numberOfGridNodesOutsideTheImageSupport;
68 RegionType gridRegion;
69 gridRegion.SetSize(totalGridSize);
71 typedef typename TransformType::SpacingType SpacingType;
72 const SpacingType &imageSpacing = this->m_Image->GetSpacing();
74 typedef typename TransformType::OriginType OriginType;
75 const OriginType &imageOrigin = this->m_Image->GetOrigin();
78 const typename TransformType::RegionType &imageRegion = this->m_Image->GetLargestPossibleRegion();
80 typename ImageType::SizeType fixedImageSize = imageRegion.GetSize();
82 SpacingType gridSpacing;
83 SpacingType gridOriginShift;
85 const unsigned int orderShift = TransformType::SplineOrder / 2;
87 for (unsigned int r = 0; r < SpaceDimension; r++)
89 const unsigned int numberOfGridCells = this->m_GridSizeInsideTheImage[r] - 1;
90 const unsigned int numberOfImagePixels = fixedImageSize[r];
93 imageSpacing[r] * static_cast<double>(numberOfImagePixels) / static_cast<double>(numberOfGridCells);
95 // Shift half image pixel to cover the image support
96 const double imageSupportShift = -imageSpacing[r] / 2.0;
98 // Shift by the number of extra grid cells required by
100 const double gridSupportShift = -1.0 * gridSpacing[r] * orderShift;
102 // Combine both shifts. They are both aligned with the coordinate
103 // system of the grid. Direction has not been considered so far.
104 gridOriginShift[r] = gridSupportShift + imageSupportShift;
107 typename ImageType::DirectionType gridDirection = this->m_Image->GetDirection();
108 SpacingType gridOriginOffset = gridDirection * gridOriginShift;
110 OriginType gridOrigin = imageOrigin + gridOriginOffset;
112 this->m_Transform->SetGridRegion(gridRegion);
113 this->m_Transform->SetGridOrigin(gridOrigin);
114 this->m_Transform->SetGridSpacing(gridSpacing);
115 this->m_Transform->SetGridDirection(gridDirection);
118 template <class TTransform, class TImage>
119 void BSplineDeformableTransformInitializer<TTransform, TImage>::PrintSelf(std::ostream &os, Indent indent) const
121 Superclass::PrintSelf(os, indent);
123 os << indent << "Transform = " << std::endl;
124 if (this->m_Transform)
126 os << indent << this->m_Transform << std::endl;
130 os << indent << "None" << std::endl;
133 os << indent << "Image = " << std::endl;
136 os << indent << this->m_Image << std::endl;
140 os << indent << "None" << std::endl;