Medical Imaging Interaction Toolkit  2018.4.99-4c24e3cb
Medical Imaging Interaction Toolkit
mitkPoint4dPropertySerializer.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 mitkPoint4dPropertySerializer_h_included
14 #define mitkPoint4dPropertySerializer_h_included
15 
17 #include "mitkProperties.h"
18 #include "mitkStringsToNumbers.h"
19 #include <mitkLocaleSwitch.h>
20 
21 namespace mitk
22 {
23  class Point4dPropertySerializer : public BasePropertySerializer
24  {
25  public:
26  mitkClassMacro(Point4dPropertySerializer, BasePropertySerializer);
27  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
28 
29  TiXmlElement *Serialize() override
30  {
31  if (const Point4dProperty *prop = dynamic_cast<const Point4dProperty *>(m_Property.GetPointer()))
32  {
33  LocaleSwitch localeSwitch("C");
34 
35  auto element = new TiXmlElement("point");
36  Point4D point = prop->GetValue();
37  element->SetAttribute("x", boost::lexical_cast<std::string>(point[0]));
38  element->SetAttribute("y", boost::lexical_cast<std::string>(point[1]));
39  element->SetAttribute("z", boost::lexical_cast<std::string>(point[2]));
40  element->SetAttribute("t", boost::lexical_cast<std::string>(point[3]));
41  return element;
42  }
43  else
44  return nullptr;
45  }
46 
47  BaseProperty::Pointer Deserialize(TiXmlElement *element) override
48  {
49  if (!element)
50  return nullptr;
51 
52  LocaleSwitch localeSwitch("C");
53 
54  std::string v_str[4];
55  if (element->QueryStringAttribute("x", &v_str[0]) != TIXML_SUCCESS)
56  return nullptr;
57  if (element->QueryStringAttribute("y", &v_str[1]) != TIXML_SUCCESS)
58  return nullptr;
59  if (element->QueryStringAttribute("z", &v_str[2]) != TIXML_SUCCESS)
60  return nullptr;
61  if (element->QueryStringAttribute("t", &v_str[3]) != TIXML_SUCCESS)
62  return nullptr;
63  Point4D v;
64  try
65  {
66  StringsToNumbers<double>(4, v_str, v);
67  }
68  catch (boost::bad_lexical_cast &e)
69  {
70  MITK_ERROR << "Could not parse string as number: " << e.what();
71  return nullptr;
72  }
73  return Point4dProperty::New(v).GetPointer();
74  }
75 
76  protected:
77  Point4dPropertySerializer() {}
78  ~Point4dPropertySerializer() override {}
79  };
80 
81 } // namespace
82 
83 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
84 MITK_REGISTER_SERIALIZER(Point4dPropertySerializer);
85 
86 #endif
MITK_REGISTER_SERIALIZER(Point4dPropertySerializer)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
Point< ScalarType, 4 > Point4D
Definition: mitkPoint.h:96
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
static Pointer New()
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:40