[vtkusers] UserTransform?

Tim Newsham newsham at lava.net
Fri Sep 12 14:52:10 EDT 2003


Hi,

    I am trying to play with the vtkCamera's UserTransform.  Unfortunately
its not doing what I expect it to do.  When I transform my points
manually I get a completely different result than when I set the
camera's UserTransform to the same matrix.  I expect the results to
be slightly different (user transform happens after perspective
transformation, right?), but shouldnt the result be similar at least?

Example code is attached, run it with and without -u and use the arrow
and page up/page down keys to affect the tranform.  When I ommit -u
it works as expected, when I use -u I dont see any transformation but
there is some weird clipping going on.

Tim N.


#!/usr/bin/python

from getopt import getopt
import sys
import math
import vtk

RANGE = 10
STEP = 3
RAD = 1
INCR = 0.005

x,y,z = 0.0,0.0,0.0

def makepolyactor(makesource, color, makeact=vtk.vtkActor) :
	src = makesource()
	map = vtk.vtkPolyDataMapper()
	map.SetInput(src.GetOutput())
	act = makeact()
	act.SetMapper(map)
	act.GetProperty().SetColor(color)
	return src,map,act
	
def makeball(rad, pos, color, makeact=vtk.vtkActor) :
	res = 8
	b,bm,ba = makepolyactor(vtk.vtkSphereSource, color, makeact)
	b.SetThetaResolution(res)
	b.SetPhiResolution(res)
	b.SetRadius(rad)
	ba.SetPosition(pos)
	return b,bm,ba

def onKey(obj, evstr) :
	global x,y,z
	key = obj.GetInteractor().GetKeySym()
	if key == "Up" :
		y += INCR
	elif key == "Down" :
		y -= INCR
	elif key == "Right" :
		x += INCR
	elif key == "Left" :
		x -= INCR
	elif key == "Prior" :
		z += INCR
	elif key == "Next" :
		z -= INCR
	#print key, "  ", x, y, z
	project()

# run the interactive viewer on these actors
def show(actlist) :
	global cam, renWin, transform

	ren = vtk.vtkRenderer()
	renWin = vtk.vtkRenderWindow()
	renWin.AddRenderer(ren)
	iren = vtk.vtkRenderWindowInteractor()
	iren.SetRenderWindow(renWin)
	istyle = vtk.vtkInteractorStyleTrackballCamera()
	istyle.AddObserver("KeyPressEvent", onKey)
	iren.SetInteractorStyle(istyle)

	map(ren.AddActor, actlist)
	ren.SetBackground(0,0,0)
	renWin.SetSize(400,250)
	cam = ren.GetActiveCamera()
	if utranopt :
		cam.SetUserTransform(transform)

	iren.Initialize()
	project()
	iren.Start()

def project() :
	global l,p,transform
	t = transform
	c,s = math.cosh(x), math.sinh(x)
	t.SetMatrix((c, 0, 0, s,
                 0, 1, 0, 0,
                 0, 0, 1, 0,
                 s, 0, 0, c),)
	c,s = math.cosh(y), math.sinh(y)
	t.Concatenate((1, 0, 0, 0,
                   0, c, 0, s,
                   0, 0, 1, 0,
                   0, s, 0, c),)
	c,s = math.cosh(z), math.sinh(z)
	t.Concatenate((1, 0, 0, 0,
                   0, 1, 0, 0,
                   0, 0, c, s,
                   0, 0, s, c),)

	if utranopt :
		cam.SetUserTransform(t)
	else :
		# reposition all balls according to transform of orig position
		for idx in range(len(p)) :
			p1 = p[idx]
			p2 = [0,0,0,0]
			t.MultiplyPoint(p1, p2)
			p2[0] /= p2[3]
			p2[1] /= p2[3]
			p2[2] /= p2[3]
			l[idx].SetPosition(p2[:3])
	renWin.Render()

def runit() :
	global l,p,transform
	transform = vtk.vtkTransform()
	l = []
	p = []

	t = transform
	t.RotateX(45)
	t.RotateY(45)
	t.RotateZ(45)
	for x in range(-RANGE,RANGE,STEP) :
		for y in range(-RANGE,RANGE,STEP) :
			for z in range(-RANGE,RANGE,STEP) :
				color = (0.3 + 0.5 * (z + RANGE) / RANGE, 0, 0)
				pos = x,y,z,1
				pos2 = [0,0,0,0]
				t.MultiplyPoint(pos, pos2)
				p.append(pos2)
				b,mb,ab = makeball(RAD, pos2[:3], color)
				l.append(ab)
	show(l)

def usage() :
	print "usage:  %s -u"
	sys.exit(0)

def main() :
	global utranopt
	utranopt = 0
	try :
		opts,args = getopt(sys.argv[1:], "u")
	except :
		usage()
	if args != [] :
		usage()
	for opt,optarg in opts :
		if opt == "-u" :
			utranopt = 1
	print ["Manually projecting", "Using camera user transform"][utranopt]
	runit()
	
if __name__ == "__main__" :
	main()




More information about the vtkusers mailing list