1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#!/usr/bin/env python
import sys
import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
def affine(val, inmin, inmax, outmin, outmax):
return (((val - inmin) / (inmax - inmin)) * (outmax - outmin)) + outmin
def buildSpiralPolyDataItem(drawOpts):
xmin, xmax, ymin, ymax = drawOpts['vp']
color = drawOpts['color']
lineWidth = drawOpts['lineWidth']
stippleType = drawOpts['stippleType']
spiralPoints = [
[0.1, 0.1],
[0.1, 0.9],
[0.9, 0.9],
[0.9, 0.2],
[0.2, 0.2],
[0.2, 0.8],
[0.8, 0.8],
[0.8, 0.3],
[0.3, 0.3],
[0.3, 0.7],
[0.7, 0.7],
[0.7, 0.4],
[0.4, 0.4],
[0.4, 0.6],
[0.6, 0.6],
[0.6, 0.5]
]
xformedPoints = [[
affine(p[0], 0.0, 1.0, xmin, xmax),
affine(p[1], 0.0, 1.0, ymin, ymax),
0.0
] for p in spiralPoints]
pd = vtk.vtkPolyData()
pd.Allocate(50)
pts = vtk.vtkPoints()
for pt in xformedPoints:
pts.InsertNextPoint(pt)
pd.SetPoints(pts)
numpts = len(xformedPoints)
pd.InsertNextCell(vtk.VTK_POLY_LINE, numpts, [idx for idx in range(numpts)])
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(4)
colors.SetNumberOfTuples(1)
colors.SetTuple4(0, color[0], color[1], color[2], 255)
# Specify line width
floatValue = vtk.vtkFloatArray()
floatValue.SetNumberOfComponents(1)
floatValue.SetName("LineWidth")
floatValue.InsertNextValue(lineWidth)
pd.GetFieldData().AddArray(floatValue)
# Specify stipple pattern
intValue = vtk.vtkIntArray()
intValue.SetNumberOfComponents(1)
intValue.SetName("StippleType")
intValue.InsertNextValue(stippleType)
pd.GetFieldData().AddArray(intValue)
item = vtk.vtkPolyDataItem()
item.SetPolyData(pd)
item.SetMappedColors(colors)
item.SetScalarMode(vtk.VTK_SCALAR_MODE_USE_CELL_DATA)
item.SetVisible(True)
return item
polyDataItemList = [
buildSpiralPolyDataItem({
'vp': [0.0, 0.5, 0.0, 0.5],
'color': [255, 0, 0],
'lineWidth': 3.5,
'stippleType': vtk.vtkPen.DASH_DOT_DOT_LINE
}),
buildSpiralPolyDataItem({
'vp': [0.0, 0.5, 0.5, 1.0],
'color': [0, 255, 0],
'lineWidth': 2.5,
'stippleType': vtk.vtkPen.DASH_LINE
}),
buildSpiralPolyDataItem({
'vp': [0.5, 1.0, 0.5, 1.0],
'color': [0, 0, 255],
'lineWidth': 1.5,
'stippleType': vtk.vtkPen.DASH_DOT_LINE
}),
buildSpiralPolyDataItem({
'vp': [0.5, 1.0, 0.0, 0.5],
'color': [255, 0, 255],
'lineWidth': 0.5,
'stippleType': vtk.vtkPen.SOLID_LINE
})
]
width = 400
height = 400
view = vtk.vtkContextView()
renWin = view.GetRenderWindow()
renWin.SetSize(width, height)
area = vtk.vtkInteractiveArea()
view.GetScene().AddItem(area)
drawAreaBounds = vtk.vtkRectd(0.0, 0.0, 1.0, 1.0)
vp = [0.0, 1.0, 0.0, 1.0]
screenGeometry = vtk.vtkRecti(int(vp[0] * width),
int(vp[2] * height),
int((vp[1] - vp[0]) * width),
int((vp[3] - vp[2]) * height))
for item in polyDataItemList:
area.GetDrawAreaItem().AddItem(item)
area.SetDrawAreaBounds(drawAreaBounds)
area.SetGeometry(screenGeometry)
area.SetFillViewport(False)
area.SetShowGrid(False)
axisLeft = area.GetAxis(vtk.vtkAxis.LEFT)
axisRight = area.GetAxis(vtk.vtkAxis.RIGHT)
axisBottom = area.GetAxis(vtk.vtkAxis.BOTTOM)
axisTop = area.GetAxis(vtk.vtkAxis.TOP)
axisTop.SetVisible(False)
axisRight.SetVisible(False)
axisLeft.SetVisible(False)
axisBottom.SetVisible(False)
axisTop.SetMargins(0, 0)
axisRight.SetMargins(0, 0)
axisLeft.SetMargins(0, 0)
axisBottom.SetMargins(0, 0)
renWin.Render()
# iren = view.GetInteractor()
# iren.Initialize()
# iren.GetInteractorStyle().SetCurrentRenderer(view.GetRenderer());
# iren.Start()
|