一:主要的知识点
1、说明
本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①围绕某个轴旋转进行模型生成
二:代码及注释
import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkCommonCore import vtkPoints from vtkmodules.vtkCommonDataModel import vtkCellArray, vtkPolyData from vtkmodules.vtkFiltersCore import vtkStripper, vtkTubeFilter from vtkmodules.vtkFiltersModeling import vtkRotationalExtrusionFilter from vtkmodules.vtkRenderingCore import ( vtkActor, vtkPolyDataMapper, vtkRenderWindow, vtkRenderWindowInteractor, vtkRenderer ) def main(): colors = vtkNamedColors() points = vtkPoints() points.InsertPoint(0, 0.01, 0.0, 0.0) points.InsertPoint(1, 1.5, 0.0, 0.0) points.InsertPoint(2, 1.5, 0.0, 3.5) points.InsertPoint(3, 1.25, 0.0, 3.75) points.InsertPoint(4, 0.75, 0.0, 4.00) points.InsertPoint(5, 0.6, 0.0, 4.35) points.InsertPoint(6, 0.7, 0.0, 4.65) points.InsertPoint(7, 1.0, 0.0, 4.75) points.InsertPoint(8, 1.0, 0.0, 5.0) points.InsertPoint(9, 0.2, 0.0, 5.0) lines = vtkCellArray() lines.InsertNextCell(10) # 创建一个新的单元(cell),需要参数指定这个单元需要多少个点 lines.InsertCellPoint(0) # 将这个点的ID添加到当前正在构建的单元中 lines.InsertCellPoint(1) lines.InsertCellPoint(2) lines.InsertCellPoint(3) lines.InsertCellPoint(4) lines.InsertCellPoint(5) lines.InsertCellPoint(6) lines.InsertCellPoint(7) lines.InsertCellPoint(8) lines.InsertCellPoint(9) profile = vtkPolyData() profile.SetPoints(points) profile.SetLines(lines) # 根据profile绕着某个轴进行旋转,获得一个完整的mesh extrude = vtkRotationalExtrusionFilter() extrude.SetInputData(profile) """ Resolution 控制 旋转拉伸时的分段数,也就是把 360° 拆分成多少个切片。 值越大,旋转生成的 3D 表面越平滑;值越小,表面就会显得棱角分明 """ extrude.SetResolution(60) """ extrude = vtkRotationalExtrusionFilter() extrude.SetInputData(profile) extrude.SetResolution(360) # 设置旋转后的 角度分辨率(采样数) extrude.SetTranslation(6) # 在旋转的同时沿着 Z 轴平移 6 个单位长度 extrude.SetDeltaRadius(1.0) # 表示在旋转过程中半径的变化量, 也就是每转一圈,物体的半径增加1个单位 extrude.SetAngle(2160.0) # 设置旋转的总角度 """ mapper = vtkPolyDataMapper() mapper.SetInputConnection(extrude.GetOutputPort()) bottle = vtkActor() bottle.SetMapper(mapper) bottle.GetProperty().SetColor(colors.GetColor3d("Mint")) """ vtkStripper 将一个网格中独立的多边形(通常是三角形或四边形)转换为三角形带(triangle strips)和多边形带(polygon strips) 方便渲染,但是在这个例子里,有没有这个区别不大,可以视为是一个优化方法。 """ stripper = vtkStripper() stripper.SetInputData(profile) # 将这条线进行膨胀并显示 tubes = vtkTubeFilter() tubes.SetInputConnection(stripper.GetOutputPort()) """ 下面这种写法,摒弃了stripper的优化,直接使用profile这个polydata也可以 """ tubes.SetInputConnection(stripper.GetOutputPort()) tubes.SetNumberOfSides(11) tubes.SetRadius(0.05) profileMapper = vtkPolyDataMapper() profileMapper.SetInputConnection(tubes.GetOutputPort()) profileActor = vtkActor() profileActor.SetMapper(profileMapper) profileActor.GetProperty().SetColor(colors.GetColor3d('Tomato')) renderer = vtkRenderer() renWin = vtkRenderWindow() renWin.AddRenderer(renderer) iren = vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) renderer.AddActor(bottle) renderer.AddActor(profileActor) renderer.SetBackground(colors.GetColor3d('Burlywood')) renWin.SetSize(640, 480) renWin.SetWindowName('Bottle'); renWin.Render() renderer.GetActiveCamera().SetPosition(1, 0, 0) renderer.GetActiveCamera().SetFocalPoint(0, 0, 0) renderer.GetActiveCamera().SetViewUp(0, 0, 1) renderer.ResetCamera() renderer.GetActiveCamera().Azimuth(30) renderer.GetActiveCamera().Elevation(30) renWin.Render() iren.Start() if __name__ == '__main__': main()