Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF: Add SpatialObject IsInsideInWorldSpace(const PointType &) overload #4440

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion Modules/Core/SpatialObjects/include/itkSpatialObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ class ITK_TEMPLATE_EXPORT SpatialObject : public DataObject
* transform may be updated explicitly by calling `GetObjectToWorldTransformInverse()`, `Update()`, or
* `SetObjectToWorldTransform(transform)` */
virtual bool
IsInsideInWorldSpace(const PointType & point, unsigned int depth = 0, const std::string & name = "") const;
IsInsideInWorldSpace(const PointType & point, unsigned int depth, const std::string & name = "") const;

/** Overload, optimized for depth = 0 and name = "": `spatialObject.IsInsideInWorldSpace(point)` is equivalent to
* `spatialObject.IsInsideInWorldSpace(point, 0, "")`, but much faster. */
bool
IsInsideInWorldSpace(const PointType & point) const;

/** World space equivalent to IsEvaluableAtInObjectSpace
* \note This member function assumes that the internal `ObjectToWorldTransformInverse` transform is up-to-date. This
Expand Down
8 changes: 8 additions & 0 deletions Modules/Core/SpatialObjects/include/itkSpatialObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ SpatialObject<TDimension>::IsInsideInWorldSpace(const PointType & point,
return IsInsideInObjectSpace(pnt, depth, name);
}

template <unsigned int TDimension>
bool
SpatialObject<TDimension>::IsInsideInWorldSpace(const PointType & point) const
{
const PointType pnt = m_ObjectToWorldTransformInverse->TransformPoint(point);
return IsInsideInObjectSpace(pnt);
}

template <unsigned int TDimension>
bool
SpatialObject<TDimension>::IsInsideChildrenInObjectSpace(const PointType & point,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,31 @@ TEST(ImageMaskSpatialObject, CornerPointIsNotInsideMaskOfZeroValues)
const double cornerPoint[] = { 1.5, 1.5 };
ASSERT_FALSE(imageMaskSpatialObject->IsInsideInObjectSpace(cornerPoint));
}

// Check that the IsInsideInWorldSpace overloads yield the same result, when depth = 0 and name = "".
TEST(ImageMaskSpatialObject, IsInsideInWorldSpaceOverloads)
{
constexpr auto imageDimension = 2U;
using ImageMaskSpatialObjectType = itk::ImageMaskSpatialObject<imageDimension>;
using MaskImageType = ImageMaskSpatialObjectType::ImageType;
using MaskPixelType = MaskImageType::PixelType;
using PointType = MaskImageType::PointType;

// Create a mask image.
const auto maskImage = MaskImageType::New();
maskImage->SetRegions(itk::Size<imageDimension>::Filled(2));
maskImage->Allocate(true);
maskImage->SetPixel({}, MaskPixelType{ 1 });
maskImage->SetSpacing(itk::MakeFilled<MaskImageType::SpacingType>(0.5));

const auto imageMaskSpatialObject = ImageMaskSpatialObjectType::New();
imageMaskSpatialObject->SetImage(maskImage);

for (const double pointValue : { -1.0, 0.0, 0.5, 1.0 })
{
const PointType point(pointValue);

EXPECT_EQ(imageMaskSpatialObject->IsInsideInWorldSpace(point),
imageMaskSpatialObject->IsInsideInWorldSpace(point, 0, ""));
}
}