Slicer  4.8
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 
97  void Clear(int removeSingletons);
98 
100  void ResetNodes();
101 
113  vtkMRMLNode* CreateNodeByClass(const char* className);
114 
135  void RegisterNodeClass(vtkMRMLNode* node, const char* tagName);
136 
141  void RegisterNodeClass(vtkMRMLNode* node);
142 
144  const char* GetClassNameByTag(const char *tagName);
145 
147  const char* GetTagByClassName(const char *className);
148 
152  void AddDefaultNode(vtkMRMLNode* node);
153  vtkMRMLNode* GetDefaultNodeByClass(const char* className);
154 
156  void RemoveAllDefaultNodes();
157 
159  vtkCollection* GetNodes();
160 
173  vtkMRMLNode* AddNode(vtkMRMLNode *nodeToAdd);
174 
190  vtkMRMLNode* AddNewNodeByClass(std::string className, std::string nodeBaseName = "");
191 
193  vtkMRMLNode* CopyNode(vtkMRMLNode *n);
194 
198  void NodeAdded(vtkMRMLNode *n);
199  void NodeAdded() {this->NodeAdded(NULL);};
200 
202  void RemoveNode(vtkMRMLNode *n);
203 
207  int IsNodePresent(vtkMRMLNode *n);
208 
216  void InitTraversal();
217 
225  vtkMRMLNode *GetNextNode();
226 
234  vtkMRMLNode *GetNextNodeByClass(const char* className);
235 
237  vtkCollection *GetNodesByName(const char* name);
238  vtkMRMLNode *GetFirstNodeByName(const char* name);
239 
251  vtkMRMLNode *GetFirstNode(const char* byName = 0, const char* byClass = 0,
252  const int* byHideFromEditors = 0,
253  bool exactNameMatch = true);
254 
256  vtkMRMLNode *GetNodeByID(const char* name);
257  vtkMRMLNode *GetNodeByID(std::string name);
258 
261  vtkCollection *GetNodesByClassByName(const char* className, const char* name);
262 
264  int GetNumberOfNodes();
265 
267  vtkMRMLNode* GetNthNode(int n);
268 
270  vtkMRMLNode* GetNthNodeByClass(int n, const char* className );
272  vtkMRMLNode* GetFirstNodeByClass(const char* className);
273 
275  int GetNumberOfNodesByClass(const char* className);
276 
278  int GetNodesByClass(const char *className, std::vector<vtkMRMLNode *> &nodes);
279 
281  vtkCollection* GetNodesByClass(const char *className);
282 
287  vtkMRMLNode* GetSingletonNode(const char* singletonTag, const char* className);
288 
295  vtkMRMLNode* GetSingletonNode(vtkMRMLNode* n);
296 
297  std::list<std::string> GetNodeClassesList();
298 
301  int GetNumberOfRegisteredNodeClasses();
304  vtkMRMLNode * GetNthRegisteredNodeClass(int n);
305 
309  bool IsNodeClassRegistered(const std::string& className);
310 
314  std::string GenerateUniqueName(const std::string& baseName);
315 
320  const char* GetUniqueNameByString(const char* baseName);
321 
323  vtkMRMLNode* InsertAfterNode( vtkMRMLNode *item, vtkMRMLNode *newItem);
325  vtkMRMLNode* InsertBeforeNode( vtkMRMLNode *item, vtkMRMLNode *newItem);
326 
328  void SetUndoOn() {UndoFlag=true;};
329  void SetUndoOff() {UndoFlag=false;};
330  bool GetUndoFlag() {return UndoFlag;};
331  void SetUndoFlag(bool flag) {UndoFlag = flag;};
332 
334  void Undo();
335 
337  void Redo();
338 
340  void ClearUndoStack();
341 
343  void ClearRedoStack();
344 
346  int GetNumberOfUndoLevels() { return (int)this->UndoStack.size();};
347 
349  int GetNumberOfRedoLevels() { return (int)this->RedoStack.size();};
350 
352  void SaveStateForUndo();
353 
355  void SaveStateForUndo(vtkMRMLNode *node);
356 
358  void SaveStateForUndo(vtkCollection *nodes);
359  void SaveStateForUndo(std::vector<vtkMRMLNode *> nodes);
360 
367  void AddReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
368  bool IsNodeReferencingNodeID(vtkMRMLNode* referencingNode, const char* id);
369 
376  vtkMRMLNode* GetNthReferencingNode(int n);
379  const char* GetNthReferencedID(int n);
380 
381  void RemoveReferencedNodeID(const char *id, vtkMRMLNode *refrencingNode);
382 
383  void RemoveNodeReferences(vtkMRMLNode *node);
384 
385  void RemoveReferencesToNode(vtkMRMLNode *node);
386 
394  void UpdateNodeReferences(vtkCollection* checkNodes=NULL);
395 
396  void CopyNodeReferences(vtkMRMLScene *scene);
397 
398  void CopyNodeChangedIDs(vtkMRMLScene *scene);
399 
401  void UpdateNodeChangedIDs();
402 
403  void RemoveUnusedNodeReferences();
404 
405  bool IsReservedID(const std::string& id);
406 
407  void AddReservedID(const char *id);
408 
409  void RemoveReservedIDs();
410 
413  const char* GetChangedID(const char* id);
414 
426  vtkCollection* GetReferencedNodes(vtkMRMLNode *node);
427 
429  void GetReferencingNodes(vtkMRMLNode* referencedNode, std::vector<vtkMRMLNode *> &referencingNodes);
430 
437  void GetReferencedSubScene(vtkMRMLNode *node, vtkMRMLScene* newScene);
438 
439  int IsFilePathRelative(const char * filepath);
440 
441  vtkSetMacro(ErrorCode,unsigned long);
442  vtkGetMacro(ErrorCode,unsigned long);
443 
450  vtkSetMacro(LoadFromXMLString,int);
451  vtkGetMacro(LoadFromXMLString,int);
452 
459  vtkSetMacro(SaveToXMLString,int);
460  vtkGetMacro(SaveToXMLString,int);
461 
462  vtkSetMacro(ReadDataOnLoad,int);
463  vtkGetMacro(ReadDataOnLoad,int);
464 
465  void SetErrorMessage(const std::string &error);
466  std::string GetErrorMessage();
467 
472  void SetSceneXMLString(const std::string &xmlString);
473 
478  const std::string& GetSceneXMLString();
479 
480  void SetErrorMessage(const char * message);
481  const char *GetErrorMessagePointer();
482 
483  vtkGetObjectMacro ( CacheManager, vtkCacheManager );
484  virtual void SetCacheManager(vtkCacheManager* );
485  vtkGetObjectMacro ( DataIOManager, vtkDataIOManager );
486  virtual void SetDataIOManager(vtkDataIOManager* );
487  vtkGetObjectMacro ( URIHandlerCollection, vtkCollection );
488  virtual void SetURIHandlerCollection(vtkCollection* );
489  vtkGetObjectMacro ( UserTagTable, vtkTagTable);
490  virtual void SetUserTagTable(vtkTagTable* );
491 
496  vtkURIHandler *FindURIHandler(const char *URI);
498  vtkURIHandler *FindURIHandlerByName ( const char *name );
500  void AddURIHandler(vtkURIHandler *handler);
501 
538  {
539  BatchProcessState = 0x0001,
540  CloseState = 0x0002 | BatchProcessState,
541  ImportState = 0x0004 | BatchProcessState,
542  RestoreState = 0x0008 | BatchProcessState,
543  SaveState = 0x0010
544  };
545 
553  int GetStates()const;
554 
556  inline bool IsBatchProcessing()const;
558  inline bool IsClosing()const;
560  inline bool IsImporting()const;
562  inline bool IsRestoring()const;
563 
598  void StartState(unsigned long state, int anticipatedMaxProgress = 0);
599 
608  void EndState(unsigned long state);
609 
611  void ProgressState(unsigned long state, int progress = 0);
612 
614  {
615  NodeAboutToBeAddedEvent = 0x2000,
619 
620  NewSceneEvent = 66030,
621  MetadataAddedEvent = 66032, // ### Slicer 4.5: Simplify - Do not explicitly set for backward compat. See issue #3472
624 
628  StateEvent = 0x2000,
629  StartEvent = 0x0100,
630  EndEvent = 0x0200,
631  ProgressEvent = 0x0400,
632 
633  StartBatchProcessEvent = StateEvent | StartEvent | BatchProcessState,
634  EndBatchProcessEvent = StateEvent | EndEvent | BatchProcessState,
635  ProgressBatchProcessEvent = StateEvent | ProgressEvent | BatchProcessState,
636 
637  StartCloseEvent = StateEvent | StartEvent | CloseState,
638  EndCloseEvent = StateEvent | EndEvent | CloseState,
639  ProgressCloseEvent = StateEvent | ProgressEvent | CloseState,
640 
641  StartImportEvent = StateEvent | StartEvent | ImportState,
642  EndImportEvent = StateEvent | EndEvent | ImportState,
643  ProgressImportEvent = StateEvent | EndEvent | ImportState,
644 
645  StartRestoreEvent = StateEvent | StartEvent | RestoreState,
646  EndRestoreEvent = StateEvent | EndEvent | RestoreState,
647  ProgressRestoreEvent = StateEvent | ProgressEvent | RestoreState,
648 
649  StartSaveEvent = StateEvent | StartEvent | SaveState,
650  EndSaveEvent = StateEvent | EndEvent | SaveState,
651  ProgressSaveEvent = StateEvent | ProgressEvent | SaveState,
652  };
653 
655  vtkGetStringMacro(LastLoadedVersion);
656  vtkSetStringMacro(LastLoadedVersion);
657 
659  vtkGetStringMacro(Version);
660  vtkSetStringMacro(Version);
661 
663  void CopyRegisteredNodesToScene(vtkMRMLScene *scene);
664 
666  void CopySingletonNodesToScene(vtkMRMLScene *scene);
667 
669  void CopyDefaultNodesToScene(vtkMRMLScene *scene);
670 
673  bool GetModifiedSinceRead();
674 
681  bool GetStorableNodesModifiedSinceRead(vtkCollection* modifiedStorableNodes = 0);
682 
688  void SetStorableNodesModifiedSinceRead();
689 
692  static void SetStorableNodesModifiedSinceRead(vtkCollection* storableNodes);
693 
694 protected:
695 
696  typedef std::map< std::string, std::set<std::string> > NodeReferencesType;
697 
698  vtkMRMLScene();
699  virtual ~vtkMRMLScene();
700 
701  void PushIntoUndoStack();
702  void PushIntoRedoStack();
703 
704  void CopyNodeInUndoStack(vtkMRMLNode *node);
705  void CopyNodeInRedoStack(vtkMRMLNode *node);
706 
710  vtkMRMLNode* AddNodeNoNotify(vtkMRMLNode *n);
711 
712  void AddReferencedNodes(vtkMRMLNode *node, vtkCollection *refNodes);
713 
715  static void SceneCallback( vtkObject *caller, unsigned long eid,
716  void *clientData, void *callData );
717 
718  std::string GenerateUniqueID(vtkMRMLNode* node);
719  std::string GenerateUniqueID(const std::string& baseID);
720  int GetUniqueIDIndex(const std::string& baseID);
721  std::string BuildID(const std::string& baseID, int idIndex)const;
722 
724  std::string GenerateUniqueName(vtkMRMLNode* node);
725 
731  int GetUniqueNameIndex(const std::string& baseName);
732 
734  std::string BuildName(const std::string& baseName, int nameIndex)const;
735 
738  void UpdateNodeIDs();
739 
741  void AddNodeID(vtkMRMLNode *node);
742 
744  void RemoveNodeID(char *nodeID);
745 
747  void ClearNodeIDs();
748 
750  NodeReferencesType::iterator FindNodeReference(const char* referencedId, vtkMRMLNode* referencingNode);
751 
752  vtkCollection* Nodes;
753  vtkMTimeType SceneModifiedTime;
754 
758  vtkCollection * URIHandlerCollection;
760 
761  std::vector<unsigned long> States;
762 
764  bool UndoFlag;
765  bool InUndo;
766 
767  std::list< vtkCollection* > UndoStack;
768  std::list< vtkCollection* > RedoStack;
769 
770  std::string URL;
771  std::string RootDirectory;
772 
773  std::map<std::string, int> UniqueIDs;
774  std::map<std::string, int> UniqueNames;
775  std::set<std::string> ReservedIDs;
776 
777  std::vector< vtkMRMLNode* > RegisteredNodeClasses;
778  std::vector< std::string > RegisteredNodeTags;
779 
780  NodeReferencesType NodeReferences; // ReferencedIDs (string), ReferencingNodes (node pointer)
781  std::map< std::string, std::string > ReferencedIDChanges;
782  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > NodeIDs;
783 
784  // Stores default nodes. If a class is created or reset (using CreateNodeByClass or Clear) and
785  // a default node is defined for it then the content of the default node will be used to initialize
786  // the class. It is useful for overriding default values that are set in a node's constructor.
787  std::map< std::string, vtkSmartPointer<vtkMRMLNode> > DefaultNodes;
788 
789  std::string ErrorMessage;
790 
791  std::string SceneXMLString;
792 
794 
796 
798 
799  vtkMTimeType NodeIDsMTime;
800 
801  void RemoveAllNodes(bool removeSingletons);
802 
803  char * Version;
805 
806  vtkCallbackCommand *DeleteEventCallback;
807 
808 private:
809 
810  vtkMRMLScene(const vtkMRMLScene&); // Not implemented
811  void operator=(const vtkMRMLScene&); // Not implemented
812 
814  int LoadIntoScene(vtkCollection* scene);
815 
816  unsigned long ErrorCode;
817 
819  vtkTimeStamp StoredTime;
820 };
821 
822 //------------------------------------------------------------------------------
824 {
825  return (this->GetStates() & vtkMRMLScene::BatchProcessState)
827 }
828 
829 //------------------------------------------------------------------------------
831 {
832  return (this->GetStates() & vtkMRMLScene::CloseState)
834 }
835 
836 //------------------------------------------------------------------------------
838 {
839  return (this->GetStates() & vtkMRMLScene::ImportState)
841 }
842 
843 //------------------------------------------------------------------------------
845 {
846  return (this->GetStates() & vtkMRMLScene::RestoreState)
848 }
849 
850 #endif
int GetNodesByClass(const char *className, std::vector< vtkMRMLNode *> &nodes)
void NodeAdded()
Definition: vtkMRMLScene.h:199
char * LastLoadedVersion
Definition: vtkMRMLScene.h:804
int GetNumberOfRedoLevels()
returns number of redo steps in the history buffer
Definition: vtkMRMLScene.h:349
char * Version
Definition: vtkMRMLScene.h:803
vtkCallbackCommand * DeleteEventCallback
Definition: vtkMRMLScene.h:806
bool IsImporting() const
Return true if the scene is in Import state, false otherwise.
Definition: vtkMRMLScene.h:837
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > NodeIDs
Definition: vtkMRMLScene.h:782
vtkDataIOManager * DataIOManager
Definition: vtkMRMLScene.h:757
std::string URL
Definition: vtkMRMLScene.h:770
void SetUndoFlag(bool flag)
Definition: vtkMRMLScene.h:331
vtkCollection * URIHandlerCollection
Definition: vtkMRMLScene.h:758
std::string RootDirectory
Definition: vtkMRMLScene.h:771
virtual bool GetModifiedSinceRead()
std::set< std::string > ReservedIDs
Definition: vtkMRMLScene.h:775
std::string SceneXMLString
Definition: vtkMRMLScene.h:791
int GetNumberOfUndoLevels()
returns number of undo steps in the history buffer
Definition: vtkMRMLScene.h:346
static vtkMRMLSceneViewNode * New()
void SetUndoOn()
Set undo on/off.
Definition: vtkMRMLScene.h:328
std::map< std::string, int > UniqueIDs
Definition: vtkMRMLScene.h:773
vtkCacheManager * CacheManager
data i/o handling members
Definition: vtkMRMLScene.h:756
A set of MRML Nodes that supports serialization and undo/redo.
Definition: vtkMRMLScene.h:54
vtkMTimeType NodeIDsMTime
Definition: vtkMRMLScene.h:799
std::string ErrorMessage
Definition: vtkMRMLScene.h:789
std::map< std::string, int > UniqueNames
Definition: vtkMRMLScene.h:774
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:793
NodeReferencesType NodeReferences
Definition: vtkMRMLScene.h:780
std::vector< vtkMRMLNode *> RegisteredNodeClasses
Definition: vtkMRMLScene.h:777
void SetUndoOff()
Definition: vtkMRMLScene.h:329
void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
void operator=(const vtkMRMLSceneViewNode &)
vtkCollection * Nodes
Definition: vtkMRMLScene.h:752
std::list< vtkCollection *> UndoStack
Definition: vtkMRMLScene.h:767
vtkMTimeType SceneModifiedTime
Definition: vtkMRMLScene.h:753
friend class vtkMRMLScene
Definition: vtkMRMLNode.h:139
virtual void UpdateNodeReferences(const char *referenceRole=NULL)
std::map< std::string, std::string > ReferencedIDChanges
Definition: vtkMRMLScene.h:781
bool IsClosing() const
Return true if the scene is in Close state, false otherwise.
Definition: vtkMRMLScene.h:830
std::vector< std::string > RegisteredNodeTags
Definition: vtkMRMLScene.h:778
bool IsRestoring() const
Return true if the scene is in Restore state, false otherwise.
Definition: vtkMRMLScene.h:844
Abstract Superclass for all specific types of MRML nodes.
Definition: vtkMRMLNode.h:135
std::list< vtkCollection *> RedoStack
Definition: vtkMRMLScene.h:768
vtkTagTable * UserTagTable
Definition: vtkMRMLScene.h:759
std::vector< unsigned long > States
Definition: vtkMRMLScene.h:761
bool IsBatchProcessing() const
Return true if the scene is in BatchProcess state, false otherwise.
Definition: vtkMRMLScene.h:823
std::map< std::string, vtkSmartPointer< vtkMRMLNode > > DefaultNodes
Definition: vtkMRMLScene.h:787
std::map< std::string, std::set< std::string > > NodeReferencesType
Definition: vtkMRMLScene.h:696
bool GetUndoFlag()
Definition: vtkMRMLScene.h:330