Slicer  4.11
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkMRMLScene.h
Go to the documentation of this file.
1 /*=auto=========================================================================
2 
3 Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
4 
5 See COPYRIGHT.txt
6 or http://www.slicer.org/copyright/copyright.txt for details.
7 
8 Program: 3D Slicer
9 Module: $RCSfile: vtkMRMLScene.h,v $
10 Date: $Date: 2006/03/17 15:10:09 $
11 Version: $Revision: 1.18 $
12 
13 =========================================================================auto=*/
14 
15 #ifndef __vtkMRMLScene_h
16 #define __vtkMRMLScene_h
17 
20 #define CURRENT_MRML_VERSION "Slicer4.4.0"
21 
22 // MRML includes
23 #include "vtkMRML.h"
24 
25 // VTK includes
26 #include <vtkObject.h>
27 #include <vtkSmartPointer.h>
28 #include <vtkWeakPointer.h>
29 
30 // STD includes
31 #include <list>
32 #include <map>
33 #include <set>
34 #include <string>
35 #include <vector>
36 
37 class vtkCacheManager;
38 class vtkDataIOManager;
39 class vtkTagTable;
40 
41 class vtkCallbackCommand;
42 class vtkCollection;
43 class vtkGeneralTransform;
44 class vtkURIHandler;
45 class vtkMRMLNode;
48 
53 //
56 class VTK_MRML_EXPORT vtkMRMLScene : public vtkObject
57 {
62  friend class vtkMRMLSceneViewNode;
63 
64 public:
65  static vtkMRMLScene *New();
66  vtkTypeMacro(vtkMRMLScene, vtkObject);
67  void PrintSelf(ostream& os, vtkIndent indent) override;
68 
70  void SetURL(const char *url);
71 
73  const char *GetURL();
74 
76  void SetRootDirectory(const char *dir);
77 
79  const char *GetRootDirectory();
80 
84  int Connect();
85 
92  int Import();
93 
96  int Commit(const char* url=nullptr);
97 
103  void Clear(int removeSingletons=0);
104 
106  void ResetNodes();
107 
125  vtkMRMLNode* CreateNodeByClass(const char* className);
126 
147  void RegisterNodeClass(vtkMRMLNode* node, const char* tagName);
148 
153  void RegisterNodeClass(vtkMRMLNode* node);
154 
156  const char* GetClassNameByTag(const char *tagName);
157 
159  const char* GetTagByClassName(const char *className);
160 
164  void AddDefaultNode(vtkMRMLNode* node);
165  vtkMRMLNode* GetDefaultNodeByClass(const char* className);
166 
168  void RemoveAllDefaultNodes();
169 
171  vtkCollection* GetNodes();
172 
185  vtkMRMLNode* AddNode(vtkMRMLNode *nodeToAdd);
186 
202  vtkMRMLNode* AddNewNodeByClass(std::string className, std::string nodeBaseName = "");
203 
211  vtkMRMLNode* AddNewNodeByClassWithID(std::string className, std::string nodeBaseName, std::string nodeID);
212 
214  vtkMRMLNode* CopyNode(vtkMRMLNode *n);
215 
219  void NodeAdded(vtkMRMLNode *n);
220  void NodeAdded() {this->NodeAdded(nullptr);}
221 
223  void RemoveNode(vtkMRMLNode *n);
224 
228  int IsNodePresent(vtkMRMLNode *n);
229 
237  void InitTraversal();
238 
246  vtkMRMLNode *GetNextNode();
247 
255  vtkMRMLNode *GetNextNodeByClass(const char* className);
256 
258  vtkCollection *GetNodesByName(const char* name);
259  vtkMRMLNode *GetFirstNodeByName(const char* name);
260 
272  vtkMRMLNode *GetFirstNode(const char* byName = nullptr, const char* byClass = nullptr,
273  const int* byHideFromEditors = nullptr,
274  bool exactNameMatch = true);
275 
277  vtkMRMLNode *GetNodeByID(const char* name);
278  vtkMRMLNode *GetNodeByID(std::string name);
279 
282  vtkCollection *GetNodesByClassByName(const char* className, const char* name);
283 
285  int GetNumberOfNodes();
286 
288  vtkMRMLNode* GetNthNode(int n);
289 
291  vtkMRMLNode* GetNthNodeByClass(int n, const char* className );
293  vtkMRMLNode* GetFirstNodeByClass(const char* className);
294 
296  int GetNumberOfNodesByClass(const char* className);
297 
299  int GetNodesByClass(const char *className, std::vector<vtkMRMLNode *> &nodes);
300 
302  vtkCollection* GetNodesByClass(const char *className);
303 
308  vtkMRMLNode* GetSingletonNode(const char* singletonTag, const char* className);
309 
316  vtkMRMLNode* GetSingletonNode(vtkMRMLNode* n);
317 
318  std::list<std::string> GetNodeClassesList();
319 
322  int GetNumberOfRegisteredNodeClasses();
325  vtkMRMLNode * GetNthRegisteredNodeClass(int n);
326 
330  bool IsNodeClassRegistered(const std::string& className);
331 
335  std::string GenerateUniqueName(const std::string& baseName);
336 
341  const char* GetUniqueNameByString(const char* baseName);
342 
344  vtkMRMLNode* InsertAfterNode(vtkMRMLNode *item, vtkMRMLNode *newItem);
346  vtkMRMLNode* InsertBeforeNode(vtkMRMLNode *item, vtkMRMLNode *newItem);
347 
349  void SetUndoOn() {UndoFlag=true;}
350  void SetUndoOff() {UndoFlag=false;}
351  bool GetUndoFlag() {return UndoFlag;}
352  void SetUndoFlag(bool flag) {UndoFlag = flag;}
353 
355  void Undo();
356 
358  void Redo();
359 
361  void ClearUndoStack();
362 
364  void ClearRedoStack();
365 
367  int GetNumberOfUndoLevels() {return static_cast<int>(this->UndoStack.size());}
368 
370  int GetNumberOfRedoLevels() {return static_cast<int>(this->RedoStack.size());}
371 
373  void SaveStateForUndo();
374 
380  void SaveStateForUndo(vtkMRMLNode *node);
381 
387  void SaveStateForUndo(vtkCollection *nodes);
388  void SaveStateForUndo(std::vector<vtkMRMLNode *> nodes);
389 
396  void AddReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
397  bool IsNodeReferencingNodeID(vtkMRMLNode* referencingNode, const char* id);
398 
405  vtkMRMLNode* GetNthReferencingNode(int n);
408  const char* GetNthReferencedID(int n);
409 
410  void RemoveReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
411 
412  void RemoveNodeReferences(vtkMRMLNode *node);
413 
414  void RemoveReferencesToNode(vtkMRMLNode *node);
415 
423  void UpdateNodeReferences(vtkCollection* checkNodes=nullptr);
424 
425  void CopyNodeReferences(vtkMRMLScene *scene);
426 
427  void CopyNodeChangedIDs(vtkMRMLScene *scene);
428 
430  void UpdateNodeChangedIDs();
431 
432  void RemoveUnusedNodeReferences();
433 
434  bool IsReservedID(const std::string& id);
435 
436  void AddReservedID(const char *id);
437 
438  void RemoveReservedIDs();
439 
442  const char* GetChangedID(const char* id);
443 
458  vtkCollection* GetReferencedNodes(vtkMRMLNode *node, bool recursive=true);
459 
461  void GetReferencingNodes(vtkMRMLNode* referencedNode, std::vector<vtkMRMLNode *> &referencingNodes);
462 
469  void GetReferencedSubScene(vtkMRMLNode *node, vtkMRMLScene* newScene);
470 
471  int IsFilePathRelative(const char * filepath);
472 
473  vtkSetMacro(ErrorCode,unsigned long);
474  vtkGetMacro(ErrorCode,unsigned long);
475 
482  vtkSetMacro(LoadFromXMLString,int);
483  vtkGetMacro(LoadFromXMLString,int);
484 
491  vtkSetMacro(SaveToXMLString,int);
492  vtkGetMacro(SaveToXMLString,int);
493 
494  vtkSetMacro(ReadDataOnLoad,int);
495  vtkGetMacro(ReadDataOnLoad,int);
496 
497  void SetErrorMessage(const std::string &error);
498  std::string GetErrorMessage();
499 
504  void SetSceneXMLString(const std::string &xmlString);
505 
510  const std::string& GetSceneXMLString();
511 
512  void SetErrorMessage(const char * message);
513  const char *GetErrorMessagePointer();
514 
515  vtkMRMLSubjectHierarchyNode* GetSubjectHierarchyNode();
516 
517  vtkGetObjectMacro(CacheManager, vtkCacheManager);
518  virtual void SetCacheManager(vtkCacheManager*);
519  vtkGetObjectMacro(DataIOManager, vtkDataIOManager);
520  virtual void SetDataIOManager(vtkDataIOManager*);
521  vtkGetObjectMacro(URIHandlerCollection, vtkCollection);
522  virtual void SetURIHandlerCollection(vtkCollection*);
523  vtkGetObjectMacro(UserTagTable, vtkTagTable);
524  virtual void SetUserTagTable(vtkTagTable*);
525 
530  vtkURIHandler *FindURIHandler(const char *URI);
532  vtkURIHandler *FindURIHandlerByName (const char *name);
534  void AddURIHandler(vtkURIHandler *handler);
535 
572  {
573  BatchProcessState = 0x0001,
574  CloseState = 0x0002 | BatchProcessState,
575  ImportState = 0x0004 | BatchProcessState,
576  RestoreState = 0x0008 | BatchProcessState,
577  SaveState = 0x0010,
578  UndoState = 0x0020,
579  RedoState = 0x0040,
580  };
581 
589  int GetStates()const;
590 
592  inline bool IsBatchProcessing()const;
594  inline bool IsClosing()const;
596  inline bool IsImporting()const;
598  inline bool IsRestoring()const;
600  inline bool IsUndoing()const;
602  inline bool IsRedoing()const;
603 
638  void StartState(unsigned long state, int anticipatedMaxProgress = 0);
639 
648  void EndState(unsigned long state);
649 
651  void ProgressState(unsigned long state, int progress = 0);
652 
654  {
655  NodeAboutToBeAddedEvent = 0x2000,
659 
660  NewSceneEvent = 66030,
661  MetadataAddedEvent = 66032, // ### Slicer 4.5: Simplify - Do not explicitly set for backward compat. See issue #3472
664 
668  StateEvent = 0x2000,
669  StartEvent = 0x0100,
670  EndEvent = 0x0200,
671  ProgressEvent = 0x0400,
672 
673  StartBatchProcessEvent = StateEvent | StartEvent | BatchProcessState,
674  EndBatchProcessEvent = StateEvent | EndEvent | BatchProcessState,
675  ProgressBatchProcessEvent = StateEvent | ProgressEvent | BatchProcessState,
676 
677  StartCloseEvent = StateEvent | StartEvent | CloseState,
678  EndCloseEvent = StateEvent | EndEvent | CloseState,
679  ProgressCloseEvent = StateEvent | ProgressEvent | CloseState,
680 
681  StartImportEvent = StateEvent | StartEvent | ImportState,
682  EndImportEvent = StateEvent | EndEvent | ImportState,
683  ProgressImportEvent = StateEvent | EndEvent | ImportState,
684 
685  StartRestoreEvent = StateEvent | StartEvent | RestoreState,
686  EndRestoreEvent = StateEvent | EndEvent | RestoreState,
687  ProgressRestoreEvent = StateEvent | ProgressEvent | RestoreState,
688 
689  StartSaveEvent = StateEvent | StartEvent | SaveState,
690  EndSaveEvent = StateEvent | EndEvent | SaveState,
691  ProgressSaveEvent = StateEvent | ProgressEvent | SaveState,
692 
693  StartUndoEvent = StateEvent | StartEvent | UndoState,
694  EndUndoEvent = StateEvent | EndEvent | UndoState,
695  ProgressUndoEvent = StateEvent | ProgressEvent | UndoState,
696 
697  StartRedoEvent = StateEvent | StartEvent | RedoState,
698  EndRedoEvent = StateEvent | EndEvent | RedoState,
699  ProgressRedoEvent = StateEvent | ProgressEvent | RedoState,
700 
701  };
702 
704  vtkGetStringMacro(LastLoadedVersion);
705  vtkSetStringMacro(LastLoadedVersion);
706 
708  vtkGetStringMacro(Version);
709  vtkSetStringMacro(Version);
710 
712  void CopyRegisteredNodesToScene(vtkMRMLScene *scene);
713 
715  void CopySingletonNodesToScene(vtkMRMLScene *scene);
716 
718  void CopyDefaultNodesToScene(vtkMRMLScene *scene);
719 
722  bool GetModifiedSinceRead();
723 
730  bool GetStorableNodesModifiedSinceRead(vtkCollection* modifiedStorableNodes = nullptr);
731 
737  void SetStorableNodesModifiedSinceRead();
738 
741  static void SetStorableNodesModifiedSinceRead(vtkCollection* storableNodes);
742 
745  void SetMaximumNumberOfSavedUndoStates(int stackSize);
746  vtkGetMacro(MaximumNumberOfSavedUndoStates, int);
747 
748 protected:
749 
750  typedef std::map< std::string, std::set<std::string> > NodeReferencesType;
751 
752  vtkMRMLScene();
753  ~vtkMRMLScene() override;
754 
755  void PushIntoUndoStack();
756  void PushIntoRedoStack();
757 
758  void CopyNodeInUndoStack(vtkMRMLNode *node);
759  void CopyNodeInRedoStack(vtkMRMLNode *node);
760 
764  vtkMRMLNode* AddNodeNoNotify(vtkMRMLNode *n);
765 
769  void AddReferencedNodes(vtkMRMLNode *node, vtkCollection *refNodes, bool recursive=true);
770 
772  static void SceneCallback(vtkObject *caller, unsigned long eid, void *clientData, void *callData);
773 
774  std::string GenerateUniqueID(vtkMRMLNode* node);
775  std::string GenerateUniqueID(const std::string& baseID);
776  int GetUniqueIDIndex(const std::string& baseID);
777  std::string BuildID(const std::string& baseID, int idIndex)const;
778 
780  std::string GenerateUniqueName(vtkMRMLNode* node);
781 
787  int GetUniqueNameIndex(const std::string& baseName);
788 
790  std::string BuildName(const std::string& baseName, int nameIndex)const;
791 
794  void UpdateNodeIDs();
795 
797  void AddNodeID(vtkMRMLNode *node);
798 
800  void RemoveNodeID(char *nodeID);
801 
803  void ClearNodeIDs();
804 
806  NodeReferencesType::iterator FindNodeReference(const char* referencedId, vtkMRMLNode* referencingNode);
807 
809  void TrimUndoStack();
810 
812  void ReserveNodeReferenceIDs(vtkMRMLNode* node);
813 
815  void GetNodeReferenceIDsFromUndoStack(std::set<std::string>& referenceIDs) const;
816 
818  bool IsNodeIDReservedByUndo(const std::string id) const;
819 
820  virtual void SetSubjectHierarchyNode(vtkMRMLSubjectHierarchyNode*);
821 
822  vtkCollection* Nodes;
823 
825  vtkWeakPointer<vtkMRMLSubjectHierarchyNode> SubjectHierarchyNode;
826 
830  vtkCollection * URIHandlerCollection;
832 
833  std::vector<unsigned long> States;
834 
836  bool UndoFlag;
837 
838  std::list< vtkCollection* > UndoStack;
839  std::list< vtkCollection* > RedoStack;
840 
841  std::string URL;
842  std::string RootDirectory;
843 
844  std::map<std::string, int> UniqueIDs;
845  std::map<std::string, int> UniqueNames;
846  std::set<std::string> ReservedIDs;
847 
848  std::vector< vtkMRMLNode* > RegisteredNodeClasses;
849  std::vector< std::string > RegisteredNodeTags;
850 
851  NodeReferencesType NodeReferences; // ReferencedIDs (string), ReferencingNodes (node pointer)
852  std::map< std::string, std::string > ReferencedIDChanges;
853  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > NodeIDs;
854 
855  // Stores default nodes. If a class is created or reset (using CreateNodeByClass or Clear) and
856  // a default node is defined for it then the content of the default node will be used to initialize
857  // the class. It is useful for overriding default values that are set in a node's constructor.
858  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > DefaultNodes;
859 
860  std::string ErrorMessage;
861 
862  std::string SceneXMLString;
863 
865 
867 
869 
870  vtkMTimeType NodeIDsMTime;
871 
872  void RemoveAllNodes(bool removeSingletons);
873 
874  char * Version;
876 
877  vtkCallbackCommand *DeleteEventCallback;
878 
879 private:
880 
881  vtkMRMLScene(const vtkMRMLScene&); // Not implemented
882  void operator=(const vtkMRMLScene&); // Not implemented
883 
885  int LoadIntoScene(vtkCollection* scene);
886 
887  unsigned long ErrorCode;
888 
890  vtkTimeStamp StoredTime;
891 };
892 
893 //------------------------------------------------------------------------------
895 {
896  return (this->GetStates() & vtkMRMLScene::BatchProcessState)
898 }
899 
900 //------------------------------------------------------------------------------
902 {
903  return (this->GetStates() & vtkMRMLScene::CloseState)
905 }
906 
907 //------------------------------------------------------------------------------
909 {
910  return (this->GetStates() & vtkMRMLScene::ImportState)
912 }
913 
914 //------------------------------------------------------------------------------
916 {
917  return (this->GetStates() & vtkMRMLScene::RestoreState)
919 }
920 
921 //------------------------------------------------------------------------------
923 {
924  return (this->GetStates() & vtkMRMLScene::UndoState)
926 }
927 
928 //------------------------------------------------------------------------------
930 {
931  return (this->GetStates() & vtkMRMLScene::RedoState)
933 }
934 
935 #endif
int GetNodesByClass(const char *className, std::vector< vtkMRMLNode *> &nodes)
void NodeAdded()
Definition: vtkMRMLScene.h:220
char * LastLoadedVersion
Definition: vtkMRMLScene.h:875
int GetNumberOfRedoLevels()
returns number of redo steps in the history buffer
Definition: vtkMRMLScene.h:370
char * Version
Definition: vtkMRMLScene.h:874
void PrintSelf(ostream &os, vtkIndent indent) override
vtkWeakPointer< vtkMRMLSubjectHierarchyNode > SubjectHierarchyNode
subject hierarchy node
Definition: vtkMRMLScene.h:825
vtkCallbackCommand * DeleteEventCallback
Definition: vtkMRMLScene.h:877
bool IsImporting() const
Return true if the scene is in Import state, false otherwise.
Definition: vtkMRMLScene.h:908
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > NodeIDs
Definition: vtkMRMLScene.h:853
vtkDataIOManager * DataIOManager
Definition: vtkMRMLScene.h:829
std::string URL
Definition: vtkMRMLScene.h:841
void SetUndoFlag(bool flag)
Definition: vtkMRMLScene.h:352
int MaximumNumberOfSavedUndoStates
Definition: vtkMRMLScene.h:835
vtkCollection * URIHandlerCollection
Definition: vtkMRMLScene.h:830
std::string RootDirectory
Definition: vtkMRMLScene.h:842
virtual bool GetModifiedSinceRead()
std::set< std::string > ReservedIDs
Definition: vtkMRMLScene.h:846
std::string SceneXMLString
Definition: vtkMRMLScene.h:862
int GetNumberOfUndoLevels()
returns number of undo steps in the history buffer
Definition: vtkMRMLScene.h:367
static vtkMRMLSceneViewNode * New()
bool IsRedoing() const
Return true if the scene is in Redo state (in the process of redoing node changes), false otherwise.
Definition: vtkMRMLScene.h:929
MRML node to represent a complete subject hierarchy tree.
void SetUndoOn()
Set undo on/off.
Definition: vtkMRMLScene.h:349
virtual void UpdateNodeReferences(const char *referenceRole=nullptr)
std::map< std::string, int > UniqueIDs
Definition: vtkMRMLScene.h:844
vtkCacheManager * CacheManager
data i/o handling members
Definition: vtkMRMLScene.h:828
A set of MRML Nodes that supports serialization and undo/redo.
Definition: vtkMRMLScene.h:56
vtkMTimeType NodeIDsMTime
Definition: vtkMRMLScene.h:870
std::string ErrorMessage
Definition: vtkMRMLScene.h:860
std::map< std::string, int > UniqueNames
Definition: vtkMRMLScene.h:845
int GetNumberOfNodeReferences(const char *referenceRole)
Return the number of node IDs for a specific reference role (and nodes as they always have the same s...
int LoadFromXMLString
Definition: vtkMRMLScene.h:864
NodeReferencesType NodeReferences
Definition: vtkMRMLScene.h:851
std::vector< vtkMRMLNode *> RegisteredNodeClasses
Definition: vtkMRMLScene.h:848
void SetUndoOff()
Definition: vtkMRMLScene.h:350
void operator=(const vtkMRMLSceneViewNode &)
vtkCollection * Nodes
Definition: vtkMRMLScene.h:822
std::list< vtkCollection *> UndoStack
Definition: vtkMRMLScene.h:838
friend class vtkMRMLScene
Definition: vtkMRMLNode.h:143
bool IsUndoing() const
Return true if the scene is in Undo state (in the process of undoing node changes), false otherwise.
Definition: vtkMRMLScene.h:922
std::map< std::string, std::string > ReferencedIDChanges
Definition: vtkMRMLScene.h:852
bool IsClosing() const
Return true if the scene is in Close state, false otherwise.
Definition: vtkMRMLScene.h:901
std::vector< std::string > RegisteredNodeTags
Definition: vtkMRMLScene.h:849
bool IsRestoring() const
Return true if the scene is in Restore state, false otherwise.
Definition: vtkMRMLScene.h:915
Abstract Superclass for all specific types of MRML nodes.
Definition: vtkMRMLNode.h:139
std::list< vtkCollection *> RedoStack
Definition: vtkMRMLScene.h:839
vtkTagTable * UserTagTable
Definition: vtkMRMLScene.h:831
int GetStates() const
Returns the current state of the scene.
std::vector< unsigned long > States
Definition: vtkMRMLScene.h:833
bool IsBatchProcessing() const
Return true if the scene is in BatchProcess state, false otherwise.
Definition: vtkMRMLScene.h:894
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > DefaultNodes
Definition: vtkMRMLScene.h:858
std::map< std::string, std::set< std::string > > NodeReferencesType
Definition: vtkMRMLScene.h:746
bool GetUndoFlag()
Definition: vtkMRMLScene.h:351