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