forked from Slicer/SlicerGitSVNArchive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vtkMRMLSceneViewNode.cxx
870 lines (771 loc) · 26.6 KB
/
vtkMRMLSceneViewNode.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*=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
Module: $RCSfile: vtkMRMLSceneViewNode.cxx,v $
Date: $Date: 2006/03/17 17:01:53 $
Version: $Revision: 1.14 $
=========================================================================auto=*/
// MRML includes
#include "vtkMRMLHierarchyNode.h"
#include "vtkMRMLScene.h"
#include "vtkMRMLSceneViewNode.h"
#include "vtkMRMLSceneViewStorageNode.h"
// VTKsys includes
#include <vtksys/SystemTools.hxx>
// VTK includes
#include <vtkCollection.h>
#include <vtkImageData.h>
#include <vtkObjectFactory.h>
#include <vtkSmartPointer.h>
// STD includes
#include <cassert>
#include <sstream>
#include <stack>
//----------------------------------------------------------------------------
vtkMRMLNodeNewMacro(vtkMRMLSceneViewNode);
//----------------------------------------------------------------------------
vtkMRMLSceneViewNode::vtkMRMLSceneViewNode()
{
this->HideFromEditors = 0;
this->SnapshotScene = nullptr;
// this->ScreenShot = vtkImageData::New();
this->ScreenShot = nullptr;
this->ScreenShotType = 0;
}
//----------------------------------------------------------------------------
vtkMRMLSceneViewNode::~vtkMRMLSceneViewNode()
{
if (this->SnapshotScene)
{
this->SnapshotScene->Delete();
this->SnapshotScene = nullptr;
}
if (this->ScreenShot)
{
this->ScreenShot->Delete();
this->ScreenShot = nullptr;
}
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::WriteXML(ostream& of, int nIndent)
{
Superclass::WriteXML(of, nIndent);
of << " screenshotType=\"" << this->GetScreenShotType() << "\"";
vtkStdString description = this->GetSceneViewDescription();
vtksys::SystemTools::ReplaceString(description,"\n","<br>");
of << " sceneViewDescription=\"" << this->XMLAttributeEncodeString(description) << "\"";
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::WriteNodeBodyXML(ostream& of, int nIndent)
{
if (!this->SnapshotScene)
{
return;
}
// first make sure that the scene view scene is to be saved relative to the same place as the main scene
this->SnapshotScene->SetRootDirectory(this->GetScene()->GetRootDirectory());
this->SetAbsentStorageFileNames();
for (int n=0; n < this->SnapshotScene->GetNodes()->GetNumberOfItems(); n++)
{
vtkMRMLNode* node = (vtkMRMLNode*)this->SnapshotScene->GetNodes()->GetItemAsObject(n);
if (node && !node->IsA("vtkMRMLSceneViewNode") && node->GetSaveWithScene())
{
vtkIndent vindent(nIndent+1);
of << vindent << "<" << node->GetNodeTagName() << "\n";
node->WriteXML(of, nIndent + 2);
of << vindent << ">";
node->WriteNodeBodyXML(of, nIndent+1);
of << "</" << node->GetNodeTagName() << ">\n";
}
}
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::ReadXMLAttributes(const char** atts)
{
int disabledModify = this->StartModify();
Superclass::ReadXMLAttributes(atts);
const char* attName;
const char* attValue;
while (*atts != nullptr)
{
attName = *(atts++);
attValue = *(atts++);
if (!strcmp(attName, "screenshotType"))
{
std::stringstream ss;
ss << attValue;
int screenshotType;
ss >> screenshotType;
this->SetScreenShotType(screenshotType);
}
else if(!strcmp(attName, "sceneViewDescription"))
{
// can have spaces in the description, don't use stringstream
vtkStdString sceneViewDescription = vtkStdString(attValue);
vtksys::SystemTools::ReplaceString(sceneViewDescription,"[br]","\n");
this->SetSceneViewDescription(sceneViewDescription);
}
}
// for backward compatibility:
// now read the screenCapture if there's a directory for them
// TODO: don't do this if there is a storage node already, but the problem
// is that the storage node will get set after, so GetStorageNode returns
// null right now
vtkStdString screenCapturePath;
if (this->GetScene() &&
this->GetScene()->GetRootDirectory())
{
screenCapturePath += this->GetScene()->GetRootDirectory();
}
else
{
screenCapturePath += ".";
}
screenCapturePath += "/";
screenCapturePath += "ScreenCaptures/";
vtkStdString screenCaptureFilename;
screenCaptureFilename += screenCapturePath;
if (this->GetID())
{
screenCaptureFilename += this->GetID();
}
else
{
screenCaptureFilename += "vtkMRMLSceneViewNodeNoID";
}
screenCaptureFilename += ".png";
if (vtksys::SystemTools::FileExists(vtksys::SystemTools::ConvertToOutputPath(screenCaptureFilename.c_str()).c_str(),true))
{
// create a storage node and use it to read the file
vtkMRMLStorageNode *storageNode = this->GetStorageNode();
if (storageNode == nullptr)
{
// only read the directory if there isn't a storage node already
this->AddDefaultStorageNode(vtksys::SystemTools::ConvertToOutputPath(screenCaptureFilename.c_str()).c_str());
storageNode = this->GetStorageNode();
if (storageNode)
{
vtkWarningMacro("ReadXMLAttributes: found the ScreenCapture directory, creating a storage node to read the image file at\n\t" << storageNode->GetFileName() << "\n\tImage data be overwritten if there is a storage node pointing to another file");
storageNode->ReadData(this);
}
}
else
{
vtkWarningMacro("ReadXMLAttributes: there is a ScreenCaptures directory with a valid file in it, but waiting to let the extant storage node read it's image file");
}
}
this->EndModify(disabledModify);
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::ProcessChildNode(vtkMRMLNode *node)
{
// for the child nodes in the scene view scene, we don't want to invoke any
// pending modified events when done processing them, so just use the bare
// DisableModifiedEventOn and SetDisableModifiedEvent calls rather than
// using StartModify and EndModify
int disabledModify = this->GetDisableModifiedEvent();
this->DisableModifiedEventOn();
int disabledModifyNode = node->GetDisableModifiedEvent();
node->DisableModifiedEventOn();
Superclass::ProcessChildNode(node);
node->SetAddToSceneNoModify(0);
if (this->SnapshotScene == nullptr)
{
this->SnapshotScene = vtkMRMLScene::New();
}
this->SnapshotScene->GetNodes()->vtkCollection::AddItem((vtkObject *)node);
this->SnapshotScene->AddNodeID(node);
node->SetScene(this->SnapshotScene);
node->SetDisableModifiedEvent(disabledModifyNode);
this->SetDisableModifiedEvent(disabledModify);
}
//----------------------------------------------------------------------------
// Copy the node's attributes to this object.
// Does NOT copy: ID, FilePrefix, Name, VolumeID
void vtkMRMLSceneViewNode::Copy(vtkMRMLNode *anode)
{
Superclass::Copy(anode);
vtkMRMLSceneViewNode *snode = (vtkMRMLSceneViewNode *) anode;
this->SetScreenShot(vtkMRMLSceneViewNode::SafeDownCast(anode)->GetScreenShot());
this->SetScreenShotType(vtkMRMLSceneViewNode::SafeDownCast(anode)->GetScreenShotType());
this->SetSceneViewDescription(vtkMRMLSceneViewNode::SafeDownCast(anode)->GetSceneViewDescription());
if (this->SnapshotScene == nullptr)
{
this->SnapshotScene = vtkMRMLScene::New();
}
else
{
this->SnapshotScene->GetNodes()->RemoveAllItems();
this->SnapshotScene->ClearNodeIDs();
}
vtkMRMLNode *node = nullptr;
if ( snode->SnapshotScene != nullptr )
{
int n;
for (n=0; n < snode->SnapshotScene->GetNodes()->GetNumberOfItems(); n++)
{
node = (vtkMRMLNode*)snode->SnapshotScene->GetNodes()->GetItemAsObject(n);
if (node)
{
node->SetScene(this->SnapshotScene);
this->SnapshotScene->AddNodeID(node);
}
}
}
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::PrintSelf(ostream& os, vtkIndent indent)
{
Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::UpdateScene(vtkMRMLScene *scene)
{
// the superclass update scene ensures that the storage node is read into
// this storable node
Superclass::UpdateScene(scene);
if (!scene)
{
return;
}
if (this->SnapshotScene)
{
// node references are in (this->SavedScene) already, so they should not be modified
// but there could have been some node ID changes, so get them and update the
// references accordingly
this->SnapshotScene->CopyNodeChangedIDs(scene);
this->SnapshotScene->UpdateNodeChangedIDs();
this->SnapshotScene->UpdateNodeReferences();
}
this->UpdateStoredScene();
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::UpdateStoredScene()
{
if (this->Scene == nullptr)
{
return;
}
if (this->SnapshotScene == nullptr)
{
return;
}
unsigned int nnodesSanpshot = this->SnapshotScene->GetNodes()->GetNumberOfItems();
unsigned int n;
vtkMRMLNode *node = nullptr;
// prevent data read in UpdateScene
for (n=0; n<nnodesSanpshot; n++)
{
node = vtkMRMLNode::SafeDownCast(this->SnapshotScene->GetNodes()->GetItemAsObject(n));
if (node)
{
node->SetAddToSceneNoModify(0);
}
}
// update nodes in the snapshot
for (n=0; n<nnodesSanpshot; n++)
{
node = vtkMRMLNode::SafeDownCast(this->SnapshotScene->GetNodes()->GetItemAsObject(n));
if (node)
{
node->UpdateScene(this->SnapshotScene);
}
}
/**
// update nodes in the snapshot
for (n=0; n<nnodesSanpshot; n++)
{
node = vtkMRMLNode::SafeDownCast(this->Nodes->GetNodes()->GetItemAsObject(n));
if (node)
{
node->SetAddToSceneNoModify(1);
}
}
***/
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::StoreScene()
{
if (this->Scene == nullptr)
{
return;
}
if (this->SnapshotScene == nullptr)
{
this->SnapshotScene = vtkMRMLScene::New();
}
else
{
this->SnapshotScene->Clear(1);
}
if (this->GetScene())
{
this->SnapshotScene->SetRootDirectory(this->GetScene()->GetRootDirectory());
}
// make sure that any storable nodes in the scene have storage nodes before
// saving them to the scene view, this prevents confusion on scene view
// restore with mismatched nodes.
std::vector<vtkMRMLNode *> nodes;
int nnodes = this->GetScene()->GetNodesByClass("vtkMRMLStorableNode", nodes);
for (int i = 0; i < nnodes; ++i)
{
vtkMRMLStorableNode *storableNode = vtkMRMLStorableNode::SafeDownCast(nodes[i]);
if (storableNode)
{
if (this->IncludeNodeInSceneView(storableNode) &&
storableNode->GetSaveWithScene() )
{
if (!storableNode->GetStorageNode())
{
// No storage node in the main scene, try add one.
storableNode->AddDefaultStorageNode();
vtkMRMLStorageNode* storageNode = storableNode->GetStorageNode();
if (storageNode)
{
std::string fileBaseName = std::string(storableNode->GetName());
std::string extension = storageNode->GetDefaultWriteFileExtension();
std::string storageFileName = fileBaseName + std::string(".") + extension;
storageNode->SetFileName(storageFileName.c_str());
}
}
}
}
}
/// \todo: GetNumberOfNodes/GetNthNode is slow, fasten by using collection
/// iterators.
for (int n=0; n < this->Scene->GetNumberOfNodes(); n++)
{
vtkMRMLNode *node = this->Scene->GetNthNode(n);
if (this->IncludeNodeInSceneView(node) &&
node->GetSaveWithScene() )
{
vtkSmartPointer<vtkMRMLNode> newNode = vtkSmartPointer<vtkMRMLNode>::Take(node->CreateNodeInstance());
newNode->SetScene(this->SnapshotScene);
int oldMode = newNode->GetDisableModifiedEvent();
newNode->DisableModifiedEventOn();
newNode->Copy(node);
newNode->SetDisableModifiedEvent(oldMode);
newNode->SetID(node->GetID());
newNode->SetAddToSceneNoModify(1);
this->SnapshotScene->AddNode(newNode);
newNode->SetAddToSceneNoModify(0);
// sanity check
assert(newNode->GetScene() == this->SnapshotScene);
}
}
this->SnapshotScene->CopyNodeReferences(this->GetScene());
this->SnapshotScene->CopyNodeChangedIDs(this->GetScene());
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::AddMissingNodes()
{
if (this->Scene == nullptr)
{
vtkWarningMacro("No scene to add nodes from");
return;
}
if (this->SnapshotScene == nullptr)
{
vtkWarningMacro("No scene to add to");
return;
}
unsigned int numNodesInSceneView = this->SnapshotScene->GetNodes()->GetNumberOfItems();
unsigned int n;
vtkMRMLNode *node = nullptr;
// build the list of nodes in the scene view
std::map<std::string, vtkMRMLNode*> snapshotMap;
for (n=0; n<numNodesInSceneView; n++)
{
node = vtkMRMLNode::SafeDownCast(this->SnapshotScene->GetNodes()->GetItemAsObject(n));
if (node && node->GetID())
{
snapshotMap[node->GetID()] = node;
}
}
if (snapshotMap.size() == 0)
{
// no missing nodes
return;
}
// add the missing ones from the scene
vtkCollectionSimpleIterator it;
vtkCollection* sceneNodes = this->Scene->GetNodes();
int nodesAdded = 0;
for (sceneNodes->InitTraversal(it);
(node = vtkMRMLNode::SafeDownCast(sceneNodes->GetNextItemAsObject(it))) ;)
{
std::map<std::string, vtkMRMLNode*>::iterator iter = snapshotMap.find(std::string(node->GetID()));
// ignore scene view nodes, the snapshot clip nodes, hierarchy nodes associated with the
// sceneview nodes nor top level scene view hierarchy nodes
if (iter == snapshotMap.end() &&
this->IncludeNodeInSceneView(node) &&
node->GetSaveWithScene())
{
vtkDebugMacro("AddMissingNodes: Adding node with id " << node->GetID());
vtkSmartPointer<vtkMRMLNode> newNode = vtkSmartPointer<vtkMRMLNode>::Take(node->CreateNodeInstance());
newNode->SetScene(this->SnapshotScene);
int oldMode = newNode->GetDisableModifiedEvent();
newNode->DisableModifiedEventOn();
newNode->Copy(node);
newNode->SetDisableModifiedEvent(oldMode);
newNode->SetID(node->GetID());
newNode->SetAddToSceneNoModify(1);
this->SnapshotScene->AddNode(newNode);
newNode->SetAddToSceneNoModify(0);
// sanity check
assert(newNode->GetScene() == this->SnapshotScene);
nodesAdded++;
}
}
vtkDebugMacro("AddMissingNodes: Added " << nodesAdded << " nodes to this scene view");
if (nodesAdded > 0)
{
// update references for any ids that got changed
this->SnapshotScene->UpdateNodeReferences();
}
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::RestoreScene(bool removeNodes)
{
if (this->Scene == nullptr)
{
vtkWarningMacro("No scene to restore onto");
return;
}
if (this->SnapshotScene == nullptr)
{
vtkWarningMacro("No nodes to restore");
return;
}
unsigned int numNodesInSceneView = this->SnapshotScene->GetNodes()->GetNumberOfItems();
unsigned int n;
vtkMRMLNode *node = nullptr;
this->Scene->StartState(vtkMRMLScene::RestoreState);
// remove nodes in the scene which are not stored in the snapshot
std::map<std::string, vtkMRMLNode*> snapshotMap;
for (n=0; n<numNodesInSceneView; n++)
{
node = vtkMRMLNode::SafeDownCast(this->SnapshotScene->GetNodes()->GetItemAsObject(n));
if (node)
{
/***
const char *newID = this->Scene->GetChangedID(node->GetID());
if (newID)
{
snapshotMap[newID] = node;
}
else
{
snapshotMap[node->GetID()] = node;
}
***/
if (node->GetID())
{
snapshotMap[node->GetID()] = node;
}
}
}
// Identify which nodes must be removed from the scene.
vtkCollectionSimpleIterator it;
vtkCollection* sceneNodes = this->Scene->GetNodes();
// Use smart pointer to ensure the nodes still exist when being removed.
// Indeed, removing a node can have the side effect of removing other nodes.
std::stack<vtkSmartPointer<vtkMRMLNode> > removedNodes;
for (sceneNodes->InitTraversal(it);
(node = vtkMRMLNode::SafeDownCast(sceneNodes->GetNextItemAsObject(it))) ;)
{
std::map<std::string, vtkMRMLNode*>::iterator iter = snapshotMap.find(std::string(node->GetID()));
// don't remove the scene view nodes, the snapshot clip nodes, hierarchy nodes associated with the
// sceneview nodes nor top level scene view hierarchy nodes
if (iter == snapshotMap.end() &&
this->IncludeNodeInSceneView(node) &&
node->GetSaveWithScene())
{
removedNodes.push(vtkSmartPointer<vtkMRMLNode>(node));
}
}
while(!removedNodes.empty())
{
vtkMRMLNode* nodeToRemove = removedNodes.top().GetPointer();
// Remove the node only if it's not part of the scene.
bool isNodeInScene = (nodeToRemove->GetScene() == this->Scene);
// Decrease reference count before removing it from the scene
// to give the opportunity of the node to be deleted in RemoveNode
// (standard behavior).
removedNodes.pop();
if (isNodeInScene)
{
if (removeNodes)
{
this->Scene->RemoveNode(nodeToRemove);
}
else
{
vtkErrorMacro("RestoreScene encountered a node in the scene that needs to be removed to restore the scene view '" << this->GetSceneViewDescription().c_str() << "'.\n\tNot removing node named '" << nodeToRemove->GetName() << "',\n\tReturning without restoring the scene.");
// signal that done trying to restore the scene
this->Scene->EndState(vtkMRMLScene::RestoreState);
// signal that there is an error state
this->Scene->SetErrorMessage("Unable to restore scene, data in main Slicer scene that is not included in the scene view");
this->Scene->SetErrorCode(1);
return;
}
}
}
std::vector<vtkMRMLNode *> addedNodes;
for (n=0; n < numNodesInSceneView; n++)
{
node = vtkMRMLNode::SafeDownCast(this->SnapshotScene->GetNodes()->GetItemAsObject(n));
if (node)
{
// don't restore certain nodes that might have been in the scene view by mistake
if (this->IncludeNodeInSceneView(node))
{
vtkMRMLNode *snode = this->Scene->GetNodeByID(node->GetID());
if (snode)
{
snode->SetScene(this->Scene);
// to prevent copying of default info if not stored in snapshot
MRMLNodeModifyBlocker blocker(snode);
snode->Copy(node);
// to prevent reading data on UpdateScene()
snode->SetAddToSceneNoModify(0);
}
else
{
vtkMRMLNode *newNode = node->CreateNodeInstance();
newNode->CopyWithScene(node);
addedNodes.push_back(newNode);
newNode->SetAddToSceneNoModify(1);
this->Scene->AddNode(newNode);
newNode->Delete();
// to prevent reading data on UpdateScene()
// but new nodes should read their data
//node->SetAddToSceneNoModify(0);
}
}
}
}
// update all nodes in the scene
//this->Scene->UpdateNodeReferences(this->Nodes);
for (sceneNodes->InitTraversal(it);
(node = vtkMRMLNode::SafeDownCast(sceneNodes->GetNextItemAsObject(it))) ;)
{
if (this->IncludeNodeInSceneView(node) && node->GetSaveWithScene())
{
node->UpdateScene(this->Scene);
}
}
//this->Scene->SetIsClosing(0);
for(n=0; n<addedNodes.size(); n++)
{
//addedNodes[n]->UpdateScene(this->Scene);
//this->Scene->InvokeEvent(vtkMRMLScene::NodeAddedEvent, addedNodes[n] );
}
this->Scene->EndState(vtkMRMLScene::RestoreState);
#ifndef NDEBUG
// sanity checks
for (sceneNodes->InitTraversal(it);
(node = vtkMRMLNode::SafeDownCast(sceneNodes->GetNextItemAsObject(it))) ;)
{
assert(node->GetScene() == this->Scene);
}
#endif
}
//----------------------------------------------------------------------------
vtkMRMLScene* vtkMRMLSceneViewNode::GetStoredScene()
{
return this->SnapshotScene;
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::SetAbsentStorageFileNames()
{
if (this->Scene == nullptr)
{
return;
}
if (this->SnapshotScene == nullptr)
{
return;
}
// TBD: determine if storage nodes in the all scene views need unique file names
// in order to support reading into scene view nodes on xml read.
unsigned int numNodesInSceneView = this->SnapshotScene->GetNodes()->GetNumberOfItems();
unsigned int n;
vtkMRMLNode *node = nullptr;
for (n=0; n<numNodesInSceneView; n++)
{
node = vtkMRMLNode::SafeDownCast(this->SnapshotScene->GetNodes()->GetItemAsObject(n));
if (node)
{
// for storage nodes replace full path with relative
vtkMRMLStorageNode *snode = vtkMRMLStorageNode::SafeDownCast(node);
if (snode)
{
vtkMRMLNode *node1 = this->Scene->GetNodeByID(snode->GetID());
if (node1)
{
vtkMRMLStorageNode *snode1 = vtkMRMLStorageNode::SafeDownCast(node1);
if (snode1)
{
snode->SetFileName(snode1->GetFileName());
int numberOfFileNames = snode1->GetNumberOfFileNames();
if (numberOfFileNames > 0)
{
snode->ResetFileNameList();
for (int i = 0; i < numberOfFileNames; ++i)
{
snode->AddFileName(snode1->GetNthFileName(i));
}
}
}
}
}
} //if (node)
} //for (n=0; n<numNodesInSceneView; n++)
}
//----------------------------------------------------------------------------
vtkMRMLStorageNode* vtkMRMLSceneViewNode::CreateDefaultStorageNode()
{
vtkMRMLScene* scene = this->GetScene();
if (scene == nullptr)
{
vtkErrorMacro("CreateDefaultStorageNode failed: scene is invalid");
return nullptr;
}
return vtkMRMLStorageNode::SafeDownCast(
scene->CreateNodeByClass("vtkMRMLSceneViewStorageNode"));
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::SetSceneViewDescription(const vtkStdString& newDescription)
{
if (this->SceneViewDescription == newDescription)
{
return;
}
this->SceneViewDescription = newDescription;
this->StorableModifiedTime.Modified();
this->Modified();
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::SetScreenShot(vtkImageData* newScreenShot)
{
this->StorableModifiedTime.Modified();
//vtkSetObjectBodyMacro(ScreenShot, vtkImageData, newScreenShot);
if (!newScreenShot)
{
if (this->ScreenShot)
{
this->ScreenShot->Delete();
}
this->ScreenShot = nullptr;
}
else
{
if (!this->ScreenShot)
{
this->ScreenShot = vtkImageData::New();
}
this->ScreenShot->DeepCopy(newScreenShot);
}
this->Modified();
}
//----------------------------------------------------------------------------
void vtkMRMLSceneViewNode::SetScreenShotType(int newScreenShotType)
{
if (this->ScreenShotType == newScreenShotType)
{
return;
}
this->ScreenShotType = newScreenShotType;
this->StorableModifiedTime.Modified();
this->Modified();
}
//----------------------------------------------------------------------------
int vtkMRMLSceneViewNode::GetNodesByClass(const char *className, std::vector<vtkMRMLNode *> &nodes)
{
if (!this->SnapshotScene)
{
return 0;
}
return this->SnapshotScene->GetNodesByClass(className, nodes);
}
//------------------------------------------------------------------------------
vtkCollection* vtkMRMLSceneViewNode::GetNodesByClass(const char *className)
{
if (!this->SnapshotScene)
{
return nullptr;
}
return this->SnapshotScene->GetNodesByClass(className);
}
//----------------------------------------------------------------------------
bool vtkMRMLSceneViewNode::IncludeNodeInSceneView(vtkMRMLNode *node)
{
if (!node)
{
return false;
}
bool includeInView = true;
// check for simple node types
// Storable nodes cannot be restored from scene views, as they would require
// storage nodes to load content, therefore we do not include them in scene views
// (except camera, which is a storable node but actually does not require a
// storage node and it is important to save in scene views).
if (node->IsA("vtkMRMLSceneViewNode") ||
node->IsA("vtkMRMLSceneViewStorageNode") ||
node->IsA("vtkMRMLSnapshotClipNode") ||
(node->IsA("vtkMRMLStorableNode") && !node->IsA("vtkMRMLCameraNode")) )
{
includeInView = false;
}
// check for scene view hierarchy nodes
else if (node->IsA("vtkMRMLHierarchyNode"))
{
// check for tagged scene view hierarchy nodes
if (node->GetAttribute("SceneViewHierarchy") != nullptr)
{
includeInView = false;
}
// is it a top level singleton node?
else if (node->GetID() && !strncmp(node->GetID(), "vtkMRMLHierarchyNodeSceneViewTopLevel", 37))
{
includeInView = false;
}
// backward compatibility: is it an old top level node?
else if (node->GetName() && !strncmp(node->GetName(), "SceneViewToplevel", 17))
{
includeInView = false;
}
else
{
vtkSmartPointer<vtkMRMLHierarchyNode> hnode = vtkMRMLHierarchyNode::SafeDownCast(node);
if (hnode)
{
// backward compatibility: non tagged hierarchy nodes
// check for hierarchy nodes that point to scene view nodes
if (hnode->GetAssociatedNode() && hnode->GetAssociatedNode()->IsA("vtkMRMLSceneViewNode"))
{
includeInView = false;
}
// getting the node might fail if it's an erroneously included hierarchy
// node in a scene view node being restored
else if (hnode->GetAssociatedNodeID() && !strncmp(hnode->GetAssociatedNodeID(), "vtkMRMLSceneViewNode", 20))
{
includeInView = false;
}
}
}
}
vtkDebugMacro("IncludeNodeInSceneView: node " << node->GetID() << " includeInView = " << includeInView);
return includeInView;
}
void vtkMRMLSceneViewNode::SetSceneViewRootDir( const char* name)
{
if (!this->SnapshotScene)
{
return;
}
this->SnapshotScene->SetRootDirectory(name);
}