Python Grid使用和布局详解


Posted in Python onJune 30, 2018

本文实例为大家分享了Python Grid使用和布局的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
 
import vtk
 
# 这个示例主要用于将不同的图像对象显示到指定的Grid中
 
def main():
 colors = vtk.vtkNamedColors()
 
 # Set the background color.
 colors.SetColor("BkgColor", [51, 77, 102, 255])
 
 titles = list()
 textMappers = list()
 textActors = list()
 
 uGrids = list()
 mappers = list()
 actors = list()
 renderers = list()
 
 uGrids.append(MakeHexagonalPrism())
 titles.append('Hexagonal Prism')
 uGrids.append(MakeHexahedron())
 titles.append('Hexahedron')
 uGrids.append(MakePentagonalPrism())
 titles.append('Pentagonal Prism')
 
 uGrids.append(MakePolyhedron())
 titles.append('Polyhedron')
 uGrids.append(MakePyramid())
 titles.append('Pyramid')
 uGrids.append(MakeTetrahedron())
 titles.append('Tetrahedron')
 
 uGrids.append(MakeVoxel())
 titles.append('Voxel')
 uGrids.append(MakeWedge())
 titles.append('Wedge')
 
 renWin = vtk.vtkRenderWindow()
 renWin.SetWindowName('Cell3D Demonstration')
 
 iRen = vtk.vtkRenderWindowInteractor()
 iRen.SetRenderWindow(renWin)
 
 # Create one text property for all
 textProperty = vtk.vtkTextProperty()
 textProperty.SetFontSize(16)
 textProperty.SetJustificationToCentered()
 
 # Create and link the mappers actors and renderers together.
 # 为每个独立的文本图形对象创建独立的Mapper和Actors,并绑定至每个grid中
 for i in range(0, len(uGrids)):
  textMappers.append(vtk.vtkTextMapper())
  textActors.append(vtk.vtkActor2D())#
 
  mappers.append(vtk.vtkDataSetMapper())
  actors.append(vtk.vtkActor())
  renderers.append(vtk.vtkRenderer())
 
  mappers[i].SetInputData(uGrids[i])
  actors[i].SetMapper(mappers[i])
  actors[i].GetProperty().SetColor(
   colors.GetColor3d("Seashell"))
  renderers[i].AddViewProp(actors[i])
 
  textMappers[i].SetInput(titles[i])
  textMappers[i].SetTextProperty(textProperty)
 
  textActors[i].SetMapper(textMappers[i])
  textActors[i].SetPosition(120, 16)
  renderers[i].AddViewProp(textActors[i])
 
  renWin.AddRenderer(renderers[i])
 
 gridDimensions = 3
 rendererSize = 300
 
 renWin.SetSize(rendererSize * gridDimensions,
     rendererSize * gridDimensions)
 
 # 渲染图形对象至不同的显示区域
 for row in range(0, gridDimensions):
  for col in range(0, gridDimensions):
   index = row * gridDimensions + col
 
   # (xmin, ymin, xmax, ymax)
   viewport = [
    float(col) * rendererSize /
    (gridDimensions * rendererSize),
    float(gridDimensions - (row + 1)) * rendererSize /
    (gridDimensions * rendererSize),
    float(col + 1) * rendererSize /
    (gridDimensions * rendererSize),
    float(gridDimensions - row) * rendererSize /
    (gridDimensions * rendererSize)]
 
   if index > len(actors) - 1:
    # Add a renderer even if there is no actor.
    # This makes the render window background all the same color.
    ren = vtk.vtkRenderer()
    ren.SetBackground(colors.GetColor3d("BkgColor"))
    ren.SetViewport(viewport)
    renWin.AddRenderer(ren)
    continue
 
   renderers[index].SetViewport(viewport)
   renderers[index].SetBackground(colors.GetColor3d("BkgColor"))
   renderers[index].ResetCamera()
   renderers[index].GetActiveCamera().Azimuth(30)
   renderers[index].GetActiveCamera().Elevation(-30)
   renderers[index].GetActiveCamera().Zoom(0.85)
   renderers[index].ResetCameraClippingRange()
 
 iRen.Initialize()
 renWin.Render()
 iRen.Start()
 
 
