三维vtk对象沿方向向量对齐后将发生移动

2024-10-02 22:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个vtk多边形数据对象,大小相等,我想沿着规范化方向向量中心对齐,如图A所示。下面的矢量是基于三个3D点(图上的红点)使用SVD(符合最佳拟合意义)计算的。这三个点的平均值用于将3D vtk对象放置在彼此的顶部

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

我的问题是:在使用vtkTransform()沿方向向量旋转/对齐所有对象后,它们在x方向上从下向上逐渐移动,因此不以向量为中心,如图B所示。谢谢你们给我的任何有用的建议,我在这里错过了什么


Tags: 对象nptransform方向向量readeractormapper
1条回答
网友
1楼 · 发布于 2024-10-02 22:31:12

我不确定,但在我看来,这“惊人”是意料之中的:

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

相关问题 更多 >