ITK  6.0.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 auto rgbaValue = itk::MakeFilled<RGBAPixelType>(1.0);
100 usePointPixel = true;
101 meshIO->SetPixelType(rgbaValue, usePointPixel);
102 LOCAL_ITK_TEST_SET_GET_VALUE(4, meshIO->GetNumberOfPointPixelComponents());
104 meshIO->GetPointPixelComponentType());
105 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::RGBA, meshIO->GetPointPixelType());
106
107 usePointPixel = false;
108 meshIO->SetPixelType(rgbaValue, usePointPixel);
109 LOCAL_ITK_TEST_SET_GET_VALUE(4, meshIO->GetNumberOfCellPixelComponents());
111 meshIO->GetCellPixelComponentType());
112 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::RGBA, meshIO->GetCellPixelType());
113
114
115 const itk::SizeValueType length = 5;
116 using VectorPixelType = itk::Vector<FloatType, length>;
117
118 auto vectorValue = itk::MakeFilled<VectorPixelType>(1.0);
119 usePointPixel = true;
120 meshIO->SetPixelType(vectorValue, usePointPixel);
121 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfPointPixelComponents());
123 meshIO->GetPointPixelComponentType());
124 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::VECTOR, meshIO->GetPointPixelType());
125
126 usePointPixel = false;
127 meshIO->SetPixelType(vectorValue, usePointPixel);
128 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfCellPixelComponents());
130 meshIO->GetCellPixelComponentType());
131 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::VECTOR, meshIO->GetCellPixelType());
132
133
134 using CovariantVectorPixelType = itk::CovariantVector<FloatType, length>;
135
136 auto covariantVectorValue = itk::MakeFilled<CovariantVectorPixelType>(1.0);
137 usePointPixel = true;
138 meshIO->SetPixelType(covariantVectorValue, usePointPixel);
139 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfPointPixelComponents());
141 meshIO->GetPointPixelComponentType());
143
144 usePointPixel = false;
145 meshIO->SetPixelType(covariantVectorValue, usePointPixel);
146 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfCellPixelComponents());
148 meshIO->GetCellPixelComponentType());
150
151
152 using FixedArrayPixelType = itk::FixedArray<FloatType, length>;
153
154 auto fixedArrayValue = itk::MakeFilled<FixedArrayPixelType>(1.0);
155 usePointPixel = true;
156 meshIO->SetPixelType(fixedArrayValue, usePointPixel);
157 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfPointPixelComponents());
159 meshIO->GetPointPixelComponentType());
161
162 usePointPixel = false;
163 meshIO->SetPixelType(fixedArrayValue, usePointPixel);
164 LOCAL_ITK_TEST_SET_GET_VALUE(length, meshIO->GetNumberOfCellPixelComponents());
166 meshIO->GetCellPixelComponentType());
168
169
170 using SymmetricSecondRankTensorPixelType = itk::SymmetricSecondRankTensor<FloatType, length>;
171
172 auto symmetricSecondRankTensorValue = itk::MakeFilled<SymmetricSecondRankTensorPixelType>(1.0);
173 usePointPixel = true;
174 meshIO->SetPixelType(symmetricSecondRankTensorValue, usePointPixel);
175 LOCAL_ITK_TEST_SET_GET_VALUE(length * (length + 1) / 2, meshIO->GetNumberOfPointPixelComponents());
177 meshIO->GetPointPixelComponentType());
179
180 usePointPixel = false;
181 meshIO->SetPixelType(symmetricSecondRankTensorValue, usePointPixel);
182 LOCAL_ITK_TEST_SET_GET_VALUE(length * (length + 1) / 2, meshIO->GetNumberOfCellPixelComponents());
184 meshIO->GetCellPixelComponentType());
186
187
188 using DiffusionTensor3DPixelType = itk::DiffusionTensor3D<FloatType>;
189
190 auto diffusionTensor3DPixelValue = itk::MakeFilled<DiffusionTensor3DPixelType>(1.0);
191 usePointPixel = true;
192 meshIO->SetPixelType(diffusionTensor3DPixelValue, usePointPixel);
193 LOCAL_ITK_TEST_SET_GET_VALUE(6, meshIO->GetNumberOfPointPixelComponents());
195 meshIO->GetPointPixelComponentType());
197
198 usePointPixel = false;
199 meshIO->SetPixelType(diffusionTensor3DPixelValue, usePointPixel);
200 LOCAL_ITK_TEST_SET_GET_VALUE(6, meshIO->GetNumberOfCellPixelComponents());
202 meshIO->GetCellPixelComponentType());
204
205
206 const itk::SizeValueType rows = 2;
207 const itk::SizeValueType cols = 2;
208 using MatrixPixelType = itk::Matrix<FloatType, rows, cols>;
209
210 auto matrixPixelValue = itk::MakeFilled<MatrixPixelType>(1.0);
211 usePointPixel = true;
212 meshIO->SetPixelType(matrixPixelValue, usePointPixel);
213 LOCAL_ITK_TEST_SET_GET_VALUE(rows * cols, meshIO->GetNumberOfPointPixelComponents());
215 meshIO->GetPointPixelComponentType());
216 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::MATRIX, meshIO->GetPointPixelType());
217
218 usePointPixel = false;
219 meshIO->SetPixelType(matrixPixelValue, usePointPixel);
220 LOCAL_ITK_TEST_SET_GET_VALUE(rows * cols, meshIO->GetNumberOfCellPixelComponents());
222 meshIO->GetCellPixelComponentType());
223 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::MATRIX, meshIO->GetCellPixelType());
224
225
226 using ComplexPixelType = std::complex<FloatType>;
227
228 ComplexPixelType complexPixelValue(1.0, 1.0);
229 usePointPixel = true;
230 meshIO->SetPixelType(complexPixelValue, usePointPixel);
231 LOCAL_ITK_TEST_SET_GET_VALUE(2, meshIO->GetNumberOfPointPixelComponents());
233 meshIO->GetPointPixelComponentType());
234 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::COMPLEX, meshIO->GetPointPixelType());
235
236 usePointPixel = false;
237 meshIO->SetPixelType(complexPixelValue, usePointPixel);
238 LOCAL_ITK_TEST_SET_GET_VALUE(2, meshIO->GetNumberOfCellPixelComponents());
240 meshIO->GetCellPixelComponentType());
241 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::COMPLEX, meshIO->GetCellPixelType());
242
243
244 using ArrayPixelType = itk::Array<FloatType>;
245
246 ArrayPixelType arrayPixelValue{};
247 usePointPixel = true;
248 meshIO->SetPixelType(arrayPixelValue, usePointPixel);
249 LOCAL_ITK_TEST_SET_GET_VALUE(arrayPixelValue.Size(), meshIO->GetNumberOfPointPixelComponents());
251 meshIO->GetPointPixelComponentType());
252 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::ARRAY, meshIO->GetPointPixelType());
253
254 usePointPixel = false;
255 meshIO->SetPixelType(arrayPixelValue, usePointPixel);
256 LOCAL_ITK_TEST_SET_GET_VALUE(arrayPixelValue.Size(), meshIO->GetNumberOfCellPixelComponents());
258 meshIO->GetCellPixelComponentType());
259 LOCAL_ITK_TEST_SET_GET_VALUE(itk::IOPixelEnum::ARRAY, meshIO->GetCellPixelType());
260
261
262 using VariableLengthVectorPixelType = itk::VariableLengthVector<FloatType>;
263
264 VariableLengthVectorPixelType variableLengthVectorValue{};
265 usePointPixel = true;
266 meshIO->SetPixelType(variableLengthVectorValue, usePointPixel);
267 LOCAL_ITK_TEST_SET_GET_VALUE(variableLengthVectorValue.Size(), meshIO->GetNumberOfPointPixelComponents());
269 meshIO->GetPointPixelComponentType());
271
272 usePointPixel = false;
273 meshIO->SetPixelType(variableLengthVectorValue, usePointPixel);
274 LOCAL_ITK_TEST_SET_GET_VALUE(variableLengthVectorValue.Size(), meshIO->GetNumberOfCellPixelComponents());
276 meshIO->GetCellPixelComponentType());
278
279
280 using VariableSizeMatrixType = itk::VariableSizeMatrix<FloatType>;
281
282 VariableSizeMatrixType matrix{};
283 usePointPixel = true;
284 meshIO->SetPixelType(matrix, usePointPixel);
285 LOCAL_ITK_TEST_SET_GET_VALUE(matrix.Rows() * matrix.Cols(), meshIO->GetNumberOfPointPixelComponents());
287 meshIO->GetPointPixelComponentType());
289
290 usePointPixel = false;
291 meshIO->SetPixelType(matrix, usePointPixel);
292 LOCAL_ITK_TEST_SET_GET_VALUE(matrix.Rows() * matrix.Cols(), meshIO->GetNumberOfCellPixelComponents());
294 meshIO->GetCellPixelComponentType());
296
297
298 // ToDo see how the above change the below
299 // Do this only for the last pixel type
301 std::cout << "ComponentSize: " << meshIO->GetComponentSize(floatComponent) << std::endl;
302
303 std::cout << "ComponentTypeAsString: " << meshIO->GetComponentTypeAsString(floatComponent) << std::endl;
304
306 std::cout << "PixelTypeAsString: " << meshIO->GetPixelTypeAsString(pixelType) << std::endl;
307
309 meshIO->SetPointComponentType(pointComponentType);
310 LOCAL_ITK_TEST_SET_GET_VALUE(pointComponentType, meshIO->GetPointComponentType());
311
313 meshIO->SetCellComponentType(cellComponentType);
314 LOCAL_ITK_TEST_SET_GET_VALUE(cellComponentType, meshIO->GetCellComponentType());
315
316 unsigned int pointDimension = 2;
317 meshIO->SetPointDimension(pointDimension);
318 LOCAL_ITK_TEST_SET_GET_VALUE(pointDimension, meshIO->GetPointDimension());
319
320 itk::MeshIOBase::SizeValueType numberOfPoints = 400;
321 meshIO->SetNumberOfPoints(numberOfPoints);
322 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfPoints, meshIO->GetNumberOfPoints());
323
324 itk::MeshIOBase::SizeValueType numberOfCells = 100;
325 meshIO->SetNumberOfCells(numberOfCells);
326 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfCells, meshIO->GetNumberOfCells());
327
328 itk::MeshIOBase::SizeValueType numberOfPointPixels = 200;
329 meshIO->SetNumberOfPointPixels(numberOfPointPixels);
330 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfPointPixels, meshIO->GetNumberOfPointPixels());
331
332 itk::MeshIOBase::SizeValueType numberOfCellPixels = 600;
333 meshIO->SetNumberOfCellPixels(numberOfCellPixels);
334 LOCAL_ITK_TEST_SET_GET_VALUE(numberOfCellPixels, meshIO->GetNumberOfCellPixels());
335
336 itk::MeshIOBase::SizeValueType cellBufferSize = 1000;
337 meshIO->SetCellBufferSize(cellBufferSize);
338 LOCAL_ITK_TEST_SET_GET_VALUE(cellBufferSize, meshIO->GetCellBufferSize());
339
341 meshIO->SetFileType(fileType);
342 LOCAL_ITK_TEST_SET_GET_VALUE(fileType, meshIO->GetFileType());
343
344 std::cout << "FileTypeAsString: " << meshIO->GetFileTypeAsString(fileType) << std::endl;
345
347 meshIO->SetByteOrder(ioByteOrder);
348 LOCAL_ITK_TEST_SET_GET_VALUE(ioByteOrder, meshIO->GetByteOrder());
349
350 std::cout << "ByteOrderAsString: " << meshIO->GetByteOrderAsString(ioByteOrder) << std::endl;
351
352 itk::MeshIOBase::ArrayOfExtensionsType supportedReadExtensions = meshIO->GetSupportedReadExtensions();
353 std::cout << "SupportedReadExtensions: " << std::endl;
354 for (auto ext : supportedReadExtensions)
355 {
356 std::cout << ext << std::endl;
357 }
358
359 itk::MeshIOBase::ArrayOfExtensionsType supportedWriteExtensions = meshIO->GetSupportedWriteExtensions();
360 std::cout << "SupportedWriteExtensions: " << std::endl;
361 for (auto ext : supportedWriteExtensions)
362 {
363 std::cout << ext << std::endl;
364 }
365
366
367 return EXIT_SUCCESS;
368}
369
370namespace itk
371{
372namespace MeshIOTestHelper
373{
374
375template <typename T>
376std::shared_ptr<void>
377MakeSharedArray(const std::size_t bufferSize)
378{
379 return std::shared_ptr<void>(new T[bufferSize], std::default_delete<T[]>());
380}
381
382inline std::shared_ptr<void>
384{
385 switch (componentType)
386 {
388 return MakeSharedArray<char>(bufferSize);
390 return MakeSharedArray<unsigned char>(bufferSize);
392 return MakeSharedArray<unsigned short>(bufferSize);
394 return MakeSharedArray<short>(bufferSize);
396 return MakeSharedArray<unsigned int>(bufferSize);
398 return MakeSharedArray<int>(bufferSize);
400 return MakeSharedArray<unsigned long>(bufferSize);
402 return MakeSharedArray<long>(bufferSize);
404 return MakeSharedArray<long long>(bufferSize);
406 return MakeSharedArray<unsigned long long>(bufferSize);
408 return MakeSharedArray<float>(bufferSize);
410 return MakeSharedArray<double>(bufferSize);
412 return MakeSharedArray<long double>(bufferSize);
414 break;
415 default:
416 break;
417 }
418
419 return nullptr;
420}
421
422} // namespace MeshIOTestHelper
423} // namespace itk
424
425#endif
Array class with size defined at construction time.
Definition: itkArray.h:48
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
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
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:86