1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
=========================================================================
Program: Visualization Toolkit
Module: TestNamedColorsIntegration.py
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================
'''
import vtk
import vtk.test.Testing
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
class StyleBaseSpike(object):
class Colors(object):
'''
Provides some wrappers for using color names.
'''
def __init__(self):
'''
Define a single instance of the NamedColors class here.
'''
self.namedColors = vtk.vtkNamedColors()
def GetRGBColor(self, colorName):
'''
Return the red, green and blue components for a
color as doubles.
'''
rgb = [0.0, 0.0, 0.0] # black
self.namedColors.GetColorRGB(colorName, rgb)
return rgb
def GetRGBAColor(self, colorName):
'''
Return the red, green, blue and alpha
components for a color as doubles.
'''
rgba = [0.0, 0.0, 0.0, 1.0] # black
self.namedColors.GetColor(colorName, rgba)
return rgba
def __init__(self, ren, renWin, iren):
self.ren = ren
self.renWin = renWin
self.iren = iren
colors = self.Colors()
self.renWin.AddRenderer(self.ren)
self.iren.SetRenderWindow(self.renWin)
self.iren.SetDesiredUpdateRate(.00001)
# Create a sphere source and actor
sphere = vtk.vtkSphereSource()
sphereMapper = vtk.vtkPolyDataMapper()
sphereMapper.SetInputConnection(sphere.GetOutputPort())
sphereActor = vtk.vtkLODActor()
sphereActor.SetMapper(sphereMapper)
sphereActor.GetProperty().SetDiffuseColor(colors.GetRGBColor('banana'))
sphereActor.GetProperty().SetSpecular(.4)
sphereActor.GetProperty().SetSpecularPower(20)
# Create the spikes using a cone source and the sphere source
cone = vtk.vtkConeSource()
cone.SetResolution(20)
glyph = vtk.vtkGlyph3D()
glyph.SetInputConnection(sphere.GetOutputPort())
glyph.SetSourceConnection(cone.GetOutputPort())
glyph.SetVectorModeToUseNormal()
glyph.SetScaleModeToScaleByVector()
glyph.SetScaleFactor(0.25)
spikeMapper = vtk.vtkPolyDataMapper()
spikeMapper.SetInputConnection(glyph.GetOutputPort())
spikeActor = vtk.vtkLODActor()
spikeActor.SetMapper(spikeMapper)
spikeActor.GetProperty().SetDiffuseColor(colors.GetRGBColor('tomato'))
spikeActor.GetProperty().SetSpecular(.4)
spikeActor.GetProperty().SetSpecularPower(20)
# Add the actors to the renderer, set the background and size
self.ren.AddActor(sphereActor)
self.ren.AddActor(spikeActor)
self.ren.SetBackground(0.1, 0.2, 0.4)
self.renWin.SetSize(300, 300)
# Render the image
self.ren.ResetCamera()
cam1 = ren.GetActiveCamera()
cam1.Zoom(1.4)
cam1.Azimuth(30)
cam1.Elevation(30)
self.renWin.Render()
class TestStyleBaseSpike(vtk.test.Testing.vtkTest):
def testStyleBaseSpike(self):
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
iRen = vtk.vtkRenderWindowInteractor()
styleBaseSpike = StyleBaseSpike(ren, renWin, iRen)
# render and interact with data
renWin.Render()
img_file = "TestStyleBaseSpike.png"
vtk.test.Testing.compareImage(iRen.GetRenderWindow(), vtk.test.Testing.getAbsImagePath(img_file), threshold=25)
vtk.test.Testing.interact()
if __name__ == "__main__":
vtk.test.Testing.main([(TestStyleBaseSpike, 'test')])
|