Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
itkBSplineDeformableTransformInitializer.txx
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 /*===================================================================
18 
19 This file is based heavily on a corresponding ITK filter.
20 
21 ===================================================================*/
22 
23 #ifndef __itkBSplineDeformableTransformInitializer_txx
24 #define __itkBSplineDeformableTransformInitializer_txx
25 
26 #include "itkBSplineDeformableTransformInitializer.h"
27 
28 namespace itk
29 {
30  template <class TTransform, class TImage>
31  BSplineDeformableTransformInitializer<TTransform, TImage>::BSplineDeformableTransformInitializer()
32  {
33  this->m_GridSizeInsideTheImage.Fill(5);
34  }
35 
36  template <class TTransform, class TImage>
37  void BSplineDeformableTransformInitializer<TTransform, TImage>::InitializeTransform() const
38  {
39  // Sanity check
40  if (!this->m_Image)
41  {
42  itkExceptionMacro("Reference Image has not been set");
43  return;
44  }
45 
46  if (!this->m_Transform)
47  {
48  itkExceptionMacro("Transform has not been set");
49  return;
50  }
51 
52  // If the image come from a filter, then update that filter.
53  if (this->m_Image->GetSource())
54  {
55  this->m_Image->GetSource()->Update();
56  }
57 
58  typedef typename TransformType::RegionType RegionType;
59 
60  typename RegionType::SizeType numberOfGridNodesOutsideTheImageSupport;
61  typename RegionType::SizeType totalGridSize;
62 
63  numberOfGridNodesOutsideTheImageSupport.Fill(TransformType::SplineOrder);
64 
65  totalGridSize = this->m_GridSizeInsideTheImage;
66  totalGridSize += numberOfGridNodesOutsideTheImageSupport;
67 
68  RegionType gridRegion;
69  gridRegion.SetSize(totalGridSize);
70 
71  typedef typename TransformType::SpacingType SpacingType;
72  const SpacingType &imageSpacing = this->m_Image->GetSpacing();
73 
74  typedef typename TransformType::OriginType OriginType;
75  const OriginType &imageOrigin = this->m_Image->GetOrigin();
76  ;
77 
78  const typename TransformType::RegionType &imageRegion = this->m_Image->GetLargestPossibleRegion();
79 
80  typename ImageType::SizeType fixedImageSize = imageRegion.GetSize();
81 
82  SpacingType gridSpacing;
83  SpacingType gridOriginShift;
84 
85  const unsigned int orderShift = TransformType::SplineOrder / 2;
86 
87  for (unsigned int r = 0; r < SpaceDimension; r++)
88  {
89  const unsigned int numberOfGridCells = this->m_GridSizeInsideTheImage[r] - 1;
90  const unsigned int numberOfImagePixels = fixedImageSize[r];
91 
92  gridSpacing[r] =
93  imageSpacing[r] * static_cast<double>(numberOfImagePixels) / static_cast<double>(numberOfGridCells);
94 
95  // Shift half image pixel to cover the image support
96  const double imageSupportShift = -imageSpacing[r] / 2.0;
97 
98  // Shift by the number of extra grid cells required by
99  // the BSpline order.
100  const double gridSupportShift = -1.0 * gridSpacing[r] * orderShift;
101 
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;
105  }
106 
107  typename ImageType::DirectionType gridDirection = this->m_Image->GetDirection();
108  SpacingType gridOriginOffset = gridDirection * gridOriginShift;
109 
110  OriginType gridOrigin = imageOrigin + gridOriginOffset;
111 
112  this->m_Transform->SetGridRegion(gridRegion);
113  this->m_Transform->SetGridOrigin(gridOrigin);
114  this->m_Transform->SetGridSpacing(gridSpacing);
115  this->m_Transform->SetGridDirection(gridDirection);
116  }
117 
118  template <class TTransform, class TImage>
119  void BSplineDeformableTransformInitializer<TTransform, TImage>::PrintSelf(std::ostream &os, Indent indent) const
120  {
121  Superclass::PrintSelf(os, indent);
122 
123  os << indent << "Transform = " << std::endl;
124  if (this->m_Transform)
125  {
126  os << indent << this->m_Transform << std::endl;
127  }
128  else
129  {
130  os << indent << "None" << std::endl;
131  }
132 
133  os << indent << "Image = " << std::endl;
134  if (this->m_Image)
135  {
136  os << indent << this->m_Image << std::endl;
137  }
138  else
139  {
140  os << indent << "None" << std::endl;
141  }
142  }
143 
144 } // namespace itk
145 
146 #endif