Skip to content

Commit

Permalink
PERF: Let SpatialObject directly access m_ObjectToWorldTransformInverse
Browse files Browse the repository at this point in the history
Replaced `this->GetObjectToWorldTransformInverse()` calls with direct access to
m_ObjectToWorldTransformInverse. A performance improvement greater than 6% was
observed, from more than 0.74 sec. down to less than 0.69 sec, when calling
`IsInsideInWorldSpace` 2^25 times (as tested on VS2019 Release).
  • Loading branch information
N-Dekker authored and dzenanz committed Jan 31, 2024
1 parent ed43915 commit 1e0475c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 16 additions & 4 deletions Modules/Core/SpatialObjects/include/itkSpatialObject.h
Expand Up @@ -317,18 +317,27 @@ class ITK_TEMPLATE_EXPORT SpatialObject : public DataObject
itkSetMacro(DefaultOutsideValue, double);
itkGetConstMacro(DefaultOutsideValue, double);

/** World space equivalent to ValueAtInObjectSpace */
/** World space equivalent to ValueAtInObjectSpace
* \note This member function assumes that the internal `ObjectToWorldTransformInverse` transform is up-to-date. This
* transform may be updated explicitly by calling `GetObjectToWorldTransformInverse()`, `Update()`, or
* `SetObjectToWorldTransform(transform)` */
virtual bool
ValueAtInWorldSpace(const PointType & point,
double & value,
unsigned int depth = 0,
const std::string & name = "") const;

/** World space equivalent to IsInsideInObjectSpace */
/** World space equivalent to IsInsideInObjectSpace
* \note This member function assumes that the internal `ObjectToWorldTransformInverse` transform is up-to-date. This
* 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;

/** World space equivalent to IsEvaluableAtInObjectSpace */
/** World space equivalent to IsEvaluableAtInObjectSpace
* \note This member function assumes that the internal `ObjectToWorldTransformInverse` transform is up-to-date. This
* transform may be updated explicitly by calling `GetObjectToWorldTransformInverse()`, `Update()`, or
* `SetObjectToWorldTransform(transform)` */
virtual bool
IsEvaluableAtInWorldSpace(const PointType & point, unsigned int depth = 0, const std::string & name = "") const;

Expand All @@ -342,7 +351,10 @@ class ITK_TEMPLATE_EXPORT SpatialObject : public DataObject
const std::string & name = "",
const DerivativeOffsetType & offset = MakeFilled<DerivativeOffsetType>(1));

/** Return the n-th order derivative value at the specified point. */
/** Return the n-th order derivative value at the specified point.
* \note This member function assumes that the internal `ObjectToWorldTransformInverse` transform is up-to-date. This
* transform may be updated explicitly by calling `GetObjectToWorldTransformInverse()`, `Update()`, or
* `SetObjectToWorldTransform(transform)` */
virtual void
DerivativeAtInWorldSpace(const PointType & point,
short unsigned int order,
Expand Down
8 changes: 4 additions & 4 deletions Modules/Core/SpatialObjects/include/itkSpatialObject.hxx
Expand Up @@ -138,7 +138,7 @@ SpatialObject<TDimension>::DerivativeAtInWorldSpace(const PointType &
const std::string & name,
const DerivativeOffsetType & offset)
{
const PointType pnt = this->GetObjectToWorldTransformInverse()->TransformPoint(point);
const PointType pnt = m_ObjectToWorldTransformInverse->TransformPoint(point);
this->DerivativeAtInObjectSpace(pnt, order, value, depth, name, offset);
}

Expand Down Expand Up @@ -180,7 +180,7 @@ SpatialObject<TDimension>::IsInsideInWorldSpace(const PointType & point,
unsigned int depth,
const std::string & name) const
{
const PointType pnt = this->GetObjectToWorldTransformInverse()->TransformPoint(point);
const PointType pnt = m_ObjectToWorldTransformInverse->TransformPoint(point);
return IsInsideInObjectSpace(pnt, depth, name);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ SpatialObject<TDimension>::IsEvaluableAtInWorldSpace(const PointType & point,
unsigned int depth,
const std::string & name) const
{
const PointType pnt = this->GetObjectToWorldTransformInverse()->TransformPoint(point);
const PointType pnt = m_ObjectToWorldTransformInverse->TransformPoint(point);
return this->IsEvaluableAtInObjectSpace(pnt, depth, name);
}

Expand Down Expand Up @@ -295,7 +295,7 @@ SpatialObject<TDimension>::ValueAtInWorldSpace(const PointType & point,
unsigned int depth,
const std::string & name) const
{
const PointType pnt = this->GetObjectToWorldTransformInverse()->TransformPoint(point);
const PointType pnt = m_ObjectToWorldTransformInverse->TransformPoint(point);
return this->ValueAtInObjectSpace(pnt, value, depth, name);
}

Expand Down

0 comments on commit 1e0475c

Please sign in to comment.