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

WIP: Add SpatialObject::WorldSpaceContext class, a faster alternative to SpatialObject::IsInsideInWorldSpace #4434

Closed

Conversation

N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented Jan 25, 2024

WorldSpaceContext::IsInsideSpatialObject serves as a faster alternative to the
existing SpatialObject::IsInsideInWorldSpace. WorldSpaceContext has already
cached the ObjectToWorldTransformInverse transform during its construction.
IsInsideSpatialObject calls the faster
SpatialObject::IsInsideInObjectSpace(const PointType &) overload, instead of
the overload with three parameters.

Using VS2022 Release, WorldSpaceContext::IsInsideSpatialObject appeared more
than twice as fast as the equivalent SpatialObject::IsInsideInWorldSpace call.


Update: If the IsInsideInWorldSpace(const PointType &) overload proposed by pull request #4440 can make it onto the master branch, I will probably drop this WorldSpaceContext proposal (#4434), because then I think WorldSpaceContext::IsInsideSpatialObject(point) won't be much faster than the equivalent SpatialObject::IsInsideInWorldSpace call anymore.

@github-actions github-actions bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:Registration Issues affecting the Registration module labels Jan 25, 2024
@N-Dekker N-Dekker changed the title Add SpatialObject::WorldSpaceContext class, a faster alternative to SpatialObject::IsInsideInWorldSpace WIP: Add SpatialObject::WorldSpaceContext class, a faster alternative to SpatialObject::IsInsideInWorldSpace Jan 26, 2024
`WorldSpaceContext::IsInsideSpatialObject` serves as a faster alternative to the
existing `SpatialObject::IsInsideInWorldSpace`. `WorldSpaceContext` has already
cached the ObjectToWorldTransformInverse transform during its construction.
`IsInsideSpatialObject` calls the faster
`SpatialObject::IsInsideInObjectSpace(const PointType &)` overload, instead of
the overload with three parameters.

Using VS2022 Release, `WorldSpaceContext::IsInsideSpatialObject` appeared more
than twice as fast as the equivalent `SpatialObject::IsInsideInWorldSpace`,
taking less than 0.35 sec. for 2^25 WorldSpaceContext::IsInsideSpatialObject
calls, while the same number of SpatialObject::IsInsideInWorldSpace calls took
more than 0.85 sec.

Included ImageMaskSpatialObject WorldSpaceContext unit test.
A major performance improvement is achieved by creating a `WorldSpaceContext`
_outside_ of the pixel-by-pixel iteration loop, and calling
`WorldSpaceContext::IsInsideSpatialObject` inside the loop, instead of
`SpatialObject::IsInsideInWorldSpace`.
const typename Superclass::FixedImageMaskType::WorldSpaceContext worldSpaceContext(Superclass::m_FixedImageMask);

/** \todo multi-thread me */
while (!fi.IsAtEnd())
{
Copy link
Member

@dzenanz dzenanz Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should MinimumMaximumImageCalculator be used here? And parallelize it? Though parallelization might not matter much in this cases, as this is almost certainly limited by memory throughput, not computation.

Copy link
Contributor Author

@N-Dekker N-Dekker Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dzenanz I guess it would be nice to use MinimumMaximumImageCalculator here indeed. If you want to do so, please go ahead 😃 I'm considering to abandon this PR (the introduction of SpatialObject::WorldSpaceContext) anyway. If we can sufficiently improve the speed of SpatialObject::IsInsideInWorldSpace (starting by pull request #4435), I don't think the introduction of SpatialObject::WorldSpaceContext is useful anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall optimizing the itk::MinimumMaximumImageFilter and was able to get surprising gains in the filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't think about that 😄

@thewtex thewtex requested a review from aylward January 28, 2024 23:00
@N-Dekker
Copy link
Contributor Author

N-Dekker commented Feb 1, 2024

This pull request (#4434, proposing WorldSpaceContext) is superseded by the following three pull requests, which should together make any existing spatialObject->IsInsideInWorldSpace(point) just as fast as what might have been achieved by WorldSpaceContext:

@N-Dekker N-Dekker closed this Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:Registration Issues affecting the Registration module type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants