1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGPURayCastMIPToComposite.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 
16 // This test covers vtkFixedPointVolumeRayCastMapper with a light where
17 // diffuse and specular components are different.
18 // This test volume renders a synthetic dataset with unsigned char values,
19 // with the composite method. The diffuse light component is gray, the
20 // light specular component is blue.
21 
22 #include "vtkSphere.h"
23 #include "vtkSampleFunction.h"
24 
25 #include "vtkTestUtilities.h"
26 #include "vtkColorTransferFunction.h"
27 #include "vtkPiecewiseFunction.h"
28 #include "vtkRenderer.h"
29 #include "vtkRenderWindow.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkVolumeProperty.h"
32 #include "vtkCamera.h"
33 #include "vtkRegressionTestImage.h"
34 #include "vtkImageShiftScale.h"
35 #include "vtkImageData.h"
36 #include "vtkPointData.h"
37 #include "vtkDataArray.h"
38 #include "vtkLight.h"
39 #include "vtkLightCollection.h"
40 #include <cassert>
41 #include "vtkFixedPointVolumeRayCastMapper.h"
42 
TestFixedPointRayCastLightComponents(int argc,char * argv[])43 int TestFixedPointRayCastLightComponents(int argc,
44                                          char *argv[])
45 {
46   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
47 
48   // Create a spherical implicit function.
49   vtkSphere *shape=vtkSphere::New();
50   shape->SetRadius(0.1);
51   shape->SetCenter(0.0,0.0,0.0);
52 
53   vtkSampleFunction *source=vtkSampleFunction::New();
54   source->SetImplicitFunction(shape);
55   shape->Delete();
56   source->SetOutputScalarTypeToDouble();
57   source->SetSampleDimensions(127,127,127); // intentional NPOT dimensions.
58   source->SetModelBounds(-100.0,100.0,-100.0,100.0,-100.0,100.0);
59   source->SetCapping(false);
60   source->SetComputeNormals(false);
61   source->SetScalarArrayName("values");
62 
63   source->Update();
64 
65   vtkDataArray *a=source->GetOutput()->GetPointData()->GetScalars("values");
66   double range[2];
67   a->GetRange(range);
68 
69   vtkImageShiftScale *t=vtkImageShiftScale::New();
70   t->SetInputConnection(source->GetOutputPort());
71   source->Delete();
72   t->SetShift(-range[0]);
73   double magnitude=range[1]-range[0];
74   if(magnitude==0.0)
75     {
76     magnitude=1.0;
77     }
78   t->SetScale(255.0/magnitude);
79   t->SetOutputScalarTypeToUnsignedChar();
80 
81   t->Update();
82 
83   vtkRenderWindow *renWin=vtkRenderWindow::New();
84   vtkRenderer *ren1=vtkRenderer::New();
85   ren1->SetBackground(0.1,0.4,0.2);
86 
87   renWin->AddRenderer(ren1);
88   ren1->Delete();
89   renWin->SetSize(301,300); // intentional odd and NPOT  width/height
90 
91   vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
92   iren->SetRenderWindow(renWin);
93   renWin->Delete();
94 
95   vtkLightCollection *lights=ren1->GetLights();
96   assert("check: lights_empty" && lights->GetNumberOfItems()==0);
97   vtkLight *light=vtkLight::New();
98   light->SetAmbientColor(0.0,0.0,0.0);
99   light->SetDiffuseColor(0.5,0.5,0.5);
100   light->SetSpecularColor(0.0,0.0,1.0);
101   light->SetIntensity(1.0);
102   // positional light is not supported by vtkFixedPointVolumeRayCastMapper
103   light->SetLightTypeToHeadlight();
104   lights->AddItem(light);
105   light->Delete();
106 
107 
108   vtkVolumeProperty *volumeProperty;
109   vtkVolume *volume;
110 
111   vtkFixedPointVolumeRayCastMapper *volumeMapper=vtkFixedPointVolumeRayCastMapper::New();
112   volumeMapper->SetSampleDistance(1.0);
113   volumeMapper->SetNumberOfThreads(1);
114 
115   volumeMapper->SetInputConnection(
116     t->GetOutputPort());
117 
118   volumeProperty=vtkVolumeProperty::New();
119   volumeMapper->SetBlendModeToComposite();
120   volumeProperty->ShadeOn();
121   volumeProperty->SetSpecularPower(128.0);
122   volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
123 
124   vtkPiecewiseFunction *compositeOpacity = vtkPiecewiseFunction::New();
125   compositeOpacity->AddPoint(0.0,1.0); // 1.0
126   compositeOpacity->AddPoint(80.0,1.0); // 1.0
127   compositeOpacity->AddPoint(80.1,0.0); // 0.0
128   compositeOpacity->AddPoint(255.0,0.0); // 0.0
129   volumeProperty->SetScalarOpacity(compositeOpacity);
130 
131   vtkColorTransferFunction *color=vtkColorTransferFunction::New();
132   color->AddRGBPoint(0.0  ,1.0,1.0,1.0); // blue
133   color->AddRGBPoint(40.0  ,1.0,1.0,1.0); // red
134   color->AddRGBPoint(255.0,1.0,1.0,1.0); // white
135   volumeProperty->SetColor(color);
136   color->Delete();
137 
138   volume=vtkVolume::New();
139   volume->SetMapper(volumeMapper);
140   volume->SetProperty(volumeProperty);
141   ren1->AddViewProp(volume);
142 
143   int retVal;
144 
145   ren1->ResetCamera();
146   renWin->Render();
147 
148   retVal = vtkTesting::Test(argc, argv, renWin, 75);
149   if (retVal == vtkRegressionTester::DO_INTERACTOR)
150     {
151     iren->Start();
152     }
153 
154   volumeMapper->Delete();
155   volumeProperty->Delete();
156   volume->Delete();
157   iren->Delete();
158   t->Delete();
159   compositeOpacity->Delete();
160 
161   return !((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR));
162 }
163