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