Reputation: 63
I am new to python and VTK so please have patience :)
On short, I have an actor and I would like to rotate it about a line I drew using vtkLineSource(). I chose my start and end points and I drew the line. Now what I thought is (please correct me if I am wrong) that I should make an axis on the line and then apply the vtkTransformWXYZ() to rotate about the axis. It does not work, it gives a weird rotation about the chosen point, but not the one I desire. I also tried defining the axis on the middle of the line I drew and apply the rotation on it, but when I try, it rotates about the global coordinates, and not the local. I also tried to give as input the point or the line, but again, the rotation is very weird.
Is there a way to define the line as an axis and rotate the actor about it? So far I tried like in the following examples: https://lorensen.github.io/VTKExamples/site/Python/PolyData/RotationAroundLine/ and https://lorensen.github.io/VTKExamples/site/Python/Rendering/Rotations/ but my actor always rotates in a weird way.
Could anyone help/point me in the right direction please?
Here is the part of my code where I am trying to rotate the actor…
###################### create line to rotate about and display it
lineStart = [16.8879, -106.476, -782.449]
lineFinish = [-17.827, -92.2757, lineStart[2]]
lineMiddle = [(lineStart[0]+lineFinish[0])/2, (lineStart[1]+lineFinish[1])/2, lineStart[2]]
lineSource = vtk.vtkLineSource()
lineSource.SetPoint1(lineStart)
lineSource.SetPoint2(lineFinish)
lineSource.Update()
mapperLine = vtk.vtkPolyDataMapper()
mapperLine.SetInputConnection(lineSource.GetOutputPort())
actorLine = vtk.vtkActor()
actorLine.SetMapper(mapperLine)
actorLine.GetProperty().SetLineWidth(4)
actorLine.GetProperty().SetColor(1,0,0)
ren.AddActor(actorLine)
############# rotate about the line
modelMapper = vtk.vtkPolyDataMapper()
modelMapper.SetInputData(cleanFilter.GetOutput())
modelActor = vtk.vtkActor()
modelActor.SetMapper(modelMapper)
modelAxesSource = vtk.vtkAxes()
modelAxesSource.SetScaleFactor(100)
modelAxesSource.SetOrigin(lineMiddle)
modelAxesMapper = vtk.vtkPolyDataMapper()
modelAxesMapper.SetInputConnection(modelAxesSource.GetOutputPort())
modelAxes = vtk.vtkActor()
modelAxes.SetMapper(modelAxesMapper)
ren.AddActor(modelAxes)
modelAxes.VisibilityOn()
##this did not work
##modelActor.SetOrientation(lineMiddle)
##modelActor.RotateZ(45)
##ren.AddActor(modelActor)
transform = vtk.vtkTransform()
transform.RotateWXYZ(45, lineMiddle)
transformFilter = vtk.vtkTransformPolyDataFilter()
transformFilter.SetTransform(transform)
transformFilter.SetInputConnection(cleanFilter.GetOutputPort())
transformFilter.Update()
NewMapper = vtk.vtkPolyDataMapper()
NewMapper.SetInputConnection(transformFilter.GetOutputPort())
actorRotated = vtk.vtkActor()
actorRotated.SetMapper(NewMapper)
ren.AddActor(actorRotated)
I have also tried the following:
rotate = vtk.vtkRotationFilter()
rotate.SetInputConnection(cleanFilter.GetOutputPort())
rotate.SetAxisToY()
rotate.SetCenter(lineMiddle)
rotate.SetAngle(45)
mapper = vtk.vtkDataSetMapper()
mapper.SetInputConnection(rotate.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
ren.AddActor(actor)
but it does not display anything.
Any help is apreciated!
Thanks in advance, Diana
Upvotes: 0
Views: 1624
Reputation: 1337
You may need to write some function like this:
def rotate(obj, angle, axis=(1, 0, 0), axis_point=(0, 0, 0), rad=False):
"""Rotate around an arbitrary `axis` passing through `axis_point`."""
if rad:
anglerad = angle
else:
anglerad = np.deg2rad(angle)
axis = utils.versor(axis)
a = np.cos(anglerad / 2)
b, c, d = -axis * np.sin(anglerad / 2)
aa, bb, cc, dd = a * a, b * b, c * c, d * d
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
R = np.array(
[
[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
[2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
[2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc],
]
)
rv = np.dot(R, obj.GetPosition() - np.array(axis_point)) + axis_point
if rad:
angle *= 180.0 / np.pi
# this vtk method only rotates in the origin of the object:
obj.RotateWXYZ(angle, axis[0], axis[1], axis[2])
pbj.SetPosition(rv)
testing it using vedo:
from vedo import *
c1 = Cube() # vtkActor
c2 = c1.clone().c('violet').alpha(0.5) # make a clone copy
v = vector(0.2,1,0)
p = vector(1,0,0)
c2.rotate(90, axis=v, point=p)
l = Line(-v+p, v+p).lw(3).c('red') # vtkActor
show(c1, c2, l, axes=1)
Upvotes: 1