1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
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  *         http://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 // Disable warning for long symbol names in this file only
19 
20 /*
21 * This is a test file for the itkImageMaskSpatialObject class.
22 * The suported pixel types does not include itkRGBPixel, itkRGBAPixel, etc...
23 * So far it only allows to manage images of simple types like unsigned short,
24 * unsigned int, or itk::Vector<...>.
25 */
26 
27 
28 #include "itkImageRegionIterator.h"
29 
30 #include "itkImageMaskSpatialObject.h"
31 
32 
itkImageMaskSpatialObjectTest(int,char * [])33 int itkImageMaskSpatialObjectTest(int, char* [])
34 {
35   constexpr unsigned int NDimensions = 3;
36 
37   using ImageMaskSpatialObject = itk::ImageMaskSpatialObject<NDimensions>;
38   using PixelType = ImageMaskSpatialObject::PixelType;
39   using ImageType = ImageMaskSpatialObject::ImageType;
40   using Iterator = itk::ImageRegionIterator<ImageType>;
41 
42   ImageType::Pointer image = ImageType::New();
43   ImageType::SizeType size = {{ 50, 50, 50 }};
44   ImageType::IndexType index = {{ 0, 0, 0 }};
45   ImageType::RegionType region;
46 
47   region.SetSize(size);
48   region.SetIndex(index);
49 
50   image->SetRegions( region );
51   image->Allocate(true); // initialize buffer to zero
52 
53   ImageType::RegionType insideRegion;
54   ImageType::SizeType insideSize   = {{ 30, 30, 30 }};
55   ImageType::IndexType insideIndex = {{ 10, 10, 10 }};
56   insideRegion.SetSize( insideSize );
57   insideRegion.SetIndex( insideIndex );
58 
59 
60   Iterator it( image, insideRegion );
61   it.GoToBegin();
62 
63   while( !it.IsAtEnd() )
64     {
65     it.Set( itk::NumericTraits< PixelType >::max() );
66     ++it;
67     }
68 
69   ImageMaskSpatialObject::Pointer maskSO = ImageMaskSpatialObject::New();
70   maskSO->Print(std::cout);
71 
72   maskSO->SetImage(image);
73   maskSO->Update();
74 
75   Iterator itr( image, region );
76   itr.GoToBegin();
77 
78   while( !itr.IsAtEnd() )
79     {
80     const ImageType::IndexType constIndex =  itr.GetIndex();
81     const bool reference = insideRegion.IsInside( constIndex );
82     ImageType::PointType point;
83     image->TransformIndexToPhysicalPoint( constIndex, point );
84     const bool test      = maskSO->IsInsideInWorldSpace( point );
85     if( test != reference )
86       {
87       std::cerr << "Error in the evaluation of IsInside() " << std::endl;
88       std::cerr << "Index failed = " << constIndex << std::endl;
89       return EXIT_FAILURE;
90       }
91     ++itr;
92     }
93 
94   maskSO->Print(std::cout);
95 
96   return EXIT_SUCCESS;
97 }
98