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 
19 #include <iostream>
20 #include "itkRegionOfInterestImageFilter.h"
21 #include "itkSimpleFilterWatcher.h"
22 #include "itkTestingMacros.h"
23 
itkRegionOfInterestImageFilterTest(int,char * [])24 int itkRegionOfInterestImageFilterTest( int, char* [] )
25 {
26 
27   constexpr unsigned int Dimension = 3;
28   using PixelType = itk::Index< Dimension >;
29 
30   using ImageType = itk::Image< PixelType, Dimension >;
31 
32   using FilterType = itk::RegionOfInterestImageFilter<
33                                       ImageType,
34                                       ImageType >;
35 
36 
37   using RegionType = ImageType::RegionType;
38   using SizeType = ImageType::SizeType;
39   using IndexType = ImageType::IndexType;
40   using DirectionType = ImageType::DirectionType;
41 
42   using IteratorType = itk::ImageRegionIterator< ImageType >;
43 
44   FilterType::Pointer filter = FilterType::New();
45 
46   EXERCISE_BASIC_OBJECT_METHODS( filter, RegionOfInterestImageFilter, ImageToImageFilter );
47 
48   ImageType::Pointer image = ImageType::New();
49 
50   IndexType start;
51   start.Fill( 0 );
52 
53   SizeType  size;
54   size[0] = 40;
55   size[1] = 40;
56   size[2] = 40;
57 
58   RegionType region;
59   region.SetIndex( start );
60   region.SetSize(  size  );
61 
62   image->SetRegions( region );
63   image->Allocate();
64 
65   DirectionType directions;
66   directions.SetIdentity();
67   directions[0][0] = 0.0;
68   directions[1][0] = 1.0;
69   directions[2][0] = 0.0;
70   directions[0][1] = 1.0;
71   directions[1][1] = 0.0;
72   directions[2][1] = 0.0;
73   image->SetDirection( directions );
74 
75   // Fill the image pixels with their own index.
76   IteratorType intr( image, region );
77   intr.GoToBegin();
78   while( !intr.IsAtEnd() )
79     {
80     intr.Set( intr.GetIndex() );
81     ++intr;
82     }
83 
84 
85   filter->SetInput( image );
86 
87   SizeType roiSize;
88   roiSize[0] = 20;
89   roiSize[1] = 20;
90   roiSize[2] = 20;
91 
92   IndexType roiStart;
93   roiStart[0] = 9;
94   roiStart[1] = 9;
95   roiStart[2] = 9;
96 
97   RegionType regionOfInterest;
98   regionOfInterest.SetIndex( roiStart );
99   regionOfInterest.SetSize(  roiSize  );
100 
101   itk::SimpleFilterWatcher watcher(filter);
102 
103   filter->SetRegionOfInterest( regionOfInterest );
104   TEST_SET_GET_VALUE( regionOfInterest, filter->GetRegionOfInterest() );
105 
106   filter->Update();
107 
108   IteratorType ot( filter->GetOutput(),
109                    filter->GetOutput()->GetLargestPossibleRegion() );
110 
111   IteratorType it( image, regionOfInterest );
112 
113   it.GoToBegin();
114   ot.GoToBegin();
115 
116   bool passed = true;
117   while( !it.IsAtEnd() )
118     {
119     IndexType inIndex  = it.Get();
120     IndexType outIndex = ot.Get();
121     if( inIndex[0] != outIndex[0] ||
122         inIndex[1] != outIndex[1] ||
123         inIndex[2] != outIndex[2] )
124       {
125       std::cerr << "Test failed at pixel " << inIndex << std::endl;
126       std::cerr << "pixel value is       " << outIndex << std::endl;
127       passed = false;
128       break;
129       }
130 
131     ++it;
132     ++ot;
133     }
134 
135   if( !passed )
136     {
137     return EXIT_FAILURE;
138     }
139 
140   std::cout << "Test PASSED !" << std::endl;
141   return EXIT_SUCCESS;
142 
143 }
144