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