Last active
July 6, 2018 11:09
-
-
Save fepegar/bb18198c9dae273e083521db422ec5c0 to your computer and use it in GitHub Desktop.
Paste this code into 3D Slicer Python console to run this example on how to use vtkGlyph3d()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
points = vtk.vtkPoints() | |
vertices = vtk.vtkCellArray() | |
sizes = vtk.vtkFloatArray() | |
sizes.SetName('Sizes') | |
colors = vtk.vtkFloatArray() | |
colors.SetName('Colors') | |
vectors = vtk.vtkFloatArray() | |
vectors.SetNumberOfComponents(3) | |
vectors.SetName('Directions') | |
zMin, zMax = -50, 50 | |
N = 1000 | |
N += 1 | |
b = 2 | |
aMax = 20 | |
z = np.linspace(zMin, zMax, N) | |
a = (zMax - z)/zMax * aMax | |
x = a * np.sin(z / b) | |
y = a * np.cos(z / b) | |
pointsArray = np.vstack((x, y, z)).T | |
diffs = np.diff(pointsArray, axis=0) | |
indices = (z - zMin) / (zMax - zMin) * 300 | |
indices = indices.clip(0, 255).astype(np.uint8) | |
for i, vectorThisToNext in enumerate(diffs): | |
point = pointsArray[i] | |
pointID = points.InsertNextPoint(point) | |
cellID = vertices.InsertNextCell(1) | |
vertices.InsertCellPoint(pointID) | |
size = a[i]/20 | |
_ = sizes.InsertNextValue(size) | |
colorIdx = indices[i] | |
_ = colors.InsertNextValue(colorIdx) | |
_ = vectors.InsertNextTuple(vectorThisToNext) | |
pointsPolyData = vtk.vtkPolyData() | |
pointsPolyData.SetPoints(points) | |
pointsPolyData.SetVerts(vertices) | |
pointsData = pointsPolyData.GetPointData() | |
_ = pointsData.SetScalars(sizes) | |
_ = pointsData.SetVectors(vectors) | |
_ = pointsData.AddArray(colors) | |
cone = vtk.vtkConeSource() | |
cone.SetResolution(18) | |
glyphFilter = vtk.vtkGlyph3D() | |
glyphFilter.SetSourceConnection(cone.GetOutputPort()) | |
glyphFilter.SetInputData(pointsPolyData) | |
modelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode') | |
modelNode.CreateDefaultDisplayNodes() | |
modelDisplay = modelNode.GetDisplayNode() | |
modelDisplay.SetAndObserveColorNodeID('vtkMRMLColorTableNodeFileViridis.txt') | |
modelDisplay.SetScalarVisibility(True) | |
modelDisplay.SetActiveScalarName('Colors') | |
checkBox = qt.QCheckBox('Show points') | |
checkBox.setFixedWidth(120) | |
checkBox.setFixedHeight(40) | |
def update(): | |
if checkBox.isChecked(): | |
modelNode.SetAndObservePolyData(pointsPolyData) | |
else: | |
modelNode.SetPolyDataConnection(glyphFilter.GetOutputPort()) | |
update() | |
_ = checkBox.toggled.connect(update) | |
checkBox.show() | |
layoutManager = slicer.app.layoutManager() | |
layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUp3DView) | |
threeDWidget = layoutManager.threeDWidget(0) | |
threeDView = threeDWidget.threeDView() | |
threeDView.resetFocalPoint() | |
threeDController = threeDWidget.threeDController() | |
threeDController.spinView(True) | |
threeDView.setAnimationIntervalMs(25) | |
threeDView.setSpinIncrement(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment