forked from Slicer/SlicerGitSVNArchive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vtkMRMLHierarchyNode.cxx
853 lines (752 loc) · 25.8 KB
/
vtkMRMLHierarchyNode.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
/*=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: vtkMRMLTransformNode.cxx,v $
Date: $Date: 2006/03/17 17:01:53 $
Version: $Revision: 1.14 $
=========================================================================auto=*/
// MRML includes
#include "vtkMRMLHierarchyNode.h"
#include "vtkMRMLScene.h"
#include "vtkMRMLHierarchyStorageNode.h"
// VTK includes
#include <vtkCollection.h>
#include <vtkObjectFactory.h>
// STD includes
#include <algorithm>
#include <sstream>
vtkCxxSetReferenceStringMacro(vtkMRMLHierarchyNode, ParentNodeIDReference);
vtkCxxSetReferenceStringMacro(vtkMRMLHierarchyNode, AssociatedNodeIDReference);
typedef std::map<std::string, std::vector< vtkMRMLHierarchyNode *> > HierarchyChildrenNodesType;
std::map< vtkMRMLScene*, HierarchyChildrenNodesType> vtkMRMLHierarchyNode::SceneHierarchyChildrenNodes = std::map< vtkMRMLScene*, HierarchyChildrenNodesType>();
std::map< vtkMRMLScene*, vtkMTimeType> vtkMRMLHierarchyNode::SceneHierarchyChildrenNodesMTime = std::map< vtkMRMLScene*, vtkMTimeType>();
double vtkMRMLHierarchyNode::MaximumSortingValue = 0;
typedef std::map<std::string, vtkMRMLHierarchyNode *> AssociatedHierarchyNodesType;
std::map< vtkMRMLScene*, AssociatedHierarchyNodesType> vtkMRMLHierarchyNode::SceneAssociatedHierarchyNodes = std::map< vtkMRMLScene*, AssociatedHierarchyNodesType>();
std::map< vtkMRMLScene*, vtkMTimeType> vtkMRMLHierarchyNode::SceneAssociatedHierarchyNodesMTime = std::map< vtkMRMLScene*, vtkMTimeType>();;
typedef vtkMRMLHierarchyNode* const vtkMRMLHierarchyNodePointer;
bool vtkMRMLHierarchyNodeSortPredicate(vtkMRMLHierarchyNodePointer d1, vtkMRMLHierarchyNodePointer d2);
bool vtkMRMLHierarchyNodeSortPredicate(vtkMRMLHierarchyNodePointer d1, vtkMRMLHierarchyNodePointer d2)
{
return d1->GetSortingValue() < d2->GetSortingValue();
}
//----------------------------------------------------------------------------
vtkMRMLNodeNewMacro(vtkMRMLHierarchyNode);
//----------------------------------------------------------------------------
vtkMRMLHierarchyNode::vtkMRMLHierarchyNode()
{
this->HideFromEditors = 0;
this->ParentNodeIDReference = nullptr;
this->AssociatedNodeIDReference = nullptr;
this->SortingValue = 0;
this->AllowMultipleChildren = 1;
}
//----------------------------------------------------------------------------
vtkMRMLHierarchyNode::~vtkMRMLHierarchyNode()
{
if (this->ParentNodeIDReference)
{
delete [] this->ParentNodeIDReference;
this->ParentNodeIDReference = nullptr;
}
if (this->AssociatedNodeIDReference)
{
delete [] this->AssociatedNodeIDReference;
this->AssociatedNodeIDReference = nullptr;
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::WriteXML(ostream& of, int nIndent)
{
Superclass::WriteXML(of, nIndent);
if (this->ParentNodeIDReference != nullptr)
{
of << " parentNodeRef=\"" << this->ParentNodeIDReference << "\"";
}
if (this->AssociatedNodeIDReference != nullptr)
{
of << " associatedNodeRef=\"" << this->AssociatedNodeIDReference << "\"";
}
of << " sortingValue=\"" << this->SortingValue << "\"";
of << " allowMultipleChildren=\"" << (this->AllowMultipleChildren ? "true" : "false") << "\"";
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::UpdateReferenceID(const char *oldID, const char *newID)
{
Superclass::UpdateReferenceID(oldID, newID);
if (this->ParentNodeIDReference && !strcmp(oldID, this->ParentNodeIDReference))
{
this->SetParentNodeID(newID);
}
else if (this->AssociatedNodeIDReference && !strcmp(oldID, this->AssociatedNodeIDReference))
{
this->SetAssociatedNodeID(newID);
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::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, "parentNodeRef"))
{
// don't reset SortingValue
double sortingValue = this->GetSortingValue();
this->SetParentNodeID(attValue);
this->SetSortingValue(sortingValue);
//this->Scene->AddReferencedNodeID(this->ParentNodeIDReference, this);
}
if (!strcmp(attName, "associatedNodeRef"))
{
this->SetAssociatedNodeID(attValue);
}
else if (!strcmp(attName, "sortingValue"))
{
std::stringstream ss;
ss << attValue;
ss >> SortingValue;
}
else if (!strcmp(attName, "allowMultipleChildren"))
{
if (!strcmp(attValue,"true"))
{
this->AllowMultipleChildren = 1;
}
else
{
this->AllowMultipleChildren = 0;
}
}
}
this->EndModify(disabledModify);
}
//----------------------------------------------------------------------------
// Copy the node's attributes to this object.
// Does NOT copy: ID, FilePrefix, Name, VolumeID
void vtkMRMLHierarchyNode::Copy(vtkMRMLNode *anode)
{
int disabledModify = this->StartModify();
Superclass::Copy(anode);
vtkMRMLHierarchyNode *node = (vtkMRMLHierarchyNode *) anode;
//this->SetParentNodeID(node->ParentNodeIDReference);
this->SetParentNodeIDReference(node->ParentNodeIDReference);
this->SetAssociatedNodeIDReference(node->AssociatedNodeIDReference);
this->SortingValue = node->SortingValue;
this->SetAllowMultipleChildren(node->AllowMultipleChildren);
this->EndModify(disabledModify);
this->InvokeHierarchyModifiedEvent();
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::PrintSelf(ostream& os, vtkIndent indent)
{
Superclass::PrintSelf(os,indent);
os << indent << "AssociatedNodeID: " <<
(this->AssociatedNodeIDReference ? this->AssociatedNodeIDReference : "(none)") << "\n";
os << indent << "ParentNodeID: " <<
(this->ParentNodeIDReference ? this->ParentNodeIDReference : "(none)") << "\n";
os << indent << "SortingValue: " << this->SortingValue << "\n";
os << indent << "AllowMultipleChildren: " << (this->AllowMultipleChildren ? "true" : "false") << "\n";
}
//----------------------------------------------------------------------------
vtkMRMLHierarchyNode* vtkMRMLHierarchyNode::GetParentNode()
{
vtkMRMLHierarchyNode* node = nullptr;
if (this->GetScene() && this->ParentNodeIDReference != nullptr )
{
vtkMRMLNode* snode = this->GetScene()->GetNodeByID(this->ParentNodeIDReference);
node = vtkMRMLHierarchyNode::SafeDownCast(snode);
}
return node;
}
//-----------------------------------------------------------
void vtkMRMLHierarchyNode::SetSceneReferences()
{
this->Superclass::SetSceneReferences();
this->Scene->AddReferencedNodeID(this->ParentNodeIDReference, this);
this->Scene->AddReferencedNodeID(this->AssociatedNodeIDReference, this);
}
//-----------------------------------------------------------
void vtkMRMLHierarchyNode::UpdateScene(vtkMRMLScene *scene)
{
Superclass::UpdateScene(scene);
}
//-----------------------------------------------------------
void vtkMRMLHierarchyNode::UpdateReferences()
{
Superclass::UpdateReferences();
if (this->ParentNodeIDReference != nullptr && this->Scene->GetNodeByID(this->ParentNodeIDReference) == nullptr)
{
this->SetParentNodeID(nullptr);
}
if (this->AssociatedNodeIDReference != nullptr && this->Scene->GetNodeByID(this->AssociatedNodeIDReference) == nullptr)
{
this->SetAssociatedNodeID(nullptr);
}
}
//-----------------------------------------------------------
void vtkMRMLHierarchyNode::SetParentNodeID(const char* ref)
{
// is it trying to set it's parent to be itself?
if (this->GetID() && ref &&
!strcmp(this->GetID(), ref))
{
vtkErrorMacro("SetParentNode: node "
<< (this->GetName() ? this->GetName() : "(unnamed)")
<< " with id " << this->GetID()
<< " tried to set itself as parent! Returning...");
return;
}
// is the parent node id already set to ref?
if (this->ParentNodeIDReference && ref &&
!strcmp(ref, this->ParentNodeIDReference))
{
return;
}
// or are both already null?
if (!this->ParentNodeIDReference && !ref)
{
return;
}
vtkMRMLHierarchyNode *oldParentNode = this->GetParentNode();
int disableModify = this->StartModify();
this->SetParentNodeIDReference(ref);
this->SetSortingValue(++MaximumSortingValue);
this->HierarchyIsModified(this->GetScene());
if (this->GetScene())
{
this->GetScene()->AddReferencedNodeID(ref, this);
}
this->EndModify(disableModify);
vtkMRMLHierarchyNode *parentNode = this->GetParentNode();
if (oldParentNode)
{
oldParentNode->InvokeEvent(vtkMRMLHierarchyNode::ChildNodeRemovedEvent, this);
oldParentNode->Modified();
}
if (parentNode)
{
parentNode->InvokeEvent(vtkMRMLHierarchyNode::ChildNodeAddedEvent, this);
parentNode->Modified();
}
if (this->GetScene())
{
this->GetScene()->Modified();
}
this->InvokeHierarchyModifiedEvent();
}
//----------------------------------------------------------------------------
vtkMRMLHierarchyNode* vtkMRMLHierarchyNode::GetTopParentNode()
{
vtkMRMLHierarchyNode *node = nullptr;
vtkMRMLHierarchyNode *parent = vtkMRMLHierarchyNode::SafeDownCast(this->GetParentNode());
if (parent == nullptr)
{
node = this;
}
else
{
node = parent->GetTopParentNode();
}
return node;
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::GetAllChildrenNodes(std::vector< vtkMRMLHierarchyNode *> &childrenNodes)
{
if (this->GetScene() == nullptr)
{
return;
}
this->UpdateChildrenMap();
std::map< vtkMRMLScene*, HierarchyChildrenNodesType>::iterator siter =
SceneHierarchyChildrenNodes.find(this->GetScene());
HierarchyChildrenNodesType::iterator iter =
siter->second.find(std::string(this->GetID()));
if (iter == siter->second.end())
{
return;
}
for (unsigned int i=0; i<iter->second.size(); i++)
{
childrenNodes.push_back(iter->second[i]);
iter->second[i]->GetAllChildrenNodes(childrenNodes);
}
}
//----------------------------------------------------------------------------
std::vector< vtkMRMLHierarchyNode *> vtkMRMLHierarchyNode::GetChildrenNodes()
{
std::vector< vtkMRMLHierarchyNode *> childrenNodes;
if (this->GetScene() == nullptr)
{
return childrenNodes;
}
this->UpdateChildrenMap();
std::map< vtkMRMLScene*, HierarchyChildrenNodesType>::iterator siter =
SceneHierarchyChildrenNodes.find(this->GetScene());
HierarchyChildrenNodesType::iterator iter =
siter->second.find(std::string(this->GetID()));
if (iter == siter->second.end())
{
return childrenNodes;
}
for (unsigned int i=0; i<iter->second.size(); i++)
{
childrenNodes.push_back(iter->second[i]);
}
// Sort the vector using predicate and std::sort
std::sort(childrenNodes.begin(), childrenNodes.end(), vtkMRMLHierarchyNodeSortPredicate);
return childrenNodes;
}
//----------------------------------------------------------------------------
vtkMRMLHierarchyNode* vtkMRMLHierarchyNode::GetNthChildNode(int index)
{
std::vector< vtkMRMLHierarchyNode *> childrenNodes = this->GetChildrenNodes();
if (index < 0 || index > (int)(childrenNodes.size()-1))
{
vtkErrorMacro("vtkMRMLHierarchyNode::GetNthChildNode() index " << index << " outside the range 0-" << childrenNodes.size()-1 );
return nullptr;
}
else
{
return childrenNodes[index];
}
}
//----------------------------------------------------------------------------
int vtkMRMLHierarchyNode::GetIndexInParent()
{
vtkMRMLHierarchyNode *pnode = this->GetParentNode();
if (pnode == nullptr)
{
vtkErrorMacro("vtkMRMLHierarchyNode::GetIndexInParent() no parent");
return -1;
}
else
{
std::vector< vtkMRMLHierarchyNode *> childrenNodes = pnode->GetChildrenNodes();
for (unsigned int i=0; i<childrenNodes.size(); i++)
{
if (childrenNodes[i] == this)
{
return i;
}
}
return -1;
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::SetIndexInParent(int index)
{
vtkMRMLHierarchyNode *pnode = this->GetParentNode();
if (pnode == nullptr)
{
vtkErrorMacro("vtkMRMLHierarchyNode::SetIndexInParent() no parent");
return;
}
else
{
std::vector< vtkMRMLHierarchyNode *> childrenNodes = pnode->GetChildrenNodes();
if (index < 0 || index >= (int)childrenNodes.size())
{
vtkErrorMacro("vtkMRMLHierarchyNode::SetIndexInParent() index " << index << ", outside the range 0-" << childrenNodes.size()-1);
return;
}
double sortValue = childrenNodes[index]->GetSortingValue();
int oldIndex = this->GetIndexInParent();
if (index == 0)
{
sortValue -= 1;
}
else if (index == (int)childrenNodes.size()-1)
{
sortValue += 1;
}
else if (index > oldIndex)
{
sortValue = 0.5*(sortValue + childrenNodes[index+1]->GetSortingValue());
}
else if (index < oldIndex)
{
sortValue = 0.5*(sortValue + childrenNodes[index-1]->GetSortingValue());
}
this->SetSortingValue(sortValue);
}
return;
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::MoveInParent(int increment)
{
if (increment == 0)
{
return;
}
vtkMRMLHierarchyNode *pnode = this->GetParentNode();
if (pnode == nullptr)
{
vtkErrorMacro("vtkMRMLHierarchyNode::MoveInParent() no parent");
return;
}
else
{
std::vector< vtkMRMLHierarchyNode *> childrenNodes = pnode->GetChildrenNodes();
int oldIndex = this->GetIndexInParent();
if (oldIndex + increment < 0 || oldIndex + increment >= (int)childrenNodes.size())
{
vtkErrorMacro("vtkMRMLHierarchyNode::MoveInParent() index " << oldIndex << ", outside the range 0-" << childrenNodes.size()-1);
return;
}
int incr1 = increment > 0 ? 1:-1;
int index1 = oldIndex;
int index2 = oldIndex + incr1;
for (int i=0; i<incr1*increment; i++)
{
// swap pair of sort values
childrenNodes = pnode->GetChildrenNodes();
double sortValue1 = childrenNodes[index1]->GetSortingValue();
double sortValue2 = childrenNodes[index2]->GetSortingValue();
childrenNodes[index1]->SortingValue = sortValue2;
childrenNodes[index2]->SortingValue = sortValue1;
// update the cache
pnode->UpdateChildrenMap();
index1 += incr1;
index2 += incr1;
}
// notify observers
this->Modified();
this->InvokeHierarchyModifiedEvent();
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::RemoveHierarchyChildrenNodes()
{
if (this->GetScene() == nullptr)
{
return;
}
char *parentID = this->GetParentNodeID();
vtkMRMLHierarchyNode *parentNode = this->GetParentNode();
std::vector< vtkMRMLHierarchyNode *> children = this->GetChildrenNodes();
for (unsigned int i=0; i<children.size(); i++)
{
vtkMRMLHierarchyNode *child = children[i];
std::vector< vtkMRMLHierarchyNode *> childChildren = child->GetChildrenNodes();
for (unsigned int j=0; j<childChildren.size(); j++)
{
childChildren[j]->SetParentNodeID(parentID);
}
if (parentNode)
{
parentNode->InvokeEvent(vtkMRMLHierarchyNode::ChildNodeRemovedEvent, child);
}
this->GetScene()->RemoveNode(child);
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::RemoveAllHierarchyChildrenNodes()
{
if (this->GetScene() == nullptr)
{
return;
}
std::vector< vtkMRMLHierarchyNode *> children = this->GetChildrenNodes();
for (unsigned int i=0; i<children.size(); i++)
{
vtkMRMLHierarchyNode *child = children[i];
std::vector< vtkMRMLHierarchyNode *> childChildren = child->GetChildrenNodes();
for (unsigned int j=0; j<childChildren.size(); j++)
{
childChildren[j]->RemoveAllHierarchyChildrenNodes();
}
this->InvokeEvent(vtkMRMLHierarchyNode::ChildNodeRemovedEvent, child);
this->GetScene()->RemoveNode(child);
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::UpdateChildrenMap()
{
if (this->GetScene() == nullptr)
{
this->SceneHierarchyChildrenNodes.clear();
this->SceneHierarchyChildrenNodesMTime.clear();
return;
}
std::map< vtkMRMLScene*, HierarchyChildrenNodesType>::iterator siter =
SceneHierarchyChildrenNodes.find(this->GetScene());
if (siter == SceneHierarchyChildrenNodes.end())
{
HierarchyChildrenNodesType h;
SceneHierarchyChildrenNodes[this->GetScene()] = h;
siter = SceneHierarchyChildrenNodes.find(this->GetScene());
SceneHierarchyChildrenNodesMTime[this->GetScene()] = 0;
}
std::map< vtkMRMLScene*, vtkMTimeType>::iterator titer =
SceneHierarchyChildrenNodesMTime.find(this->GetScene());
std::map<std::string, std::vector< vtkMRMLHierarchyNode *> >::iterator iter;
if (this->GetScene() == nullptr)
{
for (iter = siter->second.begin();
iter != siter->second.end();
iter++)
{
iter->second.clear();
}
siter->second.clear();
}
if (this->GetScene()->GetNodes()->GetMTime() > titer->second)
{
for (iter = siter->second.begin();
iter != siter->second.end();
iter++)
{
iter->second.clear();
}
siter->second.clear();
std::vector<vtkMRMLNode *> nodes;
int nnodes = this->GetScene()->GetNodesByClass("vtkMRMLHierarchyNode", nodes);
double maxSortingValue = this->MaximumSortingValue;
for (int i=0; i<nnodes; i++)
{
vtkMRMLHierarchyNode *node = vtkMRMLHierarchyNode::SafeDownCast(nodes[i]);
if (node)
{
vtkMRMLHierarchyNode *pnode = vtkMRMLHierarchyNode::SafeDownCast(node->GetParentNode());
if (pnode)
{
if (pnode->GetSortingValue() > maxSortingValue)
{
maxSortingValue = pnode->GetSortingValue();
}
iter = siter->second.find(std::string(pnode->GetID()));
if (iter == siter->second.end())
{
std::vector< vtkMRMLHierarchyNode *> children;
children.push_back(node);
siter->second[std::string(pnode->GetID())] = children;
}
else
{
iter->second.push_back(node);
}
}
}
}
titer->second = this->GetScene()->GetNodes()->GetMTime();
this->MaximumSortingValue = maxSortingValue;
}
}
void vtkMRMLHierarchyNode::HierarchyIsModified(vtkMRMLScene *scene)
{
if (scene == nullptr)
{
return;
}
SceneHierarchyChildrenNodesMTime[scene] = 0;
}
void vtkMRMLHierarchyNode::GetAssociatedChildrenNodes(vtkCollection *children,
const char* childClass)
{
if (children == nullptr)
{
return;
}
vtkMRMLScene *scene = this->GetScene();
if (scene == nullptr)
{
//vtkErrorMacro("GetChildrenAssociatedNodes: scene is null, cannot find children of this node");
return;
}
vtkMRMLNode *mnode = nullptr;
vtkMRMLHierarchyNode *hnode = nullptr;
std::string nodeClass("vtkMRMLNode");
if (childClass)
{
nodeClass = childClass;
}
int numNodes = scene->GetNumberOfNodesByClass(nodeClass.c_str());
for (int n=0; n < numNodes; n++)
{
mnode = scene->GetNthNodeByClass(n, nodeClass.c_str());
// check for a hierarchy node for this displayble node
hnode = this->GetAssociatedHierarchyNode(this->GetScene(), mnode->GetID());
while (hnode)
{
// hnode == this
if (hnode->GetID() && this->GetID() &&
strcmp(hnode->GetID(), this->GetID()) == 0)
{
children->AddItem(mnode);
break;
}
// the hierarchy node for this node may not be the one we're checking
// against, go up the tree
hnode = vtkMRMLHierarchyNode::SafeDownCast(hnode->GetParentNode());
}// end while
}// end for
}
//---------------------------------------------------------------------------
vtkMRMLHierarchyNode* vtkMRMLHierarchyNode::GetAssociatedHierarchyNode(vtkMRMLScene *scene,
const char *associatedNodeID)
{
if (associatedNodeID == nullptr)
{
std::cerr << "GetAssociatedHierarchyNode: associated node id is null" << std::endl;
return nullptr;
}
if (scene == nullptr)
{
std::cerr << "GetAssociatedHierarchyNode: scene is null" << std::endl;
return nullptr;
}
vtkMRMLHierarchyNode::UpdateAssociatedToHierarchyMap(scene);
std::map< vtkMRMLScene*, AssociatedHierarchyNodesType>::iterator siter =
SceneAssociatedHierarchyNodes.find(scene);
if (siter == SceneAssociatedHierarchyNodes.end())
{
std::cerr << "GetAssociatedHierarchyNode: didn't find an associated hierarchy node type associated with the scene" << std::endl;
return nullptr;
}
std::map<std::string, vtkMRMLHierarchyNode *>::iterator iter;
iter = siter->second.find(associatedNodeID);
if (iter != siter->second.end())
{
return iter->second;
}
else
{
return nullptr;
}
}
//----------------------------------------------------------------------------
int vtkMRMLHierarchyNode::UpdateAssociatedToHierarchyMap(vtkMRMLScene *scene)
{
if (scene == nullptr)
{
SceneAssociatedHierarchyNodes.clear();
SceneAssociatedHierarchyNodesMTime.clear();
return 0;
}
std::map< vtkMRMLScene*, AssociatedHierarchyNodesType>::iterator siter =
SceneAssociatedHierarchyNodes.find(scene);
if (siter == SceneAssociatedHierarchyNodes.end())
{
AssociatedHierarchyNodesType h;
SceneAssociatedHierarchyNodes[scene] = h;
siter = SceneAssociatedHierarchyNodes.find(scene);
SceneAssociatedHierarchyNodesMTime[scene] = 0;
}
std::map< vtkMRMLScene*, vtkMTimeType>::iterator titer =
SceneAssociatedHierarchyNodesMTime.find(scene);
if (scene->GetNodes()->GetMTime() > titer->second)
{
siter->second.clear();
std::vector<vtkMRMLNode *> nodes;
int nnodes = scene->GetNodesByClass("vtkMRMLHierarchyNode", nodes);
for (int i=0; i<nnodes; i++)
{
vtkMRMLHierarchyNode *node = vtkMRMLHierarchyNode::SafeDownCast(nodes[i]);
if (node)
{
vtkMRMLNode *mnode = node->vtkMRMLHierarchyNode::GetAssociatedNode();
if (mnode)
{
siter->second[std::string(mnode->GetID())] = node;
}
}
}
titer->second = scene->GetNodes()->GetMTime();
}
return static_cast<int>(siter->second.size());
}
//----------------------------------------------------------------------------
vtkMRMLNode* vtkMRMLHierarchyNode::GetAssociatedNode()
{
vtkMRMLNode* node = nullptr;
if (this->GetScene() && this->GetAssociatedNodeID() )
{
node = this->GetScene()->GetNodeByID(this->AssociatedNodeIDReference);
}
return node;
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::SetAssociatedNodeID(const char* ref)
{
if ((this->AssociatedNodeIDReference && ref && strcmp(ref, this->AssociatedNodeIDReference)) ||
(this->AssociatedNodeIDReference != ref))
{
this->SetAssociatedNodeIDReference(ref);
this->AssociatedHierarchyIsModified(this->GetScene());
if (this->Scene)
{
this->Scene->AddReferencedNodeID(ref, this);
}
vtkMRMLNode* node = this->GetAssociatedNode();
if (node)
{
// node->Modified();
this->InvokeHierarchyModifiedEvent(node);
}
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::AssociatedHierarchyIsModified(vtkMRMLScene *scene)
{
if (scene == nullptr)
{
return;
}
SceneAssociatedHierarchyNodesMTime[scene] = 0;
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::SetSortingValue(double value)
{
// reimplemented from vtkSet macro
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting SortingValue to " << value);
if (this->SortingValue != value)
{
this->SortingValue = value;
this->Modified();
this->InvokeHierarchyModifiedEvent();
}
}
//----------------------------------------------------------------------------
void vtkMRMLHierarchyNode::InvokeHierarchyModifiedEvent(vtkMRMLNode *node)
{
if (node)
{
// invoke it on the passed node
vtkDebugMacro("InvokeHierarchyModifiedEvent: passed node: " << node->GetID());
node->InvokeEvent(vtkMRMLNode::HierarchyModifiedEvent);
return;
}
// if there is an associated node, invoke a hierarchy modified on it to trigger updates in
// q widgets
vtkMRMLNode *associatedNode = this->GetAssociatedNode();
if (associatedNode)
{
vtkDebugMacro("InvokeHierarchyModifiedEvent: associated node " << associatedNode->GetID());
associatedNode->InvokeEvent(vtkMRMLNode::HierarchyModifiedEvent);
}
/*
else
{
// there is no node associated with this hierarchy, are there children?
if (this->GetNumberOfChildrenNodes())
{
// invoke it on the top level children
std::vector< vtkMRMLHierarchyNode* > children = this->GetChildrenNodes();
for (unsigned int i = 0; i < children.size(); ++i)
{
vtkDebugMacro("InvokeHierarchyModifiedEvent: child " << i << ", id = " << children[i]->GetID());
children[i]->InvokeEvent(vtkMRMLNode::HierarchyModifiedEvent);
}
}
}
*/
}
// End