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
mitkMaskedAlgorithmHelper.cpp
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 
18 
19 #include <itkImageMaskSpatialObject.h>
20 
21 // Mitk
22 #include <mitkImageAccessByItk.h>
23 
24 // MatchPoint
25 #include "mapMaskedRegistrationAlgorithmInterface.h"
26 #include <mapRegistrationAlgorithmInterface.h>
27 
28 namespace mitk
29 {
30 
31  MaskedAlgorithmHelper::MaskedAlgorithmHelper(map::algorithm::RegistrationAlgorithmBase* algorithm) : m_AlgorithmBase(algorithm)
32  {
33  }
34 
35  bool
37  CheckSupport(const mitk::Image* movingMask, const mitk::Image* targetMask) const
38  {
39  if (! m_AlgorithmBase) mapDefaultExceptionStaticMacro(<< "Error, cannot check data. Helper has no algorithm defined.");
40 
41  unsigned int movingDim = m_AlgorithmBase->getMovingDimensions();
42  unsigned int targetDim = m_AlgorithmBase->getTargetDimensions();
43 
44  bool result = movingDim == targetDim;
45 
46  if ( movingMask)
47  {
48  result = result && (movingMask->GetDimension() == movingDim);
49 
50  if (movingDim == 2)
51  {
52  typedef itk::Image<MaskPixelType,2> MaskImageType;
53  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
54 
55  result = result && (maskPixelType == movingMask->GetPixelType());
56  }
57  else if (movingDim == 3)
58  {
59  typedef itk::Image<MaskPixelType,3> MaskImageType;
60  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
61 
62  result = result && (maskPixelType == movingMask->GetPixelType());
63  }
64  }
65 
66  if ( targetMask)
67  {
68  result = result && (targetMask->GetDimension() == targetDim);
69 
70  if (movingDim == 2)
71  {
72  typedef itk::Image<MaskPixelType,2> MaskImageType;
73  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
74 
75  result = result && (maskPixelType == targetMask->GetPixelType());
76  }
77  else if (movingDim == 3)
78  {
79  typedef itk::Image<MaskPixelType,3> MaskImageType;
80  mitk::PixelType maskPixelType = mitk::MakePixelType<MaskImageType>();
81 
82  result = result && (maskPixelType == targetMask->GetPixelType());
83  }
84  }
85 
86  if (movingDim == 2)
87  {
88  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<2, 2> MaskedInterface;
89  const MaskedInterface* pMaskedReg = dynamic_cast<const MaskedInterface*>(m_AlgorithmBase.GetPointer());
90 
91  result = result && pMaskedReg;
92  }
93  else if (movingDim == 3)
94  {
95  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<3, 3> MaskedInterface;
96  const MaskedInterface* pMaskedReg = dynamic_cast<const MaskedInterface*>(m_AlgorithmBase.GetPointer());
97 
98  result = result && pMaskedReg;
99  }
100  else
101  {
102  result = false;
103  }
104 
105  return result;
106  };
107 
108  bool MaskedAlgorithmHelper::SetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask)
109  {
110  if (! m_AlgorithmBase) mapDefaultExceptionStaticMacro(<< "Error, cannot set data. Helper has no algorithm defined.");
111 
112  if (! CheckSupport(movingMask, targetMask)) return false;
113 
114  unsigned int movingDim = m_AlgorithmBase->getMovingDimensions();
115  unsigned int targetDim = m_AlgorithmBase->getTargetDimensions();
116 
117  if (movingDim!=targetDim) return false;
118 
119  if (movingDim == 2)
120  {
121  return DoSetMasks<2,2>(movingMask, targetMask);
122  }
123  else if (movingDim == 3)
124  {
125  return DoSetMasks<3,3>(movingMask, targetMask);
126  }
127  return false;
128  };
129 
130  template<unsigned int VImageDimension1, unsigned int VImageDimension2>
131  bool MaskedAlgorithmHelper::DoSetMasks(const mitk::Image* movingMask, const mitk::Image* targetMask)
132  {
133  typedef itk::SpatialObject<VImageDimension1> MovingSpatialType;
134  typedef itk::SpatialObject<VImageDimension2> TargetSpatialType;
135 
136  typedef ::map::algorithm::facet::MaskedRegistrationAlgorithmInterface<VImageDimension1, VImageDimension2> MaskedRegInterface;
137  MaskedRegInterface* pAlg = dynamic_cast<MaskedRegInterface*>(m_AlgorithmBase.GetPointer());
138 
139  if (!pAlg) return false;
140 
141  if (movingMask)
142  {
143  AccessFixedTypeByItk(movingMask, DoConvertMask, (MaskPixelType), (VImageDimension1));
144  typename MovingSpatialType::Pointer movingSpatial = dynamic_cast<MovingSpatialType*>(m_convertResult.GetPointer());
145  if (! movingSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask.");
146  pAlg->setMovingMask(movingSpatial);
147  }
148 
149  if (targetMask)
150  {
151  AccessFixedTypeByItk(targetMask, DoConvertMask, (MaskPixelType), (VImageDimension2));
152  typename TargetSpatialType::Pointer targetSpatial = dynamic_cast<TargetSpatialType*>(m_convertResult.GetPointer());
153  if (! targetSpatial) mapDefaultExceptionStaticMacro(<< "Error, cannot convert moving mask.");
154  pAlg->setTargetMask(targetSpatial);
155  }
156 
157  return true;
158  }
159 
160  template<typename TPixelType, unsigned int VImageDimension>
161  void MaskedAlgorithmHelper::DoConvertMask(const itk::Image<TPixelType,VImageDimension>* mask)
162  {
163  typedef itk::Image<TPixelType,VImageDimension> ImageType;
164  typedef itk::ImageMaskSpatialObject<VImageDimension> SpatialType;
165 
166  typename SpatialType::Pointer spatial = SpatialType::New();
167  spatial->SetImage(mask);
168 
169  m_convertResult = spatial.GetPointer();
170  }
171 
172 }
itk::SmartPointer< Self > Pointer
bool CheckSupport(const mitk::Image *movingMask, const mitk::Image *targetMask) const
DataCollection - Class to facilitate loading/accessing structured data.
#define AccessFixedTypeByItk(mitkImage, itkImageTypeFunction, pixelTypeSeq, dimSeq)
Access a mitk-image with known type (pixel type and dimension) by an itk-image.
map::core::discrete::Elements< 3 >::InternalImageType ImageType
MaskedAlgorithmHelper(map::algorithm::RegistrationAlgorithmBase *algorithm=NULL)
Image class for storing images.
Definition: mitkImage.h:76
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:36
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:105
bool SetMasks(const mitk::Image *movingMask, const mitk::Image *targetMask)
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:110
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.