Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Modules/Core/Common/include/itkDataObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,30 @@ class ITK_FORCE_EXPORT_MACRO(ITKCommon) DataObject : public Object
friend class ProcessObject;
friend class DataObjectError;
};

/** Makes a new data object that shares the bulk data of `data`, for use in a mini-pipeline, so that the
* mini-pipeline cannot release or rewire `data`. Returns null for a null `data`. The graft has no source: call
* `data->UpdateSource()` first when `data` is a pipeline output that may be out of date. */
template <typename TDataObject>
typename TDataObject::Pointer
MakeGraft(const TDataObject * data)
{
if (data == nullptr)
{
return nullptr;
}
auto graft = TDataObject::New();
graft->Graft(data);
return graft;
}

/** \overload */
template <typename TDataObject>
typename TDataObject::Pointer
MakeGraft(const SmartPointer<TDataObject> & data)
{
return MakeGraft(data.GetPointer());
}
} // end namespace itk

#endif
4 changes: 1 addition & 3 deletions Modules/Core/Common/include/itkImageSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ class ITK_TEMPLATE_EXPORT ImageSource
\code
// Setup the mini-pipeline to process the input to this filter
// The input is not connected to the pipeline.
auto input = InputImageType::New();
input->Graft( const_cast< InputImageType * >( this->GetInput() );
firstFilterInMiniPipeline->SetInput( input );
firstFilterInMiniPipeline->SetInput( MakeGraft( this->GetInput() ) );

// setup the mini-pipeline to calculate the correct regions
// and write to the appropriate bulk data block
Expand Down
1 change: 1 addition & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,7 @@ set(
itkIndexRangeGTest.cxx
itkIntTypesGTest.cxx
itkLightObjectGTest.cxx
itkMakeGraftGTest.cxx
itkMakeUniqueForOverwriteGTest.cxx
itkMathCastWithRangeCheckGTest.cxx
itkMathGTest.cxx
Expand Down
126 changes: 126 additions & 0 deletions Modules/Core/Common/test/itkMakeGraftGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// First include the header file to be tested:
#include "itkDataObject.h"
#include "itkImage.h"
#include "itkImportImageFilter.h"
#include "itkPointSet.h"
#include <gtest/gtest.h>
#include <type_traits>
#include <vector>

namespace
{
using ImageType = itk::Image<float, 2>;
using PointSetType = itk::PointSet<float, 2>;

ImageType::Pointer
MakeImage()
{
auto image = ImageType::New();
image->SetRegions(ImageType::SizeType::Filled(4));
image->SetSpacing(itk::MakeFilled<ImageType::SpacingType>(0.5));
image->SetOrigin(itk::MakeFilled<ImageType::PointType>(-1.5));
ImageType::DirectionType direction{};
direction(0, 1) = 1.0;
direction(1, 0) = -1.0;
image->SetDirection(direction);
image->AllocateInitialized();
return image;
}
} // namespace


TEST(MakeGraft, ImageSharesBufferAndGeometry)
{
const ImageType::ConstPointer image = MakeImage();
const auto graft = itk::MakeGraft(image.GetPointer());

static_assert(std::is_same_v<decltype(graft), const ImageType::Pointer>);
ASSERT_NE(graft, nullptr);
EXPECT_NE(graft.GetPointer(), image.GetPointer());
EXPECT_EQ(graft->GetBufferPointer(), image->GetBufferPointer());
EXPECT_EQ(graft->GetBufferedRegion(), image->GetBufferedRegion());
EXPECT_EQ(graft->GetLargestPossibleRegion(), image->GetLargestPossibleRegion());
EXPECT_EQ(graft->GetSpacing(), image->GetSpacing());
EXPECT_EQ(graft->GetOrigin(), image->GetOrigin());
EXPECT_EQ(graft->GetDirection(), image->GetDirection());
}


TEST(MakeGraft, ReleasingTheGraftKeepsTheOriginalBuffered)
{
const auto image = MakeImage();
const auto graft = itk::MakeGraft(image.GetPointer());

graft->ReleaseData();

EXPECT_EQ(graft->GetBufferedRegion().GetNumberOfPixels(), 0u);
EXPECT_EQ(image->GetBufferedRegion(), image->GetLargestPossibleRegion());
EXPECT_NE(image->GetBufferPointer(), nullptr);
}


TEST(MakeGraft, GraftOfAFilterOutputHasNoSource)
{
std::vector<float> buffer(16, 1.0f);
const auto importer = itk::ImportImageFilter<float, 2>::New();
importer->SetRegion(ImageType::RegionType(ImageType::SizeType::Filled(4)));
importer->SetImportPointer(buffer.data(), buffer.size(), false);
importer->Update();
const ImageType::Pointer image = importer->GetOutput();

const auto graft = itk::MakeGraft(image);

ASSERT_NE(image->GetSource(), nullptr);
EXPECT_EQ(graft->GetSource(), nullptr);
EXPECT_EQ(graft->GetBufferPointer(), buffer.data());
}


TEST(MakeGraft, NullGivesNull)
{
EXPECT_EQ(itk::MakeGraft(static_cast<const ImageType *>(nullptr)), nullptr);
EXPECT_EQ(itk::MakeGraft(ImageType::ConstPointer{}), nullptr);
EXPECT_EQ(itk::MakeGraft(ImageType::Pointer{}), nullptr);
}


TEST(MakeGraft, AcceptsSmartPointers)
{
const ImageType::Pointer image = MakeImage();
const ImageType::ConstPointer constImage = image;

static_assert(std::is_same_v<decltype(itk::MakeGraft(image)), ImageType::Pointer>);
static_assert(std::is_same_v<decltype(itk::MakeGraft(constImage)), ImageType::Pointer>);
EXPECT_EQ(itk::MakeGraft(image)->GetBufferPointer(), image->GetBufferPointer());
EXPECT_EQ(itk::MakeGraft(constImage)->GetBufferPointer(), image->GetBufferPointer());
}


TEST(MakeGraft, PointSetSharesPoints)
{
auto pointSet = PointSetType::New();
pointSet->SetPoint(0, itk::MakeFilled<PointSetType::PointType>(1.5));

const auto graft = itk::MakeGraft(pointSet.GetPointer());

ASSERT_NE(graft, nullptr);
EXPECT_EQ(graft->GetPoints(), pointSet->GetPoints());
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ TransformGeometryImageFilter<TInputImage, TOutputImage>::GenerateData()
}
OutputImageType * output = this->GetOutput();

auto input = InputImageType::New();
input->Graft(const_cast<InputImageType *>(this->GetInput()));
auto input = MakeGraft(this->GetInput());

// SEE HEADER FILE FOR MATH DESCRIPTION

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,6 @@ class ITK_TEMPLATE_EXPORT N4BiasFieldCorrectionImageFilter : public ImageToImage
GenerateData() override;

private:
/** An image that shares the buffer of the given one, as a mini-pipeline input whose release frees only the view. */
template <typename TImage>
static typename TImage::Pointer
MakeInputView(const TImage * image);

// N4 algorithm functions: The basic algorithm iterates between sharpening
// the intensity histogram of the corrected input image and spatially
// smoothing those results with a B-spline scalar field estimate of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,6 @@ ITK_GCC_PRAGMA_POP
namespace itk
{

template <typename TInputImage, typename TMaskImage, typename TOutputImage>
template <typename TImage>
auto
N4BiasFieldCorrectionImageFilter<TInputImage, TMaskImage, TOutputImage>::MakeInputView(const TImage * image) ->
typename TImage::Pointer
{
auto view = TImage::New();
view->Graft(image);
return view;
}

template <typename TInputImage, typename TMaskImage, typename TOutputImage>
N4BiasFieldCorrectionImageFilter<TInputImage, TMaskImage, TOutputImage>::N4BiasFieldCorrectionImageFilter()
: m_MaskLabel(NumericTraits<MaskPixelType>::OneValue())
Expand Down Expand Up @@ -615,7 +604,7 @@ typename N4BiasFieldCorrectionImageFilter<TInputImage, TMaskImage, TOutputImage>
N4BiasFieldCorrectionImageFilter<TInputImage, TMaskImage, TOutputImage>::ReconstructBiasField(
const BiasFieldControlPointLatticeType * controlPointLattice)
{
// The view made below has no source, so bring a pipeline output up to date first.
// The graft made below has no source, so bring a pipeline output up to date first.
controlPointLattice->UpdateSource();
const auto reconstructer = this->MakeBiasFieldReconstructer(controlPointLattice);

Expand Down Expand Up @@ -643,8 +632,8 @@ N4BiasFieldCorrectionImageFilter<TInputImage, TMaskImage, TOutputImage>::MakeBia
const InputImageType * inputImage = this->GetInput();

auto reconstructer = BiasFieldReconstructerType::New();
// A view, so that releasing the reconstructer's input leaves the lattice buffered.
reconstructer->SetInput(MakeInputView(controlPointLattice));
// A graft, so that releasing the reconstructer's input leaves the lattice buffered.
reconstructer->SetInput(MakeGraft(controlPointLattice));
reconstructer->SetOrigin(inputImage->GetOrigin());
reconstructer->SetSpacing(inputImage->GetSpacing());
reconstructer->SetDirection(inputImage->GetDirection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,12 @@ ConvolutionImageFilter<TInputImage, TKernelImage, TOutputImage>::ComputeConvolut
const KernelSizeType radius = this->GetKernelRadius(kernelImage);
kernelOperator.CreateToRadius(radius);

auto localInput = InputImageType::New();
localInput->Graft(this->GetInput());

// The NeighborhoodOperatorImageFilter does all the work.
using ConvolutionFilterType = NeighborhoodOperatorImageFilter<InputImageType, OutputImageType, KernelImagePixelType>;
auto convolutionFilter = ConvolutionFilterType::New();
convolutionFilter->SetOperator(kernelOperator);
convolutionFilter->OverrideBoundaryCondition(this->GetBoundaryCondition());
convolutionFilter->SetInput(localInput);
convolutionFilter->SetInput(MakeGraft(this->GetInput()));
convolutionFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
convolutionFilter->ReleaseDataFlagOn();
progress->RegisterInternalFilter(convolutionFilter, 1.0f - optionalFilterWeights);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ FFTConvolutionImageFilter<TInputImage, TKernelImage, TOutputImage, TInternalPrec
auto progress = ProgressAccumulator::New();
progress->SetMiniPipelineFilter(this);

auto localInput = InputImageType::New();
localInput->Graft(this->GetInput());
auto localInput = MakeGraft(this->GetInput());

const KernelImageType * kernelImage = this->GetKernelImage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,13 @@ void
MaskedFFTNormalizedCorrelationImageFilter<TInputImage, TOutputImage, TMaskImage>::GenerateData()
{
// Store the input images.
InputImagePointer fixedImage = InputImageType::New();
fixedImage->Graft(this->GetFixedImage());
InputImagePointer fixedImage = MakeGraft(this->GetFixedImage());

InputImagePointer movingImage = InputImageType::New();
movingImage->Graft(this->GetMovingImage());
InputImagePointer movingImage = MakeGraft(this->GetMovingImage());

MaskImagePointer fixedMask = nullptr;
if (this->GetFixedImageMask())
{
fixedMask = MaskImageType::New();
fixedMask->Graft(this->GetFixedImageMask());
}
MaskImagePointer fixedMask = MakeGraft(this->GetFixedImageMask());

MaskImagePointer movingMask = nullptr;
if (this->GetMovingImageMask())
{
movingMask = MaskImageType::New();
movingMask->Graft(this->GetMovingImageMask());
}
MaskImagePointer movingMask = MakeGraft(this->GetMovingImageMask());

this->UpdateProgress(m_AccumulatedProgress);
const OutputImagePointer outputImage = this->GetOutput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ InverseDeconvolutionImageFilter<TInputImage, TKernelImage, TOutputImage, TIntern
auto progress = ProgressAccumulator::New();
progress->SetMiniPipelineFilter(this);

auto localInput = InputImageType::New();
localInput->Graft(this->GetInput());
auto localInput = MakeGraft(this->GetInput());

const KernelImageType * kernelImage = this->GetKernelImage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ TikhonovDeconvolutionImageFilter<TInputImage, TKernelImage, TOutputImage, TInter
auto progress = ProgressAccumulator::New();
progress->SetMiniPipelineFilter(this);

auto localInput = InputImageType::New();
localInput->Graft(this->GetInput());
auto localInput = MakeGraft(this->GetInput());

const KernelImageType * kernelImage = this->GetKernelImage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ WienerDeconvolutionImageFilter<TInputImage, TKernelImage, TOutputImage, TInterna
auto progress = ProgressAccumulator::New();
progress->SetMiniPipelineFilter(this);

auto localInput = InputImageType::New();
localInput->Graft(this->GetInput());
auto localInput = MakeGraft(this->GetInput());

const KernelImageType * kernelImage = this->GetKernelImage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ TimeVaryingBSplineVelocityFieldTransform<TParametersValueType, VDimension>::Inte
closeDimensions[VDimension] = 1;
}

// A graft, so that releasing the B-spline filter's input leaves the control point lattice buffered.
auto latticeGraft = TimeVaryingVelocityFieldControlPointLatticeType::New();
latticeGraft->Graft(this->GetTimeVaryingVelocityFieldControlPointLattice());

auto bspliner = BSplineFilterType::New();
bspliner->SetInput(latticeGraft);
// A graft, so that releasing the B-spline filter's input leaves the control point lattice buffered.
bspliner->SetInput(MakeGraft(this->GetTimeVaryingVelocityFieldControlPointLattice()));
bspliner->SetSplineOrder(this->m_SplineOrder);
bspliner->SetSpacing(this->m_VelocityFieldSpacing);
bspliner->SetSize(this->m_VelocityFieldSize);
Expand All @@ -75,12 +72,9 @@ TimeVaryingBSplineVelocityFieldTransform<TParametersValueType, VDimension>::Inte

using IntegratorType = TimeVaryingVelocityFieldIntegrationImageFilter<VelocityFieldType, DisplacementFieldType>;

// A graft, so that releasing this integrator's input leaves the sampled field buffered for the inverse integrator.
auto bsplinerOutputGraft = VelocityFieldType::New();
bsplinerOutputGraft->Graft(bsplinerOutput);

auto integrator = IntegratorType::New();
integrator->SetInput(bsplinerOutputGraft);
// A graft, so that releasing this integrator's input leaves the sampled field buffered for the inverse integrator.
integrator->SetInput(MakeGraft(bsplinerOutput));
integrator->SetLowerTimeBound(this->GetLowerTimeBound());
integrator->SetUpperTimeBound(this->GetUpperTimeBound());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ TimeVaryingVelocityFieldTransform<TParametersValueType, VDimension>::IntegrateVe
{
using IntegratorType = TimeVaryingVelocityFieldIntegrationImageFilter<VelocityFieldType, DisplacementFieldType>;

// Grafts, so that releasing the integrators' inputs leaves the velocity field buffered.
const auto makeVelocityFieldGraft = [this]() {
auto graft = VelocityFieldType::New();
graft->Graft(this->GetVelocityField());
return graft;
};

// The integrators point the shared velocity field interpolator at their input; point it back afterward.
const auto restoreInterpolatorInput = [this]() {
if (this->GetVelocityFieldInterpolator())
Expand All @@ -49,7 +42,7 @@ TimeVaryingVelocityFieldTransform<TParametersValueType, VDimension>::IntegrateVe
try
{
auto integrator = IntegratorType::New();
integrator->SetInput(makeVelocityFieldGraft());
integrator->SetInput(MakeGraft(this->GetVelocityField()));
integrator->SetLowerTimeBound(this->GetLowerTimeBound());
integrator->SetUpperTimeBound(this->GetUpperTimeBound());

Expand All @@ -68,7 +61,7 @@ TimeVaryingVelocityFieldTransform<TParametersValueType, VDimension>::IntegrateVe
this->GetModifiableInterpolator()->SetInputImage(displacementField);

auto inverseIntegrator = IntegratorType::New();
inverseIntegrator->SetInput(makeVelocityFieldGraft());
inverseIntegrator->SetInput(MakeGraft(this->GetVelocityField()));
inverseIntegrator->SetLowerTimeBound(this->GetUpperTimeBound());
inverseIntegrator->SetUpperTimeBound(this->GetLowerTimeBound());
if (!this->GetVelocityFieldInterpolator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ DirectedHausdorffDistanceImageFilter<TInputImage1, TInputImage2>::BeforeThreaded
using FilterType = itk::SignedMaurerDistanceMapImageFilter<InputImage2Type, DistanceMapType>;
auto filter = FilterType::New();

auto input2 = InputImage2Type::New();
input2->Graft(const_cast<InputImage2Type *>(this->GetInput2()));

filter->SetInput(input2);
filter->SetInput(MakeGraft(this->GetInput2()));
filter->SetSquaredDistance(false);
filter->SetUseImageSpacing(m_UseImageSpacing);
filter->Update();
Expand Down
Loading
Loading