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