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 <random>
34 #include <set>
35 #include <string>
36 #include <vector>
37 
38 class vtkCacheManager;
39 class vtkDataIOManager;
40 class vtkTagTable;
41 
42 class vtkCallbackCommand;
43 class vtkCollection;
44 class vtkGeneralTransform;
45 class vtkImageData;
46 class vtkURIHandler;
48 class vtkMRMLNode;
52 class vtkMRMLStorageNode;
53 
58 //
61 class VTK_MRML_EXPORT vtkMRMLScene : public vtkObject
62 {
67  friend class vtkMRMLSceneViewNode;
68 
69 public:
70  static vtkMRMLScene *New();
71  vtkTypeMacro(vtkMRMLScene, vtkObject);
72  void PrintSelf(ostream& os, vtkIndent indent) override;
73 
75  void SetURL(const char *url);
76 
78  const char *GetURL();
79 
81  void SetRootDirectory(const char *dir);
82 
84  const char *GetRootDirectory();
85 
89  int Connect();
90 
97  int Import();
98 
101  int Commit(const char* url=nullptr);
102 
108  void Clear(int removeSingletons=0);
109 
111  void ResetNodes();
112 
130  vtkMRMLNode* CreateNodeByClass(const char* className);
131 
152  void RegisterNodeClass(vtkMRMLNode* node, const char* tagName);
153 
158  void RegisterNodeClass(vtkMRMLNode* node);
159 
161  const char* GetClassNameByTag(const char *tagName);
162 
164  const char* GetTagByClassName(const char *className);
165 
169  void AddDefaultNode(vtkMRMLNode* node);
170  vtkMRMLNode* GetDefaultNodeByClass(const char* className);
171 
173  void RemoveAllDefaultNodes();
174 
176  vtkCollection* GetNodes();
177 
190  vtkMRMLNode* AddNode(vtkMRMLNode *nodeToAdd);
191 
207  vtkMRMLNode* AddNewNodeByClass(std::string className, std::string nodeBaseName = "");
208 
216  vtkMRMLNode* AddNewNodeByClassWithID(std::string className, std::string nodeBaseName, std::string nodeID);
217 
219  vtkMRMLNode* CopyNode(vtkMRMLNode *n);
220 
224  void NodeAdded(vtkMRMLNode *n);
225  void NodeAdded() {this->NodeAdded(nullptr);}
226 
228  void RemoveNode(vtkMRMLNode *n);
229 
233  int IsNodePresent(vtkMRMLNode *n);
234 
242  void InitTraversal();
243 
251  vtkMRMLNode *GetNextNode();
252 
260  vtkMRMLNode *GetNextNodeByClass(const char* className);
261 
263  vtkCollection *GetNodesByName(const char* name);
264  vtkMRMLNode *GetFirstNodeByName(const char* name);
265 
277  vtkMRMLNode *GetFirstNode(const char* byName = nullptr, const char* byClass = nullptr,
278  const int* byHideFromEditors = nullptr,
279  bool exactNameMatch = true);
280 
282  vtkMRMLNode *GetNodeByID(const char* name);
283  vtkMRMLNode *GetNodeByID(std::string name);
284 
287  vtkCollection *GetNodesByClassByName(const char* className, const char* name);
288 
290  int GetNumberOfNodes();
291 
293  vtkMRMLNode* GetNthNode(int n);
294 
296  vtkMRMLNode* GetNthNodeByClass(int n, const char* className );
298  vtkMRMLNode* GetFirstNodeByClass(const char* className);
299 
301  int GetNumberOfNodesByClass(const char* className);
302 
304  int GetNodesByClass(const char *className, std::vector<vtkMRMLNode *> &nodes);
305 
307  vtkCollection* GetNodesByClass(const char *className);
308 
313  vtkMRMLNode* GetSingletonNode(const char* singletonTag, const char* className);
314 
321  vtkMRMLNode* GetSingletonNode(vtkMRMLNode* n);
322 
323  std::list<std::string> GetNodeClassesList();
324 
327  int GetNumberOfRegisteredNodeClasses();
330  vtkMRMLNode * GetNthRegisteredNodeClass(int n);
331 
335  bool IsNodeClassRegistered(const std::string& className);
336 
340  std::string GenerateUniqueName(const std::string& baseName);
341 
346  const char* GetUniqueNameByString(const char* baseName);
347 
349  vtkMRMLNode* InsertAfterNode(vtkMRMLNode *item, vtkMRMLNode *newItem);
351  vtkMRMLNode* InsertBeforeNode(vtkMRMLNode *item, vtkMRMLNode *newItem);
352 
354  void SetUndoOn() {UndoFlag=true;}
355  void SetUndoOff() {UndoFlag=false;}
356  bool GetUndoFlag() {return UndoFlag;}
357  void SetUndoFlag(bool flag) {UndoFlag = flag;}
358 
360  void Undo();
361 
363  void Redo();
364 
366  void ClearUndoStack();
367 
369  void ClearRedoStack();
370 
372  int GetNumberOfUndoLevels() {return static_cast<int>(this->UndoStack.size());}
373 
375  int GetNumberOfRedoLevels() {return static_cast<int>(this->RedoStack.size());}
376 
378  void SaveStateForUndo();
379 
385  void SaveStateForUndo(vtkMRMLNode *node);
386 
392  void SaveStateForUndo(vtkCollection *nodes);
393  void SaveStateForUndo(std::vector<vtkMRMLNode *> nodes);
394 
401  void AddReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
402  bool IsNodeReferencingNodeID(vtkMRMLNode* referencingNode, const char* id);
403 
410  vtkMRMLNode* GetNthReferencingNode(int n);
413  const char* GetNthReferencedID(int n);
414 
415  void RemoveReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
416 
417  void RemoveNodeReferences(vtkMRMLNode *node);
418 
419  void RemoveReferencesToNode(vtkMRMLNode *node);
420 
428  void UpdateNodeReferences(vtkCollection* checkNodes=nullptr);
429 
430  void CopyNodeReferences(vtkMRMLScene *scene);
431 
432  void CopyNodeChangedIDs(vtkMRMLScene *scene);
433 
435  void UpdateNodeChangedIDs();
436 
437  void RemoveUnusedNodeReferences();
438 
439  bool IsReservedID(const std::string& id);
440 
441  void AddReservedID(const char *id);
442 
443  void RemoveReservedIDs();
444 
447  const char* GetChangedID(const char* id);
448 
463  vtkCollection* GetReferencedNodes(vtkMRMLNode *node, bool recursive=true);
464 
466  void GetReferencingNodes(vtkMRMLNode* referencedNode, std::vector<vtkMRMLNode *> &referencingNodes);
467 
474  void GetReferencedSubScene(vtkMRMLNode *node, vtkMRMLScene* newScene);
475 
476  int IsFilePathRelative(const char * filepath);
477 
478  vtkSetMacro(ErrorCode,unsigned long);
479  vtkGetMacro(ErrorCode,unsigned long);
480 
487  vtkSetMacro(LoadFromXMLString,int);
488  vtkGetMacro(LoadFromXMLString,int);
489 
496  vtkSetMacro(SaveToXMLString,int);
497  vtkGetMacro(SaveToXMLString,int);
498 
499  vtkSetMacro(ReadDataOnLoad,int);
500  vtkGetMacro(ReadDataOnLoad,int);
501 
502  void SetErrorMessage(const std::string &error);
503  std::string GetErrorMessage();
504 
509  void SetSceneXMLString(const std::string &xmlString);
510 
515  const std::string& GetSceneXMLString();
516 
517  void SetErrorMessage(const char * message);
518 
519  vtkMRMLSubjectHierarchyNode* GetSubjectHierarchyNode();
520 
521  vtkGetObjectMacro(CacheManager, vtkCacheManager);
522  virtual void SetCacheManager(vtkCacheManager*);
523  vtkGetObjectMacro(DataIOManager, vtkDataIOManager);
524  virtual void SetDataIOManager(vtkDataIOManager*);
525  vtkGetObjectMacro(URIHandlerCollection, vtkCollection);
526  virtual void SetURIHandlerCollection(vtkCollection*);
527  vtkGetObjectMacro(UserTagTable, vtkTagTable);
528  virtual void SetUserTagTable(vtkTagTable*);
529 
534  vtkURIHandler *FindURIHandler(const char *URI);
536  vtkURIHandler *FindURIHandlerByName (const char *name);
538  void AddURIHandler(vtkURIHandler *handler);
539 
576  {
577  BatchProcessState = 0x0001,
578  CloseState = 0x0002 | BatchProcessState,
579  ImportState = 0x0004 | BatchProcessState,
580  RestoreState = 0x0008 | BatchProcessState,
581  SaveState = 0x0010,
582  UndoState = 0x0020,
583  RedoState = 0x0040,
584  };
585 
593  int GetStates()const;
594 
596  inline bool IsBatchProcessing()const;
598  inline bool IsClosing()const;
600  inline bool IsImporting()const;
602  inline bool IsRestoring()const;
604  inline bool IsUndoing()const;
606  inline bool IsRedoing()const;
607 
642  void StartState(unsigned long state, int anticipatedMaxProgress = 0);
643 
652  void EndState(unsigned long state);
653 
655  void ProgressState(unsigned long state, int progress = 0);
656 
658  {
659  NodeAboutToBeAddedEvent = 0x2000,
663 
664  NewSceneEvent = 66030,
665  MetadataAddedEvent = 66032, // ### Slicer 4.5: Simplify - Do not explicitly set for backward compat. See issue #3472
668 
672  StateEvent = 0x2000,
673  StartEvent = 0x0100,
674  EndEvent = 0x0200,
675  ProgressEvent = 0x0400,
676 
677  StartBatchProcessEvent = StateEvent | StartEvent | BatchProcessState,
678  EndBatchProcessEvent = StateEvent | EndEvent | BatchProcessState,
679  ProgressBatchProcessEvent = StateEvent | ProgressEvent | BatchProcessState,
680 
681  StartCloseEvent = StateEvent | StartEvent | CloseState,
682  EndCloseEvent = StateEvent | EndEvent | CloseState,
683  ProgressCloseEvent = StateEvent | ProgressEvent | CloseState,
684 
685  StartImportEvent = StateEvent | StartEvent | ImportState,
686  EndImportEvent = StateEvent | EndEvent | ImportState,
687  ProgressImportEvent = StateEvent | EndEvent | ImportState,
688 
689  StartRestoreEvent = StateEvent | StartEvent | RestoreState,
690  EndRestoreEvent = StateEvent | EndEvent | RestoreState,
691  ProgressRestoreEvent = StateEvent | ProgressEvent | RestoreState,
692 
693  StartSaveEvent = StateEvent | StartEvent | SaveState,
694  EndSaveEvent = StateEvent | EndEvent | SaveState,
695  ProgressSaveEvent = StateEvent | ProgressEvent | SaveState,
696 
697  StartUndoEvent = StateEvent | StartEvent | UndoState,
698  EndUndoEvent = StateEvent | EndEvent | UndoState,
699  ProgressUndoEvent = StateEvent | ProgressEvent | UndoState,
700 
701  StartRedoEvent = StateEvent | StartEvent | RedoState,
702  EndRedoEvent = StateEvent | EndEvent | RedoState,
703  ProgressRedoEvent = StateEvent | ProgressEvent | RedoState,
704 
705  };
706 
708  vtkGetStringMacro(LastLoadedVersion);
709  vtkSetStringMacro(LastLoadedVersion);
710 
712  vtkGetStringMacro(Version);
713  vtkSetStringMacro(Version);
714 
716  void CopyRegisteredNodesToScene(vtkMRMLScene *scene);
717 
719  void CopySingletonNodesToScene(vtkMRMLScene *scene);
720 
722  void CopyDefaultNodesToScene(vtkMRMLScene *scene);
723 
726  bool GetModifiedSinceRead();
727 
734  bool GetStorableNodesModifiedSinceRead(vtkCollection* modifiedStorableNodes = nullptr);
735 
741  void SetStorableNodesModifiedSinceRead();
742 
745  static void SetStorableNodesModifiedSinceRead(vtkCollection* storableNodes);
746 
749  void SetMaximumNumberOfSavedUndoStates(int stackSize);
750  vtkGetMacro(MaximumNumberOfSavedUndoStates, int);
751 
757  bool WriteToMRB(const char* filename, vtkImageData* thumbnail=nullptr, vtkMRMLMessageCollection* userMessages=nullptr);
758 
760  bool ReadFromMRB(const char* fullName, bool clear=false);
761 
765  static std::string UnpackSlicerDataBundle(const char* sdbFilePath, const char* temporaryDirectory);
766 
772  bool SaveSceneToSlicerDataBundleDirectory(const char* sdbDir, vtkImageData* thumbnail=nullptr, vtkMRMLMessageCollection* userMessages=nullptr);
773 
775  void SaveSceneScreenshot(vtkImageData* thumbnail);
776 
786  static std::string PercentEncode(std::string s);
787 
793  static std::string CreateUniqueFileName(const std::string& filename, const std::string& knownExtension = "");
794 
795 protected:
796 
797  typedef std::map< std::string, std::set<std::string> > NodeReferencesType;
798 
799  vtkMRMLScene();
800  ~vtkMRMLScene() override;
801 
802  void PushIntoUndoStack();
803  void PushIntoRedoStack();
804 
805  void CopyNodeInUndoStack(vtkMRMLNode *node);
806  void CopyNodeInRedoStack(vtkMRMLNode *node);
807 
811  vtkMRMLNode* AddNodeNoNotify(vtkMRMLNode *n);
812 
816  void AddReferencedNodes(vtkMRMLNode *node, vtkCollection *refNodes, bool recursive=true);
817 
819  static void SceneCallback(vtkObject *caller, unsigned long eid, void *clientData, void *callData);
820 
821  std::string GenerateUniqueID(vtkMRMLNode* node);
822  std::string GenerateUniqueID(const std::string& baseID);
823  int GetUniqueIDIndex(const std::string& baseID);
824  std::string BuildID(const std::string& baseID, int idIndex)const;
825 
827  std::string GenerateUniqueName(vtkMRMLNode* node);
828 
834  int GetUniqueNameIndex(const std::string& baseName);
835 
837  std::string BuildName(const std::string& baseName, int nameIndex)const;
838 
841  void UpdateNodeIDs();
842 
844  void AddNodeID(vtkMRMLNode *node);
845 
847  void RemoveNodeID(char *nodeID);
848 
850  void ClearNodeIDs();
851 
853  NodeReferencesType::iterator FindNodeReference(const char* referencedId, vtkMRMLNode* referencingNode);
854 
856  void TrimUndoStack();
857 
859  void ReserveNodeReferenceIDs(vtkMRMLNode* node);
860 
862  void GetNodeReferenceIDsFromUndoStack(std::set<std::string>& referenceIDs) const;
863 
865  bool IsNodeIDReservedByUndo(const std::string id) const;
866 
867  virtual void SetSubjectHierarchyNode(vtkMRMLSubjectHierarchyNode*);
868 
873  bool SaveStorableNodeToSlicerDataBundleDirectory(vtkMRMLStorableNode* storableNode, std::string& dataDir,
874  std::map<vtkMRMLStorageNode*, std::vector<std::string> > &originalStorageNodeFileNames, vtkMRMLMessageCollection* userMessages);
875 
876  vtkCollection* Nodes;
877 
879  vtkWeakPointer<vtkMRMLSubjectHierarchyNode> SubjectHierarchyNode;
880 
884  vtkCollection * URIHandlerCollection;
886 
887  std::vector<unsigned long> States;
888 
890  bool UndoFlag;
891 
892  std::list< vtkCollection* > UndoStack;
893  std::list< vtkCollection* > RedoStack;
894 
895  std::string URL;
896  std::string RootDirectory;
897 
898  std::map<std::string, int> UniqueIDs;
899  std::map<std::string, int> UniqueNames;
900  std::set<std::string> ReservedIDs;
901 
902  std::vector< vtkMRMLNode* > RegisteredNodeClasses;
903  std::vector< std::string > RegisteredNodeTags;
904 
905  NodeReferencesType NodeReferences; // ReferencedIDs (string), ReferencingNodes (node pointer)
906  std::map< std::string, std::string > ReferencedIDChanges;
907  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > NodeIDs;
908 
909  // Stores default nodes. If a class is created or reset (using CreateNodeByClass or Clear) and
910  // a default node is defined for it then the content of the default node will be used to initialize
911  // the class. It is useful for overriding default values that are set in a node's constructor.
912  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > DefaultNodes;
913 
914  std::string ErrorMessage;
915 
916  std::string SceneXMLString;
917 
919 
921 
923 
924  vtkMTimeType NodeIDsMTime;
925 
926  void RemoveAllNodes(bool removeSingletons);
927 
928  char * Version;
930 
931  vtkCallbackCommand *DeleteEventCallback;
932 
933  std::default_random_engine RandomGenerator;
934 
935 private:
936 
937  vtkMRMLScene(const vtkMRMLScene&); // Not implemented
938  void operator=(const vtkMRMLScene&); // Not implemented
939 
941  int LoadIntoScene(vtkCollection* scene);
942 
943  unsigned long ErrorCode;
944 
946  vtkTimeStamp StoredTime;
947 };
948 
949 //------------------------------------------------------------------------------
951 {
952  return (this->GetStates() & vtkMRMLScene::BatchProcessState)
954 }
955 
956 //------------------------------------------------------------------------------
958 {
959  return (this->GetStates() & vtkMRMLScene::CloseState)
961 }
962 
963 //------------------------------------------------------------------------------
965 {
966  return (this->GetStates() & vtkMRMLScene::ImportState)
968 }
969 
970 //------------------------------------------------------------------------------
972 {
973  return (this->GetStates() & vtkMRMLScene::RestoreState)
975 }
976 
977 //------------------------------------------------------------------------------
979 {
980  return (this->GetStates() & vtkMRMLScene::UndoState)
982 }
983 
984 //------------------------------------------------------------------------------
986 {
987  return (this->GetStates() & vtkMRMLScene::RedoState)
989 }
990 
991 #endif
int GetNodesByClass(const char *className, std::vector< vtkMRMLNode *> &nodes)
void NodeAdded()
Definition: vtkMRMLScene.h:225
char * LastLoadedVersion
Definition: vtkMRMLScene.h:929
int GetNumberOfRedoLevels()
returns number of redo steps in the history buffer
Definition: vtkMRMLScene.h:375
char * Version
Definition: vtkMRMLScene.h:928
void PrintSelf(ostream &os, vtkIndent indent) override
vtkWeakPointer< vtkMRMLSubjectHierarchyNode > SubjectHierarchyNode
subject hierarchy node
Definition: vtkMRMLScene.h:879
vtkCallbackCommand * DeleteEventCallback
Definition: vtkMRMLScene.h:931
bool IsImporting() const
Return true if the scene is in Import state, false otherwise.
Definition: vtkMRMLScene.h:964
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > NodeIDs
Definition: vtkMRMLScene.h:907
vtkDataIOManager * DataIOManager
Definition: vtkMRMLScene.h:883
std::string URL
Definition: vtkMRMLScene.h:895
void SetUndoFlag(bool flag)
Definition: vtkMRMLScene.h:357
int MaximumNumberOfSavedUndoStates
Definition: vtkMRMLScene.h:889
vtkCollection * URIHandlerCollection
Definition: vtkMRMLScene.h:884
std::string RootDirectory
Definition: vtkMRMLScene.h:896
virtual bool GetModifiedSinceRead()
std::set< std::string > ReservedIDs
Definition: vtkMRMLScene.h:900
std::string SceneXMLString
Definition: vtkMRMLScene.h:916
int GetNumberOfUndoLevels()
returns number of undo steps in the history buffer
Definition: vtkMRMLScene.h:372
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:985
MRML node to represent a complete subject hierarchy tree.
void SetUndoOn()
Set undo on/off.
Definition: vtkMRMLScene.h:354
virtual void UpdateNodeReferences(const char *referenceRole=nullptr)
std::map< std::string, int > UniqueIDs
Definition: vtkMRMLScene.h:898
MRML node to represent a 3D surface model.
vtkCacheManager * CacheManager
data i/o handling members
Definition: vtkMRMLScene.h:882
A set of MRML Nodes that supports serialization and undo/redo.
Definition: vtkMRMLScene.h:61
vtkMTimeType NodeIDsMTime
Definition: vtkMRMLScene.h:924
std::string ErrorMessage
Definition: vtkMRMLScene.h:914
std::map< std::string, int > UniqueNames
Definition: vtkMRMLScene.h:899
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:918
NodeReferencesType NodeReferences
Definition: vtkMRMLScene.h:905
std::vector< vtkMRMLNode *> RegisteredNodeClasses
Definition: vtkMRMLScene.h:902
void SetUndoOff()
Definition: vtkMRMLScene.h:355
A superclass for other storage nodes.
void operator=(const vtkMRMLSceneViewNode &)
vtkCollection * Nodes
Definition: vtkMRMLScene.h:876
std::list< vtkCollection *> UndoStack
Definition: vtkMRMLScene.h:892
friend class vtkMRMLScene
Definition: vtkMRMLNode.h:171
bool IsUndoing() const
Return true if the scene is in Undo state (in the process of undoing node changes), false otherwise.
Definition: vtkMRMLScene.h:978
std::map< std::string, std::string > ReferencedIDChanges
Definition: vtkMRMLScene.h:906
bool IsClosing() const
Return true if the scene is in Close state, false otherwise.
Definition: vtkMRMLScene.h:957
std::vector< std::string > RegisteredNodeTags
Definition: vtkMRMLScene.h:903
bool IsRestoring() const
Return true if the scene is in Restore state, false otherwise.
Definition: vtkMRMLScene.h:971
Abstract Superclass for all specific types of MRML nodes.
Definition: vtkMRMLNode.h:167
std::list< vtkCollection *> RedoStack
Definition: vtkMRMLScene.h:893
vtkTagTable * UserTagTable
Definition: vtkMRMLScene.h:885
int GetStates() const
Returns the current state of the scene.
std::vector< unsigned long > States
Definition: vtkMRMLScene.h:887
bool IsBatchProcessing() const
Return true if the scene is in BatchProcess state, false otherwise.
Definition: vtkMRMLScene.h:950
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > DefaultNodes
Definition: vtkMRMLScene.h:912
std::map< std::string, std::set< std::string > > NodeReferencesType
Definition: vtkMRMLScene.h:797
std::default_random_engine RandomGenerator
Definition: vtkMRMLScene.h:933
bool GetUndoFlag()
Definition: vtkMRMLScene.h:356