社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

用python中的vtk求三维空间中两圆柱的交集

shahriar • 5 年前 • 1892 次点击  

在python中使用vtk,我编写了一些代码来为我想要的对象创建一个actor,例如对于cylinder:

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

这个函数返回一个我稍后可以使用它的演员 vtkRender . 现在我想先找出两个给定的圆柱体 Actors 是否相交,然后找到相交点。 我能用一下 vtkTriangleFilter 在我的汽缸上用 vtkOBBTree 光线投射来确定交叉点是否发生?

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43464
 
1892 次点击  
文章 [ 1 ]  |  最新文章 5 年前
L.C.
Reply   •   1 楼
L.C.    6 年前

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

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

如果它不适合你的需要,你可以使用VTKMyPysiMeDeleLeLink将圆柱体从多数据转换成Image DATA(图像体积,体素);然后计算交叉体体积更容易和更准确(你可以使用)。 vtkImageLogic )还可以将交集转换为VTKPoDeDATA。 vtkFlyingEdges3D (快速版本的 vtkMarchingCubes )

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