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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
#!/usr/bin/env python
import sys
import math
from functools import partial
import vtk
from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot
from Tkinter import Pack
VTK_DATA_ROOT = vtkGetDataRoot()
import Tkinter
#from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor
from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
from vtk.tk.vtkTkImageViewerWidget import vtkTkImageViewerWidget
# Tkinter constants.
E = Tkinter.E
W = Tkinter.W
N = Tkinter.N
S = Tkinter.S
HORIZONTAL = Tkinter.HORIZONTAL
VERTICAL = Tkinter.VERTICAL
RIGHT = Tkinter.RIGHT
LEFT = Tkinter.LEFT
TOP = Tkinter.TOP
BOTTOM = Tkinter.BOTTOM
X = Tkinter.X
BOTH = Tkinter.BOTH
NO = Tkinter.NO
YES = Tkinter.YES
NORMAL = Tkinter.NORMAL
DISABLED = Tkinter.DISABLED
TRUE = Tkinter.TRUE
FALSE = Tkinter.FALSE
GROOVE = Tkinter.GROOVE
INSERT = Tkinter.INSERT
END = Tkinter.END
class TestTextActor3DViewer(Testing.vtkTest):
'''
Provide a testing framework for for TestTextActor3D.
Note:
root, the top-level widget for Tk,
tkrw, the vtkTkRenderWidget and
renWin, the vtkRenderindow
are accessible from any function in this class
after SetUp() has run.
'''
def SetUp(self):
'''
Set up cursor3D
'''
def OnClosing():
self.root.quit()
def AddSphere(ren):
objSource = vtk.vtkSphereSource()
objMapper = vtk.vtkPolyDataMapper()
objMapper.SetInputConnection(objSource.GetOutputPort())
objActor = vtk.vtkActor()
objActor.SetMapper(objMapper)
objActor.GetProperty().SetRepresentationToWireframe()
print "Sphere:", objActor.GetOrigin()
ren.AddActor(objActor)
def AddOneTextActor(baseTextProp):
name = "ia"
self.textActors[name] = vtk.vtkTextActor3D()
# This adjustment is needed to reduce the difference
# between the Tcl and Python versions.
self.textActors[name].SetOrigin(0, -0.127878, 0)
tprop = self.textActors[name].GetTextProperty()
tprop.ShallowCopy(baseTextProp)
tprop.SetColor(1,0,0)
# Add many text actors.
def AddManyTextActors(baseTextProp):
lut = vtk.vtkColorTransferFunction()
lut.SetColorSpaceToHSV()
lut.AddRGBPoint(0.0, 0.0, 1.0, 1.0)
lut.AddRGBPoint(1.0, 1.0, 1.0, 1.0)
for i in range(0, 10):
name = "ia" + str(i)
self.textActors[name] = vtk.vtkTextActor3D()
self.textActors[name].SetOrientation(0, i*36, 0)
#self.textActors[name].SetPosition(math.cos(i * 0.0314), 0, 0)
# This adjustment is needed to reduce the diffierence
# between the Tcl and Python versions.
self.textActors[name].SetOrigin(0, -0.127878, 0)
tprop = self.textActors[name].GetTextProperty()
tprop.ShallowCopy(baseTextProp)
value = i / 10.0
tprop.SetColor(lut.GetColor(value))
del lut
# Update all text actors
def UpdateTextActors(event):
orientation = self.scaleOrientation.get()
fontSize = self.scaleFontSize.get()
scale = self.scaleScale.get() / 10000.0
text = self.entryText.get("1.0",'end+1c')
opacity = self.scaleOpacity.get()
for actor in self.textActors.values():
actor.SetScale(scale)
actor.SetInput(text)
tprop = actor.GetTextProperty()
tprop.SetFontSize(fontSize)
tprop.SetOrientation(orientation)
tprop.SetOpacity(opacity)
self.renWin.Render()
ren = vtk.vtkRenderer()
ren.SetBackground(0.1, 0.2, 0.4)
self.renWin = vtk.vtkRenderWindow()
self.renWin.AddRenderer(ren)
#self.renWin.SetSize(600, 600)
self.root = Tkinter.Tk()
self.root.title("TestTextActor3D.py")
# Define what to do when the user explicitly closes a window.
self.root.protocol("WM_DELETE_WINDOW", OnClosing)
# The Tk render widget.
self.tkrw = vtkTkRenderWidget(
self.root, width=450, height=450, rw=self.renWin)
self.tkrw.BindTkRenderWidget()
#self.renWin.GetInteractor().GetInteractorStyle().SetCurrentStyleToTrackballCamera()
self.tkrw.pack(side=LEFT, fill=BOTH, expand=YES)
# Base text property
baseTextProp = vtk.vtkTextProperty()
baseTextProp.SetFontSize(48)
baseTextProp.ShadowOn()
baseTextProp.SetColor(1.0, 0.0, 0.0)
baseTextProp.SetFontFamilyToArial()
baseScale = 0.0025
baseText = "This is a test"
# The text actors
self.textActors = dict()
scaleLength = 200
controls = Tkinter.Frame(self.root, relief=GROOVE, bd=2)
controls.pack(
padx=2, pady=2, anchor=N+W, side=LEFT, fill=BOTH, expand=NO)
# Add control of text.
self.entryText = Tkinter.Text(controls, height=1, width=25)
self.entryText.insert(INSERT,baseText)
self.entryText.pack(padx=4, pady=4, side=TOP, fill=X, expand=NO)
self.entryText.bind('<Return>',UpdateTextActors)
self.entryText.bind('<FocusOut>',UpdateTextActors)
# Add control of orientation.
self.scaleOrientation = Tkinter.Scale(controls,
from_=0, to=360, res=1,
length= scaleLength,
orient=HORIZONTAL,
label="Text orientation:",
command=UpdateTextActors)
self.scaleOrientation.set(baseTextProp.GetOrientation())
self.scaleOrientation.pack(side=TOP, fill=X, expand=NO)
# Add control of font size.
self.scaleFontSize = Tkinter.Scale(controls,
from_=5, to=150, res=1,
length= scaleLength,
orient=HORIZONTAL,
label="Font Size:",
command=UpdateTextActors)
self.scaleFontSize.set(baseTextProp.GetFontSize())
self.scaleFontSize.pack(side=TOP, fill=X, expand=NO)
# Add control of scale.
self.scaleScale = Tkinter.Scale(controls,
from_=0, to=100, res=1,
length= scaleLength,
orient=HORIZONTAL,
label="Actor scale:",
command=UpdateTextActors)
self.scaleScale.set(baseScale * 10000.0)
self.scaleScale.pack(side=TOP, fill=X, expand=NO)
# Add control of scale.
self.scaleOpacity = Tkinter.Scale(controls,
from_=0, to=1.0, res=0.01,
length= scaleLength,
orient=HORIZONTAL,
label="Text opacity:",
command=UpdateTextActors)
self.scaleOpacity.set(baseTextProp.GetOpacity())
self.scaleOpacity.pack(side=TOP, fill=X, expand=NO)
# Create and add all the text actors.
if False:
AddSphere(ren)
AddOneTextActor(baseTextProp)
ren.ResetCamera()
else:
AddManyTextActors(baseTextProp)
ren.ResetCamera()
ren.GetActiveCamera().Elevation(30.0)
ren.GetActiveCamera().Dolly(0.40)
UpdateTextActors(0)
for actor in self.textActors.itervalues():
ren.AddActor(actor)
def DoIt(self):
self.SetUp()
self.renWin.Render()
self.tkrw.Render()
self.root.update()
# If you want to interact and use the sliders etc,
# uncomment the following line.
#self.root.mainloop()
img_file = "TestTextActor3D.png"
Testing.compareImage(self.renWin, Testing.getAbsImagePath(img_file))
Testing.interact()
if __name__ == '__main__':
cases = [(TestTextActor3DViewer, 'DoIt')]
del TestTextActor3DViewer
Testing.main(cases)
|