Debian Codegraph @

Modules/Bridge/VtkGlue/test/itkImageToVTKImageFilterTest.cxx

raw
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
19 #include "itkImageToVTKImageFilter.h"
20
21 #include "itkRandomImageSource.h"
22
23 #include "vtkMatrix3x3.h"
24
25 int
26 itkImageToVTKImageFilterTest(int, char *[])
27 {
28 const int dim = 3;
29 using ImageType = itk::Image<float, dim>;
30 using SourceType = itk::RandomImageSource<ImageType>;
31 using SpacingType = SourceType::SpacingType;
32 using OriginType = SourceType::PointType;
33 using DirectionType = SourceType::DirectionType;
34 using ConnectorType = itk::ImageToVTKImageFilter<ImageType>;
35
36 ImageType::SizeType size;
37 size[0] = 40;
38 size[1] = 10;
39 size[2] = 20;
40 auto source = SourceType::New();
41 source->SetSize(size);
42
43 SpacingType spacing;
44 spacing[0] = 0.5;
45 spacing[1] = 2;
46 spacing[2] = 1;
47 OriginType origin;
48 origin[0] = -1.5;
49 origin[1] = 0.222;
50 origin[2] = 0;
51 DirectionType direction;
52 direction.Fill(0.0);
53 direction[0][1] = 1;
54 direction[1][0] = -1;
55 direction[2][2] = 0.7;
56 source->SetSpacing(spacing);
57 source->SetOrigin(origin);
58 source->SetDirection(direction);
59
60 source->Update();
61 auto input = source->GetOutput();
62 input->Print(std::cout);
63
64 auto connector = ConnectorType::New();
65 connector->SetInput(input);
66 connector->UpdateLargestPossibleRegion();
67 connector->Update();
68
69 auto output = connector->GetOutput();
70 output->Print(std::cout);
71
72 for (int i = 0; i < dim; ++i)
73 {
74 if (input->GetLargestPossibleRegion().GetSize()[i] != static_cast<itk::SizeValueType>(output->GetDimensions()[i]))
75 {
76 std::cerr << "Error: sizes do not match for component (" << i << ")." << std::endl;
77 return EXIT_FAILURE;
78 }
79 if (input->GetSpacing()[i] != output->GetSpacing()[i])
80 {
81 std::cerr << "Error: spacings do not match for component (" << i << ")." << std::endl;
82 return EXIT_FAILURE;
83 }
84 if (input->GetOrigin()[i] != output->GetOrigin()[i])
85 {
86 std::cerr << "Error: origins do not match for component (" << i << ")." << std::endl;
87 return EXIT_FAILURE;
88 }
89 #if VTK_MAJOR_VERSION >= 9 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 90)
90 for (int j = 0; j < dim; ++j)
91 {
92 if (input->GetDirection()[i][j] != output->GetDirectionMatrix()->GetData()[i * 3 + j])
93 {
94 std::cerr << "Error: directions do not match for component (" << i << ',' << j << ")." << std::endl;
95 return EXIT_FAILURE;
96 }
97 }
98 #endif
99 }
100
101 return EXIT_SUCCESS;
102 }