1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestGPURayCastClipping.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 // This test covers cropping on volume datasets.
16
17 #include <vtkActor.h>
18 #include <vtkColorTransferFunction.h>
19 #include <vtkCommand.h>
20 #include <vtkGPUVolumeRayCastMapper.h>
21 #include <vtkImageData.h>
22 #include <vtkNew.h>
23 #include <vtkOutlineFilter.h>
24 #include <vtkPiecewiseFunction.h>
25 #include <vtkPlane.h>
26 #include <vtkPlaneCollection.h>
27 #include <vtkPolyDataMapper.h>
28 #include <vtkRegressionTestImage.h>
29 #include <vtkRenderWindow.h>
30 #include <vtkRenderWindowInteractor.h>
31 #include <vtkRenderer.h>
32 #include <vtkSmartPointer.h>
33 #include <vtkSphereSource.h>
34 #include <vtkTestUtilities.h>
35 #include <vtkTimerLog.h>
36 #include <vtkVolumeProperty.h>
37 #include <vtkXMLImageDataReader.h>
38
TestGPURayCastClipping(int argc,char * argv[])39 int TestGPURayCastClipping(int argc, char *argv[])
40 {
41 double scalarRange[2];
42
43 vtkNew<vtkActor> outlineActor;
44 vtkNew<vtkPolyDataMapper> outlineMapper;
45 vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper;
46
47 vtkNew<vtkXMLImageDataReader> reader;
48 const char* volumeFile = vtkTestUtilities::ExpandDataFileName(
49 argc, argv, "Data/vase_1comp.vti");
50
51 reader->SetFileName(volumeFile);
52 reader->Update();
53 volumeMapper->SetInputConnection(reader->GetOutputPort());
54
55 // Add outline filter
56 vtkNew<vtkOutlineFilter> outlineFilter;
57 outlineFilter->SetInputConnection(reader->GetOutputPort());
58 outlineMapper->SetInputConnection(outlineFilter->GetOutputPort());
59 outlineActor->SetMapper(outlineMapper.GetPointer());
60
61 volumeMapper->GetInput()->GetScalarRange(scalarRange);
62 volumeMapper->SetBlendModeToComposite();
63
64 // Testing prefers image comparison with small images
65 vtkNew<vtkRenderWindow> renWin;
66 renWin->SetSize(400, 400);
67
68 // Intentional odd and NPOT width/height
69 vtkNew<vtkRenderer> ren;
70 renWin->AddRenderer(ren.GetPointer());
71
72 vtkNew<vtkRenderWindowInteractor> iren;
73 iren->SetRenderWindow(renWin.GetPointer());
74
75 vtkNew<vtkPiecewiseFunction> scalarOpacity;
76 scalarOpacity->AddPoint(scalarRange[0], 0.0);
77 scalarOpacity->AddPoint(scalarRange[1], 1.0);
78
79 vtkNew<vtkVolumeProperty> volumeProperty;
80 volumeProperty->ShadeOff();
81 volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
82 volumeProperty->SetScalarOpacity(scalarOpacity.GetPointer());
83
84 vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction =
85 volumeProperty->GetRGBTransferFunction(0);
86 colorTransferFunction->RemoveAllPoints();
87 colorTransferFunction->AddRGBPoint(scalarRange[0], 0.0, 0.0, 0.0);
88 colorTransferFunction->AddRGBPoint(scalarRange[1], 1.0, 1.0, 1.0);
89
90 // Test cropping now
91 double* bounds = reader->GetOutput()->GetBounds();
92 vtkNew<vtkPlane> clipPlane1;
93 clipPlane1->SetOrigin(0.45 * (bounds[0] + bounds[1]), 0.0, 0.0);
94 clipPlane1->SetNormal(1.0, 0.0, 0.0);
95
96 vtkNew<vtkPlane> clipPlane2;
97 clipPlane2->SetOrigin(0.55 * (bounds[0] + bounds[1]), 0.0, 0.0);
98 clipPlane2->SetNormal(-1.0, 0.0, 0.0);
99
100 vtkNew<vtkPlaneCollection> clipPlaneCollection;
101 clipPlaneCollection->AddItem(clipPlane1.GetPointer());
102 clipPlaneCollection->AddItem(clipPlane2.GetPointer());
103 volumeMapper->SetClippingPlanes(clipPlaneCollection.GetPointer());
104
105 // Setup volume actor
106 vtkNew<vtkVolume> volume;
107 volume->SetMapper(volumeMapper.GetPointer());
108 volume->SetProperty(volumeProperty.GetPointer());
109
110 ren->AddViewProp(volume.GetPointer());
111 ren->AddActor(outlineActor.GetPointer());
112 ren->ResetCamera();
113 renWin->Render();
114 iren->Initialize();
115
116 int retVal = vtkRegressionTestImage( renWin.GetPointer() );
117 if( retVal == vtkRegressionTester::DO_INTERACTOR)
118 {
119 iren->Start();
120 }
121
122 return !retVal;
123 }
124