I represent a vtk poly data object that I read with vtkPlyReader
and want to align it to given normalized direction vector, so that its orientation matches with that vector.
directionVector = np.array([-0.1134, -0.0695, 0.9911])
plyReader = vtk.vtkPLYReader()
plyReader.SetFileName(filePath)
transform = vtk.vtkTransform()
transform.RotateWXYZ(-90, 0, 0, 1) #initial rotation
transformFilter = vtk.vtkTransformPolyDataFilter()
transformFilter.SetTransform(transform)
transformFilter.SetInputConnection(plyReader.GetOutputPort())
transformFilter.Update()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(transformFilter.GetOutputPort())
mapper.ScalarVisibilityOn()
mapper.Update()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.Modified()
renderer.AddActor(actor)
I know that I should use the Rotate()
function from vtkTransform
but don't know how to align it.
You can try something like:
rotation = 0 # around new axis
initaxis = [0,0,1] # old object's axis
crossvec = np.cross(initaxis, newaxis)
angle = np.arccos(np.dot(initaxis, newaxis))
T = vtk.vtkTransform()
T.PostMultiply()
T.Translate(-pos)
if rotation:
T.RotateWXYZ(rotation, initaxis)
T.RotateWXYZ(np.rad2deg(angle), crossvec)
T.Translate(pos)
E.g. vedo uses the above
from vedo import Cube, show
c0 = Cube(side=2).lw(1)
c1 = c0.clone().alpha(0.2).c('tomato')
c1.orientation([1,1,1], rotation=20).pos([2,2,0])
show(c0, c1, axes=1)