Skip to content

Commit 7c2cd63

Browse files
aylwardhjmjohnson
authored andcommitted
BUG: SpatialObjectToImageFilter didn't use reference image's index
The base class for SpatialObjectToImage filtering didn't use the index of the reference image. As a result, when, for example, rendering a spatial object into an image, if the reference image had a non-zero index, the generated image would not match the spatial extent of the reference image.
1 parent 24e7b45 commit 7c2cd63

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Modules/Core/SpatialObjects/include/itkSpatialObjectToImageFilter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ITK_TEMPLATE_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutpu
5050
using ConstPointer = SmartPointer<const Self>;
5151

5252
using OutputImageType = TOutputImage;
53+
using IndexType = typename OutputImageType::IndexType;
5354
using SizeType = typename OutputImageType::SizeType;
5455
using PointType = typename OutputImageType::PointType;
5556
using OutputImagePointer = typename OutputImageType::Pointer;
@@ -100,6 +101,7 @@ class ITK_TEMPLATE_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutpu
100101
this->SetOrigin(refImage->GetOrigin());
101102
this->SetSpacing(refImage->GetSpacing());
102103
this->SetDirection(refImage->GetDirection());
104+
this->SetIndex(refImage->GetLargestPossibleRegion().GetIndex());
103105
this->SetSize(refImage->GetLargestPossibleRegion().GetSize());
104106
}
105107

@@ -161,6 +163,12 @@ class ITK_TEMPLATE_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutpu
161163
virtual const double *
162164
GetOrigin() const;
163165

166+
/** The index of the output image. The index is the pixel
167+
* coordinates of the image region.
168+
* \sa GetSize() */
169+
itkSetMacro(Index, IndexType);
170+
itkGetConstMacro(Index, IndexType);
171+
164172
/** The spatial object being transformed can be part of a hierarchy.
165173
* How deep in the hierarchy should we descend in generating the
166174
* image? A ChildrenDepth of 0 means to only include the object
@@ -188,6 +196,7 @@ class ITK_TEMPLATE_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutpu
188196
void
189197
GenerateData() override;
190198

199+
IndexType m_Index;
191200
SizeType m_Size;
192201
double m_Spacing[OutputImageDimension];
193202
double m_Origin[OutputImageDimension];

Modules/Core/SpatialObjects/include/itkSpatialObjectToImageFilter.hxx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SpatialObjectToIm
3232
this->SetNumberOfRequiredInputs(1);
3333
m_ChildrenDepth = TInputSpatialObject::MaximumDepth;
3434
m_Size.Fill(0);
35+
m_Index.Fill(0);
3536
m_Direction.SetIdentity();
3637

3738
for (unsigned int i = 0; i < OutputImageDimension; ++i)
@@ -285,8 +286,6 @@ SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::GenerateData()
285286
InputObject->GetFamilyBoundingBoxInWorldSpace()->GetMinimum()[i]);
286287
}
287288

288-
typename OutputImageType::IndexType index;
289-
index.Fill(0);
290289
typename OutputImageType::RegionType region;
291290

292291
// If the size of the output has been explicitly specified, the filter
@@ -312,7 +311,27 @@ SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::GenerateData()
312311
{
313312
region.SetSize(size);
314313
}
315-
region.SetIndex(index);
314+
315+
IndexType index;
316+
index.Fill(0);
317+
specified = false;
318+
for (i = 0; i < OutputImageDimension; ++i)
319+
{
320+
if (m_Index[i] != 0)
321+
{
322+
specified = true;
323+
break;
324+
}
325+
}
326+
327+
if (specified)
328+
{
329+
region.SetIndex(m_Index);
330+
}
331+
else
332+
{
333+
region.SetIndex(index);
334+
}
316335

317336
OutputImage->SetLargestPossibleRegion(region); //
318337
OutputImage->SetBufferedRegion(region); // set the region
@@ -379,6 +398,7 @@ SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::PrintSelf(std::os
379398
{
380399
Superclass::PrintSelf(os, indent);
381400
os << indent << "Size : " << m_Size << std::endl;
401+
os << indent << "Index : " << m_Index << std::endl;
382402
os << indent << "Children depth : " << m_ChildrenDepth << std::endl;
383403
os << indent << "Inside Value : " << m_InsideValue << std::endl;
384404
os << indent << "Outside Value : " << m_OutsideValue << std::endl;

0 commit comments

Comments
 (0)