1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4'''
5=========================================================================
6
7  Program:   Visualization Toolkit
8  Module:    TestNamedColorsIntegration.py
9
10  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
11  All rights reserved.
12  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
13
14     This software is distributed WITHOUT ANY WARRANTY; without even
15     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16     PURPOSE.  See the above copyright notice for more information.
17
18=========================================================================
19'''
20
21import vtk
22import vtk.test.Testing
23from vtk.util.misc import vtkGetDataRoot
24VTK_DATA_ROOT = vtkGetDataRoot()
25
26class FixedPointRayCasterNearest(object):
27
28    def __init__(self, ren, renWin, iren):
29        self.ren = ren
30        self.renWin = renWin
31        self.iren = iren
32
33        # Create a gaussian
34        gs = vtk.vtkImageGaussianSource()
35        gs.SetWholeExtent(0, 30, 0, 30, 0, 30)
36        gs.SetMaximum(255.0)
37        gs.SetStandardDeviation(5)
38        gs.SetCenter(15, 15, 15)
39
40        # threshold to leave a gap that should show up for
41        # gradient opacity
42        t = vtk.vtkImageThreshold()
43        t.SetInputConnection(gs.GetOutputPort())
44        t.ReplaceInOn()
45        t.SetInValue(0)
46        t.ThresholdBetween(150, 200)
47
48        # Use a shift scale to convert to unsigned char
49        ss = vtk.vtkImageShiftScale()
50        ss.SetInputConnection(t.GetOutputPort())
51        ss.SetOutputScalarTypeToUnsignedChar()
52
53        # grid will be used for two component dependent
54        grid0 = vtk.vtkImageGridSource()
55        grid0.SetDataScalarTypeToUnsignedChar()
56        grid0.SetGridSpacing(10, 10, 10)
57        grid0.SetLineValue(200)
58        grid0.SetFillValue(10)
59        grid0.SetDataExtent(0, 30, 0, 30, 0, 30)
60
61        # use dilation to thicken the grid
62        d = vtk.vtkImageContinuousDilate3D()
63        d.SetInputConnection(grid0.GetOutputPort())
64        d.SetKernelSize(3, 3, 3)
65
66        # Now make a two component dependent
67        iac = vtk.vtkImageAppendComponents()
68        iac.AddInputConnection(d.GetOutputPort())
69        iac.AddInputConnection(ss.GetOutputPort())
70
71        # Some more gaussians for the four component indepent case
72        gs1 = vtk.vtkImageGaussianSource()
73        gs1.SetWholeExtent(0, 30, 0, 30, 0, 30)
74        gs1.SetMaximum(255.0)
75        gs1.SetStandardDeviation(4)
76        gs1.SetCenter(5, 5, 5)
77
78        t1 = vtk.vtkImageThreshold()
79        t1.SetInputConnection(gs1.GetOutputPort())
80        t1.ReplaceInOn()
81        t1.SetInValue(0)
82        t1.ThresholdBetween(150, 256)
83
84        gs2 = vtk.vtkImageGaussianSource()
85        gs2.SetWholeExtent(0, 30, 0, 30, 0, 30)
86        gs2.SetMaximum(255.0)
87        gs2.SetStandardDeviation(4)
88        gs2.SetCenter(12, 12, 12)
89
90        gs3 = vtk.vtkImageGaussianSource()
91        gs3.SetWholeExtent(0, 30, 0, 30, 0, 30)
92        gs3.SetMaximum(255.0)
93        gs3.SetStandardDeviation(4)
94        gs3.SetCenter(19, 19, 19)
95
96        t3 = vtk.vtkImageThreshold()
97        t3.SetInputConnection(gs3.GetOutputPort())
98        t3.ReplaceInOn()
99        t3.SetInValue(0)
100        t3.ThresholdBetween(150, 256)
101
102        gs4 = vtk.vtkImageGaussianSource()
103        gs4.SetWholeExtent(0, 30, 0, 30, 0, 30)
104        gs4.SetMaximum(255.0)
105        gs4.SetStandardDeviation(4)
106        gs4.SetCenter(26, 26, 26)
107
108        # we need a few append filters ...
109        iac1 = vtk.vtkImageAppendComponents()
110        iac1.AddInputConnection(t1.GetOutputPort())
111        iac1.AddInputConnection(gs2.GetOutputPort())
112
113        iac2 = vtk.vtkImageAppendComponents()
114        iac2.AddInputConnection(iac1.GetOutputPort())
115        iac2.AddInputConnection(t3.GetOutputPort())
116
117        iac3 = vtk.vtkImageAppendComponents()
118        iac3.AddInputConnection(iac2.GetOutputPort())
119        iac3.AddInputConnection(gs4.GetOutputPort())
120
121        # create the four component dependend -
122        # use lines in x, y, z for colors
123        gridR = vtk.vtkImageGridSource()
124        gridR.SetDataScalarTypeToUnsignedChar()
125        gridR.SetGridSpacing(10, 100, 100)
126        gridR.SetLineValue(250)
127        gridR.SetFillValue(100)
128        gridR.SetDataExtent(0, 30, 0, 30, 0, 30)
129
130        dR = vtk.vtkImageContinuousDilate3D()
131        dR.SetInputConnection(gridR.GetOutputPort())
132        dR.SetKernelSize(2, 2, 2)
133
134        gridG = vtk.vtkImageGridSource()
135        gridG.SetDataScalarTypeToUnsignedChar()
136        gridG.SetGridSpacing(100, 10, 100)
137        gridG.SetLineValue(250)
138        gridG.SetFillValue(100)
139        gridG.SetDataExtent(0, 30, 0, 30, 0, 30)
140
141        dG = vtk.vtkImageContinuousDilate3D()
142        dG.SetInputConnection(gridG.GetOutputPort())
143        dG.SetKernelSize(2, 2, 2)
144
145        gridB = vtk.vtkImageGridSource()
146        gridB.SetDataScalarTypeToUnsignedChar()
147        gridB.SetGridSpacing(100, 100, 10)
148        gridB.SetLineValue(0)
149        gridB.SetFillValue(250)
150        gridB.SetDataExtent(0, 30, 0, 30, 0, 30)
151
152        dB = vtk.vtkImageContinuousDilate3D()
153        dB.SetInputConnection(gridB.GetOutputPort())
154        dB.SetKernelSize(2, 2, 2)
155
156        # need some appending
157        iacRG = vtk.vtkImageAppendComponents()
158        iacRG.AddInputConnection(dR.GetOutputPort())
159        iacRG.AddInputConnection(dG.GetOutputPort())
160
161        iacRGB = vtk.vtkImageAppendComponents()
162        iacRGB.AddInputConnection(iacRG.GetOutputPort())
163        iacRGB.AddInputConnection(dB.GetOutputPort())
164
165        iacRGBA = vtk.vtkImageAppendComponents()
166        iacRGBA.AddInputConnection(iacRGB.GetOutputPort())
167        iacRGBA.AddInputConnection(ss.GetOutputPort())
168
169        # We need a bunch of opacity functions
170
171        # this one is a simple ramp to .2
172        rampPoint2 = vtk.vtkPiecewiseFunction()
173        rampPoint2.AddPoint(0, 0.0)
174        rampPoint2.AddPoint(255, 0.2)
175
176        # this one is a simple ramp to 1
177        ramp1 = vtk.vtkPiecewiseFunction()
178        ramp1.AddPoint(0, 0.0)
179        ramp1.AddPoint(255, 1.0)
180
181        # this one shows a sharp surface
182        surface = vtk.vtkPiecewiseFunction()
183        surface.AddPoint(0, 0.0)
184        surface.AddPoint(10, 0.0)
185        surface.AddPoint(50, 1.0)
186        surface.AddPoint(255, 1.0)
187
188
189        # this one is constant 1
190        constant1 = vtk.vtkPiecewiseFunction()
191        constant1.AddPoint(0, 1.0)
192        constant1.AddPoint(255, 1.0)
193
194        # this one is used for gradient opacity
195        gop = vtk.vtkPiecewiseFunction()
196        gop.AddPoint(0, 0.0)
197        gop.AddPoint(20, 0.0)
198        gop.AddPoint(60, 1.0)
199        gop.AddPoint(255, 1.0)
200
201
202        # We need a bunch of color functions
203
204        # This one is a simple rainbow
205        rainbow = vtk.vtkColorTransferFunction()
206        rainbow.SetColorSpaceToHSV()
207        rainbow.HSVWrapOff()
208        rainbow.AddHSVPoint(0, 0.1, 1.0, 1.0)
209        rainbow.AddHSVPoint(255, 0.9, 1.0, 1.0)
210
211        # this is constant red
212        red = vtk.vtkColorTransferFunction()
213        red.AddRGBPoint(0, 1, 0, 0)
214        red.AddRGBPoint(255, 1, 0, 0)
215
216        # this is constant green
217        green = vtk.vtkColorTransferFunction()
218        green.AddRGBPoint(0, 0, 1, 0)
219        green.AddRGBPoint(255, 0, 1, 0)
220
221        # this is constant blue
222        blue = vtk.vtkColorTransferFunction()
223        blue.AddRGBPoint(0, 0, 0, 1)
224        blue.AddRGBPoint(255, 0, 0, 1)
225
226        # this is constant yellow
227        yellow = vtk.vtkColorTransferFunction()
228        yellow.AddRGBPoint(0, 1, 1, 0)
229        yellow.AddRGBPoint(255, 1, 1, 0)
230
231
232        #ren = vtk.vtkRenderer()
233        #renWin = vtk.vtkRenderWindow()
234        self.renWin.AddRenderer(self.ren)
235        self.renWin.SetSize(500, 500)
236        #iren = vtk.vtkRenderWindowInteractor()
237        self.iren.SetRenderWindow(self.renWin)
238
239        self.ren.GetCullers().InitTraversal()
240        culler = self.ren.GetCullers().GetNextItem()
241        culler.SetSortingStyleToBackToFront()
242
243        # We need 25 mapper / actor pairs which we will render
244        # in a grid. Going down we will vary the input data
245        # with the top row unsigned char, then float, then
246        # two dependent components, then four dependent components
247        # then four independent components. Going across we
248        # will vary the rendering method with MIP, Composite,
249        # Composite Shade, Composite GO, and Composite GO Shade.
250
251        # Create the 5 by 5 grids
252        self.volumeProperty = [[0 for col in range(0, 5)] for row in range(0, 5)]
253        self.volumeMapper = [[0 for col in range(0, 5)] for row in range(0, 5)]
254        volume = [[0 for col in range(0, 5)] for row in range(0, 5)]
255
256        for i in range(0, 5):
257            for j in range(0, 5):
258
259                self.volumeProperty[i][j] = vtk.vtkVolumeProperty()
260                self.volumeMapper[i][j] = vtk.vtkFixedPointVolumeRayCastMapper()
261                self.volumeMapper[i][j].SetSampleDistance(0.25)
262                self.volumeMapper[i][j].SetNumberOfThreads(1)
263
264                volume[i][j] = vtk.vtkVolume()
265                volume[i][j].SetMapper(self.volumeMapper[i][j])
266                volume[i][j].SetProperty(self.volumeProperty[i][j])
267
268                volume[i][j].AddPosition(i * 30, j * 30, 0)
269
270                self.ren.AddVolume(volume[i][j])
271
272
273
274        for i in range(0, 5):
275
276            self.volumeMapper[0][i].SetInputConnection(t.GetOutputPort())
277            self.volumeMapper[1][i].SetInputConnection(ss.GetOutputPort())
278            self.volumeMapper[2][i].SetInputConnection(iac.GetOutputPort())
279            self.volumeMapper[3][i].SetInputConnection(iac3.GetOutputPort())
280            self.volumeMapper[4][i].SetInputConnection(iacRGBA.GetOutputPort())
281
282            self.volumeMapper[i][0].SetBlendModeToMaximumIntensity()
283            self.volumeMapper[i][1].SetBlendModeToComposite()
284            self.volumeMapper[i][2].SetBlendModeToComposite()
285            self.volumeMapper[i][3].SetBlendModeToComposite()
286            self.volumeMapper[i][4].SetBlendModeToComposite()
287
288            self.volumeProperty[0][i].IndependentComponentsOn()
289            self.volumeProperty[1][i].IndependentComponentsOn()
290            self.volumeProperty[2][i].IndependentComponentsOff()
291            self.volumeProperty[3][i].IndependentComponentsOn()
292            self.volumeProperty[4][i].IndependentComponentsOff()
293
294            self.volumeProperty[0][i].SetColor(rainbow)
295            self.volumeProperty[0][i].SetScalarOpacity(rampPoint2)
296            self.volumeProperty[0][i].SetGradientOpacity(constant1)
297
298            self.volumeProperty[1][i].SetColor(rainbow)
299            self.volumeProperty[1][i].SetScalarOpacity(rampPoint2)
300            self.volumeProperty[1][i].SetGradientOpacity(constant1)
301
302            self.volumeProperty[2][i].SetColor(rainbow)
303            self.volumeProperty[2][i].SetScalarOpacity(rampPoint2)
304            self.volumeProperty[2][i].SetGradientOpacity(constant1)
305
306            self.volumeProperty[3][i].SetColor(0, red)
307            self.volumeProperty[3][i].SetColor(1, green)
308            self.volumeProperty[3][i].SetColor(2, blue)
309            self.volumeProperty[3][i].SetColor(3, yellow)
310            self.volumeProperty[3][i].SetScalarOpacity(0, rampPoint2)
311            self.volumeProperty[3][i].SetScalarOpacity(1, rampPoint2)
312            self.volumeProperty[3][i].SetScalarOpacity(2, rampPoint2)
313            self.volumeProperty[3][i].SetScalarOpacity(3, rampPoint2)
314            self.volumeProperty[3][i].SetGradientOpacity(0, constant1)
315            self.volumeProperty[3][i].SetGradientOpacity(1, constant1)
316            self.volumeProperty[3][i].SetGradientOpacity(2, constant1)
317            self.volumeProperty[3][i].SetGradientOpacity(3, constant1)
318            self.volumeProperty[3][i].SetComponentWeight(0, 1)
319            self.volumeProperty[3][i].SetComponentWeight(1, 1)
320            self.volumeProperty[3][i].SetComponentWeight(2, 1)
321            self.volumeProperty[3][i].SetComponentWeight(3, 1)
322
323            self.volumeProperty[4][i].SetColor(rainbow)
324            self.volumeProperty[4][i].SetScalarOpacity(rampPoint2)
325            self.volumeProperty[4][i].SetGradientOpacity(constant1)
326
327            self.volumeProperty[i][2].ShadeOn()
328            self.volumeProperty[i][4].ShadeOn(0)
329            self.volumeProperty[i][4].ShadeOn(1)
330            self.volumeProperty[i][4].ShadeOn(2)
331            self.volumeProperty[i][4].ShadeOn(3)
332
333
334        self.volumeProperty[0][0].SetScalarOpacity(ramp1)
335        self.volumeProperty[1][0].SetScalarOpacity(ramp1)
336        self.volumeProperty[2][0].SetScalarOpacity(ramp1)
337        self.volumeProperty[3][0].SetScalarOpacity(0, surface)
338        self.volumeProperty[3][0].SetScalarOpacity(1, surface)
339        self.volumeProperty[3][0].SetScalarOpacity(2, surface)
340        self.volumeProperty[3][0].SetScalarOpacity(3, surface)
341        self.volumeProperty[4][0].SetScalarOpacity(ramp1)
342
343        self.volumeProperty[0][2].SetScalarOpacity(surface)
344        self.volumeProperty[1][2].SetScalarOpacity(surface)
345        self.volumeProperty[2][2].SetScalarOpacity(surface)
346        self.volumeProperty[3][2].SetScalarOpacity(0, surface)
347        self.volumeProperty[3][2].SetScalarOpacity(1, surface)
348        self.volumeProperty[3][2].SetScalarOpacity(2, surface)
349        self.volumeProperty[3][2].SetScalarOpacity(3, surface)
350        self.volumeProperty[4][2].SetScalarOpacity(surface)
351
352        self.volumeProperty[0][4].SetScalarOpacity(surface)
353        self.volumeProperty[1][4].SetScalarOpacity(surface)
354        self.volumeProperty[2][4].SetScalarOpacity(surface)
355        self.volumeProperty[3][4].SetScalarOpacity(0, surface)
356        self.volumeProperty[3][4].SetScalarOpacity(1, surface)
357        self.volumeProperty[3][4].SetScalarOpacity(2, surface)
358        self.volumeProperty[3][4].SetScalarOpacity(3, surface)
359        self.volumeProperty[4][4].SetScalarOpacity(surface)
360
361        self.volumeProperty[0][3].SetGradientOpacity(gop)
362        self.volumeProperty[1][3].SetGradientOpacity(gop)
363        self.volumeProperty[2][3].SetGradientOpacity(gop)
364        self.volumeProperty[3][3].SetGradientOpacity(0, gop)
365        self.volumeProperty[3][3].SetGradientOpacity(2, gop)
366        self.volumeProperty[4][3].SetGradientOpacity(gop)
367
368        self.volumeProperty[3][3].SetScalarOpacity(0, ramp1)
369        self.volumeProperty[3][3].SetScalarOpacity(2, ramp1)
370
371        self.volumeProperty[0][4].SetGradientOpacity(gop)
372        self.volumeProperty[1][4].SetGradientOpacity(gop)
373        self.volumeProperty[2][4].SetGradientOpacity(gop)
374        self.volumeProperty[3][4].SetGradientOpacity(0, gop)
375        self.volumeProperty[3][4].SetGradientOpacity(2, gop)
376        self.volumeProperty[4][4].SetGradientOpacity(gop)
377
378        self.renWin.Render()
379
380        self.ren.GetActiveCamera().Dolly(1.3)
381        self.ren.GetActiveCamera().Azimuth(15)
382        self.ren.GetActiveCamera().Elevation(5)
383        self.ren.ResetCameraClippingRange()
384
385
386        #        self.renWin.Render()
387
388    def GetVolumeProperty(self):
389        ''' Return the volumeProperty so other tests can use it.'''
390        return self.volumeProperty
391
392    def GetVolumeMapper(self):
393        ''' Return the volumeMapper so other tests can use it.'''
394        return self.volumeMapper
395
396class TestFixedPointRayCasterNearest(vtk.test.Testing.vtkTest):
397
398    def testFixedPointRayCasterNearest(self):
399        ren = vtk.vtkRenderer()
400        renWin = vtk.vtkRenderWindow()
401        iRen = vtk.vtkRenderWindowInteractor()
402
403        tFPRCN = FixedPointRayCasterNearest(ren, renWin, iRen)
404
405        # render and interact with data
406
407        renWin.Render()
408
409        img_file = "TestFixedPointRayCasterNearest.png"
410        vtk.test.Testing.compareImage(iRen.GetRenderWindow(), vtk.test.Testing.getAbsImagePath(img_file), threshold=10)
411        vtk.test.Testing.interact()
412
413if __name__ == "__main__":
414     vtk.test.Testing.main([(TestFixedPointRayCasterNearest, 'test')])
415