InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.37k stars 660 forks source link

ENH: Declare `front()` and `back()` of Index, Offset, and Size constexpr #4605

Closed N-Dekker closed 1 month ago

N-Dekker commented 2 months ago

Follow-up to pull request https://github.com/InsightSoftwareConsortium/ITK/pull/3236 commit 47bce264791a53853020a3911dd94d060cfbd655 "ENH: Declare begin(), end() of FixedArray, Index, Offset, Size constexpr"

N-Dekker commented 2 months ago

@thewtex Would this PR still be feasible for v5.4.0? It just adds constexpr keywords to front() and back() member functions.


FYI, Adding constexpr to itk::Index::back() would be a stepping stone towards evaluating the OffsetToIndexTable of BSplineInterpolationWeightFunction at compile-time, instead of at run-time: https://github.com/InsightSoftwareConsortium/ITK/blob/4d750726d06ba28c601b8a4bfac8780baa395ff1/Modules/Core/Common/include/itkBSplineInterpolationWeightFunction.h#L124

Which is likely to speed up BSplineInterpolationWeightFunction::Evaluate:

https://github.com/InsightSoftwareConsortium/ITK/blob/4d750726d06ba28c601b8a4bfac8780baa395ff1/Modules/Core/Common/include/itkBSplineInterpolationWeightFunction.hxx#L45-L48

blowekamp commented 2 months ago

@thewtex Would this PR still be feasible for v5.4.0? It just adds constexpr keywords to front() and back() member functions.

Out of my curiosity, is this for a particular issue? Has it given any measurable performance benefit?

N-Dekker commented 2 months ago

Out of my curiosity, is this for a particular issue? Has it given any measurable performance benefit?

This PR would pave the way to adding constexpr to itk::Index iteration, by itk::IndexRange. Which would allow creating the OffsetToIndexTable of BSplineInterpolationWeightFunction at compile-time, instead of at run-time. I haven't yet been able to benchmark, but it may certainly benefit its performance.

Note that itk::Index<N> is similar to std::array<itk::IndexValueType, N>, whose front() and back() are also marked "constexpr": https://en.cppreference.com/w/cpp/container/array/back

N-Dekker commented 2 months ago

I think we should save it for ITK 6 to avoid any unforeseen issues.

@thewtex OK, thanks. Marked "draft" to avoid that this PR might accidentally be merged before the release of ITK 5.4!

N-Dekker commented 1 month ago

I think this pull request may also be merged now, as v5.4.0 has been tagged (https://github.com/InsightSoftwareConsortium/ITK/pull/4603#issuecomment-2120498131)