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的Tornado框架异步编程入门实例
Apr 24 Python
使用Python发送各种形式的邮件的方法汇总
Nov 09 Python
同时安装Python2 & Python3 cmd下版本自由选择的方法
Dec 09 Python
PHP实现发送和接收JSON请求
Jun 07 Python
详解Python 正则表达式模块
Nov 05 Python
python用for循环求和的方法总结
Jul 08 Python
python实现超市商品销售管理系统
Oct 25 Python
解决IDEA 的 plugins 搜不到任何的插件问题
May 04 Python
python如何导入依赖包
Jul 13 Python
PyQt5的相对布局管理的实现
Aug 07 Python
详解python变量与数据类型
Aug 25 Python
python中出现invalid syntax报错的几种原因分析
Feb 12 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+MySQL5.0中文乱码解决方法
2006/11/20 PHP
thinkphp数据查询和遍历数组实例
2014/11/28 PHP
php计划任务之ignore_user_abort函数实现方法
2015/01/08 PHP
轻松学习jQuery插件EasyUI EasyUI创建RSS Feed阅读器
2015/11/30 Javascript
深入理解jQuery之事件移除
2016/06/02 Javascript
jQuery实现的页面遮罩层功能示例【测试可用】
2017/10/14 jQuery
Popup弹出框添加数据实现方法
2017/10/27 Javascript
原生JS写Ajax的请求函数功能
2017/12/22 Javascript
使用typescript开发angular模块并发布npm包
2018/04/19 Javascript
React Native基础入门之调试React Native应用的一小步
2018/07/02 Javascript
微信小程序实现Session功能及无法获取session问题的解决方法
2019/05/07 Javascript
微信用户访问小程序的登录过程详解
2019/09/20 Javascript
JS合并两个数组的3种方法详解
2019/10/24 Javascript
vue-cli设置publicPath小记
2020/04/14 Javascript
javascript中导出与导入实现模块化管理教程
2020/12/03 Javascript
vuex Module将 store 分割成模块的操作
2020/12/07 Vue.js
[36:33]2018DOTA2亚洲邀请赛 4.3 突围赛 EG vs Newbee 第二场
2018/04/04 DOTA
[01:04:02]DOTA2-DPC中国联赛 正赛 Elephant vs IG BO3 第二场 1月24日
2021/03/11 DOTA
[04:45]DOTA2-DPC中国联赛正赛 iG vs LBZS 赛后选手采访
2021/03/11 DOTA
Python转换itertools.chain对象为数组的方法
2020/02/07 Python
python使用建议与技巧分享(二)
2020/08/17 Python
python 如何利用argparse解析命令行参数
2020/09/11 Python
PyQT5速成教程之Qt Designer介绍与入门
2020/11/02 Python
python实现跨年表白神器--你值得拥有
2021/01/04 Python
详解CSS3的图层阴影和文字阴影效果使用
2016/06/09 HTML / CSS
马来西亚在线时尚女装商店:KEI MAG
2017/09/28 全球购物
Cinque网上商店:德国服装品牌
2019/03/17 全球购物
澳大利亚领先的男装零售连锁店:Lowes
2020/08/07 全球购物
教师考核材料
2014/05/21 职场文书
中学生思想品德评语
2014/12/31 职场文书
个人党性锻炼总结
2015/03/05 职场文书
MySQL kill不掉线程的原因
2021/05/07 MySQL
Angular性能优化之第三方组件和懒加载技术
2021/05/10 Javascript
Python import模块的缓存问题解决方案
2021/06/02 Python
浅谈Python数学建模之线性规划
2021/06/23 Python
JS轻量级函数式编程实现XDM二
2022/06/16 Javascript