日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python画正方体_python绘制立方体的方法

發布時間:2023/12/10 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python画正方体_python绘制立方体的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例為大家分享了python繪制立方體的具體代碼,供大家參考,具體內容如下

#!/usr/bin/env python

# This is (almost) a direct C++ to Python transliteration of

# /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()

效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的python画正方体_python绘制立方体的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。