vtk类之vtkImageCanvasSource2D:用现有图形绘制图像
vtkImageCanvasSource2D 数据源,开始创建一个空白的图像. 可以再上面画很多图像,如果线,管子,正方形,圆形等。
基本方法:
SetScalarTypeToUnsignedChar:设置值类型为无符号字符
SetDrawColor:设置画刷颜色
FillBox(0,511, 0,511) 背景方块
FillTube(500,20, 30,400, 5)有宽度的线段
DrawSegment(10,20, 500,510)线段
DrawCircle(400,350, 80.0) 园周
FillPixel(450,350) 填充
FillTriangle(100,100, 300,150, 150,300) 三角形
例子:
#-*- coding: UTF-8 -*- from ActorFactory import ActorFactory from vtk import * class ImageCanvasActorFactory(ActorFactory): def __init__(self): ActorFactory.__init__(self) self.__CanvasImage2D = vtkImageCanvasSource2D() def __del__(self): del self.__CanvasImage2D def __UpdateData(self): #设置图像画布的大小和标量值类型 self.__CanvasImage2D.SetScalarTypeToUnsignedChar() self.__CanvasImage2D.SetExtent(0,511, 0,511, 0,0) # 画各种图形 # 背景方块 self.__CanvasImage2D.SetDrawColor(200, 100, 50) self.__CanvasImage2D.FillBox(0,511, 0,511) # 有宽度的线段 self.__CanvasImage2D.SetDrawColor(50, 200, 37) self.__CanvasImage2D.FillTube(500,20, 30,400, 5) # 线段 self.__CanvasImage2D.SetDrawColor(255) self.__CanvasImage2D.DrawSegment(10,20, 500,510) # 园周 self.__CanvasImage2D.SetDrawColor(255, 0, 200) self.__CanvasImage2D.DrawCircle(400,350, 80.0) # 填充 self.__CanvasImage2D.SetDrawColor(170) self.__CanvasImage2D.FillPixel(450,350) #三角形 self.__CanvasImage2D.FillTriangle(100,100, 300,150, 150,300) self.__CanvasImage2D.Update() def _MakeActors(self): self.__UpdateData() plane = vtkPlaneSource() plane.SetCenter(0.0, 0.0, 0.0) plane.SetNormal(0.0, 0.0, 1.0) texture = vtkTexture() texture.SetInputConnection(self.__CanvasImage2D.GetOutputPort()) planeMapper = vtkPolyDataMapper() planeMapper.SetInputConnection(plane.GetOutputPort()) texturedPlane = self._NewActor() texturedPlane.SetMapper(planeMapper) texturedPlane.SetTexture(texture) del plane del texture del planeMapper return [texturedPlane]
输出结果: