Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkFloatLookupTablePropertySerializer.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 #ifndef mitkFloatLookupTablePropertySerializer_h_included
14 #define mitkFloatLookupTablePropertySerializer_h_included
15 
17 #include "mitkProperties.h"
18 #include <mitkLexicalCast.h>
19 #include <mitkLocaleSwitch.h>
20 
21 namespace mitk
22 {
23  class FloatLookupTablePropertySerializer : public BasePropertySerializer
24  {
25  public:
26  mitkClassMacro(FloatLookupTablePropertySerializer, BasePropertySerializer);
27  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
28 
29  TiXmlElement *Serialize() override
30  {
31  const FloatLookupTableProperty *prop = dynamic_cast<const FloatLookupTableProperty *>(m_Property.GetPointer());
32  if (prop == nullptr)
33  return nullptr;
34 
35  LocaleSwitch localeSwitch("C");
36 
37  FloatLookupTable lut = prop->GetValue();
38  // if (lut.IsNull())
39  // return nullptr; // really?
40  const FloatLookupTable::LookupTableType &map = lut.GetLookupTable();
41 
42  auto element = new TiXmlElement("FloatLookupTableTable");
43  for (auto it = map.begin(); it != map.end(); ++it)
44  {
45  auto tableEntry = new TiXmlElement("LUTValue");
46  tableEntry->SetAttribute("id", it->first);
47  tableEntry->SetAttribute("value", boost::lexical_cast<std::string>(it->second));
48  element->LinkEndChild(tableEntry);
49  }
50  return element;
51  }
52 
53  BaseProperty::Pointer Deserialize(TiXmlElement *element) override
54  {
55  if (!element)
56  return nullptr;
57 
58  LocaleSwitch localeSwitch("C");
59 
60  FloatLookupTable lut;
61  for (TiXmlElement *child = element->FirstChildElement("LUTValue"); child != nullptr;
62  child = child->NextSiblingElement("LUTValue"))
63  {
64  int tempID;
65  if (child->QueryIntAttribute("id", &tempID) != TIXML_SUCCESS)
66  return nullptr;
68  std::string value_string;
69  if (child->QueryStringAttribute("value", &value_string) != TIXML_SUCCESS)
70  return nullptr;
71  try
72  {
73  lut.SetTableValue(id, boost::lexical_cast<float>(value_string));
74  }
75  catch (boost::bad_lexical_cast &e)
76  {
77  MITK_ERROR << "Could not parse string as number: " << e.what();
78  return nullptr;
79  }
80  }
81  return FloatLookupTableProperty::New(lut).GetPointer();
82  }
83 
84  protected:
85  FloatLookupTablePropertySerializer() {}
86  ~FloatLookupTablePropertySerializer() override {}
87  };
88 } // namespace
89 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
90 MITK_REGISTER_SERIALIZER(FloatLookupTablePropertySerializer);
91 #endif
std::map< IdentifierType, ValueType > LookupTableType
MITK_REGISTER_SERIALIZER(FloatLookupTablePropertySerializer)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:40