Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
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  if (left->GetUID() != right->GetUID())
36  {
37  return false;
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:42
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
virtual UIDType GetUID() const
Get unique ID of an object.
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.