Medical Imaging Interaction Toolkit  2018.4.99-b585543d
Medical Imaging Interaction Toolkit
mitkMAPRegistrationWrapperIO.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include <iostream>
14 #include <fstream>
15 
16 #include <clocale>
17 
18 #include "mapRegistration.h"
19 #include "mapRegistrationFileWriter.h"
20 #include "mapRegistrationFileReader.h"
21 #include "mapLazyFileFieldKernelLoader.h"
22 
23 #include <mitkCustomMimeType.h>
24 #include <mitkIOMimeTypes.h>
25 #include <mitkLocaleSwitch.h>
26 
29 
30 namespace mitk
31 {
32 
38  template< unsigned int i, unsigned int j, template < unsigned int, unsigned int> class TFunctor>
39  class DimHelperSub
40  {
41  public:
42  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data)
43  {
44  if (TFunctor<i,j>::Execute(obj, data))
45  {
46  return true;
47  }
48  return DimHelperSub<i,j-1,TFunctor>::Execute(obj, data);
49  }
50  };
51 
55  template< unsigned int i, template < unsigned int, unsigned int> class TFunctor>
56  class DimHelperSub<i,1,TFunctor >
57  {
58  public:
59  static bool Execute(const mitk::MAPRegistrationWrapper*, const map::core::String&)
60  {
61  //just unwind. Go to the next "row" with DimHelper
62  return false;
63  }
64  };
65 
74  template< unsigned int i, unsigned int j, template < unsigned int, unsigned int> class TFunctor>
75  class DimHelper{
76  public:
77  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data = "")
78  {
79  if (DimHelperSub<i,j,TFunctor>::Execute(obj, data))
80  {
81  return true;
82  }
83  return DimHelper<i-1,j,TFunctor>::Execute(obj, data);
84  }
85  };
86 
90  template< unsigned int j, template < unsigned int, unsigned int> class TFunctor>
91  class DimHelper<1,j, TFunctor >
92  {
93  public:
94  static bool Execute(const mitk::MAPRegistrationWrapper*, const map::core::String&)
95  {
96  //just unwind. We are done.
97  return false;
98  }
99  };
100 
104  template<unsigned int i, unsigned int j>
105  class CanWrite
106  {
107  public:
108  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& = "")
109  {
110  bool result = false;
111 
112  result = dynamic_cast<const map::core::Registration<i,j> *>(obj->GetRegistration()) != nullptr;
113 
114  return result;
115  }
116  };
117 
120  template<unsigned int i, unsigned int j>
121  class WriteReg
122  {
123  public:
124  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data)
125  {
126  const map::core::Registration<i,j>* pReg = dynamic_cast<const map::core::Registration<i,j>*>(obj->GetRegistration());
127  if (pReg == nullptr)
128  {
129  return false;
130  }
131 
132  typedef map::io::RegistrationFileWriter<i,j> WriterType;
133  typename WriterType::Pointer writer = WriterType::New();
134 
135  writer->setExpandLazyKernels(false);
136 
137  try
138  {
139  writer->write(pReg,data);
140  }
141  catch (const itk::ExceptionObject& e)
142  {
143  std::cout << e.what() << std::endl;
144  throw;
145  }
146 
147  return true;
148  }
149  };
150 
151  MAPRegistrationWrapperIO::MAPRegistrationWrapperIO(const MAPRegistrationWrapperIO& other)
152  : AbstractFileIO(other)
153  {
154  }
155 
157  {
158  std::string category = "MatchPoint Registration File";
159  CustomMimeType customMimeType;
160  customMimeType.SetCategory(category);
161  customMimeType.AddExtension("mapr");
162  this->AbstractFileIOWriter::SetMimeType(customMimeType);
163  this->AbstractFileIOWriter::SetDescription(category);
164 
165  customMimeType.AddExtension("mapr.xml");
166  customMimeType.AddExtension("MAPR");
167  customMimeType.AddExtension("MAPR.XML");
168  this->AbstractFileIOReader::SetMimeType(customMimeType);
169  this->AbstractFileIOReader::SetDescription(category);
170 
171  this->RegisterService();
172  }
173 
174 
176  {
177  bool success = false;
178  const BaseData* input = this->GetInput();
179  if (input == nullptr)
180  {
181  mitkThrow() << "Cannot write data. Data pointer is nullptr.";
182  }
183 
184  const mitk::MAPRegistrationWrapper* wrapper = dynamic_cast<const mitk::MAPRegistrationWrapper*>(input);
185  if (wrapper == nullptr)
186  {
187  mitkThrow() << "Cannot write data. Data pointer is not a Registration wrapper.";
188  }
189 
190  std::ostream* writeStream = this->GetOutputStream();
191  std::string fileName = this->GetOutputLocation();
192  if (writeStream)
193  {
194  fileName = this->GetLocalFileName();
195  }
196 
197  // Switch the current locale to "C"
198  LocaleSwitch localeSwitch("C");
199 
200  try
201  {
202  success = DimHelper<3,3,WriteReg>::Execute(wrapper, fileName);
203  }
204  catch (const std::exception& e)
205  {
206  mitkThrow() << e.what();
207  }
208 
209  if (!success)
210  {
211  mitkThrow() << "Cannot write registration. Currently only registrations up to 4D are supported.";
212  }
213  }
214 
216  {
217  const mitk::MAPRegistrationWrapper* regWrapper = dynamic_cast<const mitk::MAPRegistrationWrapper*>(this->GetInput());
218 
219  if (regWrapper == nullptr)
220  {
222  }
223 
224  // Check if the registration dimension is supported
225  if (! DimHelper<3,3,CanWrite>::Execute(regWrapper))
226  {
228  };
229 
230  return IFileWriter::Supported;
231  }
232 
233  std::vector<BaseData::Pointer > MAPRegistrationWrapperIO::DoRead()
234  {
235  std::vector<BaseData::Pointer > result;
236 
237  LocaleSwitch("C");
238 
239  std::string fileName = this->GetLocalFileName();
240  if ( fileName.empty() )
241  {
242  mitkThrow() << "Cannot read file. Filename has not been set!";
243  }
244 
245  /* Remove the following kernel loader provider because in MITK no lazy file loading should be used
246  due to conflicts with session loading (end there usage of temporary directories)*/
247  map::io::RegistrationFileReader::LoaderStackType::unregisterProvider(map::io::LazyFileFieldKernelLoader<2,2>::getStaticProviderName());
248  map::io::RegistrationFileReader::LoaderStackType::unregisterProvider(map::io::LazyFileFieldKernelLoader<3,3>::getStaticProviderName());
249 
250  map::io::RegistrationFileReader::Pointer spReader = map::io::RegistrationFileReader::New();
251  spReader->setPreferLazyLoading(true);
252  map::core::RegistrationBase::Pointer spReg = spReader->read(fileName);
254  spRegWrapper->SetRegistration(spReg);
255 
256  result.push_back(spRegWrapper.GetPointer());
257  return result;
258  }
259 
261  {
263 
264  std::string fileName = this->GetLocalFileName();
265  std::ifstream in( fileName.c_str() );
266  if ( in.good() )
267  {
268  result = IFileReader::Supported;
269  }
270 
271  in.close();
272  return result;
273  }
274 
275  MAPRegistrationWrapperIO* MAPRegistrationWrapperIO::IOClone() const
276  {
277  return new MAPRegistrationWrapperIO(*this);
278  }
279 
280 }
Base of all data objects.
Definition: mitkBaseData.h:37
DataCollection - Class to facilitate loading/accessing structured data.
ConfidenceLevel GetWriterConfidenceLevel() const override
MAPRegistrationWrapper Wrapper class to allow the handling of MatchPoint registration objects as mitk...
static const char * GetStaticNameOfClass()
Definition: mitkBaseData.h:41
std::vector< itk::SmartPointer< BaseData > > DoRead() override
void SetMimeType(const CustomMimeType &mimeType)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
std::string GetLocalFileName() const
Get a local file name for reading.
::map::core::RegistrationBase * GetRegistration()
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:45
Convenience class to temporarily change the current locale.
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
#define mitkThrow()
void SetDescription(const std::string &description)
const BaseData * GetInput() const override
Get the input data set via SetInput().
void AddExtension(const std::string &extension)
ConfidenceLevel GetReaderConfidenceLevel() const override
void SetCategory(const std::string &category)
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
std::ostream * GetOutputStream() const override
Get the output stream.
void SetMimeType(const CustomMimeType &mimeType)
std::string GetOutputLocation() const override
Get the current output location.
Abstract class for implementing a reader and writer.
void SetDescription(const std::string &description)
Sets a human readable description of this writer.
void Write() override
Write the base data to the specified location or output stream.