Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkBaseDataCompare.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 "mitkGeometryData.h"
18 #include "mitkImage.h"
19 #include "mitkPointSet.h"
20 #include "mitkSurface.h"
21 
22 // this include after all specific type includes! (for mitk::Equal)
23 #include "mitkBaseDataCompare.h"
24 
25 #include "usGetModuleContext.h"
26 #include "usModuleContext.h"
27 #include "usServiceProperties.h"
28 
29 bool mitk::BaseDataCompare::AreEqual(const BaseData *left, const BaseData *right, ScalarType eps, bool verbose)
30 {
31  // Do basic tests that are valid for all types here.
32  // Let specializations only implement a meaningful
33  // comparison of two non-null objects of same type
34  if (left == nullptr && right == nullptr)
35  return true;
36 
37  if (AreSameClasses(left, right, verbose))
38  {
39  return InternalAreEqual(*left, *right, eps, verbose);
40  }
41 
42  return false;
43 }
44 
45 bool mitk::BaseDataCompare::AreSameClasses(const BaseData *left, const BaseData *right, bool verbose)
46 {
47  if (left == nullptr && right == nullptr)
48  return true;
49 
50  if (left == nullptr && right != nullptr)
51  {
52  if (verbose)
53  MITK_WARN << "Left data is nullptr, right data is not (type " << right->GetNameOfClass() << ")";
54  return false;
55  }
56 
57  if (left != nullptr && right == nullptr)
58  {
59  if (verbose)
60  MITK_WARN << "Right data is nullptr, left data is not (type " << left->GetNameOfClass() << ")";
61  return false;
62  }
63 
64  // two real BaseData objects, need to really compare
65  if (left->GetNameOfClass() != right->GetNameOfClass())
66  {
67  if (verbose)
68  MITK_WARN << "Mismatch: Left data is '" << left->GetNameOfClass() << "', "
69  << "right data is '" << right->GetNameOfClass() << "'";
70  return false;
71  }
72 
73  return true;
74 }
75 
77 {
78  static bool comparatorsCreated = false;
79  if (!comparatorsCreated)
80  {
81  static BaseDataCompareT<Image> imageEqual;
82  us::ServiceProperties imageProperties;
83  imageProperties["basedata"] = std::string(Image::GetStaticNameOfClass());
84  us::GetModuleContext()->RegisterService<BaseDataCompare>(&imageEqual, imageProperties);
85 
86  static BaseDataCompareTNonConst<Surface> surfaceEqual;
87  us::ServiceProperties surfaceProperties;
88  surfaceProperties["basedata"] = std::string(Surface::GetStaticNameOfClass());
89  us::GetModuleContext()->RegisterService<BaseDataCompare>(&surfaceEqual, surfaceProperties);
90 
91  static BaseDataCompareT<PointSet> pointsetEqual;
92  us::ServiceProperties pointsetProperties;
93  pointsetProperties["basedata"] = std::string(PointSet::GetStaticNameOfClass());
94  us::GetModuleContext()->RegisterService<BaseDataCompare>(&pointsetEqual, pointsetProperties);
95 
96  static BaseDataCompareT<GeometryData> geometryDataEqual;
97  us::ServiceProperties geometryDataProperties;
98  geometryDataProperties["basedata"] = std::string(GeometryData::GetStaticNameOfClass());
99  us::GetModuleContext()->RegisterService<BaseDataCompare>(&geometryDataEqual, geometryDataProperties);
100 
101  comparatorsCreated = true;
102  }
103 }
ServiceRegistrationU RegisterService(const InterfaceMap &service, const ServiceProperties &properties=ServiceProperties())
Base of all data objects.
Definition: mitkBaseData.h:39
double ScalarType
Implementation of BaseDataCompare that uses a non-const version of mitk:Equal() for comparisons...
Implementation of BaseDataCompare that uses mitk:Equal() for comparisons.
bool AreEqual(const BaseData *left, const BaseData *right, ScalarType eps=mitk::eps, bool verbose=false)
Main interface method, compares two given BaseData instances.
static const char * GetStaticNameOfClass()
static const char * GetStaticNameOfClass()
Definition: mitkImage.h:88
#define MITK_WARN
Definition: mitkLogMacros.h:23
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
static const char * GetStaticNameOfClass()
Definition: mitkSurface.h:37
static const char * GetStaticNameOfClass()
Definition: mitkPointSet.h:82
static void RegisterCoreEquals()
Register core type comparators that come with mitk::Equal() functions.
MITKCORE_EXPORT const ScalarType eps
Interface to compare two BaseData objects for (near) equality.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.