Medical Imaging Interaction Toolkit  2018.4.99-b20efe7f
Medical Imaging Interaction Toolkit
mitkClippingPropertySerializer.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 mitkClippingPropertySerializer_h_included
14 #define mitkClippingPropertySerializer_h_included
15 
17 #include "mitkClippingProperty.h"
18 #include "mitkNumericTypes.h"
19 #include <mitkLocaleSwitch.h>
20 
21 #include "mitkStringsToNumbers.h"
22 
23 namespace mitk
24 {
25  class ClippingPropertySerializer : public BasePropertySerializer
26  {
27  public:
28  mitkClassMacro(ClippingPropertySerializer, BasePropertySerializer);
29  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
30 
31  TiXmlElement *Serialize() override
32  {
33  if (const ClippingProperty *prop = dynamic_cast<const ClippingProperty *>(m_Property.GetPointer()))
34  {
35  LocaleSwitch localeSwitch("C");
36 
37  auto element = new TiXmlElement("clipping");
38  if (prop->GetClippingEnabled())
39  element->SetAttribute("enabled", "true");
40  else
41  element->SetAttribute("enabled", "false");
42  auto originElement = new TiXmlElement("origin");
43  const Point3D origin = prop->GetOrigin();
44  originElement->SetAttribute("x", boost::lexical_cast<std::string>(origin[0]));
45  originElement->SetAttribute("y", boost::lexical_cast<std::string>(origin[1]));
46  originElement->SetAttribute("z", boost::lexical_cast<std::string>(origin[2]));
47  element->LinkEndChild(originElement);
48  auto normalElement = new TiXmlElement("normal");
49  const Vector3D normal = prop->GetNormal();
50  normalElement->SetAttribute("x", boost::lexical_cast<std::string>(normal[0]));
51  normalElement->SetAttribute("y", boost::lexical_cast<std::string>(normal[1]));
52  normalElement->SetAttribute("z", boost::lexical_cast<std::string>(normal[2]));
53  element->LinkEndChild(normalElement);
54  return element;
55  }
56  else
57  return nullptr;
58  }
59 
60  BaseProperty::Pointer Deserialize(TiXmlElement *element) override
61  {
62  if (!element)
63  return nullptr;
64 
65  LocaleSwitch localeSwitch("C");
66 
67  bool enabled = std::string(element->Attribute("enabled")) == "true";
68 
69  TiXmlElement *originElement = element->FirstChildElement("origin");
70  if (originElement == nullptr)
71  return nullptr;
72  std::string origin_string[3];
73  if (originElement->QueryStringAttribute("x", &origin_string[0]) != TIXML_SUCCESS)
74  return nullptr;
75  if (originElement->QueryStringAttribute("y", &origin_string[1]) != TIXML_SUCCESS)
76  return nullptr;
77  if (originElement->QueryStringAttribute("z", &origin_string[2]) != TIXML_SUCCESS)
78  return nullptr;
79  Point3D origin;
80  try
81  {
82  StringsToNumbers<ScalarType>(3, origin_string, origin);
83  }
84  catch (boost::bad_lexical_cast &e)
85  {
86  MITK_ERROR << "Could not parse string as number: " << e.what();
87  return nullptr;
88  }
89 
90  TiXmlElement *normalElement = element->FirstChildElement("normal");
91  if (normalElement == nullptr)
92  return nullptr;
93  std::string normal_string[3];
94  if (normalElement->QueryStringAttribute("x", &normal_string[0]) != TIXML_SUCCESS)
95  return nullptr;
96  if (normalElement->QueryStringAttribute("y", &normal_string[1]) != TIXML_SUCCESS)
97  return nullptr;
98  if (normalElement->QueryStringAttribute("z", &normal_string[2]) != TIXML_SUCCESS)
99  return nullptr;
100  Vector3D normal;
101  try
102  {
103  StringsToNumbers<ScalarType>(3, normal_string, normal);
104  }
105  catch (boost::bad_lexical_cast &e)
106  {
107  MITK_ERROR << "Could not parse string as number: " << e.what();
108  return nullptr;
109  }
110 
112  cp->SetClippingEnabled(enabled);
113  return cp.GetPointer();
114  }
115 
116  protected:
117  ClippingPropertySerializer() {}
118  ~ClippingPropertySerializer() override {}
119  };
120 } // namespace
121 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
122 MITK_REGISTER_SERIALIZER(ClippingPropertySerializer);
123 #endif
#define MITK_ERROR
Definition: mitkLogMacros.h:20
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
itk::SmartPointer< Self > Pointer
Vector< ScalarType, 3 > Vector3D
Definition: mitkVector.h:130
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:40
Point< ScalarType, 3 > Point3D
Definition: mitkPoint.h:95
static Pointer New()
MITK_REGISTER_SERIALIZER(ClippingPropertySerializer)