ITK  5.4.0
Insight Toolkit
itkMeshIOTestHelper.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkMeshIOTestHelper_h
19#define itkMeshIOTestHelper_h
20
21#include "itkArray.h"
22#include "itkCommonEnums.h"
23#include "itkCovariantVector.h"
24#include "itkFixedArray.h"
26#include "itkMacro.h"
27#include "itkMatrix.h"
28#include "itkMeshIOBase.h"
29#include "itkRGBAPixel.h"
30#include "itkRGBPixel.h"
34#include "itkVector.h"
35#include <complex>
36#include <iostream>
37
38
39// Define a local macro for variable to command testing to avoid including
40// itkTestingMacros.h, which causes link issues as the module hosting this
41// file is not a testing module.
42#define LOCAL_ITK_TEST_SET_GET_VALUE(variable, command) \
43 ITK_GCC_PRAGMA_PUSH \
44 ITK_GCC_SUPPRESS_Wfloat_equal \
45 if (variable != command) \
46 { \
47 std::cerr << "Error in " << #command << std::endl; \
48 std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
49 std::cerr << "Expected " << variable << std::endl; \
50 std::cerr << "but got " << command << std::endl; \
51 return EXIT_FAILURE; \
52 } \
53 ITK_GCC_PRAGMA_POP \
54 ITK_MACROEND_NOOP_STATEMENT
55
56
57template <typename TMeshIO>
58int
60{
61 using FloatType = float;
62
63 FloatType floatValue = 1.0;
64 bool usePointPixel = true;
65 meshIO->SetPixelType(floatValue, usePointPixel);
66 LOCAL_ITK_TEST_SET_GET_VALUE(1, meshIO->GetNumberOfPointPixelComponents());
68 meshIO->GetPointPixelComponentType());
69 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::SCALAR, meshIO->GetPointPixelType());
70
71 usePointPixel = false;
72 meshIO->SetPixelType(floatValue, usePointPixel);
73 LOCAL_ITK_TEST_SET_GET_VALUE(1, meshIO->GetNumberOfCellPixelComponents());
75 meshIO->GetCellPixelComponentType());
76 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::SCALAR, meshIO->GetCellPixelType());
77
78
79 using RGBPixelType = itk::RGBPixel<FloatType>;
80
81 RGBPixelType rgbValue{ 1.0 };
82 usePointPixel = true;
83 meshIO->SetPixelType(rgbValue, usePointPixel);
84 LOCAL_ITK_TEST_SET_GET_VALUE(3, meshIO->GetNumberOfPointPixelComponents());
86 meshIO->GetPointPixelComponentType());
87 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::RGB, meshIO->GetPointPixelType());
88
89 usePointPixel = false;
90 meshIO->SetPixelType(rgbValue, usePointPixel);
91 LOCAL_ITK_TEST_SET_GET_VALUE(3, meshIO->GetNumberOfCellPixelComponents());
93 meshIO->GetCellPixelComponentType());
94 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::RGB, meshIO->GetCellPixelType());
95
96
97 using RGBAPixelType = itk::RGBAPixel<FloatType>;
98
99 RGBAPixelType rgbaValue;
100 rgbaValue.Fill(1.0);
101 usePointPixel = true;
102 meshIO->SetPixelType(rgbaValue, usePointPixel);
103 LOCAL_ITK_TEST_SET_GET_VALUE(4, meshIO->GetNumberOfPointPixelComponents());
105 meshIO->GetPointPixelComponentType());
106 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::RGBA, meshIO->GetPointPixelType());
107
108 usePointPixel = false;
109 meshIO->SetPixelType(rgbaValue, usePointPixel);
110 LOCAL_ITK_TEST_SET_GET_VALUE(4, meshIO->GetNumberOfCellPixelComponents());
112 meshIO->GetCellPixelComponentType());
113 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::RGBA, meshIO->GetCellPixelType());
114
115
116 const itk::SizeValueType length = 5;
117 using VectorPixelType = itk::Vector<FloatType, length>;
118
119 VectorPixelType vectorValue;
120 vectorValue.Fill(1.0);
121 usePointPixel = true;
122 meshIO->SetPixelType(vectorValue, usePointPixel);
123 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfPointPixelComponents());
125 meshIO->GetPointPixelComponentType());
126 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::VECTOR, meshIO->GetPointPixelType());
127
128 usePointPixel = false;
129 meshIO->SetPixelType(vectorValue, usePointPixel);
130 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfCellPixelComponents());
132 meshIO->GetCellPixelComponentType());
133 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::VECTOR, meshIO->GetCellPixelType());
134
135
136 using CovariantVectorPixelType = itk::CovariantVector<FloatType, length>;
137
138 CovariantVectorPixelType covariantVectorValue;
139 covariantVectorValue.Fill(1.0);
140 usePointPixel = true;
141 meshIO->SetPixelType(covariantVectorValue, usePointPixel);
142 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfPointPixelComponents());
144 meshIO->GetPointPixelComponentType());
146
147 usePointPixel = false;
148 meshIO->SetPixelType(covariantVectorValue, usePointPixel);
149 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfCellPixelComponents());
151 meshIO->GetCellPixelComponentType());
153
154
155 using FixedArrayPixelType = itk::FixedArray<FloatType, length>;
156
157 FixedArrayPixelType fixedArrayValue;
158 fixedArrayValue.Fill(1.0);
159 usePointPixel = true;
160 meshIO->SetPixelType(fixedArrayValue, usePointPixel);
161 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfPointPixelComponents());
163 meshIO->GetPointPixelComponentType());
165
166 usePointPixel = false;
167 meshIO->SetPixelType(fixedArrayValue, usePointPixel);
168 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfCellPixelComponents());
170 meshIO->GetCellPixelComponentType());
172
173
174 using SymmetricSecondRankTensorPixelType = itk::SymmetricSecondRankTensor<FloatType, length>;
175
176 SymmetricSecondRankTensorPixelType symmetricSecondRankTensorValue;
177 symmetricSecondRankTensorValue.Fill(1.0);
178 usePointPixel = true;
179 meshIO->SetPixelType(symmetricSecondRankTensorValue, usePointPixel);
180 LOCAL_ITK_TEST_SET_GET_VALUE(length * (length + 1) / 2, meshIO->GetNumberOfPointPixelComponents());
182 meshIO->GetPointPixelComponentType());
184
185 usePointPixel = false;
186 meshIO->SetPixelType(symmetricSecondRankTensorValue, usePointPixel);
187 LOCAL_ITK_TEST_SET_GET_VALUE(length * (length + 1) / 2, meshIO->GetNumberOfCellPixelComponents());
189 meshIO->GetCellPixelComponentType());
191
192
193 using DiffusionTensor3DPixelType = itk::DiffusionTensor3D<FloatType>;
194
195 DiffusionTensor3DPixelType diffusionTensor3DPixelValue;
196 diffusionTensor3DPixelValue.Fill(1.0);
197 usePointPixel = true;
198 meshIO->SetPixelType(diffusionTensor3DPixelValue, usePointPixel);
199 LOCAL_ITK_TEST_SET_GET_VALUE(6, meshIO->GetNumberOfPointPixelComponents());
201 meshIO->GetPointPixelComponentType());
203
204 usePointPixel = false;
205 meshIO->SetPixelType(diffusionTensor3DPixelValue, usePointPixel);
206 LOCAL_ITK_TEST_SET_GET_VALUE(6, meshIO->GetNumberOfCellPixelComponents());
208 meshIO->GetCellPixelComponentType());
210
211
212 const itk::SizeValueType rows = 2;
213 const itk::SizeValueType cols = 2;
214 using MatrixPixelType = itk::Matrix<FloatType, rows, cols>;
215
216 MatrixPixelType matrixPixelValue;
217 matrixPixelValue.Fill(1.0);
218 usePointPixel = true;
219 meshIO->SetPixelType(matrixPixelValue, usePointPixel);
220 LOCAL_ITK_TEST_SET_GET_VALUE(rows * cols, meshIO->GetNumberOfPointPixelComponents());
222 meshIO->GetPointPixelComponentType());
223 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::MATRIX, meshIO->GetPointPixelType());
224
225 usePointPixel = false;
226 meshIO->SetPixelType(matrixPixelValue, usePointPixel);
227 LOCAL_ITK_TEST_SET_GET_VALUE(rows * cols, meshIO->GetNumberOfCellPixelComponents());
229 meshIO->GetCellPixelComponentType());
230 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::MATRIX, meshIO->GetCellPixelType());
231
232
233 using ComplexPixelType = std::complex<FloatType>;
234
235 ComplexPixelType complexPixelValue(1.0, 1.0);
236 usePointPixel = true;
237 meshIO->SetPixelType(complexPixelValue, usePointPixel);
238 LOCAL_ITK_TEST_SET_GET_VALUE(2, meshIO->GetNumberOfPointPixelComponents());
240 meshIO->GetPointPixelComponentType());
241 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::COMPLEX, meshIO->GetPointPixelType());
242
243 usePointPixel = false;
244 meshIO->SetPixelType(complexPixelValue, usePointPixel);
245 LOCAL_ITK_TEST_SET_GET_VALUE(2, meshIO->GetNumberOfCellPixelComponents());
247 meshIO->GetCellPixelComponentType());
248 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::COMPLEX, meshIO->GetCellPixelType());
249
250
251 using ArrayPixelType = itk::Array<FloatType>;
252
253 ArrayPixelType arrayPixelValue;
254 arrayPixelValue.Fill(1.0);
255 usePointPixel = true;
256 meshIO->SetPixelType(arrayPixelValue, usePointPixel);
257 LOCAL_ITK_TEST_SET_GET_VALUE(arrayPixelValue.Size(), meshIO->GetNumberOfPointPixelComponents());
259 meshIO->GetPointPixelComponentType());
260 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::ARRAY, meshIO->GetPointPixelType());
261
262 usePointPixel = false;
263 meshIO->SetPixelType(arrayPixelValue, usePointPixel);
264 LOCAL_ITK_TEST_SET_GET_VALUE(arrayPixelValue.Size(), meshIO->GetNumberOfCellPixelComponents());
266 meshIO->GetCellPixelComponentType());
267 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::ARRAY, meshIO->GetCellPixelType());
268
269
270 using VariableLengthVectorPixelType = itk::VariableLengthVector<FloatType>;
271
272 VariableLengthVectorPixelType variableLengthVectorValue;
273 variableLengthVectorValue.Fill(1.0);
274 usePointPixel = true;
275 meshIO->SetPixelType(variableLengthVectorValue, usePointPixel);
276 LOCAL_ITK_TEST_SET_GET_VALUE(variableLengthVectorValue.Size(), meshIO->GetNumberOfPointPixelComponents());
278 meshIO->GetPointPixelComponentType());
280
281 usePointPixel = false;
282 meshIO->SetPixelType(variableLengthVectorValue, usePointPixel);
283 LOCAL_ITK_TEST_SET_GET_VALUE(variableLengthVectorValue.Size(), meshIO->GetNumberOfCellPixelComponents());
285 meshIO->GetCellPixelComponentType());
287
288
289 using VariableSizeMatrixType = itk::VariableSizeMatrix<FloatType>;
290
291 VariableSizeMatrixType matrix;
292 matrix.Fill(1.0);
293 usePointPixel = true;
294 meshIO->SetPixelType(matrix, usePointPixel);
295 LOCAL_ITK_TEST_SET_GET_VALUE(matrix.Rows() * matrix.Cols(), meshIO->GetNumberOfPointPixelComponents());
297 meshIO->GetPointPixelComponentType());
299
300 usePointPixel = false;
301 meshIO->SetPixelType(matrix, usePointPixel);
302 LOCAL_ITK_TEST_SET_GET_VALUE(matrix.Rows() * matrix.Cols(), meshIO->GetNumberOfCellPixelComponents());
304 meshIO->GetCellPixelComponentType());
306
307
308 // ToDo see how the above change the below
309 // Do this only for the last pixel type
311 std::cout << "ComponentSize: " << meshIO->GetComponentSize(floatComponent) << std::endl;
312
313 std::cout << "ComponentTypeAsString: " << meshIO->GetComponentTypeAsString(floatComponent) << std::endl;
314
316 std::cout << "PixelTypeAsString: " << meshIO->GetPixelTypeAsString(pixelType) << std::endl;
317
319 meshIO->SetPointComponentType(pointComponentType);
320 LOCAL_ITK_TEST_SET_GET_VALUE(pointComponentType, meshIO->GetPointComponentType());
321
323 meshIO->SetCellComponentType(cellComponentType);
324 LOCAL_ITK_TEST_SET_GET_VALUE(cellComponentType, meshIO->GetCellComponentType());
325
326 unsigned int pointDimension = 2;
327 meshIO->SetPointDimension(pointDimension);
328 LOCAL_ITK_TEST_SET_GET_VALUE(pointDimension, meshIO->GetPointDimension());
329
330 itk::MeshIOBase::SizeValueType numberOfPoints = 400;
331 meshIO->SetNumberOfPoints(numberOfPoints);
332 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfPoints, meshIO->GetNumberOfPoints());
333
334 itk::MeshIOBase::SizeValueType numberOfCells = 100;
335 meshIO->SetNumberOfCells(numberOfCells);
336 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfCells, meshIO->GetNumberOfCells());
337
338 itk::MeshIOBase::SizeValueType numberOfPointPixels = 200;
339 meshIO->SetNumberOfPointPixels(numberOfPointPixels);
340 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfPointPixels, meshIO->GetNumberOfPointPixels());
341
342 itk::MeshIOBase::SizeValueType numberOfCellPixels = 600;
343 meshIO->SetNumberOfCellPixels(numberOfCellPixels);
344 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfCellPixels, meshIO->GetNumberOfCellPixels());
345
346 itk::MeshIOBase::SizeValueType cellBufferSize = 1000;
347 meshIO->SetCellBufferSize(cellBufferSize);
348 LOCAL_ITK_TEST_SET_GET_VALUE(cellBufferSize, meshIO->GetCellBufferSize());
349
351 meshIO->SetFileType(fileType);
352 LOCAL_ITK_TEST_SET_GET_VALUE(fileType, meshIO->GetFileType());
353
354 std::cout << "FileTypeAsString: " << meshIO->GetFileTypeAsString(fileType) << std::endl;
355
357 meshIO->SetByteOrder(ioByteOrder);
358 LOCAL_ITK_TEST_SET_GET_VALUE(ioByteOrder, meshIO->GetByteOrder());
359
360 std::cout << "ByteOrderAsString: " << meshIO->GetByteOrderAsString(ioByteOrder) << std::endl;
361
362 itk::MeshIOBase::ArrayOfExtensionsType supportedReadExtensions = meshIO->GetSupportedReadExtensions();
363 std::cout << "SupportedReadExtensions: " << std::endl;
364 for (auto ext : supportedReadExtensions)
365 {
366 std::cout << ext << std::endl;
367 }
368
369 itk::MeshIOBase::ArrayOfExtensionsType supportedWriteExtensions = meshIO->GetSupportedWriteExtensions();
370 std::cout << "SupportedWriteExtensions: " << std::endl;
371 for (auto ext : supportedWriteExtensions)
372 {
373 std::cout << ext << std::endl;
374 }
375
376
377 return EXIT_SUCCESS;
378}
379
380namespace itk
381{
382namespace MeshIOTestHelper
383{
384
385template <typename T>
386std::shared_ptr<void>
387MakeSharedArray(const std::size_t bufferSize)
388{
389 return std::shared_ptr<void>(new T[bufferSize], std::default_delete<T[]>());
390}
391
392inline std::shared_ptr<void>
394{
395 switch (componentType)
396 {
398 return MakeSharedArray<char>(bufferSize);
400 return MakeSharedArray<unsigned char>(bufferSize);
402 return MakeSharedArray<unsigned short>(bufferSize);
404 return MakeSharedArray<short>(bufferSize);
406 return MakeSharedArray<unsigned int>(bufferSize);
408 return MakeSharedArray<int>(bufferSize);
410 return MakeSharedArray<unsigned long>(bufferSize);
412 return MakeSharedArray<long>(bufferSize);
414 return MakeSharedArray<long long>(bufferSize);
416 return MakeSharedArray<unsigned long long>(bufferSize);
418 return MakeSharedArray<float>(bufferSize);
420 return MakeSharedArray<double>(bufferSize);
422 return MakeSharedArray<long double>(bufferSize);
424 break;
425 default:
426 break;
427 }
428
429 return nullptr;
430}
431
432} // namespace MeshIOTestHelper
433} // namespace itk
434
435#endif
Array class with size defined at construction time.
Definition: itkArray.h:48
void Fill(TValue const &v)
Definition: itkArray.h:114
A templated class holding a n-Dimensional covariant vector.
Represent a diffusion tensor as used in DTI images.
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
void Fill(const ValueType &)
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
void Fill(const T &value)
Definition: itkMatrix.h:210
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:89
std::vector< std::string > ArrayOfExtensionsType
Definition: itkMeshIOBase.h:84
Represent Red, Green, Blue and Alpha components for color images.
Definition: itkRGBAPixel.h:60
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:59
Represent a symmetric tensor of second rank.
A templated class holding a M x N size Matrix.
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:63
SmartPointer< Self > Pointer
int TestBaseClassMethodsMeshIO(typename TMeshIO::Pointer meshIO)
#define LOCAL_ITK_TEST_SET_GET_VALUE(variable, command)
std::shared_ptr< void > MakeSharedArray(const std::vcl_size_t bufferSize)
std::shared_ptr< void > AllocateBuffer(itk::IOComponentEnum componentType, itk::SizeValueType bufferSize)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:83