1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkImageReverseIteratorTest.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include <iostream>
#include "itkImage.h"
#include "itkVector.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionReverseIterator.h"
// This routine is used to make sure that we call the "const" version
// of GetPixel() (via the operator[])
template <class T, unsigned int VImageDimension>
void TestConstPixelAccess(const itk::Image<T, VImageDimension> &in,
itk::Image<T, VImageDimension> &out)
{
typename itk::Image<T, VImageDimension>::IndexType regionStartIndex3D = {{5, 10, 15}};
typename itk::Image<T, VImageDimension>::IndexType regionEndIndex3D = {{8, 15, 17}};
T vec;
vec[0] = 5;
vec[1] = 4;
vec[2] = 3;
vec[3] = 2;
vec[4] = 1;
out[regionStartIndex3D] = vec;
out[regionEndIndex3D] = in[regionStartIndex3D];
}
int itkImageReverseIteratorTest(int, char* [] )
{
typedef itk::Vector< unsigned short, 5 > PixelType;
const unsigned int Dimension = 3;
typedef itk::Image< PixelType, Dimension > ImageType;
std::cout << "Creating an image" << std::endl;
ImageType::Pointer o3 = ImageType::New();
float origin3D[3] = { 5, 2.1, 8.1};
float spacing3D[3] = { 1.5, 2.1, 1};
ImageType::SizeType imageSize3D = {{ 20, 40, 60 }};
ImageType::SizeType bufferSize3D = {{ 8, 20, 14 }};
ImageType::SizeType regionSize3D = {{ 4, 6, 6 }};
ImageType::IndexType startIndex3D = {{5, 4, 1}};
ImageType::IndexType bufferStartIndex3D = {{2, 3, 5}};
ImageType::IndexType regionStartIndex3D = {{5, 10, 12}};
ImageType::IndexType regionEndIndex3D = {{8, 15, 17}};
ImageType::RegionType region;
region.SetSize(imageSize3D);
region.SetIndex(startIndex3D);
o3->SetLargestPossibleRegion( region );
region.SetSize(bufferSize3D);
region.SetIndex(bufferStartIndex3D);
o3->SetBufferedRegion( region );
region.SetSize(regionSize3D);
region.SetIndex(regionStartIndex3D);
o3->SetRequestedRegion( region );
o3->SetOrigin(origin3D);
o3->SetSpacing(spacing3D);
o3->Allocate();
std::cout << "Setting/Getting a pixel" << std::endl;
PixelType vec;
vec[0] = 5;
vec[1] = 4;
vec[2] = 3;
vec[3] = 2;
vec[4] = 1;
(*o3)[regionStartIndex3D] = vec;
(*o3)[regionEndIndex3D] = (*o3)[regionStartIndex3D];
TestConstPixelAccess(*o3, *o3);
itk::ImageIterator< ImageType > standardIt(o3, region);
// Iterate over a region using a simple for loop
itk::ImageRegionIterator< ImageType > it(o3, region);
for ( ; !it.IsAtEnd(); ++it)
{
ImageType::IndexType index = it.GetIndex();
std::cout << "Simple iterator loop: ";
for (unsigned int i=0; i < index.GetIndexDimension(); i++)
{
std::cout << index[i] << " ";
}
std::cout << std::endl;
}
// Iterator over the region backwards using a simple for loop
itk::ImageRegionIterator< ImageType > backIt(o3, region);
backIt = backIt.End(); // one pixel past the end of the region
do
{
--backIt;
ImageType::IndexType index = backIt.GetIndex();
std::cout << "Simple iterator backwards loop: ";
for (unsigned int i=0; i < index.GetIndexDimension(); i++)
{
std::cout << index[i] << " ";
}
std::cout << std::endl;
}
while (!backIt.IsAtBegin()); // stop when we reach the beginning
// Iterate over a region backwards using a reverse iterator
itk::ImageRegionReverseIterator< ImageType > reverseIt(o3, region);
for ( ; !reverseIt.IsAtEnd(); ++reverseIt)
{
ImageType::IndexType index = reverseIt.GetIndex();
std::cout << "Reverse iterator: ";
for (unsigned int i=0; i < index.GetIndexDimension(); i++)
{
std::cout << index[i] << " ";
}
std::cout << std::endl;
}
// Iterator over the region forwards using a reverse iterator
itk::ImageRegionReverseIterator< ImageType > backReverseIt(o3, region);
backReverseIt = backReverseIt.End(); // one pixel before the region
do
{
--backReverseIt;
// vanal exercise of Value(), Set() and Get();
PixelType pixelValue = backReverseIt.Get();
backReverseIt.Set( pixelValue );
pixelValue = backReverseIt.Value();
ImageType::IndexType index = backReverseIt.GetIndex();
std::cout << "Reverse iterator backwards loop: ";
for (unsigned int i=0; i < index.GetIndexDimension(); i++)
{
std::cout << index[i] << " ";
}
std::cout << std::endl;
}
while (!backReverseIt.IsAtBegin()); // stop when we reach the beginning
// Finally, create a ReverseIterator from an Iterator and walk each appropriately so that they match
itk::ImageRegionReverseIterator< ImageType > castBackReverseIt(it);
it = it.Begin();
it.GoToBegin();
castBackReverseIt = castBackReverseIt.End();
int status = 0;
std::cout << "It and Reverse check: ";
for ( ; !it.IsAtEnd(); ++it)
{
--castBackReverseIt;
itk::Image<itk::Vector<unsigned short, 5>, 3>::IndexType index = it.GetIndex();
itk::Image<itk::Vector<unsigned short, 5>, 3>::IndexType rindex = castBackReverseIt.GetIndex();
for (unsigned int i=0; i < index.GetIndexDimension(); i++)
{
if (index[i] != rindex[i])
{
std::cout << index[i] << " != " << rindex[i] << std::endl;
status = 1;
}
}
}
if (status == 0)
{
std::cout << "Passed" << std::endl;
}
else
{
std::cout << "Failed" << std::endl;
}
return status;
}
|