python绘制立方体的方法
作者:genispan 发布时间:2022-09-26 10:34:14
标签:python,立方体
本文实例为大家分享了python绘制立方体的具体代码,供大家参考,具体内容如下
#!/usr/bin/env python
# This is (almost) a direct C++ to Python transliteration of
# <VTK-root>/Examples/DataManipulation/Cxx/Cube.cxx from the VTK
# source distribution, which "shows how to manually create vtkPolyData"
#
# A convenience function, mkVtkIdList(), has been added and one if/else
# so the example also works in version 6 or later.
#
# Lines like `obj->Delete()` have been transliterated as `del obj` to,
# preserve the resemblance to the original C++ example, although I
# doubt this achieves anything beyond what Python's garbage collection
# would do anyway.
import vtk
# Makes a vtkIdList from a Python iterable. I'm kinda surprised that
# this is necessary, since I assumed that this kind of thing would
# have been built into the wrapper and happen transparently, but it
# seems not.
def mkVtkIdList(it):
vil = vtk.vtkIdList()
for i in it:
vil.InsertNextId(int(i))
return vil
# 绘制通用方法
def myShow(cube):
# Now we'll look at it.
cubeMapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
cubeMapper.SetInput(cube)
else:
cubeMapper.SetInputData(cube)
cubeMapper.SetScalarRange(0, 7)
cubeActor = vtk.vtkActor()
cubeActor.SetMapper(cubeMapper)
# The usual rendering stuff.
camera = vtk.vtkCamera()
camera.SetPosition(1, 1, 1)
camera.SetFocalPoint(0, 0, 0)
renderer = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
renderer.AddActor(cubeActor)
renderer.SetActiveCamera(camera)
renderer.ResetCamera()
renderer.SetBackground(0, 0, 0)
renWin.SetSize(300, 300)
# interact with data
renWin.Render()
iren.Start()
del cubeMapper
del cubeActor
del camera
del renderer
del renWin
del iren
def main():
# x = array of 8 3-tuples of float representing the vertices of a cube:
# 8个三维值代表长方体的8个顶点
x = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0),
(0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 1.0, 1.0), (0.0, 1.0, 1.0)]
# pts = array of 6 4-tuples of vtkIdType (int) representing the faces
# of the cube in terms of the above vertices
# 点的编号0-7,每个面由4个点组成
pts = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4),
(1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
# We'll create the building blocks of polydata including data attributes.
cube = vtk.vtkPolyData()
points = vtk.vtkPoints()
polys = vtk.vtkCellArray()
scalars = vtk.vtkFloatArray()
# Load the point, cell, and data attributes.
for i in range(8):
points.InsertPoint(i, x[i])
for i in range(6):
polys.InsertNextCell(mkVtkIdList(pts[i]))
for i in range(8):
scalars.InsertTuple1(i, i)
# We now assign the pieces to the vtkPolyData.
cube.SetPoints(points)
del points
cube.SetPolys(polys)
del polys
cube.GetPointData().SetScalars(scalars)
del scalars
myShow(cube)
# Clean up
del cube
main()
效果图:
来源:https://blog.csdn.net/genispan/article/details/79583193


猜你喜欢
- SQL,数据分析岗的必备技能,你可以不懂Python,R,不懂可视化,不懂机器学习。但SQL,你必须懂。要不然领导让你跑个数据来汇.....
- 有需要的人可以参考一下,如果试用过,发现问题,欢迎留言告知,不胜感激。功能介绍:1、若页面无刷新,且第一次传值小于第二次传值或者圆环初始化时
- Redis通常被认为是一种持久化的存储器关键字-值型存储,可以用于几台机子之间的数据共享平台。连接数据库 注意:假设现有几台在同一局域网内的
- 本文实例讲述了python采用getopt解析命令行输入参数的方法,分享给大家供大家参考。具体实例代码如下:import getopt im
- 本文实例讲述了GO语言延迟函数defer用法。分享给大家供大家参考。具体分析如下:defer 在声明时不会立即执行,而是在函数 return
- 微信小程序官方给了十分详细的登陆时序图,当然为了安全着想,应该加上签名加密。微信小程序端1).调用wx.login获取 code 。 2).
- 创建触发器。创建触发器语法如下:CREATE TRIGGER trigger_name trigger_time trigger_event
- 本文实例为大家分享了python实现简易动态时钟的具体代码,供大家参考,具体内容如下from turtle import *from dat
- 在Jupyter Notebook上使用Python+opencv实现如下简单车牌字符切割。关于opencv库的安装可以参考:Python下
- Python中遍历列表有以下几种方法:一、for循环遍历lists = ["m1", 1900, "m2&qu
- 前言本文旨在用最通俗的语言讲述最枯燥的基本知识这个话题比较有意思。昨天中午吃完饭间突然有个同事蹦出了一句:“like有索引吗?”,我顺口就说
- 本文实例讲述了Python实现的计算器功能。分享给大家供大家参考,具体如下:源码:# -*- coding:utf-8 -*-#! pyth
- 前言在讲解如何解决migrate报错原因前,我们先要了解migrate做了什么事情,migrate:将新生成的迁移脚本。映射到数据库中。创建
- 使用rpm安装方式安装完MySQL数据库后,数据文件的默认路径为/var/lib/mysql,然而根目录并不适合用于存储数据文件。原路径:/
- 一、程序运行1.效果展示 - 轮廓描绘看轮廓描绘效果:2.效果展示 - 颜色填充衣服和裤子颜色填充效果:二、实现过程1.绘图数据下载获取地址
- 前言: 最近在学习过程中总是遇到np.random.seed()这个问题,刚开始总是觉得不过是一个简单的随机数种子,就没太在意,后来遇到的次
- 1、接口概述1)、什么是接口?接口是提供了一种用以说明一个对象应该具有哪些方法的手段。尽管它可以表明这些方法的语义,但它并不规定这些方法应该
- 一、前言我想介绍这些功能的主要原因是它们可以帮助您避免编写循环。在某些情况下,循环运行可能会很昂贵,除此之外,这些功能将有助于提高速度。以下
- 环境使用Python 3.8–> 解释器 <执行python代码>Pycharm–
- 导语:目前点评“2008年10佳改版网站”也许为时尚早,但2008年毕竟已经过去了9个多月,周四又同时有Twitter和FriendFeed