Skip to content

Commit 0756bd3

Browse files
committed
BUG: fix handling of negative and non-zero region indices in rasterizer
Avoid creation of vector of indices, instead directly set pixel values. This allows correct 3D index to linear (buffer) index conversion. It also reduces memory used, and shortens running time. Closes #2884.
1 parent 6682f86 commit 0756bd3

File tree

2 files changed

+9
-46
lines changed

2 files changed

+9
-46
lines changed

Modules/Core/Mesh/include/itkTriangleMeshToBinaryImageFilter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ class ITK_TEMPLATE_EXPORT TriangleMeshToBinaryImageFilter : public ImageSource<T
129129
using PointVector = std::vector<PointType>;
130130
using PointArray = std::vector<std::vector<PointType>>;
131131

132-
using StencilIndexVector = std::vector<int>;
133132
/** Spacing (size of a pixel) of the output image. The
134133
* spacing is the geometric distance between image samples.
135134
* It is stored internally as double, but may be set from
@@ -245,8 +244,6 @@ class ITK_TEMPLATE_EXPORT TriangleMeshToBinaryImageFilter : public ImageSource<T
245244

246245
DirectionType m_Direction;
247246

248-
StencilIndexVector m_StencilIndex;
249-
250247
void
251248
PrintSelf(std::ostream & os, Indent indent) const override;
252249

Modules/Core/Mesh/include/itkTriangleMeshToBinaryImageFilter.hxx

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -185,46 +185,6 @@ TriangleMeshToBinaryImageFilter<TInputMesh, TOutputImage>::GenerateData()
185185

186186
RasterizeTriangles();
187187

188-
using myIteratorType = itk::ImageRegionIteratorWithIndex<OutputImageType>;
189-
190-
myIteratorType it(OutputImage, OutputImage->GetLargestPossibleRegion());
191-
192-
int DataIndex = 0;
193-
int StencilId = 0;
194-
it.GoToBegin();
195-
196-
size_t n = m_StencilIndex.size();
197-
if (n == 0)
198-
{
199-
itkWarningMacro(<< "No Image Indices Found.");
200-
}
201-
else
202-
{
203-
int StencilMin = m_StencilIndex[0];
204-
int StencilMax = m_StencilIndex[n - 1];
205-
206-
while (!it.IsAtEnd())
207-
{
208-
if (DataIndex >= StencilMin && DataIndex <= StencilMax)
209-
{
210-
if (DataIndex == m_StencilIndex[StencilId])
211-
{
212-
it.Set(m_InsideValue);
213-
StencilId++;
214-
}
215-
else
216-
{
217-
it.Set(m_OutsideValue);
218-
}
219-
}
220-
else
221-
{
222-
it.Set(m_OutsideValue);
223-
}
224-
DataIndex++;
225-
++it;
226-
}
227-
}
228188
itkDebugMacro(<< "TriangleMeshToBinaryImageFilter::Update() finished");
229189
} // end update function
230190

@@ -459,8 +419,9 @@ TriangleMeshToBinaryImageFilter<TInputMesh, TOutputImage>::RasterizeTriangles()
459419
++cellIt;
460420
}
461421

462-
// create the equivalent of vtkStencilData from our zymatrix
463-
m_StencilIndex.clear(); // prevent corruption of the filter in later updates
422+
OutputImagePointer outputImage = this->GetOutput();
423+
outputImage->FillBuffer(m_OutsideValue);
424+
464425
for (int z = extent[4]; z <= extent[5]; ++z)
465426
{
466427
for (int y = extent[2]; y <= extent[3]; ++y)
@@ -529,9 +490,14 @@ TriangleMeshToBinaryImageFilter<TInputMesh, TOutputImage>::RasterizeTriangles()
529490

530491
if (x2 >= x1)
531492
{
493+
IndexType ind;
494+
ind[1] = y;
495+
ind[2] = z;
532496
for (int idX = x1; idX <= x2; ++idX)
533497
{
534-
m_StencilIndex.push_back(idX + y * m_Size[0] + z * m_Size[0] * m_Size[1]);
498+
// TODO: check whether replacing this for loop by ImageScanlineIterator is faster
499+
ind[0] = idX;
500+
outputImage->SetPixel(ind, m_InsideValue);
535501
}
536502
}
537503
// next x1 value must be at least x2+1

0 commit comments

Comments
 (0)