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