def MakeHexagonalPrism():
 """
  3D: hexagonal prism: a wedge with an hexagonal base.
  Be careful, the base face ordering is different from wedge.
 """
 
 numberOfVertices = 12
 
 points = vtk.vtkPoints()
 
 points.InsertNextPoint(0.0, 0.0, 1.0)
 points.InsertNextPoint(1.0, 0.0, 1.0)
 points.InsertNextPoint(1.5, 0.5, 1.0)
 points.InsertNextPoint(1.0, 1.0, 1.0)
 points.InsertNextPoint(0.0, 1.0, 1.0)
 points.InsertNextPoint(-0.5, 0.5, 1.0)
 
 points.InsertNextPoint(0.0, 0.0, 0.0)
 points.InsertNextPoint(1.0, 0.0, 0.0)
 points.InsertNextPoint(1.5, 0.5, 0.0)
 points.InsertNextPoint(1.0, 1.0, 0.0)
 points.InsertNextPoint(0.0, 1.0, 0.0)
 points.InsertNextPoint(-0.5, 0.5, 0.0)
 
 hexagonalPrism = vtk.vtkHexagonalPrism()
 for i in range(0, numberOfVertices):
  hexagonalPrism.GetPointIds().SetId(i, i)
 
 ug = vtk.vtkUnstructuredGrid()
 ug.InsertNextCell(hexagonalPrism.GetCellType(),
      hexagonalPrism.GetPointIds())
 ug.SetPoints(points)
 
 return ug
 
 
def MakeHexahedron():
 """
  A regular hexagon (cube) with all faces square and three squares around
  each vertex is created below.
  Setup the coordinates of eight points
  (the two faces must be in counter clockwise
  order as viewed from the outside).
  As an exercise you can modify the coordinates of the points to create
  seven topologically distinct convex hexahedras.
 """
 numberOfVertices = 8
 
 # Create the points
 points = vtk.vtkPoints()
 points.InsertNextPoint(0.0, 0.0, 0.0)
 points.InsertNextPoint(1.0, 0.0, 0.0)
 points.InsertNextPoint(1.0, 1.0, 0.0)
 points.InsertNextPoint(0.0, 1.0, 0.0)
 points.InsertNextPoint(0.0, 0.0, 1.0)
 points.InsertNextPoint(1.0, 0.0, 1.0)
 points.InsertNextPoint(1.0, 1.0, 1.0)
 points.InsertNextPoint(0.0, 1.0, 1.0)
 
 # Create a hexahedron from the points
 hex_ = vtk.vtkHexahedron()
 for i in range(0, numberOfVertices):
  hex_.GetPointIds().SetId(i, i)
 
 # Add the points and hexahedron to an unstructured grid
 uGrid = vtk.vtkUnstructuredGrid()
 uGrid.SetPoints(points)
 uGrid.InsertNextCell(hex_.GetCellType(), hex_.GetPointIds())
 
 return uGrid
 
 
def MakePentagonalPrism():
 numberOfVertices = 10
 
 # Create the points
 points = vtk.vtkPoints()
 points.InsertNextPoint(11, 10, 10)
 points.InsertNextPoint(13, 10, 10)
 points.InsertNextPoint(14, 12, 10)
 points.InsertNextPoint(12, 14, 10)
 points.InsertNextPoint(10, 12, 10)
 points.InsertNextPoint(11, 10, 14)
 points.InsertNextPoint(13, 10, 14)
 points.InsertNextPoint(14, 12, 14)
 points.InsertNextPoint(12, 14, 14)
 points.InsertNextPoint(10, 12, 14)
 
 # Pentagonal Prism
 pentagonalPrism = vtk.vtkPentagonalPrism()
 for i in range(0, numberOfVertices):
  pentagonalPrism.GetPointIds().SetId(i, i)
 
 # Add the points and hexahedron to an unstructured grid
 uGrid = vtk.vtkUnstructuredGrid()
 uGrid.SetPoints(points)
 uGrid.InsertNextCell(pentagonalPrism.GetCellType(),
       pentagonalPrism.GetPointIds())
 
 return uGrid
 
 
