Skip to content

Commit

Permalink
STYLE: Estimate BSplineInterpolation OffsetToIndexTable at compile-time
Browse files Browse the repository at this point in the history
Replaced `BSplineInterpolationWeightFunction::m_OffsetToIndexTable` with a
local constexpr variable within `BSplineInterpolationWeightFunction::Evaluate`,
initialized at compile-time.

May improve the run-time performance of BSplineInterpolationWeightFunction.
  • Loading branch information
N-Dekker committed May 22, 2024
1 parent 996fedb commit 70c1837
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,6 @@ class ITK_TEMPLATE_EXPORT BSplineInterpolationWeightFunction
protected:
BSplineInterpolationWeightFunction() = default;
~BSplineInterpolationWeightFunction() override = default;

private:
/** Lookup table type. */
using TableType = FixedArray<IndexType, NumberOfWeights>;

/** Table mapping linear offset to indices. */
const TableType m_OffsetToIndexTable{ [] {
TableType table;
std::copy_n(ZeroBasedIndexRange<SpaceDimension>(SupportSize).cbegin(), NumberOfWeights, table.begin());
return table;
}() };
};
} // end namespace itk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ BSplineInterpolationWeightFunction<TCoordRep, VSpaceDimension, VSplineOrder>::Ev
WeightsType & weights,
IndexType & startIndex) const
{
static constexpr auto offsetToIndexTable = [] {
FixedArray<IndexType, NumberOfWeights> table{};
auto indexIterator = ZeroBasedIndexRange<SpaceDimension>(SupportSize).cbegin();

for (size_t i{}; i < NumberOfWeights; ++i)
{
table[i] = *indexIterator;
++indexIterator;
}
return table;
}();

unsigned int j, k;

// Find the starting index of the support region
Expand Down Expand Up @@ -77,7 +89,7 @@ BSplineInterpolationWeightFunction<TCoordRep, VSpaceDimension, VSplineOrder>::Ev

for (j = 0; j < SpaceDimension; ++j)
{
weights[k] *= weights1D[j][m_OffsetToIndexTable[k][j]];
weights[k] *= weights1D[j][offsetToIndexTable[k][j]];
}
}
}
Expand Down

0 comments on commit 70c1837

Please sign in to comment.