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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://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 "itkConstShapedNeighborhoodIterator.h"
void
PrintShapedNeighborhood(const itk::ConstShapedNeighborhoodIterator<TestImageType> & n)
{
itk::ConstShapedNeighborhoodIterator<TestImageType>::ConstIterator it;
std::cout << n.GetIndex() << "->[";
for (it = n.Begin(); !it.IsAtEnd(); ++it)
{
std::cout << it.Get();
}
std::cout << ']' << std::endl;
}
int
itkConstShapedNeighborhoodIteratorTest(int, char *[])
{
TestImageType::Pointer img = GetTestImage(10, 10, 5, 3);
itk::ConstShapedNeighborhoodIterator<TestImageType>::IndexType loc;
loc[0] = 4;
loc[1] = 4;
loc[2] = 2;
loc[3] = 1;
// radius of the iterator
itk::ConstShapedNeighborhoodIterator<TestImageType>::RadiusType radius;
radius[0] = radius[1] = radius[2] = radius[3] = 1;
// region over which the iterator is defined
itk::ConstShapedNeighborhoodIterator<TestImageType>::RegionType reg;
itk::ConstShapedNeighborhoodIterator<TestImageType>::SizeType sz;
itk::ConstShapedNeighborhoodIterator<TestImageType>::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);
// initialize an iterator
println("Creating ConstShapedNeighborhoodIterator");
itk::ConstShapedNeighborhoodIterator<TestImageType> it(radius, img, reg);
it.Print(std::cout);
println("Moving iterator using SetLocation()");
it.SetLocation(loc);
it.Print(std::cout);
println("Testing GoToBegin()");
it.GoToBegin();
it.Print(std::cout);
println("Testing IsAtBegin()");
std::cout << it.IsAtBegin() << std::endl;
println("Testing GoToEnd()");
it.GoToEnd();
it.Print(std::cout);
println("Testing IsAtEnd()");
std::cout << it.IsAtEnd() << std::endl;
println("Testing forward iteration");
it.GoToBegin();
itk::ConstShapedNeighborhoodIterator<TestImageType>::OffsetType off;
off[0] = 0;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
while (!it.IsAtEnd())
{
PrintShapedNeighborhood(it);
++it;
}
println("Testing reverse iteration");
it.GoToEnd();
while (!it.IsAtBegin())
{
PrintShapedNeighborhood(it);
--it;
}
println("Moving iterator: it.GoToBegin(); it += (1, 1, 1, 1)");
it.GoToBegin();
off[0] = 1;
off[1] = 1;
off[2] = 1;
off[3] = 1;
it += off;
PrintShapedNeighborhood(it);
println("Moving iterator: it -= (1, 1, 1, 1)");
it -= off;
PrintShapedNeighborhood(it);
println("Moving iterator using SetLocation()");
it.SetLocation(loc);
it.Print(std::cout);
println("Initializing ConstShapedNeighborhoodIterator");
println("...turn on [0,0,0,0], the center pixel");
off[0] = 0;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
for (unsigned int r = 0; r < 1; ++r)
{
println("...turn on [1,0,0,0]");
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [1,0,0,0] again");
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [-1,0,0,0]");
off[0] = -1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [0,-1,0,0]");
off[0] = 0;
off[1] = -1;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [0,1,0,0]");
off[0] = 0;
off[1] = 1;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn off [-1,0,0,0]");
off[0] = -1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.DeactivateOffset(off);
it.Print(std::cout);
println("...turn off [1,0,0,0]");
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.DeactivateOffset(off);
it.Print(std::cout);
println("...turn off [0,1,0,0]");
off[0] = 0;
off[1] = 1;
off[2] = 0;
off[3] = 0;
it.DeactivateOffset(off);
it.Print(std::cout);
println("...turn off [0,-1,0,0]");
off[0] = 0;
off[1] = -1;
off[2] = 0;
off[3] = 0;
it.DeactivateOffset(off);
it.Print(std::cout);
println("...turn off [0,-1,0,0] again");
off[0] = 0;
off[1] = -1;
off[2] = 0;
off[3] = 0;
it.DeactivateOffset(off);
it.Print(std::cout);
println("...turn off [0,0 ,0,0]");
off[0] = 0;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.DeactivateOffset(off);
it.Print(std::cout);
println("...turn on [1,0,0,0]");
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [1,0,0,0] again");
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [-1,0,0,0]");
off[0] = -1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [0,-1,0,0]");
off[0] = 0;
off[1] = -1;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println(" Testing it.ClearActiveList() ");
it.ClearActiveList();
it.Print(std::cout);
println(" NOW REPEAT ");
}
println("...turn on [1,0,0,0]");
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [1,0,0,0] again");
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [-1,0,0,0]");
off[0] = -1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [0,-1,0,0]");
off[0] = 0;
off[1] = -1;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
println("...turn on [0,1,0,0]");
off[0] = 0;
off[1] = 1;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
it.Print(std::cout);
std::cout << "it.GetActiveIndexListSize()=" << it.GetActiveIndexListSize();
println("Testing GetActiveIndexList()");
itk::ConstShapedNeighborhoodIterator<TestImageType>::IndexListType l = it.GetActiveIndexList();
itk::ConstShapedNeighborhoodIterator<TestImageType>::IndexListType ::const_iterator ali = l.begin();
while (ali != l.end())
{
std::cout << *ali << ' ';
++ali;
}
std::cout << std::endl;
println("Testing const iteration through the neighborhood.");
itk::ConstShapedNeighborhoodIterator<TestImageType>::ConstIterator ci = it.Begin();
println("Testing using IsAtEnd()");
while (!ci.IsAtEnd())
{
std::cout << ci.GetNeighborhoodIndex() << " -> " << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
ci++;
}
println("Testing using != it.End()");
for (ci = it.Begin(); ci != it.End(); ++ci)
{
std::cout << ci.GetNeighborhoodIndex() << " -> " << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
}
println("Testing reverse iteration using != it.Begin()");
ci = it.End();
--ci;
while (ci != it.Begin())
{
std::cout << ci.GetNeighborhoodIndex() << " -> " << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
ci--;
}
std::cout << ci.GetNeighborhoodIndex() << " -> " << ci.GetNeighborhoodOffset() << " = " << ci.Get() << std::endl;
std::cout << std::endl;
std::cout << "------------------------------" << std::endl;
std::cout << std::endl;
println("Testing activating and deactivating pixels on-the-fly");
println("it.GoToBegin(); it.ClearActiveList(); Activate 1 0 0 0 and -1 0 0 0 and 0 0 0 0");
it.GoToBegin();
it.ClearActiveList();
off[0] = -1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
off[0] = 0;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
PrintShapedNeighborhood(it);
println("Move the neighborhood two pixels using operator ++");
++it;
++it;
PrintShapedNeighborhood(it);
println("Clear the active list");
it.ClearActiveList();
PrintShapedNeighborhood(it);
println("Move the neighborhood one pixel using operator ++");
++it;
println("Reactivate the same indices");
off[0] = -1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
off[0] = 1;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
off[0] = 0;
off[1] = 0;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
PrintShapedNeighborhood(it);
println("Activate 0 1 0 0");
off[0] = 0;
off[1] = 1;
off[2] = 0;
off[3] = 0;
it.ActivateOffset(off);
PrintShapedNeighborhood(it);
println("Testing operator=");
itk::ConstShapedNeighborhoodIterator<TestImageType> oeIt;
oeIt = it;
PrintShapedNeighborhood(it);
PrintShapedNeighborhood(oeIt);
it.Print(std::cout);
oeIt.Print(std::cout);
int result = EXIT_SUCCESS;
// Iterate over a region, then change the region and iterate over the new region
{
// Create an image
using ChangeRegionTestImageType = itk::Image<int, 2>;
ChangeRegionTestImageType::IndexType imageCorner;
imageCorner.Fill(0);
ChangeRegionTestImageType::SizeType imageSize;
imageSize.Fill(4);
ChangeRegionTestImageType::RegionType imageRegion(imageCorner, imageSize);
auto image = ChangeRegionTestImageType::New();
image->SetRegions(imageRegion);
image->Allocate();
itk::ImageRegionIterator<ChangeRegionTestImageType> createImageIterator(image, imageRegion);
// Set all pixels with first index == 0 to 0, and set the rest of the image to 255
while (!createImageIterator.IsAtEnd())
{
if (createImageIterator.GetIndex()[0] == 0)
{
createImageIterator.Set(0);
}
else
{
createImageIterator.Set(255);
}
++createImageIterator;
}
// Setup and iterate over the first region
ChangeRegionTestImageType::IndexType region1Start;
region1Start.Fill(1);
ChangeRegionTestImageType::SizeType regionSize;
regionSize.Fill(1);
ChangeRegionTestImageType::RegionType region1(region1Start, regionSize);
// Create the radius (a 3x3 region)
ChangeRegionTestImageType::SizeType neighborhoodRadius;
neighborhoodRadius.Fill(1);
// Use the first two offsets
std::vector<itk::Offset<2>> offsets;
ChangeRegionTestImageType::OffsetType offset = { { -1, -1 } };
offsets.push_back(offset);
offset[0] = 0;
offset[1] = 0;
offsets.push_back(offset);
using ShapedNeighborhoodIteratorType = itk::ConstShapedNeighborhoodIterator<ChangeRegionTestImageType>;
ShapedNeighborhoodIteratorType shapedNeighborhoodIterator(neighborhoodRadius, image, region1);
// Activate all of the offsets
for (const auto & offset_item : offsets)
{
shapedNeighborhoodIterator.ActivateOffset(offset_item);
}
std::vector<int> expectedValuesRegion1(2);
expectedValuesRegion1[0] = 0;
expectedValuesRegion1[1] = 255;
unsigned int counter = 0;
// while(!shapedNeighborhoodIterator.IsAtEnd()) // no need for this loop as we are only iterating over a 1x1 region
//{
ShapedNeighborhoodIteratorType::ConstIterator pixelIterator = shapedNeighborhoodIterator.Begin();
while (!pixelIterator.IsAtEnd())
{
if (pixelIterator.Get() != expectedValuesRegion1[counter])
{
result = EXIT_FAILURE;
}
counter++;
++pixelIterator;
}
//++imageIterator;
//}
// Change iteration region
ChangeRegionTestImageType::IndexType region2start;
region2start.Fill(2);
ChangeRegionTestImageType::RegionType region2(region2start, regionSize);
shapedNeighborhoodIterator.SetRegion(region2);
shapedNeighborhoodIterator.GoToBegin();
std::vector<int> expectedValuesRegion2(2);
expectedValuesRegion2[0] = 255;
expectedValuesRegion2[1] = 255;
counter = 0;
// while(!shapedNeighborhoodIterator.IsAtEnd()) // no need for this loop as we are only iterating over a 1x1 region
//{
pixelIterator = shapedNeighborhoodIterator.Begin();
while (!pixelIterator.IsAtEnd())
{
if (pixelIterator.Get() != expectedValuesRegion2[counter])
{
result = EXIT_FAILURE;
}
counter++;
++pixelIterator;
}
//++imageIterator;
//}
} // end "Change Region" test
return result;
}
//
// this is kind of a duff test, in that it doesn't fail w/the old code
// at runtime, it won't compile at all. But it does at least do
// coverage of the newly exposed methods.
template <typename ImageType>
class MyDerivedCSNI : public itk::ConstShapedNeighborhoodIterator<ImageType>
{
public:
using Superclass = typename itk::ConstShapedNeighborhoodIterator<ImageType>;
using typename Superclass::SizeType;
using typename Superclass::IndexType;
using typename Superclass::RadiusType;
using typename Superclass::RegionType;
void
TestNewExposedProtectedMembers();
MyDerivedCSNI(const SizeType & radius, const ImageType * ptr, const RegionType & region)
: Superclass(radius, const_cast<ImageType *>(ptr), region)
{}
};
template <typename ImageType>
void
MyDerivedCSNI<ImageType>::TestNewExposedProtectedMembers()
{
bool needToUseBoundaryCondition(this->GetNeedToUseBoundaryCondition());
this->NeedToUseBoundaryConditionOn();
this->NeedToUseBoundaryConditionOff();
this->SetNeedToUseBoundaryCondition(needToUseBoundaryCondition);
}
|