Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkVector3DPropertySerializer.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 mitkVector3DPropertySerializer_h_included
18 #define mitkVector3DPropertySerializer_h_included
19 
21 #include "mitkProperties.h"
22 #include "mitkStringsToNumbers.h"
23 #include <mitkLocaleSwitch.h>
24 
25 namespace mitk
26 {
27  class Vector3DPropertySerializer : public BasePropertySerializer
28  {
29  public:
30  mitkClassMacro(Vector3DPropertySerializer, BasePropertySerializer);
31  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
32 
33  virtual TiXmlElement *Serialize() override
34  {
35  if (const Vector3DProperty *prop = dynamic_cast<const Vector3DProperty *>(m_Property.GetPointer()))
36  {
37  LocaleSwitch localeSwitch("C");
38 
39  auto element = new TiXmlElement("vector");
40  Vector3D 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  Vector3D 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 '" << v_str << "'as number: " << e.what();
72  return nullptr;
73  }
74 
75  return Vector3DProperty::New(v).GetPointer();
76  }
77 
78  protected:
79  Vector3DPropertySerializer() {}
80  virtual ~Vector3DPropertySerializer() {}
81  };
82 
83 } // namespace
84 
85 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
86 MITK_REGISTER_SERIALIZER(Vector3DPropertySerializer);
87 
88 #endif
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
static Pointer New()
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:134
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:44
MITK_REGISTER_SERIALIZER(Vector3DPropertySerializer)