Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef mitkFloatLookupTablePropertySerializer_h_included
18 #define mitkFloatLookupTablePropertySerializer_h_included
19 
21 #include "mitkProperties.h"
22 #include <boost/lexical_cast.hpp>
23 #include <mitkLocaleSwitch.h>
24 
25 namespace mitk
26 {
27  class FloatLookupTablePropertySerializer : public BasePropertySerializer
28  {
29  public:
30  mitkClassMacro(FloatLookupTablePropertySerializer, BasePropertySerializer);
31  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
32 
33  virtual TiXmlElement *Serialize() override
34  {
35  const FloatLookupTableProperty *prop = dynamic_cast<const FloatLookupTableProperty *>(m_Property.GetPointer());
36  if (prop == nullptr)
37  return nullptr;
38 
39  LocaleSwitch localeSwitch("C");
40 
41  FloatLookupTable lut = prop->GetValue();
42  // if (lut.IsNull())
43  // return NULL; // really?
44  const FloatLookupTable::LookupTableType &map = lut.GetLookupTable();
45 
46  auto element = new TiXmlElement("FloatLookupTableTable");
47  for (auto it = map.begin(); it != map.end(); ++it)
48  {
49  auto tableEntry = new TiXmlElement("LUTValue");
50  tableEntry->SetAttribute("id", it->first);
51  tableEntry->SetAttribute("value", boost::lexical_cast<std::string>(it->second));
52  element->LinkEndChild(tableEntry);
53  }
54  return element;
55  }
56 
57  virtual BaseProperty::Pointer Deserialize(TiXmlElement *element) override
58  {
59  if (!element)
60  return nullptr;
61 
62  LocaleSwitch localeSwitch("C");
63 
64  FloatLookupTable lut;
65  for (TiXmlElement *child = element->FirstChildElement("LUTValue"); child != nullptr;
66  child = child->NextSiblingElement("LUTValue"))
67  {
68  int tempID;
69  if (child->QueryIntAttribute("id", &tempID) != TIXML_SUCCESS)
70  return nullptr;
72  std::string value_string;
73  if (child->QueryStringAttribute("value", &value_string) != TIXML_SUCCESS)
74  return nullptr;
75  try
76  {
77  lut.SetTableValue(id, boost::lexical_cast<float>(value_string));
78  }
79  catch (boost::bad_lexical_cast &e)
80  {
81  MITK_ERROR << "Could not parse string as number: " << e.what();
82  return nullptr;
83  }
84  }
85  return FloatLookupTableProperty::New(lut).GetPointer();
86  }
87 
88  protected:
89  FloatLookupTablePropertySerializer() {}
90  virtual ~FloatLookupTablePropertySerializer() {}
91  };
92 } // namespace
93 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
94 MITK_REGISTER_SERIALIZER(FloatLookupTablePropertySerializer);
95 #endif
std::map< IdentifierType, ValueType > LookupTableType
MITK_REGISTER_SERIALIZER(FloatLookupTablePropertySerializer)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:44