使用Python中的VTK找到3D空间中两个圆柱的交点

2024-09-29 19:31:03 发布

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

在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和光线投射来确定相交是否发生?在

以下是两个相交的定向圆柱体: Intersected Cylinders


Tags: thetotransformmatrixactormappervtkrng
1条回答
网友
1楼 · 发布于 2024-09-29 19:31:03

首先,您需要处理vtkPolyData对象(即几何体),而不是vtkActor。您可能需要使用vtkTransformPolyDataFilter输出作为vtkPolyData(正如您在else语句中所做的那样,例如here),而不是调用setUserMatrix。在

可以使用vtkBooleanOperationPolyDataFilter:可以找到一个例子{a3}(C++中,但我确信它可以帮助)和here(在python中)。如果生成的几何体不为空,则圆柱体相交。在

如果不符合您的需要,可以使用vtkimplicitmodeler将柱面从polydata转换为imagedata(图像体积、体素);然后计算交集体积更简单、更精确(可以使用vtkImageLogic)。您还可以使用vtkFlyingEdges3D(一个快速版本的vtkMarchingCubes)将交集转换回vtkPolyData。在

编辑:正如在评论中不讨论的,因为有很多气缸执行时间是个问题。你可以试着通过计算每对圆柱的轴线之间的距离来优化这个过程,以检测它们是否相交,如果它们相交,请按照本答案第一部分所述计算相交。我的想法是:计算段之间的最短距离(一种方法是描述here,还有段到段距离的c++代码,这正是您需要的)。把这个距离和两个圆柱的半径之和比较,如果它短一些,就计算交点。在

相关问题 更多 >

    热门问题