news 2026/2/10 5:16:43

Bottle 一条曲线通过旋转形成一个瓶子的mesh

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Bottle 一条曲线通过旋转形成一个瓶子的mesh

一:主要的知识点

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()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/7 0:04:35

零基础跟我学做AI Agent(第1课:环境安装)

从今天开始,作者介绍一系列AI Agent(智能体)的开发或部署。需要的基础只是要对Python有个大概的了解,几乎是从0开始实践。涉及到的代码都会比较简单,一般不会超过50行。 一、Agent简介 AI Agent简称Agent,以…

作者头像 李华
网站建设 2026/2/5 3:16:03

2026年6大房产中介系统评测

在房产中介行业数字化转型加速的背景下,选择一款适配自身业务的管理系统成为提升运营效率、降低成本的关键。目前市场上的房产中介系统种类繁多,功能侧重点各有不同,让不少中介从业者难以抉择。本次评测聚焦6款主流房产中介系统,从…

作者头像 李华
网站建设 2026/2/7 20:14:44

12、Unix 文件管理全攻略

Unix 文件管理全攻略 1. 文件复制 在使用 cp 命令复制文件时,要特别注意路径名的准确性。如果使用 cp -R 时给出错误的路径名,可能会导致将目录树复制到自身,程序会一直运行直到文件系统被填满。 cp 命令复制文件时,新文件的所有权会变更为执行 cp 命令的用户。若…

作者头像 李华
网站建设 2026/2/8 0:40:26

13、在Unix系统中查找文件和信息的实用方法

在Unix系统中查找文件和信息的实用方法 在现代计算机的使用过程中,随着存储系统的不断增大,查找文件和信息成为了一项基本挑战。无论是文件和目录管理得井井有条、使用便于记忆的名称,还是存在大量随意命名的文件和目录,总会有需要根据文件内容、文件名或其他属性来查找文…

作者头像 李华
网站建设 2026/2/3 21:41:30

14、深入探索文件查找:从 `find` 到 Spotlight

深入探索文件查找:从 find 到 Spotlight 在操作系统中,高效地查找文件是一项重要的技能。本文将详细介绍 find 命令的使用方法,包括根据文件名、文件大小、权限和修改时间等条件进行查找,还会介绍 find 的好搭档 xargs 以及 Mac OS X 中的 Spotlight 功能。 1. …

作者头像 李华