forked from Slicer/SlicerGitSVNArchive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vtkMRMLSceneTest2.cxx
381 lines (330 loc) · 14 KB
/
vtkMRMLSceneTest2.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*=auto=========================================================================
Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH)
All Rights Reserved.
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Program: 3D Slicer
=========================================================================auto=*/
// MRML includes
#include "vtkMRMLCoreTestingMacros.h"
#include "vtkMRMLNode.h"
#include "vtkMRMLScene.h"
#include "vtkMRMLSliceNode.h"
// VTK includes
#include <vtkCallbackCommand.h>
#include <vtkCollection.h>
#include <vtkXMLDataParser.h>
// STD includes
#include <algorithm>
#include <iterator>
#include <set>
#include <sstream>
namespace
{
//---------------------------------------------------------------------------
std::vector<std::string> vector_diff(const std::vector<std::string>& v1,
const std::vector<std::string>& v2)
{
std::set<std::string> s_v1(v1.begin(), v1.end());
std::set<std::string> s_v2(v2.begin(), v2.end());
std::vector<std::string> result;
std::set_difference(s_v1.begin(), s_v1.end(),
s_v2.begin(), s_v2.end(),
std::back_inserter(result));
return result;
}
//---------------------------------------------------------------------------
class vtkMRMLSceneCallback : public vtkMRMLCoreTestingUtilities::vtkMRMLNodeCallback
{
public:
static vtkMRMLSceneCallback *New() {return new vtkMRMLSceneCallback;}
int NumberOfSingletonNodes;
// List of node that should be updated when NodeAddedEvent is caught
std::vector<std::string> NodeAddedClassNames;
virtual void ResetNumberOfEvents()
{
vtkMRMLCoreTestingUtilities::vtkMRMLNodeCallback::ResetNumberOfEvents();
this->NumberOfSingletonNodes = 0;
this->NodeAddedClassNames.clear();
}
virtual void Execute(vtkObject* caller, unsigned long eid, void *calldata)
{
vtkMRMLCoreTestingUtilities::vtkMRMLNodeCallback::Execute(caller, eid, calldata);
// Let's return if an error already occurred
if (this->CheckStatus() == EXIT_FAILURE)
{
return;
}
vtkMRMLScene* callerScene = vtkMRMLScene::SafeDownCast(caller);
if (eid == vtkMRMLScene::NodeAboutToBeAddedEvent)
{
vtkMRMLNode* node = reinterpret_cast<vtkMRMLNode*>(calldata);
if (!node)
{
SetErrorString(__LINE__, "mrmlEventCallback - NodeAboutToBeAddedEvent - node is NULL");
return;
}
}
else if (eid == vtkMRMLScene::NodeAddedEvent)
{
vtkMRMLNode* node = reinterpret_cast<vtkMRMLNode*>(calldata);
if (!node)
{
SetErrorString(__LINE__, "mrmlEventCallback - NodeAddedEvent - node is NULL");
return;
}
if (node->GetSingletonTag())
{
++this->NumberOfSingletonNodes;
}
this->NodeAddedClassNames.push_back(node->GetClassName());
}
else if (eid == vtkMRMLScene::NodeAboutToBeRemovedEvent)
{
vtkMRMLNode* node = reinterpret_cast<vtkMRMLNode*>(calldata);
if (!node)
{
SetErrorString(__LINE__, "mrmlEventCallback - NodeAboutToBeRemovedEvent - node is NULL");
return;
}
}
else if (eid == vtkMRMLScene::NodeRemovedEvent)
{
vtkMRMLNode* node = reinterpret_cast<vtkMRMLNode*>(calldata);
if (!node)
{
SetErrorString(__LINE__, "mrmlEventCallback - NodeRemovedEvent - node is NULL");
return;
}
}
else if (eid == vtkMRMLScene::StartCloseEvent)
{
if (callerScene->IsImporting())
{
SetErrorString(__LINE__, "StartCloseEvent - IsImporting is expected to be 0");
return;
}
if (!callerScene->IsClosing())
{
SetErrorString(__LINE__, "StartCloseEvent - IsClosing is expected to be 1");
return;
}
if (!callerScene->IsBatchProcessing())
{
SetErrorString(__LINE__, "StartCloseEvent - IsUpdating is expected to be 1");
return;
}
}
else if (eid == vtkMRMLScene::EndCloseEvent)
{
if (callerScene->IsImporting())
{
SetErrorString(__LINE__, "EndCloseEvent - IsImporting is expected to be 0");
return;
}
if (callerScene->IsClosing())
{
SetErrorString(__LINE__, "EndCloseEvent - IsClosing is expected to be 0");
return;
}
}
else if (eid == vtkMRMLScene::StartImportEvent)
{
if (!callerScene->IsImporting())
{
SetErrorString(__LINE__, "StartImport - ImportState is expected");
return;
}
if (!callerScene->IsBatchProcessing())
{
SetErrorString(__LINE__, "StartImport - IsBatchProcessing is expected");
return;
}
}
else if (eid == vtkMRMLScene::EndImportEvent)
{
if (callerScene->IsImporting())
{
SetErrorString(__LINE__, "EndImportEvent - ImportState is not expected");
return;
}
}
}
protected:
vtkMRMLSceneCallback()
{
this->NumberOfSingletonNodes = 0;
}
~vtkMRMLSceneCallback() {};
}; // class vtkMRMLSceneCallback
}; //namespace
//---------------------------------------------------------------------------
int vtkMRMLSceneTest2(int argc, char * argv [] )
{
if( argc < 2 )
{
std::cerr << "Error: missing arguments" << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputURL_scene.mrml " << std::endl;
return EXIT_FAILURE;
}
// Instantiate scene
vtkSmartPointer<vtkMRMLScene> scene = vtkSmartPointer<vtkMRMLScene>::New(); // vtkSmartPointer instead of vtkNew to allow SetPointer
EXERCISE_BASIC_OBJECT_METHODS(scene.GetPointer());
CHECK_INT(scene->GetNumberOfNodes(), 0);
// Add default slice orientation presets
vtkMRMLSliceNode::AddDefaultSliceOrientationPresets(scene);
// Configure mrml event observer
vtkNew<vtkMRMLSceneCallback> callback;
scene->AddObserver(vtkMRMLScene::NodeAboutToBeAddedEvent, callback.GetPointer());
scene->AddObserver(vtkMRMLScene::NodeAddedEvent, callback.GetPointer());
scene->AddObserver(vtkMRMLScene::NodeAboutToBeRemovedEvent, callback.GetPointer());
scene->AddObserver(vtkMRMLScene::NodeRemovedEvent, callback.GetPointer());
scene->AddObserver(vtkMRMLScene::StartCloseEvent, callback.GetPointer());
scene->AddObserver(vtkMRMLScene::EndCloseEvent, callback.GetPointer());
scene->AddObserver(vtkMRMLScene::StartImportEvent, callback.GetPointer());
scene->AddObserver(vtkMRMLScene::EndImportEvent, callback.GetPointer());
//---------------------------------------------------------------------------
// Make sure IsClosing, IsImporting, IsBatchProcessing default values are correct
//---------------------------------------------------------------------------
CHECK_BOOL(scene->IsClosing(), false);
CHECK_BOOL(scene->IsImporting(), false);
CHECK_BOOL(scene->IsBatchProcessing(), false);
//---------------------------------------------------------------------------
// Make sure SetIsImporting invoke events properly
//---------------------------------------------------------------------------
callback->ResetNumberOfEvents();
// it is invalid operation to end a state before starting it, should report error
TESTING_OUTPUT_ASSERT_ERRORS_BEGIN();
scene->EndState(vtkMRMLScene::ImportState);
TESTING_OUTPUT_ASSERT_ERRORS_END();
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::StartImportEvent), 0);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::EndImportEvent), 0);
callback->ResetNumberOfEvents();
scene->StartState(vtkMRMLScene::ImportState);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::StartImportEvent), 1);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::EndImportEvent), 0);
callback->ResetNumberOfEvents();
scene->EndState(vtkMRMLScene::ImportState);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::StartImportEvent), 0);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::EndImportEvent), 1);
//---------------------------------------------------------------------------
// Extract number of nodes
//---------------------------------------------------------------------------
vtkNew<vtkXMLDataParser> xmlParser;
xmlParser->SetFileName(argv[1]);
CHECK_BOOL(xmlParser->Parse()!=0, true);
int expectedNumberOfNode = xmlParser->GetRootElement()->GetNumberOfNestedElements();
CHECK_BOOL(expectedNumberOfNode>0, true);
// Loop though all exepcted node and populate expectedNodeAddedNames vector
// Note that node that can't be instantiated using CreateNodeByClass are not expected
std::vector<std::string> expectedNodeAddedClassNames; // List of node that should be added to the scene
for(int i=0; i < xmlParser->GetRootElement()->GetNumberOfNestedElements(); ++i)
{
std::string className = "vtkMRML";
className += xmlParser->GetRootElement()->GetNestedElement(i)->GetName();
// Append 'Node' prefix only if required
if (className.find("Node") != className.size() - 4)
{
className += "Node";
}
vtkSmartPointer<vtkMRMLNode> nodeSmartPointer;
nodeSmartPointer.TakeReference(scene->CreateNodeByClass(className.c_str()));
if (!nodeSmartPointer)
{
std::cout << "className:" << className << std::endl;
--expectedNumberOfNode;
}
else
{
expectedNodeAddedClassNames.push_back(className);
}
}
//---------------------------------------------------------------------------
// Check if the correct number of Events are sent - Also Keep track # of Singleton node
//---------------------------------------------------------------------------
callback->ResetNumberOfEvents();
// Load the scene
scene->SetURL( argv[1] );
scene->Connect();
CHECK_EXIT_SUCCESS(callback->CheckStatus());
std::vector<std::string> unexpectedAddedNodeNames =
vector_diff(expectedNodeAddedClassNames, callback->NodeAddedClassNames);
if (!unexpectedAddedNodeNames.empty())
{
std::cerr << "line " << __LINE__ << " - unexpectedAddedNodeNames: ";
for(size_t i = 0; i < unexpectedAddedNodeNames.size(); ++i)
{
std::cerr << unexpectedAddedNodeNames[i] << " ";
}
std::cerr << std::endl;
return EXIT_FAILURE;
}
// If additional nodes have been instantiated, let's update expectedNumberOfNode
if (static_cast<int>(callback->NodeAddedClassNames.size()) > expectedNumberOfNode)
{
expectedNumberOfNode = static_cast<int>(callback->NodeAddedClassNames.size());
}
CHECK_INT(scene->GetNumberOfNodes(), expectedNumberOfNode);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeAddedEvent), expectedNumberOfNode);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeAboutToBeAddedEvent), expectedNumberOfNode);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeRemovedEvent), 0);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeAboutToBeRemovedEvent), 0);
// Since Connect() means Clean (or close) first then import,
// let's check if one event of each has been triggered.
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::StartCloseEvent), 1);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::EndCloseEvent), 1);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::StartImportEvent), 1);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::EndImportEvent), 1);
CHECK_BOOL(scene->IsBatchProcessing(), false);
int currentNumberOfSingletonTag = callback->NumberOfSingletonNodes;
//---------------------------------------------------------------------------
// Clear all node except Singleton ones
//---------------------------------------------------------------------------
callback->ResetNumberOfEvents();
// Clear scene expect singletons
scene->Clear(0);
CHECK_EXIT_SUCCESS(callback->CheckStatus());
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeRemovedEvent) + currentNumberOfSingletonTag, expectedNumberOfNode);
CHECK_INT(scene->GetNumberOfNodes(), currentNumberOfSingletonTag);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeAboutToBeRemovedEvent), callback->GetNumberOfEvents(vtkMRMLScene::NodeRemovedEvent));
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeAboutToBeAddedEvent), 0);
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeAddedEvent), 0);
//---------------------------------------------------------------------------
// Clear all node including Singleton ones
//---------------------------------------------------------------------------
callback->ResetNumberOfEvents();
// Clear scene including singletons - Note that by now the scene should contain only singletons
scene->Clear(1);
CHECK_EXIT_SUCCESS(callback->CheckStatus());
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeAboutToBeRemovedEvent), currentNumberOfSingletonTag);
CHECK_INT(scene->GetNumberOfNodes(), 0);
#if 0
//---------------------------------------------------------------------------
// Print content
//---------------------------------------------------------------------------
vtkCollection * collection = scene->GetNodes();
std::cout << "Collection GetNumberOfItems() = ";
std::cout << collection->GetNumberOfItems() << std::endl;
std::cout << "List of Node Names in this Scene" << std::endl;
vtkCollectionSimpleIterator it;
vtkMRMLNode* node = NULL;
vtkCollection *nodes = scene->GetNodes();
for (nodes->InitTraversal(it);
(node = vtkMRMLNode::SafeDownCast(nodes->GetNextItemAsObject(it)));)
{
std::cout << " " << node->GetName() << std::endl;
}
#endif
//---------------------------------------------------------------------------
// Check if the destructor remove all nodes by checking number of NodeRemovedEvent sent
//---------------------------------------------------------------------------
// Expected number of nodes that should removed
int numberOfNodes = scene->GetNumberOfNodes();
callback->ResetNumberOfEvents();
scene = NULL;
CHECK_EXIT_SUCCESS(callback->CheckStatus());
CHECK_INT(callback->GetNumberOfEvents(vtkMRMLScene::NodeRemovedEvent), numberOfNodes);
std::cout << "Test completed successfully" << std::endl;
return EXIT_SUCCESS;
}