Medical Imaging Interaction Toolkit  2026.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>
47  {
48  public:
49  typedef unsigned int IdentifierType;
50  typedef T ValueType;
51  typedef std::map<IdentifierType, ValueType> LookupTableType;
52 
54 
57 
60 
62  virtual ~GenericLookupTable() {}
63 
65  virtual const char *GetNameOfClass() const { return "GenericLookupTable"; }
66 
71  void SetTableValue(IdentifierType id, ValueType value) { m_LookupTable[id] = value; }
72 
77  bool ValueExists(IdentifierType id) const
78  {
79  auto it = m_LookupTable.find(id);
80  return (it != m_LookupTable.end());
81  }
82 
89  {
90  auto it = m_LookupTable.find(id);
91  if (it != m_LookupTable.end())
92  return it->second;
93  else
94  throw std::range_error("id does not exist in the lookup table");
95  }
96 
100  const LookupTableType &GetLookupTable() const { return m_LookupTable; }
101 
103  bool operator==(const Self &lookupTable) const { return (m_LookupTable == lookupTable.m_LookupTable); }
104 
106  bool operator!=(const Self &lookupTable) const { return !(m_LookupTable == lookupTable.m_LookupTable); }
107 
109  virtual Self &operator=(const Self &other) // \TODO: this needs to be unit tested!
110  {
111  if (this == &other)
112  {
113  return *this;
114  }
115  else
116  {
117  m_LookupTable.clear();
119  return *this;
120  }
121  }
122 
123  friend void from_json<>(const nlohmann::json&, GenericLookupTable<T>&);
124 
125  protected:
127  };
128 
130  template <typename T>
132  {
133  j = t.GetLookupTable();
134  }
135 
137  template <typename T>
139  {
140  j.get_to(t.m_LookupTable);
141  }
142 
143 } // namespace mitk
144 
154 #define mitkSpecializeGenericLookupTable(LookupTableName, Type) \
155  \
156  class MITKCORE_EXPORT LookupTableName : public GenericLookupTable<Type> \
157  \
158  { \
159  public: \
160  typedef LookupTableName Self; \
161  typedef GenericLookupTable<Type> Superclass; \
162  virtual const char *GetNameOfClass() const { return #LookupTableName; } \
163  LookupTableName() {} \
164  LookupTableName(const LookupTableName &) = default; \
165  virtual Superclass &operator=(const Superclass &other) { return Superclass::operator=(other); } \
166  virtual ~LookupTableName() {} \
167  }; \
168  \
169  MITKCORE_EXPORT std::ostream &operator<<(std::ostream &stream, const LookupTableName & /*l*/);
170 
177 #define mitkSpecializeGenericLookupTableOperator(LookupTableName) \
178  \
179  std::ostream &mitk::operator<<(std::ostream &stream, const LookupTableName &l) \
180  \
181  { \
182  typedef LookupTableName::LookupTableType::const_iterator IterType; \
183  IterType e = l.GetLookupTable().end(); \
184  IterType b = l.GetLookupTable().begin(); \
185  stream << "["; \
186  for (IterType i = b; i != e; ++i) \
187  { \
188  if (i != b) \
189  { \
190  stream << ", "; \
191  } \
192  stream << i->first << " -> " << i->second; \
193  } \
194  return stream << "]"; \
195  };
196 #endif
Template class for generating lookup-tables.
std::map< IdentifierType, ValueType > LookupTableType
bool ValueExists(IdentifierType id) const
Check if a value exists for the given identifier.
GenericLookupTable(const GenericLookupTable &)=default
Copy constructor.
bool operator!=(const Self &lookupTable) const
Inequality comparison operator.
virtual const char * GetNameOfClass() const
Return the class name as a string.
const LookupTableType & GetLookupTable() const
Get the underlying lookup table map.
ValueType GetTableValue(IdentifierType id) const
Get the value for the given identifier.
void SetTableValue(IdentifierType id, ValueType value)
Set the value for the given identifier.
GenericLookupTable()
Default constructor.
bool operator==(const Self &lookupTable) const
Equality comparison operator.
virtual Self & operator=(const Self &other)
Assignment operator.
virtual ~GenericLookupTable()
Virtual destructor.
nlohmann::json json
Convenience header that includes all MITK numeric types.
Find image slices visible on a given plane.
void from_json(const nlohmann::json &, GenericLookupTable< T > &)
Deserialize a GenericLookupTable from JSON.
void to_json(nlohmann::json &j, const GenericLookupTable< T > &t)
Serialize a GenericLookupTable to JSON.