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 "itkNeighborhoodIteratorTestCommon.hxx"
20 #include "itkShapedNeighborhoodIterator.h"
21 
itkShapedNeighborhoodIteratorTest(int,char * [])22 int itkShapedNeighborhoodIteratorTest(int, char* [] )
23 {
24 
25   TestImageType::Pointer img = GetTestImage(10, 10, 5, 3);
26   itk::ShapedNeighborhoodIterator<TestImageType>::IndexType loc;
27   loc[0] = 4; loc[1] = 4; loc[2] = 2; loc[3] = 1;
28 
29   // radius of the iterator
30   itk::ShapedNeighborhoodIterator<TestImageType>::RadiusType radius;
31   radius[0] = radius[1] = radius[2] = radius[3] = 1;
32 
33   // region over which the iterator is defined
34   itk::ShapedNeighborhoodIterator<TestImageType>::RegionType reg;
35   itk::ShapedNeighborhoodIterator<TestImageType>::SizeType sz;
36   itk::ShapedNeighborhoodIterator<TestImageType>::IndexType idx;
37   idx[0] = idx[1] = idx[2] = 0;  idx[3] = 1;
38   sz[0] = sz[1] = 10; sz[2] = 5; sz[3] = 1;
39   reg.SetIndex(idx); reg.SetSize(sz);
40 
41   // initialize an iterator
42   println("Creating ShapedNeighborhoodIterator");
43   itk::ShapedNeighborhoodIterator<TestImageType>
44     it(radius, img, reg);
45   it.Print(std::cout);
46 
47   println("Moving iterator using SetLocation()");
48   it.SetLocation(loc);
49   it.Print(std::cout);
50 
51   println("Initializing ShapedNeighborhoodIterator");
52 
53   println("Activating some offsets");
54   println("...turn on [0,0,0,0], the center pixel");
55   itk::ShapedNeighborhoodIterator<TestImageType>::OffsetType off;
56   off[0] = 0; off[1] = 0; off[2] = 0; off[3] = 0;
57   it.ActivateOffset(off);
58   it.Print(std::cout);
59 
60   println("...turn on [1,0,0,0]");
61   off[0] = 1; off[1] = 0; off[2] = 0; off[3] = 0;
62   it.ActivateOffset(off);
63   it.Print(std::cout);
64 
65   println("...turn on [1,0,0,0] again");
66   off[0] = 1; off[1] = 0; off[2] = 0; off[3] = 0;
67   it.ActivateOffset(off);
68   it.Print(std::cout);
69 
70   println("...turn on [-1,0,0,0]");
71   off[0] = -1; off[1] = 0; off[2] = 0; off[3] = 0;
72   it.ActivateOffset(off);
73   it.Print(std::cout);
74 
75   println("...turn on [0,-1,0,0]");
76   off[0] = 0; off[1] = -1; off[2] = 0; off[3] = 0;
77   it.ActivateOffset(off);
78   it.Print(std::cout);
79 
80   println("...turn on [0,1,0,0]");
81   off[0] = 0; off[1] = 1; off[2] = 0; off[3] = 0;
82   it.ActivateOffset(off);
83   it.Print(std::cout);
84 
85   println("Testing iteration through the neighborhood.");
86   itk::ShapedNeighborhoodIterator<TestImageType>::Iterator
87     ci = it.Begin();
88 
89   println("Testing using IsAtEnd()");
90   while (! ci.IsAtEnd())
91     {
92       std::cout << ci.GetNeighborhoodIndex() << " -> "
93                 << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
94       ci++;
95     }
96 
97 
98   println("Testing using != it.End()");
99   for (ci = it.Begin(); ci != it.End(); ++ci)
100     {
101       std::cout << ci.GetNeighborhoodIndex() << " -> "
102                 << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
103     }
104 
105 
106   println("Testing reverse iteration using != it.Begin()");
107   ci = it.End();
108   --ci;
109   while (ci != it.Begin())
110     {
111       std::cout << ci.GetNeighborhoodIndex() << " -> "
112                 << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
113       ci--;
114     }
115   std::cout << ci.GetNeighborhoodIndex() << " -> "
116             << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
117 
118 
119   println("Testing read through GetPixel(itk::Offset(0,0,0,0))");
120   TestImageType::IndexType voff;
121   voff[0] = 1; voff[1] = 1; voff[2] = 1; voff[3] = 1;
122   off[0] = 0; off[1] = 0; off[2] = 0; off[3] = 0;
123   std::cout << it.GetPixel(off) << std::endl;
124 
125   println("Testing read through GetPixel(unsigned int)");
126   std::cout << it.GetPixel(it.GetNeighborhoodIndex(off)) << std::endl;
127 
128   println("Testing write through iterator dereference");
129   for (ci = it.Begin(); ci != it.End(); ++ci)
130     {
131       ci.Set(voff);
132     }
133   for (ci = it.Begin(); ci != it.End(); ++ci)
134     {
135       std::cout << ci.GetNeighborhoodIndex() << " -> "
136                 << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
137     }
138 
139 
140   println("Testing write through SetPixel(itk::Offset(0,0,0,0))");
141   voff[0] = 45000;
142   it.SetPixel(off, voff);
143    for (ci = it.Begin(); ci != it.End(); ++ci)
144     {
145       std::cout << ci.GetNeighborhoodIndex() << " -> "
146                 << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
147     }
148 
149    println("Testing iteration through the image");
150    off[0] = 0; off[1] =0; off[2] = 0; off[3] = 0;
151    for (it.GoToBegin(); !it.IsAtEnd(); ++it)
152      {
153        std::cout << it.GetPixel(off) << std::endl;
154      }
155 
156    println("Testing reverse iteration through the image");
157    off[0] = 0; off[1] =0; off[2] = 0; off[3] = 0;
158    for (it.GoToEnd(), --it; !it.IsAtBegin(); --it)
159      {
160        std::cout << it.GetPixel(off) << std::endl;
161      }
162    std::cout << it.GetPixel(off) << std::endl;
163 
164    println("testing operator=");
165    itk::ShapedNeighborhoodIterator<TestImageType> oeIt;
166    oeIt = it;
167 
168    it.Print(std::cout);
169    oeIt.Print(std::cout);
170 
171   return EXIT_SUCCESS;
172 }
173