sphere.SetNumberOfPoints(25) # Triangulate the points with vtkDelaunay3D. This generates a convex hull # of tetrahedron. delny = vtk.vtkDelaunay3D() delny.SetInputConnection(sphere.GetOutputPort()) delny.SetTolerance(0.01) # The triangulation has texture coordinates generated so we can map # a texture onto it. tmapper = vtk.vtkTextureMapToCylinder() tmapper.SetInputConnection(delny.GetOutputPort()) tmapper.PreventSeamOn() # We scale the texture coordinate to get some repeat patterns. xform = vtk.vtkTransformTextureCoords() xform.SetInputConnection(tmapper.GetOutputPort()) xform.SetScale(4, 4, 1) # vtkDataSetMapper internally uses a vtkGeometryFilter to extract the # surface from the triangulation. The output (which is vtkPolyData) is # then passed to an internal vtkPolyDataMapper which does the # rendering. mapper = vtk.vtkDataSetMapper() mapper.SetInputConnection(xform.GetOutputPort()) # A texture is loaded using an image reader. Textures are simply images. # The texture is eventually associated with an actor. bmpReader = vtk.vtkBMPReader() bmpReader.SetFileName(VTK_DATA_ROOT + "/Data/masonry.bmp") atext = vtk.vtkTexture()
def add_eye_ball(self, pose, orientation): # Eye ball sphere = vtk.vtkSphereSource() sphere.SetThetaResolution(64) sphere.SetPhiResolution(64) sphere.SetRadius(0.5) reader = vtk.vtkJPEGReader() reader.SetFileName(os.path.join(rospkg.RosPack().get_path('motion_renderer'), 'resource', 'green_eye.jpg')) texture = vtk.vtkTexture() texture.SetInputConnection(reader.GetOutputPort()) map_to_sphere = vtk.vtkTextureMapToSphere() map_to_sphere.SetInputConnection(sphere.GetOutputPort()) map_to_sphere.PreventSeamOn() xform = vtk.vtkTransformTextureCoords() xform.SetInputConnection(map_to_sphere.GetOutputPort()) xform.SetScale(1.5, 1.5, 1) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(xform.GetOutputPort()) # Left Eye Actor eye_actor = vtk.vtkActor() eye_actor.SetMapper(mapper) eye_actor.SetTexture(texture) eye_actor.SetPosition(pose[0], pose[1], pose[2]) eye_actor.RotateX(orientation[0]) eye_actor.RotateY(orientation[1]) eye_actor.RotateZ(orientation[2]) self.ren.AddActor(eye_actor) return eye_actor
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkTransformTextureCoords(), 'Processing.', ('vtkDataSet',), ('vtkDataSet',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def main(argv): colors = vtk.vtkNamedColors() if (len(argv) < 2): print("Usage: " + argv[0] + " texture(.png/.ppm) e.g. earth.ppm" + " [translate]") sys.exit(-1) translate = [0.0, 0.0, 0.0] if len(argv) > 2: translate[0] = float(argv[2]) else: translate[0] = 0.0 translate[1] = 0.0 translate[2] = 0.0 print("%f, %f, %f" % (translate[0], translate[1], translate[2])) # Create a sphere with texture coordinates source = vtk.vtkTexturedSphereSource() source.SetThetaResolution(100) source.SetPhiResolution(100) # Read texture file readerFactory = vtk.vtkImageReader2Factory() # imageReader = vtk.vtkImageReader2() imageReader = readerFactory.CreateImageReader2(argv[1]) imageReader.SetFileName(argv[1]) # Create texture texture = vtk.vtkTexture() texture.SetInputConnection(imageReader.GetOutputPort()) transformTexture = vtk.vtkTransformTextureCoords() transformTexture.SetInputConnection(source.GetOutputPort()) transformTexture.SetPosition(translate) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(transformTexture.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) actor.SetTexture(texture) renderer = vtk.vtkRenderer() renderer.AddActor(actor) renderer.SetBackground(colors.GetColor3d("Black")) renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindow.SetWindowName("TexturedSphere") renWinInteractor = vtk.vtkRenderWindowInteractor() renWinInteractor.SetRenderWindow(renderWindow) renderWindow.Render() renWinInteractor.Start()
def test_vtk_shananigans(self): sphere = vtk.vtkPointSource() sphere.SetNumberOfPoints(25) mesh = Mesh("data/wing_off_files/synth_wing_v3.off") # Triangulate the points with vtkDelaunay3D. This generates a convex hull # of tetrahedron. delny = vtk.vtkDelaunay3D() delny.SetInputConnection(sphere.GetOutputPort()) delny.SetTolerance(0.01) print(dir(mesh.pv_mesh)) # The triangulation has texture coordinates generated so we can map # a texture onto it. tmapper = vtk.vtkTextureMapToCylinder() tmapper.SetInputDataObject(mesh.pv_mesh.GetPointData().GetOutputPort()) tmapper.PreventSeamOn() # We scale the texture coordinate to get some repeat patterns. xform = vtk.vtkTransformTextureCoords() xform.SetInputConnection(tmapper.GetOutputPort()) xform.SetScale(4, 4, 1) # vtkDataSetMapper internally uses a vtkGeometryFilter to extract the # surface from the triangulation. The output (which is vtkPolyData) is # then passed to an internal vtkPolyDataMapper which does the # rendering. mapper = vtk.vtkDataSetMapper() mapper.SetInputConnection(xform.GetOutputPort()) # A texture is loaded using an image reader. Textures are simply images. # The texture is eventually associated with an actor. bmpReader = vtk.vtkPNGReader() bmpReader.SetFileName("data/textures/checkers.png") atext = vtk.vtkTexture() atext.SetInputConnection(bmpReader.GetOutputPort()) atext.InterpolateOn() triangulation = vtk.vtkActor() triangulation.SetMapper(mapper) triangulation.SetTexture(atext) # Create the standard rendering stuff. ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Add the actors to the renderer, set the background and size ren.AddActor(triangulation) ren.SetBackground(1, 1, 1) renWin.SetSize(300, 300) iren.Initialize() renWin.Render() iren.Start()
def assignTexture(actor, name, scale=1, falsecolors=False, mapTo=1): '''Assign a texture to actor from file or name in /textures directory''' if mapTo == 1: tmapper = vtk.vtkTextureMapToCylinder() elif mapTo == 2: tmapper = vtk.vtkTextureMapToSphere() elif mapTo == 3: tmapper = vtk.vtkTextureMapToPlane() setInput(tmapper, polydata(actor)) if mapTo == 1: tmapper.PreventSeamOn() xform = vtk.vtkTransformTextureCoords() xform.SetInputConnection(tmapper.GetOutputPort()) xform.SetScale(scale, scale, scale) if mapTo == 1: xform.FlipSOn() xform.Update() mapper = vtk.vtkDataSetMapper() mapper.SetInputConnection(xform.GetOutputPort()) mapper.ScalarVisibilityOff() cdir = os.path.dirname(__file__) if cdir == '': cdir = '.' fn = cdir + '/textures/' + name + ".jpg" if os.path.exists(name): fn = name elif not os.path.exists(fn): colors.printc(('Texture', name, 'not found in', cdir + '/textures'), 'r') colors.printc('Available textures:', c='m', end=' ') for ff in os.listdir(cdir + '/textures'): colors.printc(ff.split('.')[0], end=' ', c='m') print() return jpgReader = vtk.vtkJPEGReader() jpgReader.SetFileName(fn) atext = vtk.vtkTexture() atext.RepeatOn() atext.EdgeClampOff() atext.InterpolateOn() if falsecolors: atext.MapColorScalarsThroughLookupTableOn() atext.SetInputConnection(jpgReader.GetOutputPort()) actor.GetProperty().SetColor(1, 1, 1) actor.SetMapper(mapper) actor.SetTexture(atext)
# Generate texture coordinates on a "random" sphere. # create some random points in a sphere # sphere = vtk.vtkPointSource() sphere.SetNumberOfPoints(25) # triangulate the points # del1 = vtk.vtkDelaunay3D() del1.SetInputConnection(sphere.GetOutputPort()) del1.SetTolerance(0.01) # texture map the sphere (using cylindrical coordinate system) # tmapper = vtk.vtkTextureMapToCylinder() tmapper.SetInputConnection(del1.GetOutputPort()) tmapper.PreventSeamOn() xform = vtk.vtkTransformTextureCoords() xform.SetInputConnection(tmapper.GetOutputPort()) xform.SetScale(4, 4, 1) mapper = vtk.vtkDataSetMapper() mapper.SetInputConnection(xform.GetOutputPort()) # load in the texture map and assign to actor # bmpReader = vtk.vtkBMPReader() bmpReader.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/masonry.bmp") atext = vtk.vtkTexture() atext.SetInputConnection(bmpReader.GetOutputPort()) atext.InterpolateOn() triangulation = vtk.vtkActor() triangulation.SetMapper(mapper) triangulation.SetTexture(atext) # Create rendering stuff
def main(): # Read from file stlreader = vtk.vtkSTLReader() stlreader.SetFileName( r"D:\DT-cat_Synthetic_Data_Generation\vtk_generation\mino_3d_model\demo_part\F58001104949503200002.stl" ) tmapper = vtk.vtkTextureMapToCylinder() tmapper.SetInputConnection(stlreader.GetOutputPort()) tmapper.PreventSeamOn() xform = vtk.vtkTransformTextureCoords() xform.SetInputConnection(tmapper.GetOutputPort()) xform.SetScale(0.5, 0.5, 0.5) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(xform.GetOutputPort()) # A texture is loaded using an image reader. Textures are simply images. # The texture is eventually associated with an actor. #bmpReader = vtk.vtkBMPReader() #bmpReader.SetFileName("D:\DT-cat_Synthetic_Data_Generation\\texture\\2.bmp") #atext = vtk.vtkTexture() #atext.SetInputConnection(bmpReader.GetOutputPort()) #atext.InterpolateOn() cylinderActor = vtk.vtkActor() cylinderActor.RotateWXYZ(0, 1, 0, 0) cylinderActor.GetProperty().SetColor(0.2, 0.3, 0.7) cylinderActor.SetMapper( mapper) # 设置生成几何图元的Mapper。即连接一个Actor到可视化管线的末端(可视化管线的末端就是Mapper)。 #cylinderActor.SetTexture(atext) renderer = vtk.vtkRenderer() # 负责管理场景的渲染过程 renderer.AddActor(cylinderActor) light = vtk.vtkLight() light.SetPosition(400, 0, 00) renderer.AddLight(light) renderer.SetBackground(0.1, 0.2, 0.4) renWin = vtk.vtkRenderWindow() # 将操作系统与VTK渲染引擎连接到一起。 renWin.AddRenderer(renderer) renWin.SetSize(300, 300) iren = vtk.vtkRenderWindowInteractor() # 提供平台独立的响应鼠标、键盘和时钟事件的交互机制 iren.SetRenderWindow(renWin) # 交互器样式的一种,该样式下,用户是通过控制相机对物体作旋转、放大、缩小等操作 style = vtk.vtkInteractorStyleTrackballCamera() axes = vtk.vtkAxesActor() renderer.AddActor(axes) iren.SetInteractorStyle(style) iren.Initialize() iren.Start() # Clean up # del cylinder del stlreader del cylinderActor del renderer del renWin del iren