# SpatialObject.IsInsideInWorldSpace(point) much faster now!

**URL:** https://discourse.itk.org/t/spatialobject-isinsideinworldspace-point-much-faster-now/6425
**Category:** Engineering
**Created:** [February 2, 2024, 10:35am UTC](https://discourse.itk.org/t/spatialobject-isinsideinworldspace-point-much-faster-now/6425 "2024-02-02T10:35:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [February 2, 2024, 10:35am UTC](https://discourse.itk.org/t/spatialobject-isinsideinworldspace-point-much-faster-now/6425/1 "2024-02-02T10:35:58Z")

</div>

🎉 For your information, we have just achieved a significant performance improvement!

With the latest [ITK master branch](https://github.com/InsightSoftwareConsortium/ITK/commits/master) revision (commit [PERF: Make `m_ObjectToWorldTransformInverse-\>TransformPoint` non-virtual · InsightSoftwareConsortium/ITK@4ae04b3 · GitHub](https://github.com/InsightSoftwareConsortium/ITK/commit/4ae04b35faa24b27dd9884eeb40f91bb9dfc4052)) a `SpatialObject.IsInsideInWorldSpace(point)` call will be much faster than before! I observed that it has become approximately _twice as fast_, compared to the last ITK release ([v5.3.0](https://github.com/InsightSoftwareConsortium/ITK/releases/tag/v5.3.0)), or even the latest release candidate ([v5.4rc02](https://github.com/InsightSoftwareConsortium/ITK/releases/tag/v5.4rc02))

The performance improvement was achieved by three pull requests that I did, which were reviewed by @Stephen_Aylward @dzenanz and @hjmjohnson, and merged in the last few days:

- [PERF: Let SpatialObject directly access m\_ObjectToWorldTransformInverse #4435](https://github.com/InsightSoftwareConsortium/ITK/pull/4435)
- [PERF: Add SpatialObject IsInsideInWorldSpace(const PointType &) overload #4440](https://github.com/InsightSoftwareConsortium/ITK/pull/4440)
- [PERF: Make `m_ObjectToWorldTransformInverse->TransformPoint` non-virtual #4441](https://github.com/InsightSoftwareConsortium/ITK/pull/4441)

As `SpatialObject.IsInsideInWorldSpace(point)` is often called repeatedly over a large range of points (typically inside a `for` or a `while` loop), I expect a significant performance improvement for various filters, metric calculations, and registration algorithms. Especially when they involve image masks. Specifically, I would expect a performance improvement for:

- SpatialObjectToImageStatisticsCalculator
- DiffusionTensor3DReconstructionImageFilter
- ImageMomentsCalculator
- HistogramImageToImageMetric
- ImageToImageMetric
- KappaStatisticImageToImageMetric
- MatchCardinalityImageToImageMetric
- MattesMutualInformationImageToImageMetric
- MeanReciprocalSquareDifferenceImageToImageMetric
- MutualInformationImageToImageMetric
- NormalizedCorrelationImageToImageMetric
- JointHistogramMutualInformationImageToImageMetricv4
- MattesMutualInformationImageToImageMetricv4
- ImageRegistrationMethodv4

Please have a look and try it out! I’m interested to hear if you see any performance improvement in your use case!

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [February 7, 2024, 5:22pm UTC](https://discourse.itk.org/t/spatialobject-isinsideinworldspace-point-much-faster-now/6425/2 "2024-02-07T17:22:08Z")

</div>

For your information, both `spatialObject.IsInsideInWorldSpace(point)` and `spatialObject.IsInsideInObjectSpace(point)` have become even faster for masks (that is, when the spatial object is an `itk::ImageMaskSpatialObject`), now that the following pull request is merged:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/4450>
>
> \`SpatialObject\` stores the image regions of the spatial object. It appears reaso…nable for its derived class \`ImageSpatialObject\` to keep them up-to-date, in accordance with its image.
> 
> A significant performance improvement of \`ImageMaskSpatialObject\` is then achieved by using the buffered region and the requested region as cached in its base class, instead of calling the virtual functions \`GetBufferedRegion()\` and \`GetRequestedRegion()\` from its image.
> 
> \----
> 
> It's interesting to see that the common base class \`itk::SpatialObject\` already has those data members to cache the image regions (\`m\_LargestPossibleRegion\`, \`m\_RequestedRegion\`, \`m\_BufferedRegion\`), but it looks like they weren't used anywhere, as far as I can see:
> 
> https://github.com/InsightSoftwareConsortium/ITK/blob/436423d798e63e72444dd4a9d8787ffd34c5eb60/Modules/Core/SpatialObjects/include/itkSpatialObject.h#L731-L733
> 
> For the record, those data members were added to \`itk::SpatialObject\` with commit 433987062ccaee80c9a7d2f6c1ff857fe96cf929 "ENH: Removed NDimensionalSpatialObject", Julien Jomier (@jjomier), 10 February 2003.

commit: [PERF: ImageMaskSpatialObject use image regions from its base class · InsightSoftwareConsortium/ITK@671d758 · GitHub](https://github.com/InsightSoftwareConsortium/ITK/commit/671d758ac3bd54574b997032d8f72d8467e0dcea)

For `ImageMaskSpatialObject::IsInsideInObjectSpace`, I observed a performance improvement of more than 8%, with this commit.

---

<div class="post-metadata">

### Author: ![cookpa](https://discourse.itk.org/user_avatar/discourse.itk.org/cookpa/32/255_2.png) [@cookpa](https://discourse.itk.org/u/cookpa)
#### Post date: [February 9, 2024, 7:53pm UTC](https://discourse.itk.org/t/spatialobject-isinsideinworldspace-point-much-faster-now/6425/3 "2024-02-09T19:53:20Z")

</div>

Thanks for letting us know - I updated ANTs. It does seem to run a bit faster, maybe 5%, with a brain mask and the Mattes metric.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [April 9, 2024, 8:27am UTC](https://discourse.itk.org/t/spatialobject-isinsideinworldspace-point-much-faster-now/6425/4 "2024-04-09T08:27:33Z")

</div>

🎉 FYI, the `SpatialObject.IsInsideInWorldSpace(point)` performance improvements that I presented here are included with the latest ITK tag: [v5.4rc03](https://github.com/InsightSoftwareConsortium/ITK/releases/tag/v5.4rc03).