def MakePolyhedron():
 """
  Make a regular dodecahedron. It consists of twelve regular pentagonal
  faces with three faces meeting at each vertex.
 """
 # numberOfVertices = 20
 numberOfFaces = 12
 # numberOfFaceVertices = 5
 
 points = vtk.vtkPoints()
 points.InsertNextPoint(1.21412, 0, 1.58931)
 points.InsertNextPoint(0.375185, 1.1547, 1.58931)
 points.InsertNextPoint(-0.982247, 0.713644, 1.58931)
 points.InsertNextPoint(-0.982247, -0.713644, 1.58931)
 points.InsertNextPoint(0.375185, -1.1547, 1.58931)
 points.InsertNextPoint(1.96449, 0, 0.375185)
 points.InsertNextPoint(0.607062, 1.86835, 0.375185)
 points.InsertNextPoint(-1.58931, 1.1547, 0.375185)
 points.InsertNextPoint(-1.58931, -1.1547, 0.375185)
 points.InsertNextPoint(0.607062, -1.86835, 0.375185)
 points.InsertNextPoint(1.58931, 1.1547, -0.375185)
 points.InsertNextPoint(-0.607062, 1.86835, -0.375185)
 points.InsertNextPoint(-1.96449, 0, -0.375185)
 points.InsertNextPoint(-0.607062, -1.86835, -0.375185)
 points.InsertNextPoint(1.58931, -1.1547, -0.375185)
 points.InsertNextPoint(0.982247, 0.713644, -1.58931)
 points.InsertNextPoint(-0.375185, 1.1547, -1.58931)
 points.InsertNextPoint(-1.21412, 0, -1.58931)
 points.InsertNextPoint(-0.375185, -1.1547, -1.58931)
 points.InsertNextPoint(0.982247, -0.713644, -1.58931)
 
 # Dimensions are [numberOfFaces][numberOfFaceVertices]
 dodechedronFace = [
  [0, 1, 2, 3, 4],
  [0, 5, 10, 6, 1],
  [1, 6, 11, 7, 2],
  [2, 7, 12, 8, 3],
  [3, 8, 13, 9, 4],
  [4, 9, 14, 5, 0],
  [15, 10, 5, 14, 19],
  [16, 11, 6, 10, 15],
  [17, 12, 7, 11, 16],
  [18, 13, 8, 12, 17],
  [19, 14, 9, 13, 18],
  [19, 18, 17, 16, 15]
 ]
 
 dodechedronFacesIdList = vtk.vtkIdList()
 # Number faces that make up the cell.
 dodechedronFacesIdList.InsertNextId(numberOfFaces)
 for face in dodechedronFace:
  # Number of points in the face == numberOfFaceVertices
  dodechedronFacesIdList.InsertNextId(len(face))
  # Insert the pointIds for that face.
  [dodechedronFacesIdList.InsertNextId(i) for i in face]
 
 uGrid = vtk.vtkUnstructuredGrid()
 uGrid.InsertNextCell(vtk.VTK_POLYHEDRON, dodechedronFacesIdList)
 uGrid.SetPoints(points)
 
 return uGrid
 
 
def MakePyramid():
 """
  Make a regular square pyramid.
 """
 numberOfVertices = 5
 
 points = vtk.vtkPoints()
 
 p = [
  [1.0, 1.0, 0.0],
  [-1.0, 1.0, 0.0],
  [-1.0, -1.0, 0.0],
  [1.0, -1.0, 0.0],
  [0.0, 0.0, 1.0]
 ]
 for pt in p:
  points.InsertNextPoint(pt)
 
 pyramid = vtk.vtkPyramid()
 for i in range(0, numberOfVertices):
  pyramid.GetPointIds().SetId(i, i)
 
 ug = vtk.vtkUnstructuredGrid()
 ug.SetPoints(points)
 ug.InsertNextCell(pyramid.GetCellType(), pyramid.GetPointIds())
 
 return ug
 
 
