0

I have several vtk poly data objects, equal in size that I want to align along a normalized direction vector centrically, shown on figure A. The vector below is calculated based on the three 3D points (red dots on figures) using SVD (in line of best fit sense). The mean of that three points is used to place the 3D vtk objects top of each other.

reader = vtk.vtkPLYReader()
reader.SetFileName(filePath)

# Align the read poly data along the direction vector. 
originPointMean = np.array([-13.7071, -160.8437, 1317.6533])
directionVector = np.array([-0.1134, -0.0695, 0.9911])
initAxis = [0, 0, 1]  # old object's axis

crossVec = np.cross(initAxis, directionVector) # Calc axis of rotation
angle = np.arccos(np.dot(initAxis, directionVector)) # Calc rotation angle

transform = vtk.vtkTransform()
transform.RotateWXYZ(-90, 0, 0, 1) #An initial rotation around Z axis  
transform.RotateWXYZ(np.rad2deg(angle), crossVec) # Rotate it along direction vector

transformFilter = vtk.vtkTransformPolyDataFilter()
transformFilter.SetTransform(transform)
transformFilter.SetInputConnection(reader.GetOutputPort())

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(transformFilter.GetOutputPort())
mapper.ScalarVisibilityOn()

actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetOpacity(1.0)
actor.SetPosition(originPointMean [0], originPointMean [1], originPointMean [2] - 32) # Add some distance between objects in Z axis 

renderer.AddActor(actor)

enter image description here

My problem is: after rotating/aligning all the objects along the direction vector using vtkTransform() they are shifted in x direction increasingly from bottom to top and therefore not centered on the vector as shown on figure B. Thank you for the any helpful advice, what I am missing here.

mystic.06
  • 189
  • 8

1 Answers1

0

I'm not sure but it looks to me that this "staggering" is expected:

from vedo import *
import numpy as np

c1 = Cylinder(height=0.3).z(0).c('red')
c2 = Cylinder(height=0.3).z(1).c('green')
c3 = Cylinder(height=0.3).z(2).c('blue')

p = [3,0,0]
v = [1,1,1]
items = [c1,c2,c3]
for it in list(items):
    itc = it.clone().shift(p).orientation(v)
    items.append(itc)

group = (c1+c2+c3).shift(p).orientation(v)
show([items, group], N=2, axes=1, sharecam=False)

enter image description here

mmusy
  • 1,292
  • 9
  • 10
  • Yes, that shift is expected, since the direction vector is oblique in one direction, so that the centered object on that is shifted. What I don't understand ist, how this happens although the objects are in the same size and they are placed centrically around the vector. I guess somehow I need to find a ratio for that shift, so that I can place the objects based on that ratio. – mystic.06 Jun 08 '21 at 11:57
  • sorry - I cannot see any extra displacement apart from the expected one.. – mmusy Jun 09 '21 at 09:23