VTK的扫掠(Extrusion)造型算法

2020-04-01 21:21:35|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:VTK|来源:唯设编程网

扫掠(extrusion)是一种将已有对象沿特定路径扫掠行成新对象的模型构造算法,比如我们可以将矩形平面沿法线方向扫掠构造出长方体,将圆形平面沿轴线方向扫掠构造出圆柱体。VTK支持扫掠的算法类主要包括vtkLinearExtrusionFilter和vtkRotationalExtrusionFilter,vtkLinearExtrusionFilter将polygonal数据沿着特定方向扫掠形成新的polygonal数据,vtkRotationalExtrusionFilter可以实现螺旋扫掠功能,从而构造出弹簧效果。

下面的例子先构造一个空心平面矩形,然后使用vtkLinearExtrusionFilter沿法线方向拉伸,得到最终三维几何体,程序的主要代码如下:

    points = vtk.vtkPoints()
    #外侧矩形角点
    points.InsertPoint(0, 2.0, 0.0, 1.0)
    points.InsertPoint(1, -2.0, 0.0, 1.0)
    points.InsertPoint(2, -2.0, 0.0, -1.0)
    points.InsertPoint(3, 2.0, 0.0, -1.0)
    #内测矩形角点
    points.InsertPoint(4, 1.0, 0.0, 0.5)
    points.InsertPoint(5, -1.0, 0.0, 0.5)
    points.InsertPoint(6, -1.0, 0.0, -0.5)
    points.InsertPoint(7, 1.0, 0.0, -0.5)

    poly = vtk.vtkCellArray()
    #第一个cell
    poly.InsertNextCell(4)
    poly.InsertCellPoint(0)
    poly.InsertCellPoint(1)
    poly.InsertCellPoint(5)
    poly.InsertCellPoint(4)
    # 第二个cell
    poly.InsertNextCell(4)
    poly.InsertCellPoint(5)
    poly.InsertCellPoint(1)
    poly.InsertCellPoint(2)
    poly.InsertCellPoint(6)
    # 第三个cell
    poly.InsertNextCell(4)
    poly.InsertCellPoint(6)
    poly.InsertCellPoint(2)
    poly.InsertCellPoint(3)
    poly.InsertCellPoint(7)
    # 第四个cell
    poly.InsertNextCell(4)
    poly.InsertCellPoint(7)
    poly.InsertCellPoint(3)
    poly.InsertCellPoint(0)
    poly.InsertCellPoint(4)

    profile = vtk.vtkPolyData()
    profile.SetPoints(points)
    profile.SetPolys(poly)

    #拉伸
    extrude = vtk.vtkLinearExtrusionFilter()
    extrude.SetInputData(profile)
    extrude.SetVector(0.0, 10.0, 0.0)

    normals = vtk.vtkPolyDataNormals()
    normals.SetInputConnection(extrude.GetOutputPort())
    normals.SetFeatureAngle(60)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(normals.GetOutputPort())

    spring = vtk.vtkActor()
    spring.SetMapper(mapper)
    spring.GetProperty().SetColor(0.0, 0.79, 0.34)

    ren = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)

    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())

    ren.AddActor(spring)

    renWin.Render()
    iren.Start()

最终的运行效果如下:

<123>
发表评论0条 】
网友评论(共?条评论)..
VTK的扫掠(Extrusion)造型算法