Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkScalarListLookupTablePropertySerializer.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 
15 
16 
18 {
19  const ScalarListLookupTableProperty* prop =
20  dynamic_cast<const ScalarListLookupTableProperty*>(m_Property.GetPointer());
21 
22  if (prop == nullptr)
23  {
24  MITK_ERROR << "Serialization: Property is NULL";
25  return nullptr;
26  }
27 
28  ScalarListLookupTable lut = prop->GetValue();
30 
31  TiXmlElement* mapElement = new TiXmlElement("ScalarListLookupTable");
32 
33  for (ScalarListLookupTable::LookupTableType::const_iterator mapIter = map.begin();
34  mapIter != map.end(); ++mapIter)
35  {
36  const ScalarListLookupTable::ValueType& list = mapIter->second;
37  TiXmlElement* listElement = new TiXmlElement("List");
38  listElement->SetAttribute("name", mapIter->first);
39 
40  for (ScalarListLookupTable::ValueType::const_iterator listIter = list.begin();
41  listIter != list.end(); ++listIter)
42  {
43  TiXmlElement* valueElement = new TiXmlElement("Element");
44  valueElement->SetDoubleAttribute("value", *listIter);
45  listElement->LinkEndChild(valueElement);
46  }
47 
48  mapElement->LinkEndChild(listElement);
49  }
50 
51  return mapElement;
52 }
53 
56 {
57  if (!element)
58  {
59  MITK_ERROR << "Deserialization: Element is NULL";
60  return nullptr;
61  }
62 
64 
65  for (TiXmlElement* listElement = element->FirstChildElement("List");
66  listElement != nullptr; listElement = listElement->NextSiblingElement("List"))
67  {
68  std::string name;
69 
70  if (listElement->Attribute("name") != nullptr)
71  {
72  name = listElement->Attribute("name");
73  }
74  else
75  {
76  MITK_ERROR << "Deserialization: No element with attribute 'name' found";
77  return nullptr;
78  }
79 
81 
82  for (TiXmlElement* valueElement = listElement->FirstChildElement("Element");
83  valueElement != nullptr;
84  valueElement = valueElement->NextSiblingElement("Element"))
85  {
86  double value;
87 
88  if (valueElement->QueryDoubleAttribute("value", &value) == TIXML_WRONG_TYPE)
89  {
90  MITK_ERROR << "Deserialization: No element with attribute 'value' found";
91  return nullptr;
92  }
93 
94  list.push_back(value);
95  }
96 
97  lut.SetTableValue(name, list);
98  }
99 
100  return ScalarListLookupTableProperty::New(lut).GetPointer();
101 }
102 
104 
106  const mitk::BaseProperty *prop)
107 {
108  mitk::ScalarListLookupTablePropertySerializer::Pointer lutSerializer = mitk::ScalarListLookupTablePropertySerializer::New();
109 
110  lutSerializer->SetProperty(prop);
111  auto xmlLut = lutSerializer->Serialize();
112 
113  TiXmlPrinter printer;
114  xmlLut->Accept(&printer);
115  printer.SetStreamPrinting();
116  return printer.Str();
117 }
118 
120  const std::string &value)
121 {
122  mitk::ScalarListLookupTablePropertySerializer::Pointer lutSerializer = mitk::ScalarListLookupTablePropertySerializer::New();
123 
124  TiXmlDocument doc;
125  doc.Parse(value.c_str());
126  return lutSerializer->Deserialize(doc.RootElement());
127 }
MITKMODELFIT_EXPORT mitk::BaseProperty::Pointer deserializeXMLToScalarListLookupTableProperty(const std::string &value)
void SetTableValue(const KeyType &key, const ValueType &value)
Sets the list at the given map key to the given value.
TiXmlElement * Serialize() override
Serializes given BaseProperty object.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
BaseProperty::Pointer Deserialize(TiXmlElement *element) override
Deserializes given TiXmlElement.
Serializer for the ScalarListLookupTableProperty so it can be written and read from file...
Data class for modelfit properties that store a map of lists (e.g. static parameters).
Abstract base class for properties.
const LookupTableType & GetLookupTable() const
Returns the map of lists.
MITK_REGISTER_SERIALIZER(ScalarListLookupTablePropertySerializer)
BaseProperty::ConstPointer m_Property
std::map< KeyType, ValueType > LookupTableType
MITKMODELFIT_EXPORT ::std::string serializeScalarListLookupTablePropertyToXML(const mitk::BaseProperty *prop)