Skip to content

Commit

Permalink
BUG: SpatialObjectToImageFilter didn't use reference image's index
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aylward authored and hjmjohnson committed Dec 13, 2021
1 parent 24e7b45 commit 7c2cd63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ITK_TEMPLATE_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutpu
using ConstPointer = SmartPointer<const Self>;

using OutputImageType = TOutputImage;
using IndexType = typename OutputImageType::IndexType;
using SizeType = typename OutputImageType::SizeType;
using PointType = typename OutputImageType::PointType;
using OutputImagePointer = typename OutputImageType::Pointer;
Expand Down Expand Up @@ -100,6 +101,7 @@ class ITK_TEMPLATE_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutpu
this->SetOrigin(refImage->GetOrigin());
this->SetSpacing(refImage->GetSpacing());
this->SetDirection(refImage->GetDirection());
this->SetIndex(refImage->GetLargestPossibleRegion().GetIndex());
this->SetSize(refImage->GetLargestPossibleRegion().GetSize());
}

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

/** The index of the output image. The index is the pixel
* coordinates of the image region.
* \sa GetSize() */
itkSetMacro(Index, IndexType);
itkGetConstMacro(Index, IndexType);

/** The spatial object being transformed can be part of a hierarchy.
* How deep in the hierarchy should we descend in generating the
* image? A ChildrenDepth of 0 means to only include the object
Expand Down Expand Up @@ -188,6 +196,7 @@ class ITK_TEMPLATE_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutpu
void
GenerateData() override;

IndexType m_Index;
SizeType m_Size;
double m_Spacing[OutputImageDimension];
double m_Origin[OutputImageDimension];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SpatialObjectToIm
this->SetNumberOfRequiredInputs(1);
m_ChildrenDepth = TInputSpatialObject::MaximumDepth;
m_Size.Fill(0);
m_Index.Fill(0);
m_Direction.SetIdentity();

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

typename OutputImageType::IndexType index;
index.Fill(0);
typename OutputImageType::RegionType region;

// If the size of the output has been explicitly specified, the filter
Expand All @@ -312,7 +311,27 @@ SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::GenerateData()
{
region.SetSize(size);
}
region.SetIndex(index);

IndexType index;
index.Fill(0);
specified = false;
for (i = 0; i < OutputImageDimension; ++i)
{
if (m_Index[i] != 0)
{
specified = true;
break;
}
}

if (specified)
{
region.SetIndex(m_Index);
}
else
{
region.SetIndex(index);
}

OutputImage->SetLargestPossibleRegion(region); //
OutputImage->SetBufferedRegion(region); // set the region
Expand Down Expand Up @@ -379,6 +398,7 @@ SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::PrintSelf(std::os
{
Superclass::PrintSelf(os, indent);
os << indent << "Size : " << m_Size << std::endl;
os << indent << "Index : " << m_Index << std::endl;
os << indent << "Children depth : " << m_ChildrenDepth << std::endl;
os << indent << "Inside Value : " << m_InsideValue << std::endl;
os << indent << "Outside Value : " << m_OutsideValue << std::endl;
Expand Down

0 comments on commit 7c2cd63

Please sign in to comment.