1 #include "vtkNew.h"
2 #include "vtkScalarsToColors.h"
3 #include "vtkStringArray.h"
4 #include "vtkVariant.h"
5 #include "vtkVariantArray.h"
6
7 //----------------------------------------------------------------------------
TestRange()8 static bool TestRange()
9 {
10 bool success = true;
11
12 vtkNew<vtkScalarsToColors> lut;
13
14 // Check default range.
15 const double *range = lut->GetRange();
16 if (range[0] != 0.0 || range[1] != 255.0)
17 {
18 cerr << "Default range wrong\n";
19 success = false;
20 }
21
22 // nop range change.
23 lut->SetRange(0.0, 255.0);
24 range = lut->GetRange();
25 if (range[0] != 0.0 || range[1] != 255.0)
26 {
27 cerr << "nop range change failed\n";
28 success = false;
29 }
30
31 // actual range change.
32 lut->SetRange(100.0, 200.0);
33 range = lut->GetRange();
34 if (range[0] != 100.0 || range[1] != 200.0)
35 {
36 cerr << "range change failed\n";
37 success = false;
38 }
39
40 return success;
41 }
42
43 //----------------------------------------------------------------------------
TestAlpha()44 static bool TestAlpha()
45 {
46 bool success = true;
47
48 vtkNew<vtkScalarsToColors> lut;
49
50 // Check default alpha.
51 double alpha = lut->GetAlpha();
52 if (alpha != 1.0)
53 {
54 cerr << "Default alpha wrong\n";
55 success = false;
56 }
57
58 // Set out of range.
59 lut->SetAlpha(-12345.6);
60 alpha = lut->GetAlpha();
61 if (alpha != 0.0)
62 {
63 cerr << "Alpha clamp fail\n";
64 success = false;
65 }
66
67 lut->SetAlpha(45657.8);
68 alpha = lut->GetAlpha();
69 if (alpha != 1.0)
70 {
71 cerr << "Alpha clamp fail\n";
72 success = false;
73 }
74
75 return success;
76 }
77
78 //----------------------------------------------------------------------------
TestGetColorAndMapValue()79 static bool TestGetColorAndMapValue()
80 {
81 bool success = true;
82
83 vtkNew<vtkScalarsToColors> lut;
84
85 double rgb[3] = {0.1, 0.2, 0.3};
86 const unsigned char * rgba = NULL;
87
88 // Sane range.
89 lut->SetRange(0.0, 1.0);
90 lut->GetColor(0.5, rgb);
91 rgba = lut->MapValue(0.5);
92
93 if (rgb[0] != 0.5 || rgb[1] != 0.5 || rgb[2] != 0.5)
94 {
95 cerr << "GetColor result wrong\n";
96 success = false;
97 }
98 if (rgba[0] != 128 || rgba[1] != 128 || rgba[2] != 128 || rgba[3] != 255)
99 {
100 cerr << "MapValue result wrong\n";
101 success = false;
102 }
103
104
105 // Tiny range.
106 lut->SetRange(0.0, 1e-80);
107 lut->GetColor(1e-79, rgb);
108 rgba = lut->MapValue(1e-79);
109
110 if (rgb[0] != 1e-62 || rgb[1] != 1e-62 || rgb[2] != 1e-62)
111 {
112 cerr << "GetColor result wrong\n";
113 success = false;
114 }
115 if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0 || rgba[3] != 255)
116 {
117 cerr << "MapValue result wrong\n";
118 success = false;
119 }
120
121 return success;
122 }
123
124 //----------------------------------------------------------------------------
TestDeepCopy()125 static bool TestDeepCopy()
126 {
127 bool success = true;
128
129 vtkNew<vtkScalarsToColors> lut;
130
131 vtkNew<vtkStringArray> ann;
132 ann->InsertNextValue("HelloWorld");
133 vtkNew<vtkVariantArray> val;
134 val->InsertNextValue(vtkVariant(123.4));
135 lut->SetAnnotations(val.Get(), ann.Get());
136
137 // Test nop DeepCopy.
138 vtkNew<vtkScalarsToColors> copy1;
139 copy1->DeepCopy(NULL);
140
141 // Test actual copy.
142 vtkNew<vtkScalarsToColors> copy2;
143 copy2->DeepCopy(lut.Get());
144
145 vtkStringArray* ann2 = copy2->GetAnnotations();
146 vtkAbstractArray* val2 = copy2->GetAnnotatedValues();
147 if (!ann2 || !val2)
148 {
149 cerr << "Annotations not copied\n";
150 success = false;
151 }
152 if (ann.Get() == ann2 || val.Get() == val2)
153 {
154 cerr << "Annotations only shallow copied\n";
155 success = false;
156 }
157 int idx = lut->GetAnnotatedValueIndex(123.4);
158 if (idx != 0)
159 {
160 cerr << "Could not find annotated value 123.4.\n";
161 success = false;
162 }
163
164 return success;
165 }
166
167 //----------------------------------------------------------------------------
TestGeneral()168 static bool TestGeneral()
169 {
170 bool success = true;
171
172 vtkNew<vtkScalarsToColors> lut;
173
174 lut->SetAnnotations(NULL, NULL);
175 vtkStringArray* ann2 = lut->GetAnnotations();
176 vtkAbstractArray* val2 = lut->GetAnnotatedValues();
177 if (ann2 || val2)
178 {
179 cerr << "Annotations set to NULL but didn't return NULL\n";
180 success = false;
181 }
182
183 vtkNew<vtkStringArray> ann;
184 ann->InsertNextValue("Foo");
185 vtkNew<vtkVariantArray> val;
186 val->InsertNextValue(vtkVariant(10.3));
187 lut->SetAnnotations(val.Get(), ann.Get());
188 ann2 = lut->GetAnnotations();
189 val2 = lut->GetAnnotatedValues();
190 if (!ann2 || !val2)
191 {
192 cerr << "Annotations set to non-NULL but returned NULL\n";
193 success = false;
194 }
195
196 int idx = lut->GetAnnotatedValueIndex(10.3);
197 if (idx != 0)
198 {
199 cerr << "Could not find annotated value 10.3.\n";
200 success = false;
201 }
202
203 idx = lut->GetAnnotatedValueIndex("Narf");
204 if (idx >= 0)
205 {
206 cerr << "Found unexpected annotated value \"Narf\".\n";
207 success = false;
208 }
209
210 ann->InsertNextValue("Not hardly!");
211 val->InsertNextValue("Narf");
212 ann->InsertNextValue("Fezzik");
213 val->InsertNextValue(vtkVariant(20));
214 lut->SetAnnotations(val.Get(), ann.Get());
215
216 idx = lut->GetAnnotatedValueIndex("Narf");
217 if (idx != 1)
218 {
219 cerr << "Couldn't find newly-annotated value (\"Narf\").\n";
220 success = false;
221 }
222
223 lut->SetAnnotations(NULL, NULL);
224 ann2 = lut->GetAnnotations();
225 val2 = lut->GetAnnotatedValues();
226 if (ann2 || val2)
227 {
228 cerr << "Annotations again set to NULL but didn't return NULL\n";
229 success = false;
230 }
231
232 return success;
233 }
234
235 //----------------------------------------------------------------------------
TestScalarsToColors(int,char * [])236 int TestScalarsToColors(int, char*[])
237 {
238 bool success1 = TestRange();
239 bool success2 = TestAlpha();
240 bool success3 = TestGetColorAndMapValue();
241 bool success4 = TestDeepCopy();
242 bool success5 = TestGeneral();
243
244 if (success1 && success2 && success3 && success4 && success5)
245 {
246 return EXIT_SUCCESS;
247 }
248 else
249 {
250 return EXIT_FAILURE;
251 }
252 }
253