1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestDiscretizableColorTransferFunction.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 #include "vtkDiscretizableColorTransferFunction.h"
17 #include "vtkDoubleArray.h"
18 #include "vtkPiecewiseFunction.h"
19 #include "vtkSmartPointer.h"
20 #include "vtkUnsignedCharArray.h"
21 
22 #include <cstring>
23 
24 //----------------------------------------------------------------------------
TestDiscretizableColorTransferFunctionOpacity(int,char * [])25 int TestDiscretizableColorTransferFunctionOpacity(int, char*[])
26 {
27   // Discretizable color transfer function
28   const double controlPoints[] = {0.0, 1.0, 0.0, 0.0,
29     255.0, 0.0, 0.0, 1.0};
30 
31   vtkSmartPointer<vtkDiscretizableColorTransferFunction> dctf =
32     vtkSmartPointer<vtkDiscretizableColorTransferFunction>::New();
33   for (int i = 0; i < 2; ++i)
34   {
35     const double* xrgb = controlPoints + (i * 4);
36     dctf->AddRGBPoint(xrgb[0], xrgb[1], xrgb[2], xrgb[3]);
37   }
38 
39   // Scalar opacity transfer function
40   const double opacityControlPoints[] = {0.0, 0.0, 255.0, 0.5};
41 
42   vtkSmartPointer<vtkPiecewiseFunction> pf =
43     vtkSmartPointer<vtkPiecewiseFunction>::New();
44   for (int i = 0; i < 2; ++i)
45   {
46     const double* xalpha = opacityControlPoints + (i * 2);
47     pf->AddPoint(xalpha[0], xalpha[1]);
48   }
49 
50   // Enable opacity mapping
51   dctf->SetScalarOpacityFunction(pf);
52   dctf->EnableOpacityMappingOn();
53   dctf->Build();
54 
55   // Input scalars
56   double inputScalars[] = {0.0, 127.0, 255.0};
57   vtkSmartPointer<vtkDoubleArray> da =
58     vtkSmartPointer<vtkDoubleArray>::New();
59   for (int i = 0; i < 3; i++)
60   {
61     da->InsertNextTuple1(inputScalars[i]);
62   }
63   // Output colors
64   unsigned char mapScalarsThroughTableOutput[12];
65   vtkSmartPointer<vtkUnsignedCharArray> mapScalarsOutput;
66 
67   //--------------------------------------------------------------------------
68   //  Colors mapping only. Output format = VTK_RGB
69   //--------------------------------------------------------------------------
70 
71   // Map void* array to opacity using first entry point
72   dctf->MapScalarsThroughTable(inputScalars, mapScalarsThroughTableOutput,
73     VTK_DOUBLE, 3, 1, VTK_RGB);
74   // Map data array to opacity using second entry point
75   mapScalarsOutput.TakeReference(
76     dctf->MapScalars(da, VTK_COLOR_MODE_DEFAULT, -1));
77 
78   unsigned char* mapScalarsOutputPtr =
79     reinterpret_cast<unsigned char*>(mapScalarsOutput->GetVoidPointer(0));
80   for (int i = 0; i < 3; ++i)
81   {
82     for (int k = 0; k < 3; ++k)
83     {
84       if (mapScalarsThroughTableOutput[i * 3 + k] != mapScalarsOutputPtr[i * 4 + k])
85       {
86         return EXIT_FAILURE;
87       }
88     }
89   }
90   //--------------------------------------------------------------------------
91   //  Colors and opacity mapping. Output format = VTK_RGBA
92   //--------------------------------------------------------------------------
93 
94   // Map void* array to opacity using first entry point
95   dctf->MapScalarsThroughTable(inputScalars, mapScalarsThroughTableOutput,
96     VTK_DOUBLE, 3, 1, VTK_RGBA);
97   // Map data array to opacity using second entry point
98   mapScalarsOutput.TakeReference(
99     dctf->MapScalars(da, VTK_COLOR_MODE_MAP_SCALARS, -1));
100 
101   if (std::memcmp(mapScalarsThroughTableOutput,
102     mapScalarsOutput->GetVoidPointer(0), 3 * 4 * sizeof(unsigned char)))
103   {
104     return EXIT_FAILURE;
105   }
106 
107   return EXIT_SUCCESS;
108 }
109