在python中使用VTK我写了一些代码来为我想要的对象创建一个actor,例如用于气缸:
def cylinder_object(startPoint, endPoint, radius, my_color="DarkRed"):
USER_MATRIX = True
colors = vtk.vtkNamedColors()
cylinderSource = vtk.vtkCylinderSource()
cylinderSource.SetRadius(radius)
cylinderSource.SetResolution(50)
rng = vtk.vtkMinimalStandardRandomSequence()
rng.SetSeed(8775070) # For testing.8775070
# Compute a basis
normalizedX = [0] * 3
normalizedY = [0] * 3
normalizedZ = [0] * 3
# The X axis is a vector from start to end
vtk.vtkMath.Subtract(endPoint, startPoint, normalizedX)
length = vtk.vtkMath.Norm(normalizedX)
vtk.vtkMath.Normalize(normalizedX)
# The Z axis is an arbitrary vector cross X
arbitrary = [0] * 3
for i in range(0, 3):
rng.Next()
arbitrary[i] = rng.GetRangeValue(-10, 10)
vtk.vtkMath.Cross(normalizedX, arbitrary, normalizedZ)
vtk.vtkMath.Normalize(normalizedZ)
# The Y axis is Z cross X
vtk.vtkMath.Cross(normalizedZ, normalizedX, normalizedY)
matrix = vtk.vtkMatrix4x4()
# Create the direction cosine matrix
matrix.Identity()
for i in range(0, 3):
matrix.SetElement(i, 0, normalizedX[i])
matrix.SetElement(i, 1, normalizedY[i])
matrix.SetElement(i, 2, normalizedZ[i])
# Apply the transforms
transform = vtk.vtkTransform()
transform.Translate(startPoint) # translate to starting point
transform.Concatenate(matrix) # apply direction cosines
transform.RotateZ(-90.0) # align cylinder to x axis
transform.Scale(1.0, length, 1.0) # scale along the height vector
transform.Translate(0, .5, 0) # translate to start of cylinder
# Transform the polydata
transformPD = vtk.vtkTransformPolyDataFilter()
transformPD.SetTransform(transform)
transformPD.SetInputConnection(cylinderSource.GetOutputPort())
# Create a mapper and actor for the arrow
mapper = vtk.vtkPolyDataMapper()
actor = vtk.vtkActor()
if USER_MATRIX:
mapper.SetInputConnection(cylinderSource.GetOutputPort())
actor.SetUserMatrix(transform.GetMatrix())
else:
mapper.SetInputConnection(transformPD.GetOutputPort())
actor.SetMapper(mapper)
actor.GetProperty().SetColor(colors.GetColor3d(my_color))
return actor
这个函数返回一个actor,我可以稍后使用vtkRender
渲染它。现在我想要的是首先找到两个给定的圆柱体Actors
是否相交,第二个找到交叉点。我可以在我的圆柱上使用vtkTriangleFilter
并使用vtkOBBTree
和光线投射来查找交叉点是否发生?
首先,您需要处理vtkPolyData对象(即几何体),而不是vtkActor。您可能需要使用vtkTransformPolyDataFilter输出作为vtkPolyData(就像在else语句中所做的那样 - 示例here)而不是调用setUserMatrix。
你可以使用vtkBooleanOperationPolyDataFilter:一个例子可以找到here(在C ++中,但我相信它可以帮助)和here(在Python中)。如果生成的几何体不为空,则圆柱相交。
如果它不符合您的需要,您可以使用vtkImplicitModeller将圆柱体从polydata转换为imagedata(图像体积,体素);然后计算交叉点体积更容易,更准确(你可以使用vtkImageLogic)。您还可以使用vtkFlyingEdges3D(vtkMarchingCubes的快速版本)将交集转换回vtkPolyData。
编辑:在评论中讨论,因为有很多柱面执行时间是一个问题。您可以尝试通过计算每对圆柱的轴之间的距离来检测它们相交的IF,并且如果它们相同,则可以尝试优化该过程,如本答案第一部分所述计算交点。我的想法如下:计算段之间的最短距离(一种方法被描述为here,还有段到段距离的c ++代码,这就是你需要的)。将距离与两个圆柱体的半径之和进行比较,如果它更短,则计算交点。