Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkCoreObjectFactory.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 "mitkCoreObjectFactory.h"
18 #include "mitkConfig.h"
19 
20 #include "mitkColorProperty.h"
21 #include "mitkDataNode.h"
23 #include "mitkGeometry3D.h"
24 #include "mitkGeometryData.h"
25 #include "mitkImage.h"
27 #include "mitkLookupTable.h"
29 #include "mitkPlaneGeometry.h"
30 #include "mitkPlaneGeometryData.h"
33 #include "mitkPointSet.h"
36 #include "mitkProperties.h"
37 #include "mitkPropertyList.h"
38 #include "mitkSlicedGeometry3D.h"
40 #include "mitkStringProperty.h"
41 #include "mitkSurface.h"
42 #include "mitkSurface.h"
43 #include "mitkSurfaceVtkMapper2D.h"
44 #include "mitkSurfaceVtkMapper3D.h"
45 #include "mitkTimeGeometry.h"
50 #include <mitkImageVtkMapper2D.h>
51 
52 // Legacy Support:
53 #include <mitkCoreServices.h>
56 
58 {
59  MITK_DEBUG << "CoreObjectFactory: registering extra factory of type " << factory->GetNameOfClass();
61  // Register Legacy Reader and Writer
62  this->RegisterLegacyReaders(factory);
63  this->RegisterLegacyWriters(factory);
64 }
65 
67 {
68  MITK_DEBUG << "CoreObjectFactory: un-registering extra factory of type " << factory->GetNameOfClass();
69  this->UnRegisterLegacyWriters(factory);
70  this->UnRegisterLegacyReaders(factory);
71  try
72  {
73  m_ExtraFactories.erase(factory);
74  }
75  catch (std::exception const &e)
76  {
77  MITK_ERROR << "Caugt exception while unregistering: " << e.what();
78  }
79 }
80 
82 {
83  static mitk::CoreObjectFactory::Pointer instance;
84  if (instance.IsNull())
85  {
86  instance = mitk::CoreObjectFactory::New();
87  }
88  return instance;
89 }
90 
92 {
93  for (std::map<mitk::CoreObjectFactoryBase *, std::list<mitk::LegacyFileReaderService *>>::iterator iter =
94  m_LegacyReaders.begin();
95  iter != m_LegacyReaders.end();
96  ++iter)
97  {
98  for (auto &elem : iter->second)
99  {
100  delete elem;
101  }
102  }
103 
104  for (std::map<mitk::CoreObjectFactoryBase *, std::list<mitk::LegacyFileWriterService *>>::iterator iter =
105  m_LegacyWriters.begin();
106  iter != m_LegacyWriters.end();
107  ++iter)
108  {
109  for (auto &elem : iter->second)
110  {
111  delete elem;
112  }
113  }
114 }
115 
117 {
118  if (node == NULL)
119  return;
120 
121  mitk::DataNode::Pointer nodePointer = node;
122 
123  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(node->GetData());
124  if (image.IsNotNull() && image->IsInitialized())
125  {
127  }
128 
129  mitk::PlaneGeometryData::Pointer planeGeometry = dynamic_cast<mitk::PlaneGeometryData *>(node->GetData());
130  if (planeGeometry.IsNotNull())
131  {
133  }
134 
135  mitk::Surface::Pointer surface = dynamic_cast<mitk::Surface *>(node->GetData());
136  if (surface.IsNotNull())
137  {
140  }
141 
142  mitk::PointSet::Pointer pointSet = dynamic_cast<mitk::PointSet *>(node->GetData());
143  if (pointSet.IsNotNull())
144  {
147  }
148  for (ExtraFactoriesContainer::iterator it = m_ExtraFactories.begin(); it != m_ExtraFactories.end(); ++it)
149  {
150  (*it)->SetDefaultProperties(node);
151  }
152 }
153 
155 {
156  static bool alreadyDone = false;
157  if (!alreadyDone)
158  {
159  CreateFileExtensionsMap();
160 
161  // RegisterLegacyReaders(this);
162  // RegisterLegacyWriters(this);
163 
164  alreadyDone = true;
165  }
166 }
167 
169 {
170  mitk::Mapper::Pointer newMapper = NULL;
171  mitk::Mapper::Pointer tmpMapper = NULL;
172 
173  // check whether extra factories provide mapper
174  for (ExtraFactoriesContainer::iterator it = m_ExtraFactories.begin(); it != m_ExtraFactories.end(); ++it)
175  {
176  tmpMapper = (*it)->CreateMapper(node, id);
177  if (tmpMapper.IsNotNull())
178  newMapper = tmpMapper;
179  }
180 
181  if (newMapper.IsNull())
182  {
183  mitk::BaseData *data = node->GetData();
184 
186  {
187  if ((dynamic_cast<Image *>(data) != NULL))
188  {
189  newMapper = mitk::ImageVtkMapper2D::New();
190  newMapper->SetDataNode(node);
191  }
192  else if ((dynamic_cast<PlaneGeometryData *>(data) != NULL))
193  {
195  newMapper->SetDataNode(node);
196  }
197  else if ((dynamic_cast<Surface *>(data) != NULL))
198  {
199  newMapper = mitk::SurfaceVtkMapper2D::New();
200  // cast because SetDataNode is not virtual
201  mitk::SurfaceVtkMapper2D *castedMapper = dynamic_cast<mitk::SurfaceVtkMapper2D *>(newMapper.GetPointer());
202  castedMapper->SetDataNode(node);
203  }
204  else if ((dynamic_cast<PointSet *>(data) != NULL))
205  {
206  newMapper = mitk::PointSetVtkMapper2D::New();
207  newMapper->SetDataNode(node);
208  }
209  }
210  else if (id == mitk::BaseRenderer::Standard3D)
211  {
212  if ((dynamic_cast<PlaneGeometryData *>(data) != NULL))
213  {
215  newMapper->SetDataNode(node);
216  }
217  else if ((dynamic_cast<Surface *>(data) != NULL))
218  {
219  newMapper = mitk::SurfaceVtkMapper3D::New();
220  newMapper->SetDataNode(node);
221  }
222  else if ((dynamic_cast<PointSet *>(data) != NULL))
223  {
224  newMapper = mitk::PointSetVtkMapper3D::New();
225  newMapper->SetDataNode(node);
226  }
227  }
228  }
229 
230  return newMapper;
231 }
232 
234 {
235  MultimapType aMap;
236  for (ExtraFactoriesContainer::iterator it = m_ExtraFactories.begin(); it != m_ExtraFactories.end(); ++it)
237  {
238  aMap = (*it)->GetFileExtensionsMap();
239  this->MergeFileExtensions(m_FileExtensionsMap, aMap);
240  }
241  this->CreateFileExtensions(m_FileExtensionsMap, m_FileExtensions);
242  return m_FileExtensions.c_str();
243 }
244 
246 {
247  std::pair<MultimapType::iterator, MultimapType::iterator> pairOfIter;
248  for (MultimapType::iterator it = inputMap.begin(); it != inputMap.end(); ++it)
249  {
250  bool duplicateFound = false;
251  pairOfIter = fileExtensionsMap.equal_range((*it).first);
252  for (MultimapType::iterator it2 = pairOfIter.first; it2 != pairOfIter.second; ++it2)
253  {
254  // cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
255  std::string aString = (*it2).second;
256  if (aString.compare((*it).second) == 0)
257  {
258  // cout << " DUP!! [" << (*it).first << ", " << (*it).second << "]" << endl;
259  duplicateFound = true;
260  break;
261  }
262  }
263  if (!duplicateFound)
264  {
265  fileExtensionsMap.insert(std::pair<std::string, std::string>((*it).first, (*it).second));
266  }
267  }
268 }
269 
271 {
272  return m_FileExtensionsMap;
273 }
274 
276 {
277  /*
278  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.dcm", "DICOM files"));
279  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.DCM", "DICOM files"));
280  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.dc3", "DICOM files"));
281  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.DC3", "DICOM files"));
282  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.gdcm", "DICOM files"));
283  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.seq", "DKFZ Pic"));
284  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.seq.gz", "DKFZ Pic"));
285  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.dcm", "Sets of 2D slices"));
286  m_FileExtensionsMap.insert(std::pair<std::string, std::string>("*.gdcm", "Sets of 2D slices"));
287  */
288 }
289 
291 {
292  MultimapType aMap;
293  for (ExtraFactoriesContainer::iterator it = m_ExtraFactories.begin(); it != m_ExtraFactories.end(); ++it)
294  {
295  aMap = (*it)->GetSaveFileExtensionsMap();
296  this->MergeFileExtensions(m_SaveFileExtensionsMap, aMap);
297  }
298  this->CreateFileExtensions(m_SaveFileExtensionsMap, m_SaveFileExtensions);
299  return m_SaveFileExtensions.c_str();
300 }
301 
303 {
304  return m_SaveFileExtensionsMap;
305 }
306 
308 {
309  FileWriterList allWriters = m_FileWriters;
310  // sort to merge lists later on
311  typedef std::set<mitk::FileWriterWithInformation::Pointer> FileWriterSet;
312  FileWriterSet fileWritersSet;
313 
314  fileWritersSet.insert(allWriters.begin(), allWriters.end());
315 
316  // collect all extra factories
317  for (ExtraFactoriesContainer::iterator it = m_ExtraFactories.begin(); it != m_ExtraFactories.end(); ++it)
318  {
319  FileWriterList list2 = (*it)->GetFileWriters();
320 
321  // add them to the sorted set
322  fileWritersSet.insert(list2.begin(), list2.end());
323  }
324 
325  // write back to allWriters to return a list
326  allWriters.clear();
327  allWriters.insert(allWriters.end(), fileWritersSet.begin(), fileWritersSet.end());
328 
329  return allWriters;
330 }
331 
332 void mitk::CoreObjectFactory::MapEvent(const mitk::Event *, const int)
333 {
334 }
335 
336 std::string mitk::CoreObjectFactory::GetDescriptionForExtension(const std::string &extension)
337 {
338  std::multimap<std::string, std::string> fileExtensionMap = GetSaveFileExtensionsMap();
339  for (std::multimap<std::string, std::string>::iterator it = fileExtensionMap.begin(); it != fileExtensionMap.end();
340  ++it)
341  if (it->first == extension)
342  return it->second;
343  return ""; // If no matching extension was found, return emtpy string
344 }
345 
346 void mitk::CoreObjectFactory::RegisterLegacyReaders(mitk::CoreObjectFactoryBase *factory)
347 {
348  // We are not really interested in the string, just call the method since
349  // many readers initialize the map the first time when this method is called
350  factory->GetFileExtensions();
351 
352  std::map<std::string, std::vector<std::string>> extensionsByCategories;
353  std::multimap<std::string, std::string> fileExtensionMap = factory->GetFileExtensionsMap();
354  for (std::multimap<std::string, std::string>::iterator it = fileExtensionMap.begin(); it != fileExtensionMap.end();
355  ++it)
356  {
357  std::string extension = it->first;
358  // remove "*."
359  extension = extension.erase(0, 2);
360 
361  extensionsByCategories[it->second].push_back(extension);
362  }
363 
364  for (auto &extensionsByCategorie : extensionsByCategories)
365  {
366  m_LegacyReaders[factory].push_back(
367  new mitk::LegacyFileReaderService(extensionsByCategorie.second, extensionsByCategorie.first));
368  }
369 }
370 
371 void mitk::CoreObjectFactory::UnRegisterLegacyReaders(mitk::CoreObjectFactoryBase *factory)
372 {
373  std::map<mitk::CoreObjectFactoryBase *, std::list<mitk::LegacyFileReaderService *>>::iterator iter =
374  m_LegacyReaders.find(factory);
375  if (iter != m_LegacyReaders.end())
376  {
377  for (auto &elem : iter->second)
378  {
379  delete elem;
380  }
381 
382  m_LegacyReaders.erase(iter);
383  }
384 }
385 
386 void mitk::CoreObjectFactory::RegisterLegacyWriters(mitk::CoreObjectFactoryBase *factory)
387 {
388  // Get all external Writers
390 
391  // We are not really interested in the string, just call the method since
392  // many writers initialize the map the first time when this method is called
393  factory->GetSaveFileExtensions();
394 
395  MultimapType fileExtensionMap = factory->GetSaveFileExtensionsMap();
396 
397  for (mitk::CoreObjectFactory::FileWriterList::iterator it = writers.begin(); it != writers.end(); ++it)
398  {
399  std::vector<std::string> extensions = (*it)->GetPossibleFileExtensions();
400  if (extensions.empty())
401  continue;
402 
403  std::string description;
404  for (std::vector<std::string>::iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
405  {
406  if (ext->empty())
407  continue;
408 
409  std::string extension = *ext;
410  std::string extensionWithStar = extension;
411  if (extension.find_first_of('*') == 0)
412  {
413  // remove "*."
414  extension = extension.substr(0, extension.size() - 2);
415  }
416  else
417  {
418  extensionWithStar.insert(extensionWithStar.begin(), '*');
419  }
420 
421  for (MultimapType::iterator fileExtensionIter = fileExtensionMap.begin();
422  fileExtensionIter != fileExtensionMap.end();
423  ++fileExtensionIter)
424  {
425  if (fileExtensionIter->first == extensionWithStar)
426  {
427  description = fileExtensionIter->second;
428  break;
429  }
430  }
431  if (!description.empty())
432  break;
433  }
434  if (description.empty())
435  {
436  description = std::string("Legacy ") + (*it)->GetNameOfClass() + " Reader";
437  }
438 
439  mitk::FileWriter::Pointer fileWriter(it->GetPointer());
440  mitk::LegacyFileWriterService *lfws = new mitk::LegacyFileWriterService(fileWriter, description);
441  m_LegacyWriters[factory].push_back(lfws);
442  }
443 }
444 
445 void mitk::CoreObjectFactory::UnRegisterLegacyWriters(mitk::CoreObjectFactoryBase *factory)
446 {
447  std::map<mitk::CoreObjectFactoryBase *, std::list<mitk::LegacyFileWriterService *>>::iterator iter =
448  m_LegacyWriters.find(factory);
449  if (iter != m_LegacyWriters.end())
450  {
451  for (auto &elem : iter->second)
452  {
453  delete elem;
454  }
455 
456  m_LegacyWriters.erase(iter);
457  }
458 }
Vtk-based mapper for cutting 2D slices out of Surfaces.
std::multimap< std::string, std::string > MultimapType
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:32
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
Set the default properties for general image rendering.
virtual void MapEvent(const mitk::Event *event, const int eventID)
ExtraFactoriesContainer m_ExtraFactories
Base of all data objects.
Definition: mitkBaseData.h:39
#define MITK_ERROR
Definition: mitkLogMacros.h:24
virtual MultimapType GetFileExtensionsMap()=0
virtual const char * GetSaveFileExtensions() override
This method gets the supported (save) file extensions as string.
virtual const char * GetFileExtensions() override
This method gets the supported (open) file extensions as string.
virtual void UnRegisterExtraFactory(CoreObjectFactoryBase *factory)
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
std::string GetDescriptionForExtension(const std::string &extension)
virtual MultimapType GetSaveFileExtensionsMap()=0
virtual void SetDataNode(DataNode *_arg)
Set the DataNode containing the data to map.
virtual void SetDefaultProperties(mitk::DataNode *node) override
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
virtual MultimapType GetFileExtensionsMap() override
get the defined (open) file extension map
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
set the default properties for this mapper
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
set the default properties for this mapper
static Pointer New()
static Pointer New()
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:79
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
void CreateFileExtensionsMap()
initialize the file extension entries for open and save
virtual MultimapType GetSaveFileExtensionsMap() override
get the defined (save) file extension map
Image class for storing images.
Definition: mitkImage.h:76
virtual void RegisterExtraFactory(CoreObjectFactoryBase *factory)
virtual const char * GetFileExtensions()=0
Data class containing PlaneGeometry objects.
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
set the default properties for this mapper
virtual FileWriterList GetFileWriters()
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=NULL, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
static Pointer New()
virtual const char * GetSaveFileExtensions()=0
virtual Mapper::Pointer CreateMapper(mitk::DataNode *node, MapperSlotId slotId) override
void MergeFileExtensions(MultimapType &fileExtensionsMap, MultimapType inputMap)
Merge the input map into the fileExtensionsMap. Duplicate entries are removed.
static Pointer New()
static Pointer New()
unsigned int MapperSlotId
Definition: mitkCommon.h:37
std::multimap< std::string, std::string > MultimapType
std::list< mitk::FileWriterWithInformation::Pointer > FileWriterList
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
static Pointer New()