VTK-Python 8. 德洛内三角形网

#!/usr/bin/python

import vtk
import random

points = vtk.vtkPoints()

# 随机生成一些列的点
# random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。
# 以下偏移值-0.25<=n<=0.25
for x in range(10):
    for y in range(10):
        points.InsertNextPoint(x + random.uniform(-.25, .25),
                               y + random.uniform(-.25, .25), 0)

aPolyData = vtk.vtkPolyData()
aPolyData.SetPoints(points)

aCellArray = vtk.vtkCellArray()

boundary = vtk.vtkPolyData()
boundary.SetPoints(aPolyData.GetPoints())
boundary.SetPolys(aCellArray)

# 创建德洛内对象
# 德洛内(Delaunay)三角网的定义: 它是一系列相连的但不重叠的三角形的集合, 而且这些三角形的外接圆不包含这个面域的其他任何点
delaunay = vtk.vtkDelaunay2D()
if vtk.VTK_MAJOR_VERSION <= 5:
    delaunay.SetInput(aPolyData.GetOutput())
    delaunay.SetSource(boundary)
else:
    delaunay.SetInputData(aPolyData)
    delaunay.SetSourceData(boundary)

delaunay.Update()

meshMapper = vtk.vtkPolyDataMapper()
meshMapper.SetInputConnection(delaunay.GetOutputPort())

meshActor = vtk.vtkActor()
meshActor.SetMapper(meshMapper)
meshActor.GetProperty().SetEdgeColor(0, 0, 1)
meshActor.GetProperty().SetInterpolationToFlat()
meshActor.GetProperty().SetRepresentationToWireframe()

boundaryMapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
    boundaryMapper.SetInputConnection(boundary.GetProducerPort())
else:
    boundaryMapper.SetInputData(boundary)

boundaryActor = vtk.vtkActor()
boundaryActor.SetMapper(boundaryMapper)
boundaryActor.GetProperty().SetColor(1, 0, 0)

renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(meshActor)
renderer.AddActor(boundaryActor)
renderer.SetBackground(.3, .6, .3)

renderWindowInteractor.Initialize()
renderWindow.Render()
renderWindowInteractor.Start()

VTK-Python 8. 德洛内三角形网_第1张图片

你可能感兴趣的:(VTK-Python)