Python Grid使用和布局详解

本文实例为大家分享了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()

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

(0)

相关推荐

  • Python Tkinter简单布局实例教程

    本文实例展示了Python Tkinter实现简单布局的方法,示例中备有较为详尽的注释,便于读者理解.分享给大家供大家参考之用.具体如下: # -*- coding: utf-8 -*- from Tkinter import * root = Tk() # 80x80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置 root.geometry('80x80+10+10') # 填充方向 ''' Label(root, text = 'l1', bg = 'red').pack(f

  • Python模块结构与布局操作方法实例分析

    本文实例讲述了Python模块结构与布局操作方法.分享给大家供大家参考,具体如下: #coding=utf8 #起始行 #!/usr/bin/env python #模块文档 ''''' 合理的Module布局: (1) 起始行(Unix) (2) 模块文档 (3) 模块导入 (4) 变量定义 (5) 类定义 (6) 函数定义 (7) 主程序 ----------------------------- (1) 起始行(Unix) 通常只有在类Unix环境下才使用起始行,有起始行可以输入脚本名来执

  • python的tkinter布局之简单的聊天窗口实现方法

    本文实例展示了一个python的tkinter布局的简单聊天窗口.分享给大家供大家参考之用.具体方法如下: 该实例展示的是一个简单的聊天窗口,可以实现下方输入聊天内容,点击发送,可以增加到上方聊天记录列表中.现在只是"单机"版. 右侧预留了空位可以放点儿其它东西.感兴趣的读者可以进一步做成socket双方互聊. 以下是功能代码部分: from Tkinter import * import datetime import time root = Tk() root.title(unic

  • Python Grid使用和布局详解

    本文实例为大家分享了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() tex

  • python数据分析工具之 matplotlib详解

    不论是数据挖掘还是数学建模,都免不了数据可视化的问题.对于 Python 来说,matplotlib 是最著名的绘图库,它主要用于二维绘图,当然也可以进行简单的三维绘图.它不但提供了一整套和 Matlab 相似但更为丰富的命令,让我们可以非常快捷地用 python 可视化数据. matplotlib基础 # 安装 pip install matplotlib 两种绘图风格: MATLAB风格: 基本函数是 plot,分别取 x,y 的值,然后取到坐标(x,y)后,对不同的连续点进行连线. 面向对

  • python Cartopy的基础使用详解

    前言 常用地图底图的绘制一般由Basemap或者cartopy模块完成,由于Basemap库是基于python2开发的一个模块,目前已经不开发维护.故简单介绍cartopy模块的一些基础操作. 一.基础介绍 首先导入相关模块. import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature from cartopy.mpl.ticker

  • 最强Python可视化绘图库Plotly详解用法

    今天给大家分享一篇可视化干货,介绍的是功能强大的开源 Python 绘图库 Plotly,教你如何用超简单的(甚至只要一行)代码,绘制出更棒的图表. 我之前一直使用 matplotlib ,由于它复杂的语法,我已经"沉没"在里面太多的时间成本.这也导致我花费了不知多少个深夜,在 StackOverflow 上搜索如何"格式化日期"或"增加第二个Y轴". 但我们现在有一个更好的选择了 ,比如易于使用.文档健全.功能强大的开源 Python 绘图库

  •  python用matplotlib可视化绘图详解

    目录 1.Matplotlib 简介 2.Matplotlib图形绘制 1)折线图 2)柱状图 3)条形图 3)饼图 4)散点图 5)直方图 6)箱型图 7)子图 1.Matplotlib 简介 Matplotlib 简介: Matplotlib 是一个python的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形,matplotlib 对于图像美化方面比较完善,可以自定义线条的颜色和样式,可以在一张绘图纸上绘制多张小图,也可以在一张图上绘制多条线,可以很方便地将数据可

  • Python利用Matplotlib绘制图表详解

    目录 前言 折线图绘制与显示 绘制数学函数图像 散点图绘制 绘制柱状图 绘制直方图 饼图 前言 Matplotlib 是 Python 中类似 MATLAB 的绘图工具,如果您熟悉 MATLAB,那么可以很快的熟悉它. Matplotlib 提供了一套面向对象绘图的 API,它可以轻松地配合 Python GUI 工具包(比如 PyQt,WxPython.Tkinter)在应用程序中嵌入图形.与此同时,它也支持以脚本的形式在 Python.IPython Shell.Jupyter Notebo

  • Python中的flask框架详解

    Flask是一个Python编写的Web 微框架,让我们可以使用Python语言快速实现一个网站或Web服务.本文参考自Flask官方文档,大部分代码引用自官方文档. 安装flask 首先我们来安装Flask.最简单的办法就是使用pip. pip install flask 然后打开一个Python文件,输入下面的内容并运行该文件.然后访问localhost:5000,我们应当可以看到浏览器上输出了hello world. from flask import Flask app = Flask(

  • Python NumPy教程之索引详解

    目录 为什么我们需要 NumPy 使用索引数组进行索引 索引类型 基本切片和索引 高级索引 NumPy 或 Numeric Python 是一个用于计算同质 n 维数组的包.在 numpy 维度中称为轴. 为什么我们需要 NumPy 出现了一个问题,当 python 列表已经存在时,为什么我们需要 NumPy.答案是我们不能直接对两个列表的所有元素执行操作.例如,我们不能直接将两个列表相乘,我们必须逐个元素地进行.这就是 NumPy 发挥作用的地方. 示例 #1: # 演示需要 NumPy 的

  • Python探索之ModelForm代码详解

    这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,对,你没猜错,相信自己的英语水平. 先来一个简单的例子来看一下这个东西怎么用: 比如我们的数据库中有这样一张学生表,字段有姓名,年龄,爱好,邮箱,电话,住址,注册时间等等一大堆信息,现在让你写一个创建学生的页面,你的后台应该怎么写呢? 首先我们会在前端一个一个罗列出这些字段,让用户去填写,然后我们从后天一个一个接收用户的输入,创建一个新的学生对象,保存 其实,重点不是这些,而是合法性验证,我们需要在前端

  • python装饰器实例大详解

    一.作用域 在python中,作用域分为两种:全局作用域和局部作用域. 全局作用域是定义在文件级别的变量,函数名.而局部作用域,则是定义函数内部. 关于作用域,我们要理解两点: a.在全局不能访问到局部定义的变量 b.在局部能够访问到全局定义的变量,但是不能修改全局定义的变量(当然有方法可以修改) 下面我们来看看下面实例: x = 1 def funx(): x = 10 print(x) # 打印出10 funx() print(x) # 打印出1 如果局部没有定义变量x,那么函数内部会从内往

随机推荐