import vtk import time import datetime class VTKScreen(): """ a vtk render window for displaying geometry """ def __init__(self, width=1280, height=720): self.width=width self.height=height self.ren = vtk.vtkRenderer() self.renWin = vtk.vtkRenderWindow() self.renWin.AddRenderer(self.ren) self.renWin.SetSize(self.width,self.height) #iren = vtk.vtkRenderWindowInteractor() #iren.SetRenderWindow(renWin) #iren.Initialize() #iren.Start() self.camera = vtk.vtkCamera() self.camera.SetClippingRange(1, 100) self.camera.SetFocalPoint(0, 0, 0) self.camera.SetPosition(0, 15, 5) self.camera.SetViewAngle(30) self.camera.SetViewUp(0, 0, 1) self.ren.SetActiveCamera(self.camera) def addActor(self, actor): self.ren.AddActor(actor) def render(self): self.renWin.Render() class Cone(vtk.vtkActor): def __init__(self, resolution=60, center=(-2,0,0), color=(1,1,0) ): self.src = vtk.vtkConeSource() self.src.SetResolution(resolution) self.src.SetCenter(center) self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInput(self.src.GetOutput()) self.SetMapper(self.mapper) self.SetColor(color) def SetColor(self, color): self.GetProperty().SetColor(color) class Sphere(vtk.vtkActor): def __init__(self, thetaresolution=50, phiresolution=50, center=(0,2,0), color=(1,0,0)): self.src = vtk.vtkSphereSource() self.src.SetCenter(0,2,0) self.src.SetThetaResolution(50) self.src.SetPhiResolution(50) self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInput(self.src.GetOutput()) self.SetMapper(self.mapper) self.SetColor(color) def SetColor(self, color): self.GetProperty().SetColor(color) class Cube(vtk.vtkActor): def __init__(self,center=(2,2,0) , color=(0,1,0) ): self.src = vtk.vtkCubeSource() self.src.SetCenter(center) self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInput(self.src.GetOutput()) self.SetMapper(self.mapper) self.SetColor(color) def SetColor(self, color): self.GetProperty().SetColor(color) class Cylinder(vtk.vtkActor): def __init__(self,center=(0,-2,0) , radius=0.5, height=2, color=(0,1,1), resolution=50 ): self.src = vtk.vtkCylinderSource() self.src.SetCenter(center) self.src.SetHeight(height) self.src.SetRadius(radius) self.src.SetResolution(resolution) # SetResolution # SetCapping(int) # CappingOn() CappingOff() self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInput(self.src.GetOutput()) self.SetMapper(self.mapper) self.SetColor(color) def SetColor(self, color): self.GetProperty().SetColor(color) class Line(vtk.vtkActor): # Get/SetResolution(int) def __init__(self,p1=(0,0,0) , p2=(1,1,1), color=(0,1,1) ): self.src = vtk.vtkLineSource() self.src.SetPoint1(p1) self.src.SetPoint2(p2) self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInput(self.src.GetOutput()) self.SetMapper(self.mapper) self.SetColor(color) def SetColor(self, color): self.GetProperty().SetColor(color) class Point(vtk.vtkActor): # Get/SetResolution(int) def __init__(self,center=(0,0,0) , radius=0.4, color=(1,2,3) ): self.src = vtk.vtkPointSource() self.src.SetCenter(center) self.src.SetRadius(radius) self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInput(self.src.GetOutput()) self.SetMapper(self.mapper) self.SetColor(color) def SetColor(self, color): self.GetProperty().SetColor(color) class Arrow(vtk.vtkActor): def __init__(self, center=(0,0,0), color=(0,0,1) ): self.src = vtk.vtkArrowSource() #self.src.SetCenter(center) self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInput(self.src.GetOutput()) self.SetMapper(self.mapper) self.SetColor(color) def SetColor(self, color): self.GetProperty().SetColor(color) class Text(vtk.vtkTextActor): def __init__(self, text="text",size=18,color=(1,1,1),pos=(100,100)): self.SetText(text) self.properties=self.GetTextProperty() self.properties.SetFontFamilyToArial() self.properties.SetFontSize(size) self.SetColor(color) self.SetPos(pos) def SetColor(self,color): self.properties.SetColor(color) def SetPos(self, pos): self.SetDisplayPosition(pos[0], pos[1]) def SetText(self, text): self.SetInput(text) # TODO # vtkAxes # #---------------------------------------------------------------- myscreen = VTKScreen() c1 = Cone() s1 = Sphere() cu1 = Cube() a = Arrow() t = Text() cyl=Cylinder() l = Line() p = Point(center=(2,3,1), radius=2) t.SetPos( (myscreen.width-200, myscreen.height-30) ) myscreen.addActor(c1) myscreen.addActor(s1) myscreen.addActor(cu1) myscreen.addActor(a) myscreen.addActor(t) myscreen.addActor(cyl) myscreen.addActor(l) myscreen.addActor(p) myscreen.render() for n in range(1,360): #te2.SetInput("Frame %d" % n) myscreen.camera.Azimuth( 1 ) t.SetText(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) myscreen.render() time.sleep(0.01) raw_input("Press Enter to terminate") #a.SetColor( (1,0,1) ) #myscreen.render() #raw_input("Press Enter to terminate")