def MakeTetrahedron():
 """
  Make a tetrahedron.
 """
 numberOfVertices = 4
 
 points = vtk.vtkPoints()
 points.InsertNextPoint(0, 0, 0)
 points.InsertNextPoint(1, 0, 0)
 points.InsertNextPoint(1, 1, 0)
 points.InsertNextPoint(0, 1, 1)
 
 tetra = vtk.vtkTetra()
 for i in range(0, numberOfVertices):
  tetra.GetPointIds().SetId(i, i)
 
 cellArray = vtk.vtkCellArray()
 cellArray.InsertNextCell(tetra)
 
 unstructuredGrid = vtk.vtkUnstructuredGrid()
 unstructuredGrid.SetPoints(points)
 unstructuredGrid.SetCells(vtk.VTK_TETRA, cellArray)
 
 return unstructuredGrid
 
 
def MakeVoxel():
 """
  A voxel is a representation of a regular grid in 3-D space.
 """
 numberOfVertices = 8
 
 points = vtk.vtkPoints()
 points.InsertNextPoint(0, 0, 0)
 points.InsertNextPoint(1, 0, 0)
 points.InsertNextPoint(0, 1, 0)
 points.InsertNextPoint(1, 1, 0)
 points.InsertNextPoint(0, 0, 1)
 points.InsertNextPoint(1, 0, 1)
 points.InsertNextPoint(0, 1, 1)
 points.InsertNextPoint(1, 1, 1)
 
 voxel = vtk.vtkVoxel()
 for i in range(0, numberOfVertices):
  voxel.GetPointIds().SetId(i, i)
 
 ug = vtk.vtkUnstructuredGrid()
 ug.SetPoints(points)
 ug.InsertNextCell(voxel.GetCellType(), voxel.GetPointIds())
 
 return ug
 
 
def MakeWedge():
 """
  A wedge consists of two triangular ends and three rectangular faces.
 """
 
 numberOfVertices = 6
 
 points = vtk.vtkPoints()
 
 points.InsertNextPoint(0, 1, 0)
 points.InsertNextPoint(0, 0, 0)
 points.InsertNextPoint(0, .5, .5)
 points.InsertNextPoint(1, 1, 0)
 points.InsertNextPoint(1, 0.0, 0.0)
 points.InsertNextPoint(1, .5, .5)
 
 wedge = vtk.vtkWedge()
 for i in range(0, numberOfVertices):
  wedge.GetPointIds().SetId(i, i)
 
 ug = vtk.vtkUnstructuredGrid()
 ug.SetPoints(points)
 ug.InsertNextCell(wedge.GetCellType(), wedge.GetPointIds())
 
 return ug
 
 
def WritePNG(renWin, fn, magnification=1):
 """
  Screenshot
  Write out a png corresponding to the render window.
  :param: renWin - the render window.
  :param: fn - the file name.
  :param: magnification - the magnification.
 """
 windowToImageFilter = vtk.vtkWindowToImageFilter()
 windowToImageFilter.SetInput(renWin)
 windowToImageFilter.SetMagnification(magnification)
 # Record the alpha (transparency) channel
 # windowToImageFilter.SetInputBufferTypeToRGBA()
 windowToImageFilter.SetInputBufferTypeToRGB()
 # Read from the back buffer
 windowToImageFilter.ReadFrontBufferOff()
 windowToImageFilter.Update()
 
 writer = vtk.vtkPNGWriter()
 writer.SetFileName(fn)
 writer.SetInputConnection(windowToImageFilter.GetOutputPort())
 writer.Write()
 
 
if __name__ == '__main__':
 main()

Python Grid使用和布局详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python基于tkinter模块实现的改名小工具示例
Jul 27 Python
python中import reload __import__的区别详解
Oct 16 Python
python中使用%与.format格式化文本方法解析
Dec 27 Python
详解python3中tkinter知识点
Jun 21 Python
基于Python的PIL库学习详解
May 10 Python
pandas DataFrame行或列的删除方法的实现示例
Aug 02 Python
python将时分秒转换成秒的实例
Dec 07 Python
python Popen 获取输出,等待运行完成示例
Dec 30 Python
Python爬取365好书中小说代码实例
Feb 28 Python
python GUI库图形界面开发之PyQt5复选框控件QCheckBox详细使用方法与实例
Feb 28 Python
pyqt5中动画的使用详解
Apr 01 Python
Python+OpenCV图像处理—— 色彩空间转换
Oct 22 Python
Python 删除连续出现的指定字符的实例
Jun 29 #Python
使用python语言,比较两个字符串是否相同的实例
Jun 29 #Python
python去除文件中重复的行实例
Jun 29 #Python
python获取指定字符串中重复模式最高的字符串方法
Jun 29 #Python
Python 利用内置set函数对字符串和列表进行去重的方法
Jun 29 #Python
Linux下python与C++使用dlib实现人脸检测
Jun 29 #Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 #Python
You might like
php入门学习知识点二 PHP简单的分页过程与原理
2011/07/14 PHP
php+memcache实现的网站在线人数统计代码
2014/07/04 PHP
php数组转成json格式的方法
2015/03/09 PHP
PHP实现转盘抽奖算法分享
2020/04/15 PHP
PHP大文件切割上传功能实例分析
2019/07/01 PHP
用javascript实现的激活输入框后隐藏初始内容
2007/06/29 Javascript
深入理解JavaScript中的传值与传引用
2013/12/09 Javascript
javascript调试过程中找不到哪里出错的可能原因
2013/12/16 Javascript
node.js中的fs.utimesSync方法使用说明
2014/12/15 Javascript
Jquery 实现checkbox全选方法
2015/01/28 Javascript
jQuery右侧选项卡焦点图片轮播特效代码分享
2015/09/05 Javascript
jQuery实现简单隔行变色的方法
2016/02/20 Javascript
jquery动态遍历Json对象的属性和值的方法
2016/07/27 Javascript
JS创建对象的写法示例
2016/11/04 Javascript
用jQuery实现圆点图片轮播效果
2017/03/19 Javascript
webpack-dev-server远程访问配置方法
2018/02/22 Javascript
Vue.js实现可排序的表格组件功能示例
2019/02/19 Javascript
vue 项目中当访问路由不存在的时候默认访问404页面操作
2020/08/31 Javascript
[27:28]Ti4 冒泡赛第二天 iG vs NEWBEE 1
2014/07/15 DOTA
详解python中的json和字典dict
2018/06/22 Python
python3使用pandas获取股票数据的方法
2018/12/22 Python
深入了解和应用Python 装饰器 @decorator
2019/04/02 Python
基于python的socket实现单机五子棋到双人对战
2020/03/24 Python
pyqt5 QProgressBar清空进度条的实例
2019/06/21 Python
python 批量解压压缩文件的实例代码
2019/06/27 Python
Python高级特性之闭包与装饰器实例详解
2019/11/19 Python
Python彻底删除文件夹及其子文件方式
2019/12/23 Python
Python用类实现扑克牌发牌的示例代码
2020/06/01 Python
美国在线眼镜商城:Eyeglasses.com
2017/06/26 全球购物
校园网站的创业计划书范文
2013/12/30 职场文书
婚前财产公证书
2014/04/10 职场文书
结对共建协议书
2014/08/20 职场文书
教师党员自我剖析材料
2014/09/29 职场文书
回复函格式及范文
2015/07/14 职场文书
pandas 操作 Excel操作总结
2021/03/31 Python
Golang日志包的使用
2022/04/20 Golang