Skip to content

Instantly share code, notes, and snippets.

@jbvimort
Created December 21, 2015 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbvimort/a4401a77299eb6823bc9 to your computer and use it in GitHub Desktop.
Save jbvimort/a4401a77299eb6823bc9 to your computer and use it in GitHub Desktop.
import os
import unittest
from __main__ import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
import json
#
# dataStructureExemple
#
class dataStructureExemple(ScriptedLoadableModule):
"""Uses ScriptedLoadableModule base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = "dataStructureExemple" # TODO make this more human readable by adding spaces
self.parent.categories = ["Examples"]
self.parent.dependencies = []
self.parent.contributors = ["Jean-Baptiste Vimort (University of Michigan)"] # replace with "Firstname Lastname (Organization)"
self.parent.helpText = """
This is an example for the new data structure used in
Q3DC, Pick and Paint, Angle Planes, Easy Clip and
Surface Registration.
"""
self.parent.acknowledgementText = """
This is an example for the new data structure used in
Q3DC, Pick and Paint, Angle Planes, Easy Clip and
Surface Registration.
"""
#
# dataStructureExempleWidget
#
class dataStructureExempleWidget(ScriptedLoadableModuleWidget):
"""Uses ScriptedLoadableModuleWidget base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def setup(self):
self.logic = dataStructureExempleLogic()
ScriptedLoadableModuleWidget.setup(self)
# Instantiate and connect widgets ...
#
# Parameters Area
#
parametersCollapsibleButton = ctk.ctkCollapsibleButton()
parametersCollapsibleButton.text = "Parameters"
self.layout.addWidget(parametersCollapsibleButton)
# Layout within the dummy collapsible button
parametersFormLayout = qt.QFormLayout(parametersCollapsibleButton)
self.createMarkup = qt.QPushButton("createMarkup")
self.createMarkup.enabled = True
parametersFormLayout.addRow(self.createMarkup)
self.createMarkup.connect('clicked(bool)', self.onCreateMarkup)
self.displayAtribute = qt.QPushButton("displayAtribute")
self.displayAtribute.enabled = True
parametersFormLayout.addRow(self.displayAtribute)
self.displayAtribute.connect('clicked(bool)', self.onDisplayAtribute)
self.clearScene = qt.QPushButton("clearScene")
self.clearScene.enabled = True
parametersFormLayout.addRow(self.clearScene)
self.clearScene.connect('clicked(bool)', self.onClearScene)
# Add vertical spacer
self.layout.addStretch(1)
def onCreateMarkup(self):
landmarks = slicer.vtkMRMLMarkupsFiducialNode()
slicer.mrmlScene.AddNode(landmarks)
landmarks.AddFiducial(0, 0, 0)
landmarks.SetName("1")
landmarkDescription = dict()
for n in range(landmarks.GetNumberOfMarkups()):
markupID = landmarks.GetNthMarkupID(n)
landmarkDescription[markupID] = dict()
landmarkLabel = landmarks.GetName() + '-' + str(n + 1)
landmarkDescription[markupID]["landmarkLabel"] = landmarkLabel
landmarkDescription[markupID]["ROIradius"] = 0
landmarkDescription[markupID]["projection"] = dict()
landmarkDescription[markupID]["projection"]["isProjected"] = False
landmarkDescription[markupID]["projection"]["closestPointIndex"] = None
landmarkDescription[markupID]["midPoint"] = dict()
landmarkDescription[markupID]["midPoint"]["definedByThisMarkup"] = list()
landmarkDescription[markupID]["midPoint"]["isMidPoint"] = False
landmarkDescription[markupID]["midPoint"]["Point1"] = None
landmarkDescription[markupID]["midPoint"]["Point2"] = None
landmarks.SetAttribute("landmarkDescription",self.logic.encodeJSON(landmarkDescription))
planeDescription = dict()
landmarks.SetAttribute("planeDescription",self.logic.encodeJSON(planeDescription))
landmarks.SetAttribute("isClean",self.logic.encodeJSON({"isClean":False}))
landmarks.SetAttribute("lastTransformID",None)
def onDisplayAtribute(self):
markupsNode = slicer.mrmlScene.GetNodesByName("1").GetItemAsObject(0)
landmarkDescription = markupsNode.GetAttribute("landmarkDescription")
print "JSON string:"
print landmarkDescription
print "Data structure:"
print self.logic.decodeJSON(landmarkDescription)
def onClearScene(self):
slicer.mrmlScene.Clear(0)
#
# dumyExtenctionLogic
#
class dataStructureExempleLogic(ScriptedLoadableModuleLogic):
def encodeJSON(self, input):
encodedString = json.dumps(input)
encodedString = encodedString.replace('\"', '\'')
return encodedString
def decodeJSON(self, input):
input = input.replace('\'','\"')
return self.byteify(json.loads(input))
def byteify(self, input):
if isinstance(input, dict):
return {self.byteify(key):self.byteify(value) for key,value in input.iteritems()}
elif isinstance(input, list):
return [self.byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
class dataStructureExempleTest(ScriptedLoadableModuleTest):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment