Skip to content

Commit

Permalink
BUG: BresenhamLine was producing more indices than necessary
Browse files Browse the repository at this point in the history
Closes #2926.
  • Loading branch information
dzenanz committed Dec 8, 2021
1 parent 3474342 commit 5edff15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
7 changes: 3 additions & 4 deletions Modules/Core/Common/include/itkBresenhamLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@

namespace itk
{
/* a simple class that will return an array of indexes that are
/* A simple class that will return an array of indices that are
* offsets along the line. The line will be described by a vector and a
* length */

* length. */
template <unsigned int VDimension>
class ITK_TEMPLATE_EXPORT BresenhamLine
{
Expand All @@ -46,7 +45,7 @@ class ITK_TEMPLATE_EXPORT BresenhamLine

/** Build a line in a specified Direction. */
OffsetArray
BuildLine(LType Direction, unsigned int length);
BuildLine(LType Direction, IdentifierType length);

/** Build a line between two pixels. */
IndexArray
Expand Down
20 changes: 15 additions & 5 deletions Modules/Core/Common/include/itkBresenhamLine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace itk
{
template <unsigned int VDimension>
auto
BresenhamLine<VDimension>::BuildLine(LType Direction, unsigned int length) -> OffsetArray
BresenhamLine<VDimension>::BuildLine(LType Direction, IdentifierType length) -> OffsetArray
{
// copied from the line iterator
/** Variables that drive the Bresenham-Algorithm */
Expand Down Expand Up @@ -118,19 +118,29 @@ BresenhamLine<VDimension>::BuildLine(IndexType p0, IndexType p1) -> IndexArray
{
itk::Point<float, VDimension> point0;
itk::Point<float, VDimension> point1;
IdentifierType maxDistance = 0;
for (unsigned int i = 0; i < VDimension; ++i)
{
point0[i] = p0[i];
point1[i] = p1[i];
IdentifierType distance = std::abs(p0[i] - p1[i]) + 1;
if (distance > maxDistance)
{
maxDistance = distance;
}
}

const auto distance = itk::Math::RoundHalfIntegerToEven<unsigned int, double>(point0.EuclideanDistanceTo(point1));
OffsetArray offsets = this->BuildLine(point1 - point0, distance);
OffsetArray offsets = this->BuildLine(point1 - point0, maxDistance + 1);

IndexArray indices(offsets.size());
IndexArray indices;
indices.reserve(offsets.size()); // we might not have to use the last one
for (unsigned int i = 0; i < offsets.size(); ++i)
{
indices[i] = p0 + offsets[i];
indices.push_back(p0 + offsets[i]);
if (indices.back() == p1)
{
break;
}
}
return indices;
}
Expand Down

0 comments on commit 5edff15

Please sign in to comment.