Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkPropertySerializationTest.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 #include "mitkTestingMacros.h"
14 
15 #include "mitkCoreObjectFactory.h"
16 
17 #include "mitkBaseProperty.h"
18 #include "mitkProperties.h"
19 #include <mitkAnnotationProperty.h>
20 #include <mitkClippingProperty.h>
21 #include <mitkColorProperty.h>
23 /*
24 #include <mitkGridRepresentationProperty.h>
25 #include <mitkGridVolumeMapperProperty.h>
26 */
27 #include <mitkModalityProperty.h>
28 //#include <mitkOdfNormalizationMethodProperty.h>
29 //#include <mitkOdfScaleByProperty.h>
30 #include <mitkGroupTagProperty.h>
34 #include <mitkStringProperty.h>
41 
43 #include "mitkPropertyList.h"
45 
46 #include <mitkImage.h>
47 #include <mitkPointSet.h>
48 #include <mitkSurface.h>
49 #include <mitkVtkWidgetRendering.h>
50 
51 /*
52 #include <mitkCone.h>
53 #include <mitkContour.h>
54 #include <mitkContourSet.h>
55 #include <mitkCuboid.h>
56 #include <mitkCylinder.h>
57 #include <mitkEllipsoid.h>
58 #include <mitkExtrudedContour.h>
59 #include <mitkMesh.h>
60 #include <mitkPlane.h>
61 #include <mitkUnstructuredGrid.h>
62 */
63 
64 void TestAllProperties(const mitk::PropertyList *propList);
65 
70 int mitkPropertySerializationTest(int /* argc */, char * /*argv*/ [])
71 {
72  MITK_TEST_BEGIN("PropertySerializationTest");
73 
74  mitk::PropertyListSerializer::Pointer serializer =
75  mitk::PropertyListSerializer::New(); // make sure something from the lib is actually used (registration of
76  // serializers)
77 
78  /* build list of properties that will be serialized and deserialized */
80  propList->SetProperty("booltrue", mitk::BoolProperty::New(true));
81  propList->SetProperty("boolfalse", mitk::BoolProperty::New(false));
82  propList->SetProperty("int", mitk::IntProperty::New(-32));
83  propList->SetProperty("float", mitk::FloatProperty::New(-31.337));
84  propList->SetProperty("double", mitk::DoubleProperty::New(-31.337));
85  propList->SetProperty("string", mitk::StringProperty::New("Hello MITK"));
86  mitk::Point3D p3d;
87  mitk::FillVector3D(p3d, 1.0, 2.2, -3.3);
88  propList->SetProperty("p3d", mitk::Point3dProperty::New(p3d));
89  mitk::Point3I p3i;
90  mitk::FillVector3D(p3i, 1, 2, -3);
91  propList->SetProperty("p3i", mitk::Point3iProperty::New(p3i));
92  mitk::Point4D p4d;
93  mitk::FillVector4D(p4d, 1.5, 2.6, -3.7, 4.44);
94  propList->SetProperty("p4d", mitk::Point4dProperty::New(p4d));
95  mitk::Vector3D v3d;
96  mitk::FillVector3D(v3d, 1.0, 2.2, -3.3);
97  propList->SetProperty("v3d", mitk::Vector3DProperty::New(v3d));
98  propList->SetProperty("annotation", mitk::AnnotationProperty::New("My Annotation", p3d));
99  propList->SetProperty("clipping", mitk::ClippingProperty::New(p3d, v3d));
100  propList->SetProperty("color", mitk::ColorProperty::New(1.0, 0.2, 0.2));
101  // mitk::EnumerationProperty::Pointer en = mitk::EnumerationProperty::New();
102  // en->AddEnum("PC", 1); en->AddEnum("Playstation", 2); en->AddEnum("Wii", 111); en->AddEnum("XBox", 7);
103  // en->SetValue("XBox");
104  // propList->SetProperty("enum", en);
105  /*
106  propList->SetProperty("gridrep", mitk::GridRepresentationProperty::New(2));
107  propList->SetProperty("gridvol", mitk::GridVolumeMapperProperty::New(0));
108  */
109  propList->SetProperty("modality", mitk::ModalityProperty::New("Color Doppler"));
110  // propList->SetProperty("OdfNormalizationMethodProperty", mitk::OdfNormalizationMethodProperty::New("Global
111  // Maximum"));
112  // propList->SetProperty("OdfScaleByProperty", mitk::OdfScaleByProperty::New("Principal Curvature"));
113  propList->SetProperty("PlaneOrientationProperty",
114  mitk::PlaneOrientationProperty::New("Arrows in positive direction"));
115  propList->SetProperty("VtkInterpolationProperty", mitk::VtkInterpolationProperty::New("Gouraud"));
116  propList->SetProperty("VtkRepresentationProperty", mitk::VtkRepresentationProperty::New("Surface"));
117  propList->SetProperty("VtkResliceInterpolationProperty", mitk::VtkResliceInterpolationProperty::New("Cubic"));
118  propList->SetProperty("VtkScalarModeProperty", mitk::VtkScalarModeProperty::New("PointFieldData"));
119  propList->SetProperty("VtkVolumeRenderingProperty", mitk::VtkVolumeRenderingProperty::New("COMPOSITE"));
121  blt.SetTableValue(0, true);
122  blt.SetTableValue(1, false);
123  blt.SetTableValue(2, true);
124  propList->SetProperty("BoolLookupTableProperty", mitk::BoolLookupTableProperty::New(blt));
126  flt.SetTableValue(0, 3.1);
127  flt.SetTableValue(1, 3.3);
128  flt.SetTableValue(2, 7.0);
129  propList->SetProperty("FloatLookupTableProperty", mitk::FloatLookupTableProperty::New(flt));
131  ilt.SetTableValue(0, 3);
132  ilt.SetTableValue(1, 2);
133  ilt.SetTableValue(2, 11);
134  propList->SetProperty("IntLookupTableProperty", mitk::IntLookupTableProperty::New(ilt));
136  slt.SetTableValue(0, "Hello");
137  slt.SetTableValue(1, "MITK");
138  slt.SetTableValue(2, "world");
139  propList->SetProperty("StringLookupTableProperty", mitk::StringLookupTableProperty::New(slt));
140  propList->SetProperty("GroupTagProperty", mitk::GroupTagProperty::New());
141  propList->SetProperty("LevelWindowProperty", mitk::LevelWindowProperty::New(mitk::LevelWindow(100.0, 50.0)));
143  lt->ChangeOpacityForAll(0.25);
144  lt->ChangeOpacity(17, 0.88);
145  propList->SetProperty("LookupTableProperty", mitk::LookupTableProperty::New(lt));
146  propList->SetProperty("StringProperty", mitk::StringProperty::New("Oh why, gruel world"));
147  // mitk::TransferFunction::Pointer tf = mitk::TransferFunction::New();
148  // tf->SetTransferFunctionMode(1);
149  // propList->SetProperty("TransferFunctionProperty", mitk::TransferFunctionProperty::New(tf));
150 
151  MITK_TEST_CONDITION_REQUIRED(propList->GetMap()->size() > 0, "Initialize PropertyList");
152 
153  TestAllProperties(propList);
154 
155  /* test default property lists of basedata objects */
156  // activate the following tests after MaterialProperty is deleted
157 
159  node->SetData(mitk::PointSet::New());
160  TestAllProperties(node->GetPropertyList());
161  node->SetData(mitk::Image::New());
162  TestAllProperties(node->GetPropertyList());
163  node->SetData(mitk::Surface::New());
164  TestAllProperties(node->GetPropertyList());
165  node->SetData(mitk::VtkWidgetRendering::New());
166  TestAllProperties(node->GetPropertyList());
167 
168  /*
169  node->SetData(mitk::Contour::New());
170  TestAllProperties(node->GetPropertyList());
171  node->SetData(mitk::ContourSet::New());
172  TestAllProperties(node->GetPropertyList());
173  node->SetData(mitk::Mesh::New());
174  TestAllProperties(node->GetPropertyList());
175  node->SetData(mitk::Cone::New());
176  TestAllProperties(node->GetPropertyList());
177  node->SetData(mitk::Cuboid::New());
178  TestAllProperties(node->GetPropertyList());
179  node->SetData(mitk::Cylinder::New());
180  TestAllProperties(node->GetPropertyList());
181  node->SetData(mitk::Ellipsoid::New());
182  TestAllProperties(node->GetPropertyList());
183  node->SetData(mitk::ExtrudedContour::New());
184  TestAllProperties(node->GetPropertyList());
185  node->SetData(mitk::Plane::New());
186  TestAllProperties(node->GetPropertyList());
187  //node->SetData(mitk::TrackingVolume::New()); // TrackingVolume is in IGT Module, it does not have special
188  properties, therefore we skip it here
189  //TestAllProperties(node->GetPropertyList());
190  node->SetData(mitk::UnstructuredGrid::New());
191  TestAllProperties(node->GetPropertyList());
192  */
193 
194  /* untested base data types:
195  BaseDataTestImplementation
196  RenderWindowFrame
197  GeometryData
198  mitk::PlaneGeometryData
199  GradientBackground
200  ItkBaseDataAdapter
201  SlicedData
202  OdfImage
203  SeedsImage
204  TensorImage
205  BoundingObject
206  BoundingObjectGroup
207  */
208 
209  MITK_TEST_END();
210 }
211 
213 {
214  assert(propList);
215 
216  /* try to serialize each property in the list, then deserialize again and check for equality */
217  for (auto it = propList->GetMap()->begin();
218  it != propList->GetMap()->end();
219  ++it)
220  {
221  const mitk::BaseProperty *prop = it->second;
222  // construct name of serializer class
223  std::string serializername = std::string(prop->GetNameOfClass()) + "Serializer";
224  std::list<itk::LightObject::Pointer> allSerializers =
225  itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str());
226  MITK_TEST_CONDITION(allSerializers.size() > 0, std::string("Creating serializers for ") + serializername);
227  if (allSerializers.size() == 0)
228  {
229  MITK_TEST_OUTPUT(<< "serialization not possible, skipping " << prop->GetNameOfClass());
230  continue;
231  }
232  if (allSerializers.size() > 1)
233  {
234  MITK_TEST_OUTPUT(<< "Warning: " << allSerializers.size() << " serializers found for " << prop->GetNameOfClass()
235  << "testing only the first one.");
236  }
237  auto *serializer =
238  dynamic_cast<mitk::BasePropertySerializer *>(allSerializers.begin()->GetPointer());
239  MITK_TEST_CONDITION(serializer != nullptr, serializername + std::string(" is valid"));
240  if (serializer != nullptr)
241  {
242  serializer->SetProperty(prop);
243  TiXmlElement *valueelement = nullptr;
244  try
245  {
246  valueelement = serializer->Serialize();
247  }
248  catch (...)
249  {
250  }
251  MITK_TEST_CONDITION(valueelement != nullptr, std::string("Serialize property with ") + serializername);
252 
253  if (valueelement == nullptr)
254  {
255  MITK_TEST_OUTPUT(<< "serialization failed, skipping deserialization");
256  continue;
257  }
258 
259  mitk::BaseProperty::Pointer deserializedProp = serializer->Deserialize(valueelement);
260  MITK_TEST_CONDITION(deserializedProp.IsNotNull(), "serializer created valid property");
261  if (deserializedProp.IsNotNull())
262  {
263  MITK_TEST_CONDITION(*(deserializedProp.GetPointer()) == *prop,
264  "deserialized property equals initial property for type " << prop->GetNameOfClass());
265  }
266  }
267  else
268  {
269  MITK_TEST_OUTPUT(<< "created serializer object is of class "
270  << allSerializers.begin()->GetPointer()->GetNameOfClass())
271  }
272  } // for all properties
273 }
static Pointer New()
static Pointer New()
void SetTableValue(IdentifierType id, ValueType value)
static Pointer New()
static Pointer New()
static Pointer New()
static Pointer New()
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
int mitkPropertySerializationTest(int, char *[])
Test for all PropertySerializer classes.
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
Key-value list holding instances of BaseProperty.
static Pointer New()
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
#define MITK_TEST_OUTPUT(x)
Output some text.
The LevelWindow class Class to store level/window values.
static Pointer New()
static Pointer New()
static Pointer New()
Abstract base class for properties.
static Pointer New()
static Pointer New()
#define MITK_TEST_CONDITION(COND, MSG)
void TestAllProperties(const mitk::PropertyList *propList)
Base class for objects that serialize BaseProperty types.
static Pointer New()
void FillVector4D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z, mitk::ScalarType t)
Definition: mitkArray.h:114
static Pointer New()
static Pointer New()
static Pointer New()
static Pointer New()
static Pointer New()
static Pointer New()
specializations of GenericLookupTable
and MITK_TEST_END()
static Pointer New()
static Pointer New()
static Pointer New()
static Pointer New()
const PropertyMap * GetMap() const