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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#!/usr/bin/env python
"""This test ensures that the mapper does not change the LookupTable
that may be shared among different mappers. Specifically, the vtkMapper
and vtkScalarsToColorsPainter classes use the LookupTable to map scalars
to colors. When they do this they set the LookupTable's Alpha ivar.
They should do it in a manner that the LUT's original value is reset.
If this is not done strange errors show up. For example if you use an
ImagePlaneWidget and share its LUT with another mapper that has either a
different opacity, then the image plane widget will pick up the wrong
opacity leading to subtle errors. This test ensures that this does not
happen by testing MapScalars explicitly and also by creating an
ImagePlaneWidget. """
import os
import os.path
import vtk
from vtk.test import Testing
class TestMapperLUT(Testing.vtkTest):
def testMapScalars(self):
"""Test if the mapper sets the Alpha of the LUT."""
# Create dummy data.
p = vtk.vtkPolyData()
pts = vtk.vtkPoints()
pts.InsertNextPoint((0,0,0))
sc = vtk.vtkFloatArray()
sc.InsertNextValue(10.0)
p.SetPoints(pts)
p.GetPointData().SetScalars(sc)
l = vtk.vtkLookupTable()
m = vtk.vtkPolyDataMapper()
m.SetInputData(p)
m.SetScalarRange(0.0, 10.0)
m.SetLookupTable(l)
ret = m.MapScalars(0.5)
self.assertEqual(l.GetAlpha(), 1.0)
def testImagePlaneWidget(self):
"A more rigorous test using the image plane widget."
# This test is largely copied from
# Widgets/Python/TestImagePlaneWidget.py
# Load some data.
v16 = vtk.vtkVolume16Reader()
v16.SetDataDimensions(64, 64)
v16.SetDataByteOrderToLittleEndian()
v16.SetFilePrefix(os.path.join(Testing.VTK_DATA_ROOT,
"Data", "headsq", "quarter"))
v16.SetImageRange(1, 93)
v16.SetDataSpacing(3.2, 3.2, 1.5)
v16.Update()
xMin, xMax, yMin, yMax, zMin, zMax = v16.GetExecutive().GetWholeExtent(v16.GetOutputInformation(0))
img_data = v16.GetOutput()
spacing = img_data.GetSpacing()
sx, sy, sz = spacing
origin = img_data.GetOrigin()
ox, oy, oz = origin
# An outline is shown for context.
outline = vtk.vtkOutlineFilter()
outline.SetInputData(img_data)
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)
# The shared picker enables us to use 3 planes at one time
# and gets the picking order right
picker = vtk.vtkCellPicker()
picker.SetTolerance(0.005)
# The 3 image plane widgets are used to probe the dataset.
planeWidgetX = vtk.vtkImagePlaneWidget()
planeWidgetX.DisplayTextOn()
planeWidgetX.SetInputData(img_data)
planeWidgetX.SetPlaneOrientationToXAxes()
planeWidgetX.SetSliceIndex(32)
planeWidgetX.SetPicker(picker)
planeWidgetX.SetKeyPressActivationValue("x")
prop1 = planeWidgetX.GetPlaneProperty()
prop1.SetColor(1, 0, 0)
planeWidgetY = vtk.vtkImagePlaneWidget()
planeWidgetY.DisplayTextOn()
planeWidgetY.SetInputData(img_data)
planeWidgetY.SetPlaneOrientationToYAxes()
planeWidgetY.SetSliceIndex(32)
planeWidgetY.SetPicker(picker)
planeWidgetY.SetKeyPressActivationValue("y")
prop2 = planeWidgetY.GetPlaneProperty()
prop2.SetColor(1, 1, 0)
planeWidgetY.SetLookupTable(planeWidgetX.GetLookupTable())
# for the z-slice, turn off texture interpolation:
# interpolation is now nearest neighbour, to demonstrate
# cross-hair cursor snapping to pixel centers
planeWidgetZ = vtk.vtkImagePlaneWidget()
planeWidgetZ.DisplayTextOn()
planeWidgetZ.SetInputData(img_data)
planeWidgetZ.SetPlaneOrientationToZAxes()
planeWidgetZ.SetSliceIndex(46)
planeWidgetZ.SetPicker(picker)
planeWidgetZ.SetKeyPressActivationValue("z")
prop3 = planeWidgetZ.GetPlaneProperty()
prop3.SetColor(0, 0, 1)
planeWidgetZ.SetLookupTable(planeWidgetX.GetLookupTable())
# Now create another actor with an opacity < 1 and with some
# scalars.
p = vtk.vtkPolyData()
pts = vtk.vtkPoints()
pts.InsertNextPoint((0,0,0))
sc = vtk.vtkFloatArray()
sc.InsertNextValue(1.0)
p.SetPoints(pts)
p.GetPointData().SetScalars(sc)
m = vtk.vtkPolyDataMapper()
m.SetInputData(p)
# Share the lookup table of the widgets.
m.SetLookupTable(planeWidgetX.GetLookupTable())
m.UseLookupTableScalarRangeOn()
dummyActor = vtk.vtkActor()
dummyActor.SetMapper(m)
dummyActor.GetProperty().SetOpacity(0.0)
# Create the RenderWindow and Renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.SetMultiSamples(0)
renWin.AddRenderer(ren)
# Add the dummy actor.
ren.AddActor(dummyActor)
# Add the outline actor to the renderer, set the background
# color and size
ren.AddActor(outlineActor)
renWin.SetSize(600, 600)
ren.SetBackground(0.1, 0.1, 0.2)
current_widget = planeWidgetZ
mode_widget = planeWidgetZ
# Set the interactor for the widgets
iact = vtk.vtkRenderWindowInteractor()
iact.SetRenderWindow(renWin)
planeWidgetX.SetInteractor(iact)
planeWidgetX.On()
planeWidgetY.SetInteractor(iact)
planeWidgetY.On()
planeWidgetZ.SetInteractor(iact)
planeWidgetZ.On()
# Create an initial interesting view
ren.ResetCamera();
cam1 = ren.GetActiveCamera()
cam1.Elevation(110)
cam1.SetViewUp(0, 0, -1)
cam1.Azimuth(45)
ren.ResetCameraClippingRange()
iact.Initialize()
renWin.Render()
if __name__ == "__main__":
Testing.main([(TestMapperLUT, 'test')])
|