Medical Imaging Interaction Toolkit  2016.11.0
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,
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 #include <iostream>
18 #include <fstream>
19 
20 #include <clocale>
21 
22 #include "mapRegistration.h"
23 #include "mapRegistrationFileWriter.h"
24 #include "mapRegistrationFileReader.h"
25 #include "mapLazyFileFieldKernelLoader.h"
26 
27 #include <mitkCustomMimeType.h>
28 #include <mitkIOMimeTypes.h>
29 
32 
33 namespace mitk
34 {
35 
38  struct LocaleSwitch
39  {
40  LocaleSwitch(const std::string& newLocale)
41  : m_OldLocale(std::setlocale(LC_ALL, NULL))
42  , m_NewLocale(newLocale)
43  {
44  if (m_OldLocale == NULL)
45  {
46  m_OldLocale = "";
47  }
48  else if (m_NewLocale != m_OldLocale)
49  {
50  // set the locale
51  if (std::setlocale(LC_ALL, m_NewLocale.c_str()) == NULL)
52  {
53  MITK_INFO << "Could not set locale " << m_NewLocale;
54  m_OldLocale = NULL;
55  }
56  }
57  }
58 
60  {
61  if (m_OldLocale != NULL && std::setlocale(LC_ALL, m_OldLocale) == NULL)
62  {
63  MITK_INFO << "Could not reset locale " << m_OldLocale;
64  }
65  }
66 
67  private:
68  const char* m_OldLocale;
69  const std::string m_NewLocale;
70  };
71 
77  template< unsigned int i, unsigned int j, template < unsigned int, unsigned int> class TFunctor>
78  class DimHelperSub
79  {
80  public:
81  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data)
82  {
83  if (TFunctor<i,j>::Execute(obj, data))
84  {
85  return true;
86  }
87  return DimHelperSub<i,j-1,TFunctor>::Execute(obj, data);
88  }
89  };
90 
94  template< unsigned int i, template < unsigned int, unsigned int> class TFunctor>
95  class DimHelperSub<i,1,TFunctor >
96  {
97  public:
98  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data)
99  {
100  //just unwind. Go to the next "row" with DimHelper
101  return false;
102  }
103  };
104 
113  template< unsigned int i, unsigned int j, template < unsigned int, unsigned int> class TFunctor>
114  class DimHelper{
115  public:
116  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data = "")
117  {
118  if (DimHelperSub<i,j,TFunctor>::Execute(obj, data))
119  {
120  return true;
121  }
122  return DimHelper<i-1,j,TFunctor>::Execute(obj, data);
123  }
124  };
125 
129  template< unsigned int j, template < unsigned int, unsigned int> class TFunctor>
130  class DimHelper<1,j, TFunctor >
131  {
132  public:
133  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data)
134  {
135  //just unwind. We are done.
136  return false;
137  }
138  };
139 
143  template<unsigned int i, unsigned int j>
144  class CanWrite
145  {
146  public:
147  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data = "")
148  {
149  bool result = false;
150 
151  result = dynamic_cast<const map::core::Registration<i,j> *>(obj->GetRegistration()) != NULL;
152 
153  return result;
154  }
155  };
156 
159  template<unsigned int i, unsigned int j>
160  class WriteReg
161  {
162  public:
163  static bool Execute(const mitk::MAPRegistrationWrapper* obj, const map::core::String& data)
164  {
165  const map::core::Registration<i,j>* pReg = dynamic_cast<const map::core::Registration<i,j>*>(obj->GetRegistration());
166  if (pReg == NULL)
167  {
168  return false;
169  }
170 
171  typedef map::io::RegistrationFileWriter<i,j> WriterType;
172  typename WriterType::Pointer writer = WriterType::New();
173 
174  writer->setExpandLazyKernels(false);
175 
176  try
177  {
178  writer->write(pReg,data);
179  }
180  catch (itk::ExceptionObject e)
181  {
182  std::cout << e << std::endl;
183  throw;
184  }
185 
186  return true;
187  }
188  };
189 
190  MAPRegistrationWrapperIO::MAPRegistrationWrapperIO(const MAPRegistrationWrapperIO& other)
191  : AbstractFileIO(other)
192  {
193  }
194 
196  {
197  std::string category = "MatchPoint Registration File";
198  CustomMimeType customMimeType;
199  customMimeType.SetCategory(category);
200  customMimeType.AddExtension("mapr");
201  this->AbstractFileIOWriter::SetMimeType(customMimeType);
202  this->AbstractFileIOWriter::SetDescription(category);
203 
204  customMimeType.AddExtension("mapr.xml");
205  customMimeType.AddExtension("MAPR");
206  customMimeType.AddExtension("MAPR.XML");
207  this->AbstractFileIOReader::SetMimeType(customMimeType);
208  this->AbstractFileIOReader::SetDescription(category);
209 
210  this->RegisterService();
211  }
212 
213 
215  {
216  bool success = false;
217  const BaseData* input = this->GetInput();
218  if (input == NULL)
219  {
220  mitkThrow() << "Cannot write data. Data pointer is NULL.";
221  }
222 
223  const mitk::MAPRegistrationWrapper* wrapper = dynamic_cast<const mitk::MAPRegistrationWrapper*>(input);
224  if (wrapper == NULL)
225  {
226  mitkThrow() << "Cannot write data. Data pointer is not a Registration wrapper.";
227  }
228 
229  std::ostream* writeStream = this->GetOutputStream();
230  std::string fileName = this->GetOutputLocation();
231  if (writeStream)
232  {
233  fileName = this->GetLocalFileName();
234  }
235 
236  // Switch the current locale to "C"
237  LocaleSwitch localeSwitch("C");
238 
239  try
240  {
241  success = DimHelper<3,3,WriteReg>::Execute(wrapper, fileName);
242  }
243  catch (const std::exception& e)
244  {
245  mitkThrow() << e.what();
246  }
247 
248  if (!success)
249  {
250  mitkThrow() << "Cannot write registration. Currently only registrations up to 4D are supported.";
251  }
252  }
253 
255  {
256  const mitk::MAPRegistrationWrapper* regWrapper = dynamic_cast<const mitk::MAPRegistrationWrapper*>(this->GetInput());
257 
258  if (regWrapper == NULL)
259  {
261  }
262 
263  // Check if the registration dimension is supported
264  if (! DimHelper<3,3,CanWrite>::Execute(regWrapper))
265  {
267  };
268 
269  return IFileWriter::Supported;
270  }
271 
272  std::vector<BaseData::Pointer > MAPRegistrationWrapperIO::Read()
273  {
274  std::vector<BaseData::Pointer > result;
275 
276  LocaleSwitch("C");
277 
278  std::string fileName = this->GetLocalFileName();
279  if ( fileName.empty() )
280  {
281  mitkThrow() << "Cannot read file. Filename has not been set!";
282  }
283 
284  /* Remove the following kernel loader provider because in MITK no lazy file loading should be used
285  due to conflicts with session loading (end there usage of temporary directories)*/
286  map::io::RegistrationFileReader::LoaderStackType::unregisterProvider(map::io::LazyFileFieldKernelLoader<2,2>::getStaticProviderName());
287  map::io::RegistrationFileReader::LoaderStackType::unregisterProvider(map::io::LazyFileFieldKernelLoader<3,3>::getStaticProviderName());
288 
290  spReader->setPreferLazyLoading(true);
291  map::core::RegistrationBase::Pointer spReg = spReader->read(fileName);
293  spRegWrapper->SetRegistration(spReg);
294 
295  result.push_back(spRegWrapper.GetPointer());
296  return result;
297  }
298 
300  {
302 
303  std::string fileName = this->GetLocalFileName();
304  std::ifstream in( fileName.c_str() );
305  if ( in.good() )
306  {
307  result = IFileReader::Supported;
308  }
309 
310  in.close();
311  return result;
312  }
313 
314  MAPRegistrationWrapperIO* MAPRegistrationWrapperIO::IOClone() const
315  {
316  return new MAPRegistrationWrapperIO(*this);
317  }
318 
319 }
itk::SmartPointer< Self > Pointer
virtual void Write()
Write the base data to the specified location or output stream.
#define MITK_INFO
Definition: mitkLogMacros.h:22
Base of all data objects.
Definition: mitkBaseData.h:39
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
MAPRegistrationWrapper Wrapper class to allow the handling of MatchPoint registration objects as mitk...
virtual std::vector< itk::SmartPointer< BaseData > > Read()
Reads a path or stream and creates a list of BaseData objects.
std::string GetLocalFileName() const
Get a local file name for reading.
void SetMimeType(const CustomMimeType &mimeType)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
virtual ConfidenceLevel GetWriterConfidenceLevel() const
map::core::RegistrationBase * GetRegistration()
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)
virtual const BaseData * GetInput() const override
Get the input data set via SetInput().
static const char * GetStaticNameOfClass()
void AddExtension(const std::string &extension)
LocaleSwitch(const std::string &newLocale)
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
virtual ConfidenceLevel GetReaderConfidenceLevel() const
virtual std::ostream * GetOutputStream() const override
Get the output stream.
void SetMimeType(const CustomMimeType &mimeType)
virtual std::string GetOutputLocation() const override
Get the current output location.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:49
Abstract class for implementing a reader and writer.
void SetDescription(const std::string &description)
Sets a human readable description of this writer.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.