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