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