Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
mitkGenericLookupTable.h
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 #ifndef mitkGenericLookupTable_h
14 #define mitkGenericLookupTable_h
15 
16 #include <map>
17 #include <sstream>
18 #include <cstdlib>
19 #include <string>
20 
21 #include <itkDataObject.h>
22 
23 #include "mitkNumericTypes.h"
24 #include <MitkCoreExports.h>
25 
26 #include <nlohmann/json.hpp>
27 
28 namespace mitk
29 {
30  template <typename T> class GenericLookupTable;
31  template <typename T> void from_json(const nlohmann::json&, GenericLookupTable<T>&);
32 
45  template <typename T>
46  class GenericLookupTable
47  {
48  public:
49  typedef unsigned int IdentifierType;
50  typedef T ValueType;
51  typedef std::map<IdentifierType, ValueType> LookupTableType;
52 
54 
56  virtual ~GenericLookupTable() {}
57  virtual const char *GetNameOfClass() const { return "GenericLookupTable"; }
58  void SetTableValue(IdentifierType id, ValueType value) { m_LookupTable[id] = value; }
59  bool ValueExists(IdentifierType id) const
60  {
61  auto it = m_LookupTable.find(id);
62  return (it != m_LookupTable.end());
63  }
64 
66  {
67  auto it = m_LookupTable.find(id);
68  if (it != m_LookupTable.end())
69  return it->second;
70  else
71  throw std::range_error("id does not exist in the lookup table");
72  }
73 
74  const LookupTableType &GetLookupTable() const { return m_LookupTable; }
75  bool operator==(const Self &lookupTable) const { return (m_LookupTable == lookupTable.m_LookupTable); }
76  bool operator!=(const Self &lookupTable) const { return !(m_LookupTable == lookupTable.m_LookupTable); }
77  virtual Self &operator=(const Self &other) // \TODO: this needs to be unit tested!
78  {
79  if (this == &other)
80  {
81  return *this;
82  }
83  else
84  {
85  m_LookupTable.clear();
87  return *this;
88  }
89  }
90 
91  friend void from_json<>(const nlohmann::json&, GenericLookupTable<T>&);
92 
93  protected:
95  };
96 
97  template <typename T>
99  {
100  j = t.GetLookupTable();
101  }
102 
103  template <typename T>
105  {
106  j.get_to(t.m_LookupTable);
107  }
108 
109 } // namespace mitk
110 
118 #define mitkSpecializeGenericLookupTable(LookupTableName, Type) \
119  \
120  class MITKCORE_EXPORT LookupTableName : public GenericLookupTable<Type> \
121  \
122  { \
123  public: \
124  typedef LookupTableName Self; \
125  typedef GenericLookupTable<Type> Superclass; \
126  virtual const char *GetNameOfClass() const { return #LookupTableName; } \
127  LookupTableName() {} \
128  virtual Superclass &operator=(const Superclass &other) { return Superclass::operator=(other); } \
129  virtual ~LookupTableName() {} \
130  }; \
131  \
132  MITKCORE_EXPORT std::ostream &operator<<(std::ostream &stream, const LookupTableName & /*l*/);
133 
139 #define mitkSpecializeGenericLookupTableOperator(LookupTableName) \
140  \
141  std::ostream &mitk::operator<<(std::ostream &stream, const LookupTableName &l) \
142  \
143  { \
144  typedef LookupTableName::LookupTableType::const_iterator IterType; \
145  IterType e = l.GetLookupTable().end(); \
146  IterType b = l.GetLookupTable().begin(); \
147  stream << "["; \
148  for (IterType i = b; i != e; ++i) \
149  { \
150  if (i != b) \
151  { \
152  stream << ", "; \
153  } \
154  stream << i->first << " -> " << i->second; \
155  } \
156  return stream << "]"; \
157  };
158 #endif
mitk::GenericLookupTable::GenericLookupTable
GenericLookupTable()
Definition: mitkGenericLookupTable.h:55
mitk::to_json
void to_json(nlohmann::json &j, const GenericLookupTable< T > &t)
Definition: mitkGenericLookupTable.h:98
mitk::GenericLookupTable::operator=
virtual Self & operator=(const Self &other)
Definition: mitkGenericLookupTable.h:77
mitk::GenericLookupTable::SetTableValue
void SetTableValue(IdentifierType id, ValueType value)
Definition: mitkGenericLookupTable.h:58
mitk::GenericLookupTable::Self
GenericLookupTable Self
Definition: mitkGenericLookupTable.h:53
mitk::GenericLookupTable::GetLookupTable
const LookupTableType & GetLookupTable() const
Definition: mitkGenericLookupTable.h:74
mitk::GenericLookupTable::GetNameOfClass
virtual const char * GetNameOfClass() const
Definition: mitkGenericLookupTable.h:57
mitk::from_json
void from_json(const nlohmann::json &, GenericLookupTable< T > &)
Definition: mitkGenericLookupTable.h:104
mitk::GenericLookupTable::ValueType
T ValueType
Definition: mitkGenericLookupTable.h:50
mitk::GenericLookupTable::operator!=
bool operator!=(const Self &lookupTable) const
Definition: mitkGenericLookupTable.h:76
mitk::GenericLookupTable::GetTableValue
ValueType GetTableValue(IdentifierType id) const
Definition: mitkGenericLookupTable.h:65
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::GenericLookupTable::ValueExists
bool ValueExists(IdentifierType id) const
Definition: mitkGenericLookupTable.h:59
mitk::DICOMCachedValueInfo
Definition: mitkDICOMImageBlockDescriptor.h:33
json
nlohmann::json json
Definition: mitkModelTestFixture.h:29
mitk::GenericLookupTable::IdentifierType
unsigned int IdentifierType
Definition: mitkGenericLookupTable.h:49
MitkCoreExports.h
mitkNumericTypes.h
mitk::GenericLookupTable::m_LookupTable
LookupTableType m_LookupTable
Definition: mitkGenericLookupTable.h:94
mitk::GenericLookupTable::operator==
bool operator==(const Self &lookupTable) const
Definition: mitkGenericLookupTable.h:75
mitk::GenericLookupTable::LookupTableType
std::map< IdentifierType, ValueType > LookupTableType
Definition: mitkGenericLookupTable.h:51
mitk::GenericLookupTable
Template class for generating lookup-tables.
Definition: mitkGenericLookupTable.h:30
mitk::GenericLookupTable::~GenericLookupTable
virtual ~GenericLookupTable()
Definition: mitkGenericLookupTable.h:56