1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestColorByCellDataStringArray.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 "vtkTestUtilities.h"
17 #include "vtkRegressionTestImage.h"
18 
19 #include <vtkActor.h>
20 #include <vtkCellData.h>
21 #include <vtkDiscretizableColorTransferFunction.h>
22 #include <vtkNew.h>
23 #include <vtkPolyData.h>
24 #include <vtkPolyDataMapper.h>
25 #include <vtkRenderer.h>
26 #include <vtkRenderWindow.h>
27 #include <vtkRenderWindowInteractor.h>
28 #include <vtkSphereSource.h>
29 #include <vtkStdString.h>
30 #include <vtkStringArray.h>
31 
32 
TestColorByCellDataStringArray(int argc,char * argv[])33 int TestColorByCellDataStringArray(int argc, char* argv[])
34 {
35   vtkNew<vtkSphereSource> sphere;
36   sphere->Update();
37 
38   vtkNew<vtkPolyData> polydata;
39   polydata->ShallowCopy(sphere->GetOutput());
40 
41   // Set up string array associated with cells
42   vtkNew<vtkStringArray> sArray;
43   sArray->SetName("color");
44   sArray->SetNumberOfComponents(1);
45   sArray->SetNumberOfTuples(polydata->GetNumberOfCells());
46 
47   vtkVariant colors[5];
48   colors[0] = "red";
49   colors[1] = "blue";
50   colors[2] = "green";
51   colors[3] = "yellow";
52   colors[4] = "cyan";
53 
54   // Round-robin assignment of color strings
55   for (int i = 0; i < polydata->GetNumberOfCells(); ++i)
56   {
57     sArray->SetValue(i, colors[i % 5].ToString());
58   }
59 
60   vtkCellData* cd = polydata->GetCellData();
61   cd->AddArray(sArray);
62 
63   // Set up transfer function
64   vtkNew<vtkDiscretizableColorTransferFunction> tfer;
65   tfer->IndexedLookupOn();
66   tfer->SetNumberOfIndexedColors(5);
67   tfer->SetIndexedColor(0, 1.0, 0.0, 0.0);
68   tfer->SetIndexedColor(1, 0.0, 0.0, 1.0);
69   tfer->SetIndexedColor(2, 0.0, 1.0, 0.0);
70   tfer->SetIndexedColor(3, 1.0, 1.0, 0.0);
71   tfer->SetIndexedColor(4, 0.0, 1.0, 1.0);
72 
73   vtkStdString red("red");
74   tfer->SetAnnotation(red, red);
75   vtkStdString blue("blue");
76   tfer->SetAnnotation(blue, blue);
77   vtkStdString green("green");
78   tfer->SetAnnotation(green, green);
79   vtkStdString yellow("yellow");
80   tfer->SetAnnotation(yellow, yellow);
81   vtkStdString cyan("cyan");
82   tfer->SetAnnotation(cyan, cyan);
83 
84   vtkNew<vtkPolyDataMapper> mapper;
85   mapper->SetInputDataObject(polydata);
86   mapper->SetLookupTable(tfer);
87   mapper->ScalarVisibilityOn();
88   mapper->SetScalarModeToUseCellFieldData();
89   mapper->SelectColorArray("color");
90 
91   vtkNew<vtkActor> actor;
92   actor->SetMapper(mapper);
93 
94   vtkNew<vtkRenderer> renderer;
95   renderer->AddActor(actor);
96 
97   vtkNew<vtkRenderWindow> renderWindow;
98   renderWindow->AddRenderer(renderer);
99 
100   vtkNew<vtkRenderWindowInteractor> iren;
101   iren->SetRenderWindow(renderWindow);
102 
103   renderWindow->Render();
104 
105   int retVal = vtkRegressionTestImage(renderWindow);
106   if (retVal == vtkRegressionTester::DO_INTERACTOR)
107   {
108     iren->Start();
109   }
110 
111   return !retVal;
112 }
113