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