-
Notifications
You must be signed in to change notification settings - Fork 543
/
vtkMRMLMarkupsDisplayableManager.cxx
986 lines (883 loc) · 32.7 KB
/
vtkMRMLMarkupsDisplayableManager.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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
#include "vtkMRMLMarkupsDisplayableManager.h"
#include "vtkMRMLAbstractSliceViewDisplayableManager.h"
// MarkupsModule/MRML includes
#include <vtkMRMLMarkupsAngleNode.h>
#include <vtkMRMLMarkupsClosedCurveNode.h>
#include <vtkMRMLMarkupsCurveNode.h>
#include <vtkMRMLMarkupsDisplayNode.h>
#include <vtkMRMLMarkupsFiducialNode.h>
#include <vtkMRMLMarkupsLineNode.h>
#include <vtkMRMLMarkupsNode.h>
#include <vtkMRMLMarkupsPlaneNode.h>
// MarkupsModule/VTKWidgets includes
#include <vtkSlicerLineWidget.h>
#include <vtkSlicerLineRepresentation2D.h>
#include <vtkSlicerLineRepresentation3D.h>
#include <vtkSlicerClosedCurveWidget.h>
#include <vtkSlicerCurveWidget.h>
#include <vtkSlicerCurveRepresentation2D.h>
#include <vtkSlicerCurveRepresentation3D.h>
#include <vtkSlicerAngleWidget.h>
#include <vtkSlicerAngleRepresentation2D.h>
#include <vtkSlicerAngleRepresentation3D.h>
#include <vtkSlicerPlaneWidget.h>
#include <vtkSlicerPointsWidget.h>
#include <vtkSlicerPointsRepresentation2D.h>
#include <vtkSlicerPointsRepresentation3D.h>
// MRMLDisplayableManager includes
#include <vtkMRMLDisplayableManagerGroup.h>
#include <vtkMRMLInteractionEventData.h>
#include <vtkMRMLModelDisplayableManager.h>
// MRML includes
#include <vtkEventBroker.h>
#include <vtkMRMLApplicationLogic.h>
#include <vtkMRMLFolderDisplayNode.h>
#include <vtkMRMLInteractionNode.h>
#include <vtkMRMLScene.h>
#include <vtkMRMLSelectionNode.h>
#include <vtkMRMLSliceLogic.h>
#include <vtkMRMLSliceNode.h>
#include <vtkMRMLTransformNode.h>
#include <vtkMRMLViewNode.h>
// VTK includes
#include <vtkAbstractWidget.h>
#include <vtkCallbackCommand.h>
#include <vtkCamera.h>
#include <vtkEvent.h>
#include <vtkGeneralTransform.h>
#include <vtkMarkupsGlyphSource2D.h>
#include <vtkMath.h>
#include <vtkMatrix4x4.h>
#include <vtkNew.h>
#include <vtkObjectFactory.h>
#include <vtkPropCollection.h>
#include <vtkProperty2D.h>
#include <vtkRendererCollection.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSlicerMarkupsWidgetRepresentation2D.h>
#include <vtkSlicerMarkupsWidget.h>
#include <vtkSphereSource.h>
#include <vtkTextProperty.h>
#include <vtkWidgetRepresentation.h>
// STD includes
#include <algorithm>
#include <map>
#include <vector>
#include <sstream>
#include <string>
typedef void (*fp)();
#define NUMERIC_ZERO 0.001
//---------------------------------------------------------------------------
vtkStandardNewMacro (vtkMRMLMarkupsDisplayableManager);
//---------------------------------------------------------------------------
// vtkMRMLMarkupsDisplayableManager methods
//---------------------------------------------------------------------------
vtkMRMLMarkupsDisplayableManager::vtkMRMLMarkupsDisplayableManager()
{
this->Focus.insert("vtkMRMLMarkupsAngleNode");
this->Focus.insert("vtkMRMLMarkupsFiducialNode");
this->Focus.insert("vtkMRMLMarkupsLineNode");
this->Focus.insert("vtkMRMLMarkupsCurveNode");
this->Focus.insert("vtkMRMLMarkupsClosedCurveNode");
this->Focus.insert("vtkMRMLMarkupsPlaneNode");
this->Helper = vtkMRMLMarkupsDisplayableManagerHelper::New();
this->Helper->SetDisplayableManager(this);
this->DisableInteractorStyleEventsProcessing = 0;
this->Focus.insert("vtkMRMLMarkupsNode");
// by default, this displayableManager handles a 2d view, so the SliceNode
// must be set when it's assigned to a viewer
this->SliceNode = nullptr;
this->LastClickWorldCoordinates[0]=0.0;
this->LastClickWorldCoordinates[1]=0.0;
this->LastClickWorldCoordinates[2]=0.0;
this->LastClickWorldCoordinates[3]=1.0;
}
//---------------------------------------------------------------------------
vtkMRMLMarkupsDisplayableManager::~vtkMRMLMarkupsDisplayableManager()
{
this->DisableInteractorStyleEventsProcessing = 0;
this->Helper->Delete();
this->SliceNode = nullptr;
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "DisableInteractorStyleEventsProcessing = " << this->DisableInteractorStyleEventsProcessing << std::endl;
if (this->SliceNode &&
this->SliceNode->GetID())
{
os << indent << "Slice node id = " << this->SliceNode->GetID() << std::endl;
}
else
{
os << indent << "No slice node" << std::endl;
}
}
//---------------------------------------------------------------------------
vtkMRMLSliceNode * vtkMRMLMarkupsDisplayableManager::GetMRMLSliceNode()
{
return vtkMRMLSliceNode::SafeDownCast(this->GetMRMLDisplayableNode());
}
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::Is2DDisplayableManager()
{
return this->GetMRMLSliceNode() != nullptr;
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::RequestRender()
{
if (!this->GetMRMLScene())
{
return;
}
if (!this->GetMRMLScene()->IsBatchProcessing())
{
this->Superclass::RequestRender();
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::UpdateFromMRML()
{
// this gets called from RequestRender, so make sure to jump out quickly if possible
if (this->GetMRMLScene() == nullptr || this->Focus.empty())
{
return;
}
// turn off update from mrml requested, as we're doing it now, and create
// widget requests a render which checks this flag before calling update
// from mrml again
this->SetUpdateFromMRMLRequested(false);
std::vector<vtkMRMLNode*> markupNodes;
this->GetMRMLScene()->GetNodesByClass("vtkMRMLMarkupsNode", markupNodes);
for (std::vector< vtkMRMLNode* >::iterator nodeIt = markupNodes.begin(); nodeIt != markupNodes.end(); ++nodeIt)
{
vtkMRMLMarkupsNode *markupsNode = vtkMRMLMarkupsNode::SafeDownCast(*nodeIt);
if (!markupsNode)
{
continue;
}
if (this->GetHelper()->MarkupsNodes.find(markupsNode) != this->GetHelper()->MarkupsNodes.end())
{
// node added already
continue;
}
this->GetHelper()->AddMarkupsNode(markupsNode);
}
std::vector<vtkMRMLNode*> markupsDisplayNodes;
this->GetMRMLScene()->GetNodesByClass("vtkMRMLMarkupsDisplayNode", markupsDisplayNodes);
for (std::vector< vtkMRMLNode* >::iterator nodeIt = markupsDisplayNodes.begin(); nodeIt != markupsDisplayNodes.end(); ++nodeIt)
{
vtkMRMLMarkupsDisplayNode *markupsDisplayNode = vtkMRMLMarkupsDisplayNode::SafeDownCast(*nodeIt);
if (!markupsDisplayNode)
{
continue;
}
if (this->GetHelper()->MarkupsDisplayNodesToWidgets.find(markupsDisplayNode) != this->GetHelper()->MarkupsDisplayNodesToWidgets.end())
{
// node added already
continue;
}
this->GetHelper()->AddDisplayNode(markupsDisplayNode);
}
// Remove observed markups nodes that have been deleted from the scene
for (vtkMRMLMarkupsDisplayableManagerHelper::MarkupsNodesIt markupsIterator = this->Helper->MarkupsNodes.begin();
markupsIterator != this->Helper->MarkupsNodes.end();
/*upon deletion the increment is done already, so don't increment here*/)
{
vtkMRMLMarkupsNode *markupsNode = *markupsIterator;
if (this->GetMRMLScene()->IsNodePresent(markupsNode))
{
++markupsIterator;
}
else
{
// display node is not in the scene anymore, delete the widget
vtkMRMLMarkupsDisplayableManagerHelper::MarkupsNodesIt markupsIteratorToRemove = markupsIterator;
++markupsIterator;
this->Helper->RemoveMarkupsNode(*markupsIteratorToRemove);
this->Helper->MarkupsNodes.erase(markupsIteratorToRemove);
}
}
// Remove widgets corresponding deleted display nodes
for (vtkMRMLMarkupsDisplayableManagerHelper::DisplayNodeToWidgetIt widgetIterator = this->Helper->MarkupsDisplayNodesToWidgets.begin();
widgetIterator != this->Helper->MarkupsDisplayNodesToWidgets.end();
/*upon deletion the increment is done already, so don't increment here*/)
{
vtkMRMLMarkupsDisplayNode *markupsDisplayNode = widgetIterator->first;
if (this->GetMRMLScene()->IsNodePresent(markupsDisplayNode))
{
++widgetIterator;
}
else
{
// display node is not in the scene anymore, delete the widget
vtkMRMLMarkupsDisplayableManagerHelper::DisplayNodeToWidgetIt widgetIteratorToRemove = widgetIterator;
++widgetIterator;
vtkSlicerMarkupsWidget* widgetToRemove = widgetIteratorToRemove->second;
this->Helper->DeleteWidget(widgetToRemove);
this->Helper->MarkupsDisplayNodesToWidgets.erase(widgetIteratorToRemove);
}
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::SetMRMLSceneInternal(vtkMRMLScene* newScene)
{
Superclass::SetMRMLSceneInternal(newScene);
// after a new scene got associated, we want to make sure everything old is gone
this->OnMRMLSceneEndClose();
if (newScene)
{
this->AddObserversToInteractionNode();
}
else
{
// there's no scene to get the interaction node from, so this won't do anything
this->RemoveObserversFromInteractionNode();
}
vtkDebugMacro("SetMRMLSceneInternal: add observer on interaction node now?");
// clear out the map of glyph types
//this->Helper->ClearNodeGlyphTypes();
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager
::ProcessMRMLNodesEvents(vtkObject *caller, unsigned long event, void *callData)
{
vtkMRMLMarkupsNode * markupsNode = vtkMRMLMarkupsNode::SafeDownCast(caller);
vtkMRMLInteractionNode * interactionNode = vtkMRMLInteractionNode::SafeDownCast(caller);
if (markupsNode)
{
bool renderRequested = false;
for (int displayNodeIndex = 0; displayNodeIndex < markupsNode->GetNumberOfDisplayNodes(); displayNodeIndex++)
{
vtkMRMLMarkupsDisplayNode* displayNode = vtkMRMLMarkupsDisplayNode::SafeDownCast(markupsNode->GetNthDisplayNode(displayNodeIndex));
vtkSlicerMarkupsWidget *widget = this->Helper->GetWidget(displayNode);
if (!widget)
{
// if a new display node is added or display node view node IDs are changed then we may need to create a new widget
this->Helper->AddDisplayNode(displayNode);
widget = this->Helper->GetWidget(displayNode);
}
if (!widget)
{
continue;
}
widget->UpdateFromMRML(markupsNode, event, callData);
if (widget->GetNeedToRender())
{
renderRequested = true;
widget->NeedToRenderOff();
}
}
if (renderRequested)
{
this->RequestRender();
}
}
else if (interactionNode)
{
if (event == vtkMRMLInteractionNode::InteractionModeChangedEvent)
{
// loop through all widgets and update the widget status
for (vtkMRMLMarkupsDisplayableManagerHelper::DisplayNodeToWidgetIt widgetIterator = this->Helper->MarkupsDisplayNodesToWidgets.begin();
widgetIterator != this->Helper->MarkupsDisplayNodesToWidgets.end(); ++widgetIterator)
{
vtkSlicerMarkupsWidget* widget = widgetIterator->second;
if (!widget)
{
continue;
}
vtkMRMLInteractionEventData* eventData = reinterpret_cast<vtkMRMLInteractionEventData*>(callData);
widget->Leave(eventData);
}
}
}
else
{
this->Superclass::ProcessMRMLNodesEvents(caller, event, callData);
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::OnMRMLSceneEndClose()
{
vtkDebugMacro("OnMRMLSceneEndClose: remove observers?");
// run through all nodes and remove node and widget
this->Helper->RemoveAllWidgetsAndNodes();
this->SetUpdateFromMRMLRequested(true);
this->RequestRender();
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::OnMRMLSceneEndImport()
{
this->SetUpdateFromMRMLRequested(true);
this->UpdateFromMRMLScene();
//this->Helper->SetAllWidgetsToManipulate();
this->RequestRender();
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::UpdateFromMRMLScene()
{
if (this->GetMRMLDisplayableNode())
{
this->UpdateFromMRML();
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::OnMRMLSceneNodeAdded(vtkMRMLNode* node)
{
if (!node || !this->GetMRMLScene())
{
return;
}
// if the scene is still updating, jump out
if (this->GetMRMLScene()->IsBatchProcessing())
{
this->SetUpdateFromMRMLRequested(true);
return;
}
if (node->IsA("vtkMRMLInteractionNode"))
{
this->AddObserversToInteractionNode();
return;
}
if (node->IsA("vtkMRMLMarkupsNode"))
{
this->Helper->AddMarkupsNode(vtkMRMLMarkupsNode::SafeDownCast(node));
}
// and render again
this->RequestRender();
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::AddObserversToInteractionNode()
{
if (!this->GetMRMLScene())
{
return;
}
// also observe the interaction node for changes
vtkMRMLInteractionNode *interactionNode = this->GetInteractionNode();
if (interactionNode)
{
vtkDebugMacro("AddObserversToInteractionNode: interactionNode found");
vtkNew<vtkIntArray> interactionEvents;
interactionEvents->InsertNextValue(vtkMRMLInteractionNode::InteractionModeChangedEvent);
interactionEvents->InsertNextValue(vtkMRMLInteractionNode::InteractionModePersistenceChangedEvent);
interactionEvents->InsertNextValue(vtkMRMLInteractionNode::EndPlacementEvent);
vtkObserveMRMLNodeEventsMacro(interactionNode, interactionEvents.GetPointer());
}
else { vtkDebugMacro("AddObserversToInteractionNode: No interaction node!"); }
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::RemoveObserversFromInteractionNode()
{
if (!this->GetMRMLScene())
{
return;
}
// find the interaction node
vtkMRMLInteractionNode *interactionNode = this->GetInteractionNode();
if (interactionNode)
{
vtkUnObserveMRMLNodeMacro(interactionNode);
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::OnMRMLSceneNodeRemoved(vtkMRMLNode* node)
{
bool modified = false;
vtkMRMLMarkupsNode *markupsNode = vtkMRMLMarkupsNode::SafeDownCast(node);
if (markupsNode)
{
this->Helper->RemoveMarkupsNode(markupsNode);
modified = true;
}
vtkMRMLMarkupsDisplayNode *markupsDisplayNode = vtkMRMLMarkupsDisplayNode::SafeDownCast(node);
if (markupsDisplayNode)
{
this->Helper->RemoveDisplayNode(markupsDisplayNode);
modified = true;
}
if (modified)
{
this->RequestRender();
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::OnMRMLDisplayableNodeModifiedEvent(vtkObject* caller)
{
vtkDebugMacro("OnMRMLDisplayableNodeModifiedEvent");
if (!caller)
{
vtkErrorMacro("OnMRMLDisplayableNodeModifiedEvent: Could not get caller.");
return;
}
vtkMRMLSliceNode * sliceNode = vtkMRMLSliceNode::SafeDownCast(caller);
if (sliceNode)
{
// the associated renderWindow is a 2D SliceView
// this is the entry point for all events fired by one of the three sliceviews
// (e.g. change slice number, zoom etc.)
// we remember that this instance of the displayableManager deals with 2D
// this is important for widget creation etc. and save the actual SliceNode
// because during Slicer startup the SliceViews fire events, it will be always set correctly
this->SliceNode = sliceNode;
// now we call the handle for specific sliceNode actions
this->OnMRMLSliceNodeModifiedEvent();
// and exit
return;
}
vtkMRMLViewNode * viewNode = vtkMRMLViewNode::SafeDownCast(caller);
if (viewNode)
{
// the associated renderWindow is a 3D View
vtkDebugMacro("OnMRMLDisplayableNodeModifiedEvent: This displayableManager handles a ThreeD view.");
return;
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::OnMRMLSliceNodeModifiedEvent()
{
bool renderRequested = false;
// run through all markup nodes in the helper
vtkMRMLMarkupsDisplayableManagerHelper::DisplayNodeToWidgetIt it
= this->Helper->MarkupsDisplayNodesToWidgets.begin();
while(it != this->Helper->MarkupsDisplayNodesToWidgets.end())
{
// we loop through all widgets
vtkSlicerMarkupsWidget* widget = (it->second);
widget->UpdateFromMRML(this->SliceNode, vtkCommand::ModifiedEvent);
if (widget->GetNeedToRender())
{
renderRequested = true;
widget->NeedToRenderOff();
}
++it;
}
if (renderRequested)
{
this->RequestRender();
}
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::OnInteractorStyleEvent(int eventid)
{
Superclass::OnInteractorStyleEvent(eventid);
}
//---------------------------------------------------------------------------
vtkSlicerMarkupsWidget* vtkMRMLMarkupsDisplayableManager::GetWidget(vtkMRMLMarkupsDisplayNode * node)
{
return this->Helper->GetWidget(node);
}
//---------------------------------------------------------------------------
/// Check if it is the correct displayableManager
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::IsCorrectDisplayableManager()
{
vtkMRMLSelectionNode *selectionNode = this->GetMRMLApplicationLogic()->GetSelectionNode();
if (selectionNode == nullptr)
{
vtkErrorMacro ("IsCorrectDisplayableManager: No selection node in the scene.");
return false;
}
if (selectionNode->GetActivePlaceNodeClassName() == nullptr)
{
return false;
}
// the purpose of the displayableManager is hardcoded
return this->IsManageable(selectionNode->GetActivePlaceNodeClassName());
}
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::IsManageable(vtkMRMLNode* node)
{
return (this->Focus.find(node->GetClassName()) != this->Focus.end());
}
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::IsManageable(const char* nodeClassName)
{
return nodeClassName && (this->Focus.find(nodeClassName) != this->Focus.end());
}
//---------------------------------------------------------------------------
vtkMRMLMarkupsNode* vtkMRMLMarkupsDisplayableManager::CreateNewMarkupsNode(
const std::string &markupsNodeClassName)
{
// create the MRML node
std::string nodeName = "M";
if (markupsNodeClassName == "vtkMRMLMarkupsFiducialNode")
{
nodeName = "F";
}
else if (markupsNodeClassName == "vtkMRMLMarkupsAngleNode")
{
nodeName = "A";
}
else if (markupsNodeClassName == "vtkMRMLMarkupsLineNode")
{
nodeName = "L";
}
else if (markupsNodeClassName == "vtkMRMLMarkupsCurveNode")
{
nodeName = "C";
}
else if (markupsNodeClassName == "vtkMRMLMarkupsClosedCurveNode")
{
nodeName = "C";
}
else if (markupsNodeClassName == "vtkMRMLMarkupsPlaneNode")
{
nodeName = "P";
}
nodeName = this->GetMRMLScene()->GenerateUniqueName(nodeName);
vtkMRMLMarkupsNode* markupsNode = vtkMRMLMarkupsNode::SafeDownCast(
this->GetMRMLScene()->AddNewNodeByClass(markupsNodeClassName, nodeName));
markupsNode->AddDefaultStorageNode();
markupsNode->CreateDefaultDisplayNodes();
return markupsNode;
}
//---------------------------------------------------------------------------
vtkSlicerMarkupsWidget* vtkMRMLMarkupsDisplayableManager::FindClosestWidget(vtkMRMLInteractionEventData *callData, double &closestDistance2)
{
vtkSlicerMarkupsWidget* closestWidget = nullptr;
closestDistance2 = VTK_DOUBLE_MAX;
for (vtkMRMLMarkupsDisplayableManagerHelper::DisplayNodeToWidgetIt widgetIterator = this->Helper->MarkupsDisplayNodesToWidgets.begin();
widgetIterator != this->Helper->MarkupsDisplayNodesToWidgets.end(); ++widgetIterator)
{
vtkSlicerMarkupsWidget* widget = widgetIterator->second;
if (!widget)
{
continue;
}
double distance2FromWidget = VTK_DOUBLE_MAX;
if (widget->CanProcessInteractionEvent(callData, distance2FromWidget))
{
if (!closestWidget || distance2FromWidget < closestDistance2)
{
closestDistance2 = distance2FromWidget;
closestWidget = widget;
}
}
}
return closestWidget;
}
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::CanProcessInteractionEvent(vtkMRMLInteractionEventData* eventData, double &closestDistance2)
{
vtkMRMLInteractionNode* interactionNode = this->GetInteractionNode();
// New point can be placed anywhere
int eventid = eventData->GetType();
// We allow mouse move with the shift modifier to be processed while in place mode so that we can continue to update the
// preview positionm, even when using shift + mouse-move to adjust the crosshair position.
if ((eventid == vtkCommand::MouseMoveEvent
&& (eventData->GetModifiers() == vtkEvent::NoModifier ||
(eventData->GetModifiers() & vtkEvent::ShiftModifier &&
interactionNode && interactionNode->GetCurrentInteractionMode() == vtkMRMLInteractionNode::Place)))
|| eventid == vtkCommand::Move3DEvent
/*|| (eventid == vtkCommand::LeftButtonPressEvent && eventData->GetModifiers() == vtkEvent::NoModifier)
|| eventid == vtkCommand::LeftButtonReleaseEvent
|| eventid == vtkCommand::RightButtonReleaseEvent
|| eventid == vtkCommand::EnterEvent
|| eventid == vtkCommand::LeaveEvent*/)
{
vtkMRMLSelectionNode *selectionNode = this->GetSelectionNode();
if (!interactionNode || !selectionNode)
{
return false;
}
if (interactionNode->GetCurrentInteractionMode() == vtkMRMLInteractionNode::Place
&& this->IsManageable(selectionNode->GetActivePlaceNodeClassName()))
{
// If there is a suitable markups node for placement but it is not available in current view
// then we do not allow placement (placement would create a new markup node for this view,
// which would probably not what users want - they would like to place using the current markups node)
bool canPlaceInThisView = false;
vtkMRMLMarkupsNode* markupsNode = this->GetActiveMarkupsNodeForPlacement();
if (markupsNode)
{
int numberOfDisplayNodes = markupsNode->GetNumberOfDisplayNodes();
vtkMRMLAbstractViewNode* viewNode = vtkMRMLAbstractViewNode::SafeDownCast(this->GetMRMLDisplayableNode());
for (int displayNodeIndex = 0; displayNodeIndex < numberOfDisplayNodes; displayNodeIndex++)
{
if (markupsNode->GetNthDisplayNode(displayNodeIndex)->IsDisplayableInView(viewNode->GetID()))
{
canPlaceInThisView = true;
break;
}
}
}
else
{
// a new markups node will be created
canPlaceInThisView = true;
}
if (canPlaceInThisView)
{
closestDistance2 = 0.0;
return true;
}
}
}
if (eventid == vtkCommand::LeaveEvent && this->LastActiveWidget != nullptr)
{
if (this->LastActiveWidget->GetMarkupsDisplayNode() && this->LastActiveWidget->GetMarkupsDisplayNode()->HasActiveComponent())
{
// this widget has active component, therefore leave event is relevant
closestDistance2 = 0.0;
return this->LastActiveWidget;
}
}
// Other interactions
bool canProcess = (this->FindClosestWidget(eventData, closestDistance2) != nullptr);
if (!canProcess && this->LastActiveWidget != nullptr
&& (eventid == vtkCommand::MouseMoveEvent || eventid == vtkCommand::Move3DEvent) )
{
// interaction context (e.g. mouse) is moved away from the widget -> deactivate if it's the same context that activated it
std::vector<std::string> contextsWithActiveComponents =
this->LastActiveWidget->GetMarkupsDisplayNode()->GetActiveComponentInteractionContexts();
if (std::find(contextsWithActiveComponents.begin(), contextsWithActiveComponents.end(), eventData->GetInteractionContextName())
!= contextsWithActiveComponents.end() )
{
this->LastActiveWidget->Leave(eventData);
this->LastActiveWidget = nullptr;
}
}
return canProcess;
}
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::ProcessInteractionEvent(vtkMRMLInteractionEventData* eventData)
{
if (this->GetDisableInteractorStyleEventsProcessing())
{
return false;
}
int eventid = eventData->GetType();
if (eventid == vtkCommand::LeaveEvent)
{
if (this->LastActiveWidget != nullptr)
{
this->LastActiveWidget->Leave(eventData);
this->LastActiveWidget = nullptr;
}
}
// Find/create active widget
vtkSlicerMarkupsWidget* activeWidget = nullptr;
if (this->GetInteractionNode()->GetCurrentInteractionMode() == vtkMRMLInteractionNode::Place)
{
activeWidget = this->GetWidgetForPlacement();
if (activeWidget)
{
activeWidget->SetWidgetState(vtkSlicerMarkupsWidget::WidgetStateDefine);
}
}
else
{
double closestDistance2 = VTK_DOUBLE_MAX;
activeWidget = this->FindClosestWidget(eventData, closestDistance2);
}
// Deactivate previous widget
if (this->LastActiveWidget != nullptr && this->LastActiveWidget != activeWidget)
{
this->LastActiveWidget->Leave(eventData);
}
this->LastActiveWidget = activeWidget;
if (!activeWidget)
{
// deactivate widget if we move far from it
if (eventid == vtkCommand::MouseMoveEvent && this->LastActiveWidget != nullptr)
{
this->LastActiveWidget->Leave(eventData);
this->LastActiveWidget = nullptr;
}
return false;
}
// Pass on the interaction event to the active widget
return activeWidget->ProcessInteractionEvent(eventData);
}
//---------------------------------------------------------------------------
vtkMRMLMarkupsNode* vtkMRMLMarkupsDisplayableManager::GetActiveMarkupsNodeForPlacement()
{
vtkMRMLSelectionNode *selectionNode = this->GetSelectionNode();
if (!selectionNode)
{
return nullptr;
}
const char *activeMarkupsID = selectionNode->GetActivePlaceNodeID();
vtkMRMLMarkupsNode *markupsNode = vtkMRMLMarkupsNode::SafeDownCast(this->GetMRMLScene()->GetNodeByID(activeMarkupsID));
if (!markupsNode)
{
return nullptr;
}
// Additional checks for placement
const char* placeNodeClassName = selectionNode->GetActivePlaceNodeClassName();
if (!placeNodeClassName)
{
return nullptr;
}
if (!this->IsManageable(placeNodeClassName))
{
return nullptr;
}
if (std::string(markupsNode->GetClassName()) != placeNodeClassName)
{
return nullptr;
}
return markupsNode;
}
//---------------------------------------------------------------------------
int vtkMRMLMarkupsDisplayableManager::GetCurrentInteractionMode()
{
vtkMRMLInteractionNode *interactionNode = this->GetInteractionNode();
if (!interactionNode)
{
return 0;
}
return interactionNode->GetCurrentInteractionMode();
}
//---------------------------------------------------------------------------
vtkSlicerMarkupsWidget* vtkMRMLMarkupsDisplayableManager::GetWidgetForPlacement()
{
if (this->GetCurrentInteractionMode() != vtkMRMLInteractionNode::Place)
{
return nullptr;
}
vtkMRMLSelectionNode *selectionNode = this->GetSelectionNode();
if (!selectionNode)
{
return nullptr;
}
std::string placeNodeClassName = (selectionNode->GetActivePlaceNodeClassName() ? selectionNode->GetActivePlaceNodeClassName() : nullptr);
if (!this->IsManageable(placeNodeClassName.c_str()))
{
return nullptr;
}
// Check if the active markups node is already the right class, and if yes then use that
vtkMRMLMarkupsNode *activeMarkupsNode = this->GetActiveMarkupsNodeForPlacement();
// Do not create a new widget if the markup is not displayable in this view
if (activeMarkupsNode)
{
bool canPlaceInThisView = false;
int numberOfDisplayNodes = activeMarkupsNode->GetNumberOfDisplayNodes();
vtkMRMLAbstractViewNode* viewNode = vtkMRMLAbstractViewNode::SafeDownCast(this->GetMRMLDisplayableNode());
for (int displayNodeIndex = 0; displayNodeIndex < numberOfDisplayNodes; displayNodeIndex++)
{
if (activeMarkupsNode->GetNthDisplayNode(displayNodeIndex)->IsDisplayableInView(viewNode->GetID()))
{
canPlaceInThisView = true;
break;
}
}
if (!canPlaceInThisView)
{
return nullptr;
}
}
if (activeMarkupsNode && activeMarkupsNode->GetMaximumNumberOfControlPoints() > 0
&& activeMarkupsNode->GetNumberOfControlPoints() >= activeMarkupsNode->GetMaximumNumberOfControlPoints())
{
// maybe reached maximum number of points - if yes, then create a new widget
if (activeMarkupsNode->GetNumberOfControlPoints() == activeMarkupsNode->GetMaximumNumberOfControlPoints())
{
// one more point than the maximum
vtkSlicerMarkupsWidget *slicerWidget = this->Helper->GetWidget(activeMarkupsNode);
if (slicerWidget && !slicerWidget->IsPointPreviewed())
{
// no preview is shown, so the widget is actually complete
activeMarkupsNode = nullptr;
}
}
else
{
// clearly over the maximum number of points
activeMarkupsNode = nullptr;
}
}
// If there is no active markups node then create a new one
if (!activeMarkupsNode)
{
activeMarkupsNode = this->CreateNewMarkupsNode(placeNodeClassName);
selectionNode->SetReferenceActivePlaceNodeID(activeMarkupsNode->GetID());
}
if (!activeMarkupsNode)
{
return nullptr;
}
vtkSlicerMarkupsWidget *slicerWidget = this->Helper->GetWidget(activeMarkupsNode);
return slicerWidget;
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::SetHasFocus(bool hasFocus)
{
if (!hasFocus && this->LastActiveWidget!=nullptr)
{
this->LastActiveWidget->Leave(nullptr);
this->LastActiveWidget = nullptr;
}
}
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::GetGrabFocus()
{
return (this->LastActiveWidget != nullptr && this->LastActiveWidget->GetGrabFocus());
}
//---------------------------------------------------------------------------
bool vtkMRMLMarkupsDisplayableManager::GetInteractive()
{
return (this->LastActiveWidget != nullptr && this->LastActiveWidget->GetInteractive());
}
//---------------------------------------------------------------------------
int vtkMRMLMarkupsDisplayableManager::GetMouseCursor()
{
if (!this->LastActiveWidget)
{
return VTK_CURSOR_DEFAULT;
}
return this->LastActiveWidget->GetMouseCursor();
}
//---------------------------------------------------------------------------
vtkSlicerMarkupsWidget * vtkMRMLMarkupsDisplayableManager::CreateWidget(vtkMRMLMarkupsDisplayNode* markupsDisplayNode)
{
vtkMRMLMarkupsNode* markupsNode = markupsDisplayNode->GetMarkupsNode();
if (!markupsNode)
{
return nullptr;
}
vtkMRMLAbstractViewNode* viewNode = vtkMRMLAbstractViewNode::SafeDownCast(this->GetMRMLDisplayableNode());
vtkRenderer* renderer = this->GetRenderer();
vtkSlicerMarkupsWidget* widget = nullptr;
if (vtkMRMLMarkupsAngleNode::SafeDownCast(markupsNode))
{
widget = vtkSlicerAngleWidget::New();
}
else if (vtkMRMLMarkupsFiducialNode::SafeDownCast(markupsNode))
{
widget = vtkSlicerPointsWidget::New();
}
else if (vtkMRMLMarkupsLineNode::SafeDownCast(markupsNode))
{
widget = vtkSlicerLineWidget::New();
}
else if (vtkMRMLMarkupsCurveNode::SafeDownCast(markupsNode))
{
widget = vtkSlicerCurveWidget::New();
}
else if (vtkMRMLMarkupsClosedCurveNode::SafeDownCast(markupsNode))
{
widget = vtkSlicerClosedCurveWidget::New();
}
else if (vtkMRMLMarkupsPlaneNode::SafeDownCast(markupsNode))
{
widget = vtkSlicerPlaneWidget::New();
}
else
{
vtkErrorMacro("vtkMRMLMarkupsDisplayableManager::CreateWidget failed: invalid display node class " << markupsNode->GetClassName());
return nullptr;
}
widget->SetMRMLApplicationLogic(this->GetMRMLApplicationLogic());
widget->CreateDefaultRepresentation(markupsDisplayNode, viewNode, renderer);
return widget;
}
//---------------------------------------------------------------------------
void vtkMRMLMarkupsDisplayableManager::ConvertDeviceToXYZ(double x, double y, double xyz[3])
{
vtkMRMLAbstractSliceViewDisplayableManager::ConvertDeviceToXYZ(this->GetInteractor(), this->GetMRMLSliceNode(), x, y, xyz);
}