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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License" << std::endl;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkNeighborhoodIteratorTestCommon.hxx"
#include "itkConstNeighborhoodIteratorWithOnlyIndex.h"
template <typename TImage>
typename TImage::Pointer itkConstNeighborhoodIteratorWithOnlyIndexTestGetTestImage(int d1, int d2, int d3, int d4)
{
itk::Size<4> sizeND;
sizeND[0] = d1;
sizeND[1] = d2;
sizeND[2] = d3;
sizeND[3] = d4;
itk::Index<4> origND;
origND.Fill(0);
itk::ImageRegion<4> RegionND;
RegionND.SetSize(sizeND);
RegionND.SetIndex(origND);
typename TImage::Pointer imageND = TImage::New();
imageND->SetLargestPossibleRegion(RegionND);
imageND->SetBufferedRegion(RegionND);
imageND->SetRequestedRegion(RegionND);
return imageND;
}
template< typename TImage >
int itkConstNeighborhoodIteratorWithOnlyIndexTestRun()
{
typename TImage::Pointer img = itkConstNeighborhoodIteratorWithOnlyIndexTestGetTestImage<TImage>(10, 10, 5, 3);
typedef TImage ImageType;
typedef itk::ConstNeighborhoodIteratorWithOnlyIndex< ImageType > ConstNeighborhoodIteratorType;
typedef typename ConstNeighborhoodIteratorType::IndexType IndexType;
IndexType loc;
loc[0] = 4; loc[1] = 4; loc[2] = 2; loc[3] = 1;
typename ConstNeighborhoodIteratorType::RadiusType radius;
radius[0] = radius[1] = radius[2] = radius[3] = 1;
typename ConstNeighborhoodIteratorType::RegionType reg;
typename ConstNeighborhoodIteratorType::SizeType sz;
IndexType idx;
idx[0] = idx[1] = idx[2] = 0; idx[3] = 1;
sz[0] = sz[1] = 10; sz[2] = 5; sz[3] = 1;
reg.SetIndex(idx);
reg.SetSize(sz);
std::cout << "Creating ConstNeighborhoodIterator" << std::endl;
ConstNeighborhoodIteratorType it(radius, img, reg);
std::cout << "Moving iterator using SetLocation() to loc: " << loc << std::endl;
it.SetLocation(loc);
it.Print(std::cout);
if( it.GetIndex() != loc )
{
std::cerr << "Error with SetLocation." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test GetIndex( NeighborhoodIndex )" << std::endl;
if( it.GetIndex( it.GetCenterNeighborhoodIndex() ) != loc )
{
std::cerr << "Error getting index from center nhood index. Returned " << it.GetIndex( it.GetCenterNeighborhoodIndex() ) << std::endl;
return EXIT_FAILURE;
}
IndexType truthIndex;
for( unsigned int i=0; i < 4; i++ )
{
truthIndex[i] = loc[i] - radius[i];
}
if( it.GetIndex( 0 ) != truthIndex )
{
std::cerr << "Error getting index from nhood index 0. Returned " << it.GetIndex( 0 ) << std::endl;
}
std::cout << "Testing GoToBegin()" << std::endl;
it.GoToBegin();
it.Print(std::cout);
if( it.GetIndex() != idx )
{
std::cerr << "Error with GoToBegin. Expected: " << idx << ", GetIndex: " << it.GetIndex() << std::endl;
return EXIT_FAILURE;
}
std::cout << "Testing IsAtBegin()" << std::endl;
std::cout << it.IsAtBegin() << std::endl;
if( ! it.IsAtBegin() )
{
std::cerr << "Error with IsAtBegin." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Testing GoToEnd()" << std::endl;
it.GoToEnd();
it.Print(std::cout);
std::cout << "Testing IsAtEnd()" << std::endl;
std::cout << it.IsAtEnd() << std::endl;
if( ! it.IsAtEnd() )
{
std::cerr << "Error with either IsAtEnd or GoToEnd." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Testing forward iteration" << std::endl;
it.GoToBegin();
IndexType index = it.GetIndex();
++it;
if( it.GetIndex()[0] != index[0]+1 )
{
std::cerr << "Error with fwd iteration" << std::endl;
return EXIT_FAILURE;
}
while (! it.IsAtEnd())
{
++it;
}
// fwd iterate by a line
it.GoToBegin();
index = it.GetIndex();
for( unsigned int i = 0; i < sz[0]; i++ )
{
++it;
}
if( it.GetIndex()[0] != index[0] || it.GetIndex()[1] != index[1]+1 )
{
std::cerr << "Error with fwd iteration by one line." << std::endl;
return EXIT_FAILURE;
}
// fwd iterate by a slice
index = it.GetIndex();
for( unsigned int i = 0; i < sz[0]*sz[1]; i++ )
{
++it;
}
if( it.GetIndex()[0] != index[0] || it.GetIndex()[1] != index[1] || it.GetIndex()[2] != index[2] + 1 )
{
std::cerr << "Error with fwd iteration by one slice." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Testing reverse iteration" << std::endl;
it.GoToEnd();
index = it.GetIndex();
--it;
if( it.GetIndex()[3] != index[3]-1 )
{
std::cerr << "Error with reverse iteration" << std::endl;
return EXIT_FAILURE;
}
index = it.GetIndex();
for( unsigned int i = 0; i < sz[0]; i++ )
{
--it;
}
if( it.GetIndex()[0] != index[0] || it.GetIndex()[1] != index[1] - 1 )
{
std::cerr << "Error with reverse iteration by one line." << std::endl;
return EXIT_FAILURE;
}
index = it.GetIndex();
for( unsigned int i = 0; i < sz[0]*sz[1]; i++ )
{
--it;
}
if( it.GetIndex()[0] != index[0] || it.GetIndex()[1] != index[1] || it.GetIndex()[2] != index[2] - 1 )
{
std::cerr << "Error with reverse iteration by one slice." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Moving iterator using SetLocation()" << std::endl;
it.SetLocation(loc);
it.Print(std::cout);
std::cout << "Testing GetIndex()" << std::endl;
std::cout << it.GetIndex() << std::endl;
std::cout << "Testing GetBoundingBoxAsImageRegion" << std::endl;
std::cout << it.GetBoundingBoxAsImageRegion() << std::endl;
////
std::cout << "Testing random access iteration" << std::endl;
typename ImageType::Pointer ra_img = itkConstNeighborhoodIteratorWithOnlyIndexTestGetTestImage<TImage>(10, 10, 5, 3);
loc[0] = 4; loc[1] = 4; loc[2] = 2; loc[3] = 1;
radius[0] = radius[1] = radius[2] = radius[3] = 1;
std::cout << "Creating ConstNeighborhoodIterator" << std::endl;
ConstNeighborhoodIteratorType ra_it(radius, ra_img, ra_img->GetRequestedRegion());
ConstNeighborhoodIteratorType copy_it;
std::cout << "Test copying." << std::endl;
copy_it = ra_it;
if( copy_it != ra_it || ! (copy_it == ra_it) )
{
std::cerr << "Failure with copying or equality comparison." << std::endl;
return EXIT_FAILURE;
}
if( ! (copy_it >= ra_it) || ! (copy_it <= ra_it) )
{
std::cerr << "Failure with copying or >= or <= comparison." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Testing random access" << std::endl;
ra_it.SetLocation( loc );
std::cout << "Adding [1, 1, 1, 1]" << std::endl;
OffsetType a_off;
a_off.Fill(1);
ra_it += a_off;
for( unsigned int i=0; i < 4; i++ )
{
IndexType ind = ra_it.GetIndex();
if( ind[i] != loc[i] + 1 )
{
std::cerr << "Error with adding offset " << a_off << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "Test iterator comparisons" << std::endl;
if( ! (copy_it < ra_it) || ! (copy_it <= ra_it) )
{
std::cerr << "Error with < or <=." << std::endl;
return EXIT_FAILURE;
}
if( ! (copy_it < ra_it) || ! (copy_it <= ra_it) )
{
std::cerr << "Error with < or <=." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Subtracting [1, 1, 1, 1]" << std::endl;
ra_it -= a_off;
for( unsigned int i=0; i < 4; i++ )
{
IndexType ind = ra_it.GetIndex();
if( ind[i] != loc[i] )
{
std::cerr << "Error with substracting offset " << a_off << std::endl;
return EXIT_FAILURE;
}
}
copy_it = ra_it;
std::cout << "Adding [0 0 0 2]" << std::endl;
a_off.Fill(0);
a_off[3] = 2;
ra_it += a_off;
IndexType truth = loc;
truth += a_off;
for( unsigned int i=0; i < 4; i++ )
{
IndexType ind = ra_it.GetIndex();
if( ind[i] != truth[i] )
{
std::cerr << "Error with adding offset " << a_off << std::endl;
return EXIT_FAILURE;
}
}
if( ra_it < copy_it || ra_it <= copy_it || copy_it > ra_it || copy_it >= ra_it )
{
std::cerr << "Error with > or >= comparison after adding offset." << std::endl;
return EXIT_FAILURE;
}
copy_it = ra_it;
std::cout << "Adding [0 8 0 0]" << std::endl;
a_off.Fill(0);
a_off[1] = 8;
ra_it += a_off;
truth += a_off;
for( unsigned int i=0; i < 4; i++ )
{
IndexType ind = ra_it.GetIndex();
if( ind[i] != truth[i] )
{
std::cerr << "Error with adding offset " << a_off << std::endl;
return EXIT_FAILURE;
}
}
if( ra_it < copy_it || ra_it <= copy_it || copy_it > ra_it || copy_it >= ra_it )
{
std::cerr << "Error with comparison after adding offset " << a_off << std::endl;
std::cerr << (ra_it < copy_it) << ", " << (ra_it <= copy_it) << ", " << (copy_it > ra_it) << ", " << (copy_it >= ra_it) << std::endl;
return EXIT_FAILURE;
}
copy_it = ra_it;
std::cout << "Adding [5 -3 2 -1]" << std::endl;
a_off[0] = 5;
a_off[1] = -3;
a_off[2] = 2;
a_off[3] = -1;
ra_it += a_off;
truth += a_off;
for( unsigned int i=0; i < 4; i++ )
{
IndexType ind = ra_it.GetIndex();
if( ind[i] != truth[i] )
{
std::cerr << "Error with adding offset " << a_off << std::endl;
return EXIT_FAILURE;
}
}
if( ra_it > copy_it || ra_it >= copy_it || copy_it < ra_it || copy_it <= ra_it )
{
std::cerr << "Error with comparison after adding offset." << std::endl;
return EXIT_FAILURE;
}
//
// Test IndexInBounds
//
std::cout << "Testing IndexInBounds" << std::endl;
int dims[4] = {13,11,9,7};
typename ImageType::Pointer iib_img = itkConstNeighborhoodIteratorWithOnlyIndexTestGetTestImage<TImage>(dims[0], dims[1], dims[2], dims[3]);
radius[0] = 4;
radius[1] = 3;
radius[2] = 2;
radius[3] = 1;
std::cout << "Creating ConstNeighborhoodIterator" << std::endl;
typedef ConstNeighborhoodIteratorType IteratorType;
IteratorType iib_it(radius, iib_img, iib_img->GetRequestedRegion());
typename IteratorType::OffsetType resultOffset;
typename IteratorType::OffsetType internalIndex;
typename IteratorType::IndexType centerLoc;
centerLoc[0] = static_cast<typename IteratorType::IndexType::IndexValueType>(dims[0] / 2);
centerLoc[1] = static_cast<typename IteratorType::IndexType::IndexValueType>(dims[1] / 2);
centerLoc[2] = static_cast<typename IteratorType::IndexType::IndexValueType>(dims[2] / 2);
centerLoc[3] = static_cast<typename IteratorType::IndexType::IndexValueType>(dims[3] / 2);
iib_it.GoToBegin();
bool inside = iib_it.IndexInBounds( 0, internalIndex, resultOffset );
if( inside )
{
std::cerr << "IndexInBounds failed for index 0, expected false." << std::endl;
return EXIT_FAILURE;
}
for( unsigned int n = 0; n < 4; n++ )
{
if( resultOffset[n] != static_cast<itk::OffsetValueType>( radius[n] ) )
{
std::cerr << "IndexInBounds failed. Expected resultOffset of " << radius << ", but got " << resultOffset << std::endl;
return EXIT_FAILURE;
}
}
inside = iib_it.IndexInBounds( iib_it.Size()-1, internalIndex, resultOffset );
if( ! inside )
{
std::cerr << "IndexInBounds failed for index size-1, expected true." << std::endl;
return EXIT_FAILURE;
}
for( unsigned int n = 0; n < 4; n++ )
{
if( resultOffset[n] != 0 )
{
std::cerr << "IndexInBounds failed. Expected resultOffset of 0, but got " << resultOffset << std::endl;
return EXIT_FAILURE;
}
}
// Test min boundary by dimension
int result = EXIT_SUCCESS;
for( unsigned int n = 0; n < 4; n++ )
{
typename IteratorType::IndexType boundaryLoc = centerLoc;
boundaryLoc[n] = 0;
iib_it.SetLocation( boundaryLoc );
if( iib_it.IndexInBounds( 0, internalIndex, resultOffset ) )
{
std::cerr << "IndexInBounds failed for min boundaryLoc: " << boundaryLoc << " and dimension: " << n << ". Expected false." << std::endl;
result = EXIT_FAILURE;
}
}
// Test max boundary by dimension
for( unsigned int n = 0; n < 4; n++ )
{
typename IteratorType::IndexType boundaryLoc = centerLoc;
boundaryLoc[n] = dims[n]-1;
iib_it.SetLocation( boundaryLoc );
if( iib_it.IndexInBounds( iib_it.Size()-1, internalIndex, resultOffset ) )
{
std::cerr << "IndexInBounds failed for max boundaryLoc: " << boundaryLoc << " and dimension: " << n << ". Expected false." << std::endl;
result = EXIT_FAILURE;
}
}
// Test center
iib_it.SetLocation( centerLoc );
inside = iib_it.IndexInBounds( 0, internalIndex, resultOffset );
if( ! inside )
{
std::cerr << "IndexInBounds failed for index 0, expected true." << std::endl;
result = EXIT_FAILURE;
}
return result;
}
int itkConstNeighborhoodIteratorWithOnlyIndexTest(int, char* [] )
{
std::cout << "*** Testing with itk::Image" << std::endl << std::endl;
if( itkConstNeighborhoodIteratorWithOnlyIndexTestRun< itk::Image<char,4> >() == EXIT_FAILURE )
{
std::cerr << "XXX Failed with itk::Image XXX" << std::endl;
return EXIT_FAILURE;
}
std::cout << std::endl << std::endl << "*** Testing with itk::ImageBase" << std::endl << std::endl;
if ( itkConstNeighborhoodIteratorWithOnlyIndexTestRun< itk::ImageBase<4> >() == EXIT_FAILURE )
{
std::cerr << "XXX Failed with itk::ImageBase XXX" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|