3D Slicer Interactive Modules Steve Pieper, PhD NA-MIC

Download Report

Transcript 3D Slicer Interactive Modules Steve Pieper, PhD NA-MIC

NA-MIC
National Alliance for Medical Image Computing
http://na-mic.org
3D Slicer
Interactive Modules
Steve Pieper, PhD
Review of Module Types
•
•
•
•
Built-In Modules
– Few Core Features Linked to Slicer3.cxx
– Non-Core Modules becoming Loadable
Loadable Modules
– Dynamically Discovered and Loaded
– Can Access Application’s MRML Scene Instance and Call
SlicerBaseGUI and Related APIs (vtkRendering, KWWidgets…)
Scripted Modules
– Like Loadable, but Written in Tcl or Python
Command Line Modules
– Can Run as Stand Alone Executables
– Provide Standard Command Line Parsing, which Allows Slicer to
Generate GUI Automatically
– Can be Compiled as Shared Library for Dynamic Link to Slicer
2
National Alliance for Medical Image Computing
http://na-mic.org
This Session
• Discuss Implementation of Loadable and Scripted Modules
– Module Architecture
– Implementation Options
– Issues to be Consider
• Example Interactive Modules
• Documentation:
•
http://www.slicer.org/slicerWiki/index.php/Building_a_Slicer_3_Module_GUI
• (Linked from the developer section of slicer.org)
3
National Alliance for Medical Image Computing
http://na-mic.org
Why Interactve Modules
• Continuous Feedback
– Perform Calculation while Adjusting a Value
– Responding to Render Window Events (e.g. 3D Widgets)
• Periodic Updates / Animations
• Injecting New Data
– Connecting to Devices (e.g. OpenIGTLink)
– Connecting to Servers (e.g. QueryAtlas, FetchMI)
• Defining New Data Types and Storing State
– New MRML Nodes can be Displayable (e.g. Volume or Model
Subclasses)
– Interaction State in Nodes is Serialized in MRML Scene
• Define Multi-Step Workflows (e.g. EMSegmenter)
4
National Alliance for Medical Image Computing
http://na-mic.org
C++ Superclasses
• Modules Inherit Much of their Behavior
– Look and Feel
– Application Settings
– Common API, Wrapping
http://www.na-mic.org/Slicer/Documentation/Slicer3/html/index.html
5
National Alliance for Medical Image Computing
http://na-mic.org
Classes to Implement
• Subclasses of:
– vtkMRMLNode
– vtkSlicerLogic
– vtkSlicerModuleGUI
• Default Behavior is Often
Adequate
• Follows VTK Conventions
for Object Management
and Event Handling
• Use KWWidgets to Define
GUI
6
National Alliance for Medical Image Computing
http://na-mic.org
Constructor/Destructor
• Allocate Widgets in
Constructor
• In Destructor, Set Parent
to NULL to Avoid
Reference Deadlock
(and Leaks)
• Constructor Called at
System Start
• Enter / Exit
– Called when Module
Changes
– Can be used to
Create GUI on the
Fly
7
National Alliance for Medical Image Computing
http://na-mic.org
Build GUI / TearDownGUI
• Initialize the Module
• Construct the Widgets and
Layout of the Page
• Rely on Slicer-Custom
KWWidgets like the
vtkSlicerNodeSelectorWidget
• TearDownGUI (Optional)
Unpacks
8
National Alliance for Medical Image Computing
http://na-mic.org
Processing Events
• AddGUIObservers / RemoveGUIObservers
– List Events to Watch
– Inherited Callback
Function Calls
ProcessGUIEvents
• ProcessGUIEvents
– Maps Events
To Actions
9
National Alliance for Medical Image Computing
http://na-mic.org
Manipulating MRML
• Modules Should Save Parameter State in MRML Nodes
– Supports Undo/Redo and Serialization of Parameters to Scene
File
• Use of Node Selector Widgets Simplifies Input/Output
– Can be Configured to Allow Creation of New Output Nodes or
Select Existing Nodes to Overwrite
• Typically there is a Logic Method to Apply Parameters using a VTK
Pipeline
– Parameters Copied from MRML Node to Processing Classes
– End of Pipeline Connected to MRML Volume or Model Node
10
National Alliance for Medical Image Computing
http://na-mic.org
Coordinate Issues
• MRML Volume Nodes
– Track the IJKtoRAS Transformation Explicitly
• Map from Pixel Space to Patient Space in Millimeters
– Hold Data in vtkImageData Instance Variable
• Origin is 0,0,0
• Spacing is 1,1,1
– vtkImageChangeInformation Filter is Sometimes Needed
• MRML Model Nodes
– Vertices are in RAS Millimeters
• Optionally Copy the TransformNodeID to Put Output Node in Same
Spot of Transformation Hierarchy as Input Node
11
National Alliance for Medical Image Computing
http://na-mic.org
Event Issues
• Avoid Event Loops
– VTK Events are Handled Synchronously (on the Stack)
• Modifed Event from Node May Cause GUI to Update
• GUI Update May Trigger Second Call to ProcessGUIEvents
– Can Use Guard Variables to Only Enter ProcessGUIEvents
Once
– Can Perform Atomic Updates
node->DisableModifiedEventOn();
/* set a series of parameter values */
node->DisableModifiedEventOff();
node->InvokePendingModifiedEvent();
12
National Alliance for Medical Image Computing
http://na-mic.org
Calling Other Modules
• Interactive Modules
– Draw on the Public API of the MRML Nodes and Logic
• Command Line Modules
– Construct Appropriate Parameter Node
– Invoke Module in Separate Thread
– Observe Scene Events to Check for Completion Status
13
National Alliance for Medical Image Computing
http://na-mic.org
Scripted Modules
• Map C++ Methods to Script Code
• Parameter Node Provides Keyword / Value Pair Serialization
• Code Can Often Be Reloaded
– Don't Need to Exit Slicer or Reload Data
– Can Be Significant Time Savings
• Slicer3/Modules/ScriptedModuleExample
– in Slicer3 svn trunk, not 3.2 or Stanford branches
– ScriptedModuleExampleClone Procedure
• Creates All Files Needed for New Module
14
National Alliance for Medical Image Computing
http://na-mic.org
Example Modules
• Modules/GradientAnisotropicDiffusion (a.k.a GAD) is "Prototype"
Module to Review
• Slicer3/Modules/Editor
– Complex Module with Many Features
– Interacts with MRML, and Volumes
– Applies Filter Operations
– Provides Real-Time Feedback to Mouse Events
• Available if Python Enabled in your Slicer3 Build
– PythonGADScriptedModule.py
– Modules/Python has Several Examples
15
National Alliance for Medical Image Computing
http://na-mic.org
Module Considerations
• CommandLineModule is an Interactive Module that Wraps and
Manages the Execution Model
• Command Line Modules
– Simpler to Program
• Pure ITK
• Any Executable (even MATLAB Wrapped with Shell Script)
– Can Run in Separate Thread
– Can be Packaged as Executable or Shared Library
– Can Used in Shell Scripts or Distributed Applications
• Interactive Modules Require Greater Effort but have More Flexibility
16
National Alliance for Medical Image Computing
http://na-mic.org