InsightSoftwareConsortium / InsightSoftwareConsortium/ITK
ConstShapedNeighborhoodIterator doesn't loop over all possibilities
- Dominant language
- C++
- Stars
- 1.7k
- Forks
- 748
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 64
Description
In a region of 12×12 pixels, an iterator is expected to hit all 144 possibilities. However it gets only 142 of them.
### Steps to Reproduce
```c++
#include "itkImage.h"
#include "itkConstShapedNeighborhoodIterator.h"
#include "itkConstantBoundaryCondition.h"
int man(int argc, char * argv[])
{
using PixelType = unsigned char;
constexpr unsigned int Dimension = 2;
using ImageType = itk::Image;
using ImagePointer = typename ImageType::Pointer;
using RegionType = typename ImageType::RegionType;
using IndexType = typename RegionType::IndexType;
using SizeType = typename RegionType::SizeType;
using OffsetType = typename RegionType::OffsetType;
ImagePointer imageTest = ImageType::New();
imageTest->SetRegions(RegionType{ IndexType{ 0, 0 }, SizeType{ 10, 10 } });
imageTest->Allocate();
using BoundaryConditionType = itk::ConstantBoundaryCondition;
using SquareIterator = itk::ConstShapedNeighborhoodIterator;
using RadiusType = typename SquareIterator::RadiusType;
SquareIterator sqIt(RadiusType{ 1, 1 }, imageTest, RegionType{ IndexType{ -1, -1 }, SizeType{ 12, 12 } });
sqIt.ActivateOffset(OffsetType{ 0, 0 });
sqIt.ActivateOffset(OffsetType{ 0, 1 });
sqIt.ActivateOffset(OffsetType{ 1, 0 });
sqIt.ActivateOffset(OffsetType{ 1, 1 });
int numberOfIterations{ 0 };
for (sqIt.GoToBegin(); !sqIt.IsAtEnd(); ++sqIt)
{
++numberOfIterations;
}
std::cout << "numberOfIterations = " << numberOfIterations << " but should be 144." << std::endl;
}
```
### Expected behavior
Prints `numberOfIterations = 144 but should be 144.`
### Actual behavior
Prints `numberOfIterations = 142 but should be 144.`
### Reproducibility
100%
### Versions
Master branch (currently 25dea2f73d974855fbdfb13f5f2d7f25e4ec8a4e).
### Environment
Ubuntu 20.04. gcc 10.2.0.
### Additional information.
I am not completely sure by any means, but maybe the problem is that the region for the square iterator includes values with `x < 0`. Because of this, the `.end()` value that is calculated as the left-most position of the row after the region can be misinterpreted as being near the right end of the previous row. If that's true, maybe it is also true that a computed `WrapOffset` is negative when it is implicitly assumed to always be positive.
Contributor guide
Assessment
This issue has not been assessed yet.