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