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