Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkDataNodeTest.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 "mitkDataNode.h"
14 
15 #include "mitkVtkPropRenderer.h"
16 #include <vtkWindow.h>
17 
18 #include "mitkTestingMacros.h"
19 
20 #include <iostream>
21 
22 // Basedata Test
23 #include <mitkGeometryData.h>
24 #include <mitkImage.h>
25 #include <mitkPlaneGeometryData.h>
26 #include <mitkPointSet.h>
27 #include <mitkSurface.h>
28 
29 // Mapper Test
30 #include <mitkImageVtkMapper2D.h>
32 #include <mitkSurfaceVtkMapper2D.h>
33 
37 #include <mitkSurfaceVtkMapper3D.h>
38 
39 // Interactors
41 
42 // Property list Test
43 #include <mitkImageGenerator.h>
44 
53 class mitkDataNodeTestClass
54 {
55 public:
56  static void TestDataSetting(mitk::DataNode::Pointer dataNode)
57  {
58  mitk::BaseData::Pointer baseData;
59 
60  // nullptr pointer Test
61  dataNode->SetData(baseData);
62  MITK_TEST_CONDITION(baseData == dataNode->GetData(), "Testing if a nullptr pointer was set correctly")
63 
64  baseData = mitk::GeometryData::New();
65  dataNode->SetData(baseData);
66  MITK_TEST_CONDITION(baseData == dataNode->GetData(), "Testing if a GeometryData object was set correctly")
67 
68  baseData = mitk::PlaneGeometryData::New();
69  dataNode->SetData(baseData);
70  MITK_TEST_CONDITION(baseData == dataNode->GetData(), "Testing if a PlaneGeometryData object was set correctly")
71 
72  baseData = mitk::PointSet::New();
73  dataNode->SetData(baseData);
74  MITK_TEST_CONDITION(baseData == dataNode->GetData(), "Testing if a PointSet object was set correctly")
75 
76  baseData = mitk::Image::New();
77  dataNode->SetData(baseData);
78  MITK_TEST_CONDITION(baseData == dataNode->GetData(), "Testing if a Image object was set correctly")
79 
80  baseData = mitk::Surface::New();
81  dataNode->SetData(baseData);
82  MITK_TEST_CONDITION(baseData == dataNode->GetData(), "Testing if a Surface object was set correctly")
83 
84  dataNode->SetData(nullptr);
85  MITK_TEST_CONDITION(nullptr == dataNode->GetData(),
86  "Testing if base data (already set) was replaced by a nullptr pointer")
87  }
88  static void TestMapperSetting(mitk::DataNode::Pointer dataNode)
89  {
90  // tests the SetMapper() method
91  // in dataNode is a mapper vector which can be accessed by index
92  // in this test method we use only slot 0 (filled with null) and slot 1
93  // so we also test the destructor of the mapper classes
94  mitk::Mapper::Pointer mapper;
95 
96  dataNode->SetMapper(0, mapper);
97  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(0), "Testing if a nullptr pointer was set correctly")
98 
100  dataNode->SetMapper(1, mapper);
101  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(1), "Testing if a PlaneGeometryDataMapper2D was set correctly")
102  MITK_TEST_CONDITION(dataNode == mapper->GetDataNode(), "Testing if the mapper returns the right DataNode")
103 
104  mapper = mitk::ImageVtkMapper2D::New();
105  dataNode->SetMapper(1, mapper);
106  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(1), "Testing if a ImageVtkMapper2D was set correctly")
107  MITK_TEST_CONDITION(dataNode == mapper->GetDataNode(), "Testing if the mapper returns the right DataNode")
108 
110  dataNode->SetMapper(1, mapper);
111  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(1), "Testing if a PointSetVtkMapper2D was set correctly")
112  MITK_TEST_CONDITION(dataNode == mapper->GetDataNode(), "Testing if the mapper returns the right DataNode")
113 
115  dataNode->SetMapper(1, mapper);
116  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(1), "Testing if a SurfaceGLMapper2D was set correctly")
117  MITK_TEST_CONDITION(dataNode == mapper->GetDataNode(), "Testing if the mapper returns the right DataNode")
118 
120  dataNode->SetMapper(1, mapper);
121  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(1), "Testing if a PlaneGeometryDataVtkMapper3D was set correctly")
122  MITK_TEST_CONDITION(dataNode == mapper->GetDataNode(), "Testing if the mapper returns the right DataNode")
123 
125  dataNode->SetMapper(1, mapper);
126  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(1), "Testing if a PointSetVtkMapper3D was set correctly")
127  MITK_TEST_CONDITION(dataNode == mapper->GetDataNode(), "Testing if the mapper returns the right DataNode")
128 
130  dataNode->SetMapper(1, mapper);
131  MITK_TEST_CONDITION(mapper == dataNode->GetMapper(1), "Testing if a SurfaceVtkMapper3D was set correctly")
132  MITK_TEST_CONDITION(dataNode == mapper->GetDataNode(), "Testing if the mapper returns the right DataNode")
133  }
134 
135  static void TestInteractorSetting(mitk::DataNode::Pointer dataNode)
136  {
137  // this method tests the SetInteractor() and GetInteractor methods
138  // the DataInteractor base class calls the DataNode->SetInteractor method
139 
141 
142  MITK_TEST_CONDITION(interactor == dataNode->GetDataInteractor(),
143  "Testing if a nullptr pointer was set correctly (DataInteractor)")
144 
145  interactor = mitk::PointSetDataInteractor::New();
146  interactor->SetEventConfig("PointSetConfig.xml");
147  interactor->SetDataNode(dataNode);
148  MITK_TEST_CONDITION(interactor == dataNode->GetDataInteractor(),
149  "Testing if a PointSetDataInteractor was set correctly")
150 
151  interactor = mitk::PointSetDataInteractor::New();
152  dataNode->SetDataInteractor(interactor);
153  MITK_TEST_CONDITION(interactor == dataNode->GetDataInteractor(),
154  "Testing if a PointSetDataInteractor was set correctly")
155  }
156  static void TestPropertyList(mitk::DataNode::Pointer dataNode)
157  {
158  mitk::PropertyList::Pointer propertyList = dataNode->GetPropertyList();
159 
160  MITK_TEST_CONDITION(dataNode->GetPropertyList() != nullptr, "Testing if the constructor set the propertylist")
161 
162  dataNode->SetIntProperty("int", -31337);
163  int x;
164  dataNode->GetIntProperty("int", x);
165  MITK_TEST_CONDITION(x == -31337, "Testing Set/GetIntProperty");
166 
167  dataNode->SetBoolProperty("bool", true);
168  bool b;
169  dataNode->GetBoolProperty("bool", b);
170  MITK_TEST_CONDITION(b == true, "Testing Set/GetBoolProperty");
171  dataNode->SetFloatProperty("float", -31.337);
172  float y;
173  dataNode->GetFloatProperty("float", y);
174  MITK_TEST_CONDITION(y - -31.337 < 0.01, "Testing Set/GetFloatProperty");
175  double yd = 0;
176  dataNode->GetDoubleProperty("float", yd);
177  MITK_TEST_CONDITION(mitk::Equal(yd, static_cast<double>(y)), "Testing GetDoubleProperty");
178 
179  double d = sqrt(2.0);
180  dataNode->SetDoubleProperty("double", d);
181  double read_d;
182  MITK_TEST_CONDITION(dataNode->GetDoubleProperty("double", read_d), "Testing GetDoubleProperty");
183  MITK_TEST_CONDITION(d == read_d, "Testing Set/GetDoubleProperty"); // Equal does not the same thing
184  dataNode->SetStringProperty("string", "MITK");
185  std::string s = "GANZVIELPLATZ";
186  dataNode->GetStringProperty("string", s);
187  MITK_TEST_CONDITION(s == "MITK", "Testing Set/GetStringProperty");
188 
189  std::string name = "MyTestName";
190  dataNode->SetName(name.c_str());
191  MITK_TEST_CONDITION(dataNode->GetName() == name, "Testing Set/GetName");
192  name = "MySecondTestName";
193  dataNode->SetName(name);
194  MITK_TEST_CONDITION(dataNode->GetName() == name, "Testing Set/GetName(std::string)");
195 
196  MITK_TEST_CONDITION(propertyList == dataNode->GetPropertyList(),
197  "Testing if the propertylist has changed during the last tests")
198  }
199 
200  static void TestDataPropertiesFallback(mitk::DataNode::Pointer dataNode)
201  {
202  const std::string name = "Catch Me If You Can";
203 
204  auto image = mitk::Image::New();
205  image->SetProperty(name, mitk::StringProperty::New("Don't tase me, bro!"));
206 
207  dataNode->SetData(image);
208 
209  auto property = dataNode->GetProperty(name.c_str());
210  MITK_TEST_CONDITION(nullptr != property, "Testing GetProperty data property fallback (new behavior)");
211 
212  property = dataNode->GetProperty(name.c_str(), nullptr, false);
213  MITK_TEST_CONDITION(nullptr == property, "Testing GetProperty data property fallback (old behavior)");
214  }
215 
216  static void TestSelected(mitk::DataNode::Pointer dataNode)
217  {
218  vtkRenderWindow *renderWindow = vtkRenderWindow::New();
219 
221  mitk::VtkPropRenderer::New("the first renderer", renderWindow);
222 
223  // with BaseRenderer==Null
224  MITK_TEST_CONDITION(!dataNode->IsSelected(), "Testing if this node is not set as selected")
225 
226  dataNode->SetSelected(true);
227  MITK_TEST_CONDITION(dataNode->IsSelected(), "Testing if this node is set as selected")
228  dataNode->SetSelected(false);
229 
230  dataNode->SetSelected(true, base);
231 
232  MITK_TEST_CONDITION(dataNode->IsSelected(base), "Testing if this node with right base renderer is set as selected")
233 
234  // Delete RenderWindow correctly
235  renderWindow->Delete();
236  }
237  static void TestGetMTime(mitk::DataNode::Pointer dataNode)
238  {
239  unsigned long time;
240  time = dataNode->GetMTime();
242 
243  dataNode->SetData(pointSet);
244  MITK_TEST_CONDITION(time != dataNode->GetMTime(),
245  "Testing if the node timestamp is updated after adding data to the node")
246 
247  mitk::Point3D point;
248  point.Fill(3.0);
249  pointSet->SetPoint(0, point);
250 
251  // less or equal because dataNode timestamp is little later then the basedata timestamp
252  MITK_TEST_CONDITION(pointSet->GetMTime() <= dataNode->GetMTime(),
253  "Testing if the node timestamp is updated after base data was modified")
254 
255  // testing if changing anything in the property list also sets the node in a modified state
256  unsigned long lastModified = dataNode->GetMTime();
257  dataNode->SetIntProperty("testIntProp", 2344);
258  MITK_TEST_CONDITION(lastModified <= dataNode->GetMTime(),
259  "Testing if the node timestamp is updated after property list was modified")
260  }
261  static void TestSetDataUnderPropertyChange(void)
262  {
264  mitk::Image::Pointer additionalImage = mitk::Image::New();
265 
267 
268  image = mitk::ImageGenerator::GenerateRandomImage<unsigned char>(3u, 3u);
269 
270  dataNode->SetData(image);
271 
272  const float defaultOutlineWidth = 1.0;
273  float outlineWidth = 0;
274  dataNode->GetPropertyValue("outline width", outlineWidth);
275 
276  MITK_TEST_CONDITION(mitk::Equal(outlineWidth, defaultOutlineWidth),
277  "Testing if the SetData set the default property list")
278 
279  dataNode->SetProperty("outline width", mitk::FloatProperty::New(42.0));
280  dataNode->SetData(image);
281  dataNode->GetPropertyValue("outline width", outlineWidth);
282 
283  MITK_TEST_CONDITION(mitk::Equal(outlineWidth, 42.0),
284  "Testing if the SetData does not set anything if image data is identical")
285 
286  dataNode->SetData(additionalImage);
287  dataNode->GetPropertyValue("outline width", outlineWidth);
288 
289  MITK_TEST_CONDITION(mitk::Equal(outlineWidth, 42.0),
290  "Testing if the SetData does not set the default property list if image data is already set")
291 
292  mitk::Surface::Pointer surface = mitk::Surface::New();
293  dataNode->SetData(surface);
294 
295  MITK_TEST_CONDITION(dataNode->GetPropertyValue("outline width", outlineWidth) == false,
296  "Testing if SetData cleared previous property list and set the default property list if data "
297  "of different type has been set")
298  }
299 }; // mitkDataNodeTestClass
300 int mitkDataNodeTest(int /* argc */, char * /*argv*/ [])
301 {
302  // always start with this!
303  MITK_TEST_BEGIN("DataNode")
304 
305  // let's create an object of our class
307 
308  // first test: did this work?
309  // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since
310  // it makes no sense to continue without an object.
311  MITK_TEST_CONDITION_REQUIRED(myDataNode.IsNotNull(), "Testing instantiation")
312 
313  // test setData() Method
314  mitkDataNodeTestClass::TestDataSetting(myDataNode);
315  mitkDataNodeTestClass::TestMapperSetting(myDataNode);
316  //
317  // note, that no data is set to the dataNode
318  mitkDataNodeTestClass::TestInteractorSetting(myDataNode);
319  mitkDataNodeTestClass::TestPropertyList(myDataNode);
320  mitkDataNodeTestClass::TestDataPropertiesFallback(myDataNode);
321  mitkDataNodeTestClass::TestSelected(myDataNode);
322  mitkDataNodeTestClass::TestGetMTime(myDataNode);
323  mitkDataNodeTestClass::TestSetDataUnderPropertyChange();
324 
325  // write your own tests here and use the macros from mitkTestingMacros.h !!!
326  // do not write to std::cout and do not return from this function yourself!
327 
328  // always end with this!
329  MITK_TEST_END()
330 }
static Pointer New()
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
DataCollection - Class to facilitate loading/accessing structured data.
int mitkDataNodeTest(int, char *[])
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
static Pointer New()
static Pointer New()
static Pointer New()
#define MITK_TEST_CONDITION(COND, MSG)
static Pointer New()
mitk::Image::Pointer image
static Pointer New()
static Pointer New()
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static Pointer New()
and MITK_TEST_END()
static Pointer New(const char *_arga, vtkRenderWindow *_argb)
static Pointer New()
static Pointer New()
static Pointer New()
static Pointer New()