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 // Testcode for the itk::LineIterator.
20 
21 #include <iostream>
22 #include <fstream>
23 #include "itkLineIterator.h"
24 #include "itkTimeProbe.h"
25 #include "itkTestingMacros.h"
26 
27 
itkLineIteratorTest(int argc,char * argv[])28 int itkLineIteratorTest(int argc, char*argv[])
29 {
30   constexpr int Dimension = 2;
31   using PixelType = unsigned char;
32   using ImageType = itk::Image<PixelType, Dimension>;
33   using IndexType = ImageType::RegionType::IndexType;
34 
35  if (argc < 2)
36     {
37     std::cerr << "Usage: " << std::endl;
38     std::cerr << itkNameOfTestExecutableMacro(argv) << "   baselinefilename" << std::endl;
39     return EXIT_FAILURE;
40     }
41 
42 
43   // Set up a test image
44   ImageType::RegionType::IndexType index;
45   index.Fill(0);
46 
47   ImageType::RegionType::SizeType size;
48   size.Fill(200);
49 
50   ImageType::RegionType region;
51   region.SetIndex(index);
52   region.SetSize(size);
53 
54   ImageType::Pointer output = ImageType::New();
55   output->SetRegions(region);
56   output->Allocate(true); // initialize buffer to zero
57 
58   // First test: empty line
59   IndexType startIndex;
60   IndexType endIndex;
61 
62   startIndex[0] = 11;
63   startIndex[1] = 13;
64   endIndex[0] = 11;
65   endIndex[1] = 13;
66 
67   using LineIteratorType = itk::LineIterator<ImageType>;
68   LineIteratorType across(output, startIndex, endIndex);
69 
70   // First test: currentIndex initialization
71   if (startIndex != across.GetIndex())
72     {
73     std::cerr << "Error! Index should be startIndex.\n";
74     return EXIT_FAILURE;
75     }
76 
77   // Second test: IsAtEnd() is one pixel past the endIndex
78   if (across.IsAtEnd())
79     {
80     std::cerr << "Error! Iterator should not be at end.\n";
81     return EXIT_FAILURE;
82     }
83 
84   ++across;
85   if (!across.IsAtEnd())
86     {
87     std::cerr << "Error! Iterator should be at end.\n";
88     return EXIT_FAILURE;
89     }
90 
91   // Third test: draw some lines and read the baseline txt file to compare
92   // the point indexies
93 
94   std::ifstream baselineFile(argv[1]);
95   if (baselineFile.fail())
96     {
97     std::cerr<< "Error opening file with name :"<< argv[1]<<std::endl;
98     return EXIT_FAILURE;
99     }
100   std::vector<IndexType> baselineIndex;
101 
102   IndexType tmpIndex;
103 
104   baselineFile >> tmpIndex[0] >> tmpIndex[1];
105   while( !baselineFile.eof() )
106   {
107     baselineIndex.push_back(tmpIndex);
108     baselineFile >> tmpIndex[0] >> tmpIndex[1];
109   }
110 
111   baselineFile.close();
112 
113   itk::TimeProbe timer;
114   timer.Start();
115 
116   startIndex.Fill(10);
117   endIndex.Fill(189);
118   LineIteratorType it(output, startIndex, endIndex);
119 
120   std::vector<IndexType>::iterator itBaseline;
121   itBaseline = baselineIndex.begin();
122   while (!it.IsAtEnd())
123     {
124     it.Set(255);
125     if (it.GetIndex() == *itBaseline) {
126         ++itBaseline;
127         ++it;
128        }
129     else{
130        std::cerr<< "different than baseline."<< std::endl;
131        return EXIT_FAILURE;
132        }
133     }
134 
135   startIndex.Fill(50);
136   endIndex[0] = 150;
137   endIndex[1] = startIndex[1];
138   it = LineIteratorType(output, startIndex, endIndex);
139   while (!it.IsAtEnd())
140     {
141     it.Set(150);
142     if (it.GetIndex() == *itBaseline) {
143         ++itBaseline;
144         ++it;
145        }
146     else{
147        std::cerr<< "different than baseline."<< std::endl;
148        return EXIT_FAILURE;
149        }
150     }
151 
152   startIndex.Fill(120);
153   endIndex[0] = 50;
154   endIndex[1] = 100;
155   it = LineIteratorType(output, startIndex, endIndex);
156   while (!it.IsAtEnd())
157     {
158     it.Set(150);
159     if (it.GetIndex() == *itBaseline) {
160         ++itBaseline;
161         ++it;
162        }
163     else{
164        std::cerr<< "different than baseline."<< std::endl;
165        return EXIT_FAILURE;
166        }
167     }
168 
169   timer.Stop();
170   std::cerr << "Line drawing took " << timer.GetMean() << " seconds.\n";
171 
172 
173   return EXIT_SUCCESS;
174 }
175