| 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 "itkVTKImageToImageFilter.h" |
| 20 |
|
| 21 |
#include "vtkImageNoiseSource.h" |
| 22 |
#include "vtkMatrix3x3.h" |
| 23 |
#include "vtkSmartPointer.h" |
| 24 |
|
| 25 |
int |
| 26 |
itkVTKImageToImageFilterTest(int, char *[]) |
| 27 |
{ |
| 28 |
const int dim = 2; |
| 29 |
using ImageType = itk::Image<double, dim>; |
| 30 |
using VTKNoiseType = vtkSmartPointer<vtkImageNoiseSource>; |
| 31 |
using ConnectorType = itk::VTKImageToImageFilter<ImageType>; |
| 32 |
|
| 33 |
VTKNoiseType noise_source = VTKNoiseType::New(); |
| 34 |
noise_source->SetWholeExtent(0, 20, 0, 20, 0, 0); |
| 35 |
noise_source->SetMinimum(0.0); |
| 36 |
noise_source->SetMaximum(1.0); |
| 37 |
noise_source->Update(); |
| 38 |
|
| 39 |
auto input = noise_source->GetOutput(); |
| 40 |
input->SetSpacing(0.1, 2.0, 0.0); |
| 41 |
input->SetOrigin(-0.1, -10, 0.0); |
| 42 |
#if VTK_MAJOR_VERSION >= 9 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 90) |
| 43 |
input->SetDirectionMatrix(0, 1, 0, -1, 0, 0, 0, 0, 1); |
| 44 |
#endif |
| 45 |
input->Print(std::cout); |
| 46 |
|
| 47 |
auto connector = ConnectorType::New(); |
| 48 |
connector->SetInput(input); |
| 49 |
connector->Update(); |
| 50 |
|
| 51 |
auto output = connector->GetOutput(); |
| 52 |
output->Print(std::cout); |
| 53 |
|
| 54 |
|
| 55 |
for (int i = 0; i < dim; ++i) |
| 56 |
{ |
| 57 |
if (output->GetLargestPossibleRegion().GetSize()[i] != static_cast<itk::SizeValueType>(input->GetDimensions()[i])) |
| 58 |
{ |
| 59 |
std::cerr << "Error: sizes do not match for component (" << i << ")." << std::endl; |
| 60 |
return EXIT_FAILURE; |
| 61 |
} |
| 62 |
if (output->GetSpacing()[i] != input->GetSpacing()[i]) |
| 63 |
{ |
| 64 |
std::cerr << "Error: spacings do not match for component (" << i << ")." << std::endl; |
| 65 |
return EXIT_FAILURE; |
| 66 |
} |
| 67 |
if (output->GetOrigin()[i] != input->GetOrigin()[i]) |
| 68 |
{ |
| 69 |
std::cerr << "Error: origins do not match for component (" << i << ")." << std::endl; |
| 70 |
return EXIT_FAILURE; |
| 71 |
} |
| 72 |
#if VTK_MAJOR_VERSION >= 9 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 90) |
| 73 |
for (int j = 0; j < dim; ++j) |
| 74 |
{ |
| 75 |
if (output->GetDirection()[i][j] != input->GetDirectionMatrix()->GetData()[i * 3 + j]) |
| 76 |
{ |
| 77 |
std::cerr << "Error: directions do not match for component (" << i << ',' << j << ")." << std::endl; |
| 78 |
return EXIT_FAILURE; |
| 79 |
} |
| 80 |
} |
| 81 |
#endif |
| 82 |
} |
| 83 |
|
| 84 |
#if VTK_MAJOR_VERSION >= 9 || (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 90) |
| 85 |
// Try again with an direction matrix that isn't supported |
| 86 |
// and ensure it throws an exception |
| 87 |
input->SetDirectionMatrix(0, 1, 0, 0, 0, 1, 1, 0, 0); |
| 88 |
try |
| 89 |
{ |
| 90 |
connector->Update(); |
| 91 |
} |
| 92 |
catch (const itk::ExceptionObject & e) |
| 93 |
{ |
| 94 |
std::string expectedErrorSubString = "is not equal to 0.0"; |
| 95 |
std::string fullErrorString = e.GetDescription(); |
| 96 |
if (fullErrorString.find(expectedErrorSubString) != std::string::npos) |
| 97 |
{ |
| 98 |
std::cout << "\nTest passed: the expected exception was thrown: " << e << std::endl; |
| 99 |
return EXIT_SUCCESS; |
| 100 |
} |
| 101 |
std::cerr << "\nTest failed: an unexpected exception was thrown: " << e << std::endl; |
| 102 |
return EXIT_FAILURE; |
| 103 |
} |
| 104 |
|
| 105 |
return EXIT_FAILURE; |
| 106 |
#else |
| 107 |
return EXIT_SUCCESS; |
| 108 |
#endif |
| 109 |
} |