Slicer  4.10
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 
29 // STD includes
30 #include <list>
31 #include <map>
32 #include <vector>
33 #include <string>
34 #include <set>
35 
36 class vtkCacheManager;
37 class vtkDataIOManager;
38 class vtkTagTable;
39 
40 class vtkCallbackCommand;
41 class vtkCollection;
42 class vtkGeneralTransform;
43 class vtkURIHandler;
44 class vtkMRMLNode;
46 
51 //
54 class VTK_MRML_EXPORT vtkMRMLScene : public vtkObject
55 {
60  friend class vtkMRMLSceneViewNode;
61 
62 public:
63  static vtkMRMLScene *New();
64  vtkTypeMacro(vtkMRMLScene, vtkObject);
65  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
66 
68  void SetURL(const char *url);
69 
71  const char *GetURL();
72 
74  void SetRootDirectory(const char *dir);
75 
77  const char *GetRootDirectory();
78 
82  int Connect();
83 
90  int Import();
91 
94  int Commit(const char* url=NULL);
95 
101  void Clear(int removeSingletons=0);
102 
104  void ResetNodes();
105 
123  vtkMRMLNode* CreateNodeByClass(const char* className);
124 
145  void RegisterNodeClass(vtkMRMLNode* node, const char* tagName);
146 
151  void RegisterNodeClass(vtkMRMLNode* node);
152 
154  const char* GetClassNameByTag(const char *tagName);
155 
157  const char* GetTagByClassName(const char *className);
158 
162  void AddDefaultNode(vtkMRMLNode* node);
163  vtkMRMLNode* GetDefaultNodeByClass(const char* className);
164 
166  void RemoveAllDefaultNodes();
167 
169  vtkCollection* GetNodes();
170 
183  vtkMRMLNode* AddNode(vtkMRMLNode *nodeToAdd);
184 
200  vtkMRMLNode* AddNewNodeByClass(std::string className, std::string nodeBaseName = "");
201 
203  vtkMRMLNode* CopyNode(vtkMRMLNode *n);
204 
208  void NodeAdded(vtkMRMLNode *n);
209  void NodeAdded() {this->NodeAdded(NULL);};
210 
212  void RemoveNode(vtkMRMLNode *n);
213 
217  int IsNodePresent(vtkMRMLNode *n);
218 
226  void InitTraversal();
227 
235  vtkMRMLNode *GetNextNode();
236 
244  vtkMRMLNode *GetNextNodeByClass(const char* className);
245 
247  vtkCollection *GetNodesByName(const char* name);
248  vtkMRMLNode *GetFirstNodeByName(const char* name);
249 
261  vtkMRMLNode *GetFirstNode(const char* byName = 0, const char* byClass = 0,
262  const int* byHideFromEditors = 0,
263  bool exactNameMatch = true);
264 
266  vtkMRMLNode *GetNodeByID(const char* name);
267  vtkMRMLNode *GetNodeByID(std::string name);
268 
271  vtkCollection *GetNodesByClassByName(const char* className, const char* name);
272 
274  int GetNumberOfNodes();
275 
277  vtkMRMLNode* GetNthNode(int n);
278 
280  vtkMRMLNode* GetNthNodeByClass(int n, const char* className );
282  vtkMRMLNode* GetFirstNodeByClass(const char* className);
283 
285  int GetNumberOfNodesByClass(const char* className);
286 
288  int GetNodesByClass(const char *className, std::vector<vtkMRMLNode *> &nodes);
289 
291  vtkCollection* GetNodesByClass(const char *className);
292 
297  vtkMRMLNode* GetSingletonNode(const char* singletonTag, const char* className);
298 
305  vtkMRMLNode* GetSingletonNode(vtkMRMLNode* n);
306 
307  std::list<std::string> GetNodeClassesList();
308 
311  int GetNumberOfRegisteredNodeClasses();
314  vtkMRMLNode * GetNthRegisteredNodeClass(int n);
315 
319  bool IsNodeClassRegistered(const std::string& className);
320 
324  std::string GenerateUniqueName(const std::string& baseName);
325 
330  const char* GetUniqueNameByString(const char* baseName);
331 
333  vtkMRMLNode* InsertAfterNode( vtkMRMLNode *item, vtkMRMLNode *newItem);
335  vtkMRMLNode* InsertBeforeNode( vtkMRMLNode *item, vtkMRMLNode *newItem);
336 
338  void SetUndoOn() {UndoFlag=true;};
339  void SetUndoOff() {UndoFlag=false;};
340  bool GetUndoFlag() {return UndoFlag;};
341  void SetUndoFlag(bool flag) {UndoFlag = flag;};
342 
344  void Undo();
345 
347  void Redo();
348 
350  void ClearUndoStack();
351 
353  void ClearRedoStack();
354 
356  int GetNumberOfUndoLevels() { return (int)this->UndoStack.size();};
357 
359  int GetNumberOfRedoLevels() { return (int)this->RedoStack.size();};
360 
362  void SaveStateForUndo();
363 
365  void SaveStateForUndo(vtkMRMLNode *node);
366 
368  void SaveStateForUndo(vtkCollection *nodes);
369  void SaveStateForUndo(std::vector<vtkMRMLNode *> nodes);
370 
377  void AddReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
378  bool IsNodeReferencingNodeID(vtkMRMLNode* referencingNode, const char* id);
379 
386  vtkMRMLNode* GetNthReferencingNode(int n);
389  const char* GetNthReferencedID(int n);
390 
391  void RemoveReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
392 
393  void RemoveNodeReferences(vtkMRMLNode *node);
394 
395  void RemoveReferencesToNode(vtkMRMLNode *node);
396 
404  void UpdateNodeReferences(vtkCollection* checkNodes=NULL);
405 
406  void CopyNodeReferences(vtkMRMLScene *scene);
407 
408  void CopyNodeChangedIDs(vtkMRMLScene *scene);
409 
411  void UpdateNodeChangedIDs();
412 
413  void RemoveUnusedNodeReferences();
414 
415  bool IsReservedID(const std::string& id);
416 
417  void AddReservedID(const char *id);
418 
419  void RemoveReservedIDs();
420 
423  const char* GetChangedID(const char* id);
424 
439  vtkCollection* GetReferencedNodes(vtkMRMLNode *node, bool recursive=true);
440 
442  void GetReferencingNodes(vtkMRMLNode* referencedNode, std::vector<vtkMRMLNode *> &referencingNodes);
443 
450  void GetReferencedSubScene(vtkMRMLNode *node, vtkMRMLScene* newScene);
451 
452  int IsFilePathRelative(const char * filepath);
453 
454  vtkSetMacro(ErrorCode,unsigned long);
455  vtkGetMacro(ErrorCode,unsigned long);
456 
463  vtkSetMacro(LoadFromXMLString,int);
464  vtkGetMacro(LoadFromXMLString,int);
465 
472  vtkSetMacro(SaveToXMLString,int);
473  vtkGetMacro(SaveToXMLString,int);
474 
475  vtkSetMacro(ReadDataOnLoad,int);
476  vtkGetMacro(ReadDataOnLoad,int);
477 
478  void SetErrorMessage(const std::string &error);
479  std::string GetErrorMessage();
480 
485  void SetSceneXMLString(const std::string &xmlString);
486 
491  const std::string& GetSceneXMLString();
492 
493  void SetErrorMessage(const char * message);
494  const char *GetErrorMessagePointer();
495 
496  vtkGetObjectMacro ( CacheManager, vtkCacheManager );
497  virtual void SetCacheManager(vtkCacheManager* );
498  vtkGetObjectMacro ( DataIOManager, vtkDataIOManager );
499  virtual void SetDataIOManager(vtkDataIOManager* );
500  vtkGetObjectMacro ( URIHandlerCollection, vtkCollection );
501  virtual void SetURIHandlerCollection(vtkCollection* );
502  vtkGetObjectMacro ( UserTagTable, vtkTagTable);
503  virtual void SetUserTagTable(vtkTagTable* );
504 
509  vtkURIHandler *FindURIHandler(const char *URI);
511  vtkURIHandler *FindURIHandlerByName ( const char *name );
513  void AddURIHandler(vtkURIHandler *handler);
514 
551  {
552  BatchProcessState = 0x0001,
553  CloseState = 0x0002 | BatchProcessState,
554  ImportState = 0x0004 | BatchProcessState,
555  RestoreState = 0x0008 | BatchProcessState,
556  SaveState = 0x0010
557  };
558 
566  int GetStates()const;
567 
569  inline bool IsBatchProcessing()const;
571  inline bool IsClosing()const;
573  inline bool IsImporting()const;
575  inline bool IsRestoring()const;
576 
611  void StartState(unsigned long state, int anticipatedMaxProgress = 0);
612 
621  void EndState(unsigned long state);
622 
624  void ProgressState(unsigned long state, int progress = 0);
625 
627  {
628  NodeAboutToBeAddedEvent = 0x2000,
632 
633  NewSceneEvent = 66030,
634  MetadataAddedEvent = 66032, // ### Slicer 4.5: Simplify - Do not explicitly set for backward compat. See issue #3472
637 
641  StateEvent = 0x2000,
642  StartEvent = 0x0100,
643  EndEvent = 0x0200,
644  ProgressEvent = 0x0400,
645 
646  StartBatchProcessEvent = StateEvent | StartEvent | BatchProcessState,
647  EndBatchProcessEvent = StateEvent | EndEvent | BatchProcessState,
648  ProgressBatchProcessEvent = StateEvent | ProgressEvent | BatchProcessState,
649 
650  StartCloseEvent = StateEvent | StartEvent | CloseState,
651  EndCloseEvent = StateEvent | EndEvent | CloseState,
652  ProgressCloseEvent = StateEvent | ProgressEvent | CloseState,
653 
654  StartImportEvent = StateEvent | StartEvent | ImportState,
655  EndImportEvent = StateEvent | EndEvent | ImportState,
656  ProgressImportEvent = StateEvent | EndEvent | ImportState,
657 
658  StartRestoreEvent = StateEvent | StartEvent | RestoreState,
659  EndRestoreEvent = StateEvent | EndEvent | RestoreState,
660  ProgressRestoreEvent = StateEvent | ProgressEvent | RestoreState,
661 
662  StartSaveEvent = StateEvent | StartEvent | SaveState,
663  EndSaveEvent = StateEvent | EndEvent | SaveState,
664  ProgressSaveEvent = StateEvent | ProgressEvent | SaveState,
665  };
666 
668  vtkGetStringMacro(LastLoadedVersion);
669  vtkSetStringMacro(LastLoadedVersion);
670 
672  vtkGetStringMacro(Version);
673  vtkSetStringMacro(Version);
674 
676  void CopyRegisteredNodesToScene(vtkMRMLScene *scene);
677 
679  void CopySingletonNodesToScene(vtkMRMLScene *scene);
680 
682  void CopyDefaultNodesToScene(vtkMRMLScene *scene);
683 
686  bool GetModifiedSinceRead();
687 
694  bool GetStorableNodesModifiedSinceRead(vtkCollection* modifiedStorableNodes = 0);
695 
701  void SetStorableNodesModifiedSinceRead();
702 
705  static void SetStorableNodesModifiedSinceRead(vtkCollection* storableNodes);
706 
707 protected:
708 
709  typedef std::map< std::string, std::set<std::string> > NodeReferencesType;
710 
711  vtkMRMLScene();
712  virtual ~vtkMRMLScene();
713 
714  void PushIntoUndoStack();
715  void PushIntoRedoStack();
716 
717  void CopyNodeInUndoStack(vtkMRMLNode *node);
718  void CopyNodeInRedoStack(vtkMRMLNode *node);
719 
723  vtkMRMLNode* AddNodeNoNotify(vtkMRMLNode *n);
724 
728  void AddReferencedNodes(vtkMRMLNode *node, vtkCollection *refNodes, bool recursive=true);
729 
731  static void SceneCallback( vtkObject *caller, unsigned long eid,
732  void *clientData, void *callData );
733 
734  std::string GenerateUniqueID(vtkMRMLNode* node);
735  std::string GenerateUniqueID(const std::string& baseID);
736  int GetUniqueIDIndex(const std::string& baseID);
737  std::string BuildID(const std::string& baseID, int idIndex)const;
738 
740  std::string GenerateUniqueName(vtkMRMLNode* node);
741 
747  int GetUniqueNameIndex(const std::string& baseName);
748 
750  std::string BuildName(const std::string& baseName, int nameIndex)const;
751 
754  void UpdateNodeIDs();
755 
757  void AddNodeID(vtkMRMLNode *node);
758 
760  void RemoveNodeID(char *nodeID);
761 
763  void ClearNodeIDs();
764 
766  NodeReferencesType::iterator FindNodeReference(const char* referencedId, vtkMRMLNode* referencingNode);
767 
768  vtkCollection* Nodes;
769 
773  vtkCollection * URIHandlerCollection;
775 
776  std::vector<unsigned long> States;
777 
779  bool UndoFlag;
780  bool InUndo;
781 
782  std::list< vtkCollection* > UndoStack;
783  std::list< vtkCollection* > RedoStack;
784 
785  std::string URL;
786  std::string RootDirectory;
787 
788  std::map<std::string, int> UniqueIDs;
789  std::map<std::string, int> UniqueNames;
790  std::set<std::string> ReservedIDs;
791 
792  std::vector< vtkMRMLNode* > RegisteredNodeClasses;
793  std::vector< std::string > RegisteredNodeTags;
794 
795  NodeReferencesType NodeReferences; // ReferencedIDs (string), ReferencingNodes (node pointer)
796  std::map< std::string, std::string > ReferencedIDChanges;
797  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > NodeIDs;
798 
799  // Stores default nodes. If a class is created or reset (using CreateNodeByClass or Clear) and
800  // a default node is defined for it then the content of the default node will be used to initialize
801  // the class. It is useful for overriding default values that are set in a node's constructor.
802  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > DefaultNodes;
803 
804  std::string ErrorMessage;
805 
806  std::string SceneXMLString;
807 
809 
811 
813 
814  vtkMTimeType NodeIDsMTime;
815 
816  void RemoveAllNodes(bool removeSingletons);
817 
818  char * Version;
820 
821  vtkCallbackCommand *DeleteEventCallback;
822 
823 private:
824 
825  vtkMRMLScene(const vtkMRMLScene&); // Not implemented
826  void operator=(const vtkMRMLScene&); // Not implemented
827 
829  int LoadIntoScene(vtkCollection* scene);
830 
831  unsigned long ErrorCode;
832 
834  vtkTimeStamp StoredTime;
835 };
836 
837 //------------------------------------------------------------------------------
839 {
840  return (this->GetStates() & vtkMRMLScene::BatchProcessState)
842 }
843 
844 //------------------------------------------------------------------------------
846 {
847  return (this->GetStates() & vtkMRMLScene::CloseState)
849 }
850 
851 //------------------------------------------------------------------------------
853 {
854  return (this->GetStates() & vtkMRMLScene::ImportState)
856 }
857 
858 //------------------------------------------------------------------------------
860 {
861  return (this->GetStates() & vtkMRMLScene::RestoreState)
863 }
864 
865 #endif
int GetNodesByClass(const char *className, std::vector< vtkMRMLNode *> &nodes)
void NodeAdded()
Definition: vtkMRMLScene.h:209
char * LastLoadedVersion
Definition: vtkMRMLScene.h:819
int GetNumberOfRedoLevels()
returns number of redo steps in the history buffer
Definition: vtkMRMLScene.h:359
char * Version
Definition: vtkMRMLScene.h:818
vtkCallbackCommand * DeleteEventCallback
Definition: vtkMRMLScene.h:821
bool IsImporting() const
Return true if the scene is in Import state, false otherwise.
Definition: vtkMRMLScene.h:852
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > NodeIDs
Definition: vtkMRMLScene.h:797
vtkDataIOManager * DataIOManager
Definition: vtkMRMLScene.h:772
std::string URL
Definition: vtkMRMLScene.h:785
void SetUndoFlag(bool flag)
Definition: vtkMRMLScene.h:341
vtkCollection * URIHandlerCollection
Definition: vtkMRMLScene.h:773
std::string RootDirectory
Definition: vtkMRMLScene.h:786
virtual bool GetModifiedSinceRead()
std::set< std::string > ReservedIDs
Definition: vtkMRMLScene.h:790
std::string SceneXMLString
Definition: vtkMRMLScene.h:806
int GetNumberOfUndoLevels()
returns number of undo steps in the history buffer
Definition: vtkMRMLScene.h:356
static vtkMRMLSceneViewNode * New()
void SetUndoOn()
Set undo on/off.
Definition: vtkMRMLScene.h:338
std::map< std::string, int > UniqueIDs
Definition: vtkMRMLScene.h:788
vtkCacheManager * CacheManager
data i/o handling members
Definition: vtkMRMLScene.h:771
A set of MRML Nodes that supports serialization and undo/redo.
Definition: vtkMRMLScene.h:54
vtkMTimeType NodeIDsMTime
Definition: vtkMRMLScene.h:814
std::string ErrorMessage
Definition: vtkMRMLScene.h:804
std::map< std::string, int > UniqueNames
Definition: vtkMRMLScene.h:789
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:808
NodeReferencesType NodeReferences
Definition: vtkMRMLScene.h:795
std::vector< vtkMRMLNode *> RegisteredNodeClasses
Definition: vtkMRMLScene.h:792
void SetUndoOff()
Definition: vtkMRMLScene.h:339
void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
void operator=(const vtkMRMLSceneViewNode &)
vtkCollection * Nodes
Definition: vtkMRMLScene.h:768
std::list< vtkCollection *> UndoStack
Definition: vtkMRMLScene.h:782
friend class vtkMRMLScene
Definition: vtkMRMLNode.h:142
virtual void UpdateNodeReferences(const char *referenceRole=NULL)
std::map< std::string, std::string > ReferencedIDChanges
Definition: vtkMRMLScene.h:796
bool IsClosing() const
Return true if the scene is in Close state, false otherwise.
Definition: vtkMRMLScene.h:845
std::vector< std::string > RegisteredNodeTags
Definition: vtkMRMLScene.h:793
bool IsRestoring() const
Return true if the scene is in Restore state, false otherwise.
Definition: vtkMRMLScene.h:859
Abstract Superclass for all specific types of MRML nodes.
Definition: vtkMRMLNode.h:138
std::list< vtkCollection *> RedoStack
Definition: vtkMRMLScene.h:783
vtkTagTable * UserTagTable
Definition: vtkMRMLScene.h:774
int GetStates() const
Returns the current state of the scene.
std::vector< unsigned long > States
Definition: vtkMRMLScene.h:776
bool IsBatchProcessing() const
Return true if the scene is in BatchProcess state, false otherwise.
Definition: vtkMRMLScene.h:838
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > DefaultNodes
Definition: vtkMRMLScene.h:802
std::map< std::string, std::set< std::string > > NodeReferencesType
Definition: vtkMRMLScene.h:709
bool GetUndoFlag()
Definition: vtkMRMLScene.h:340