From 8f5f9b187e3c76ad23ff5586a0289f1b50c67601 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Sun, 22 Dec 2024 16:50:20 -0600 Subject: [PATCH 1/3] STYLE: Prefer compile-time constexpr The values of m_Factor* are known at compile- time, so compute them only at compile-time. --- .../itkWindowedSincInterpolateImageFunction.h | 16 ++++++------- ...tkWindowedSincInterpolateImageFunction.hxx | 23 ------------------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h index b0a2be4e4d2..09ac2e1b344 100644 --- a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h +++ b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h @@ -46,7 +46,7 @@ class ITK_TEMPLATE_EXPORT CosineWindowFunction private: /** Equal to \f$ \frac{\pi}{2 m} \f$ */ - static const double m_Factor; + static constexpr double m_Factor = itk::Math::pi / (2 * VRadius); }; /** @@ -68,7 +68,7 @@ class ITK_TEMPLATE_EXPORT HammingWindowFunction private: /** Equal to \f$ \frac{\pi}{m} \f$ */ - static const double m_Factor; + static constexpr double m_Factor = itk::Math::pi / VRadius; }; /** @@ -90,7 +90,7 @@ class ITK_TEMPLATE_EXPORT WelchWindowFunction private: /** Equal to \f$ \frac{1}{m^2} \f$ */ - static const double m_Factor; + static constexpr double m_Factor = 1.0 / (VRadius * VRadius); }; /** @@ -112,14 +112,14 @@ class ITK_TEMPLATE_EXPORT LanczosWindowFunction if (A == 0.0) { return static_cast(1.0); - } + } // namespace Function const double z = m_Factor * A; return static_cast(std::sin(z) / z); - } + } // namespace itk private: /** Equal to \f$ \frac{\pi}{m} \f$ */ - static const double m_Factor; + static constexpr double m_Factor = itk::Math::pi / VRadius; }; /** @@ -141,10 +141,10 @@ class ITK_TEMPLATE_EXPORT BlackmanWindowFunction private: /** Equal to \f$ \frac{\pi}{m} \f$ */ - static const double m_Factor1; + static constexpr double m_Factor1 = itk::Math::pi / VRadius; /** Equal to \f$ \frac{2 \pi}{m} \f$ */ - static const double m_Factor2; + static constexpr double m_Factor2 = 2.0 * itk::Math::pi / VRadius; }; } // namespace Function diff --git a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.hxx b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.hxx index fbf5460a377..b2e49d40eeb 100644 --- a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.hxx +++ b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.hxx @@ -23,29 +23,6 @@ namespace itk { - -// Constant definitions for functions -namespace Function -{ -template -const double CosineWindowFunction::m_Factor = itk::Math::pi / (2 * VRadius); - -template -const double HammingWindowFunction::m_Factor = itk::Math::pi / VRadius; - -template -const double WelchWindowFunction::m_Factor = 1.0 / (VRadius * VRadius); - -template -const double LanczosWindowFunction::m_Factor = itk::Math::pi / VRadius; - -template -const double BlackmanWindowFunction::m_Factor1 = itk::Math::pi / VRadius; - -template -const double BlackmanWindowFunction::m_Factor2 = 2.0 * itk::Math::pi / VRadius; -} // end namespace Function - template Date: Fri, 27 Dec 2024 14:24:52 -0600 Subject: [PATCH 2/3] STYLE: Move static constexpr to local scope where needed These constant values are only used inside operator(), so move local (static constexpr) variable of operator() instead of a member variable of the class. --- .../itkWindowedSincInterpolateImageFunction.h | 50 ++++++++----------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h index 09ac2e1b344..1c98fbc40cd 100644 --- a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h +++ b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h @@ -25,6 +25,7 @@ namespace itk { +// clang-format off namespace Function { /** @@ -41,12 +42,10 @@ class ITK_TEMPLATE_EXPORT CosineWindowFunction inline TOutput operator()(const TInput & A) const { - return static_cast(std::cos(A * m_Factor)); + /** Equal to \f$ \frac{\pi}{2 m} \f$ */ + static constexpr double factor = Math::pi / (2 * VRadius); + return static_cast(std::cos(A * factor)); } - -private: - /** Equal to \f$ \frac{\pi}{2 m} \f$ */ - static constexpr double m_Factor = itk::Math::pi / (2 * VRadius); }; /** @@ -63,12 +62,10 @@ class ITK_TEMPLATE_EXPORT HammingWindowFunction inline TOutput operator()(const TInput & A) const { - return static_cast(0.54 + 0.46 * std::cos(A * m_Factor)); + /** Equal to \f$ \frac{\pi}{m} \f$ */ + static constexpr double factor = Math::pi / VRadius; + return static_cast(0.54 + 0.46 * std::cos(A * factor)); } - -private: - /** Equal to \f$ \frac{\pi}{m} \f$ */ - static constexpr double m_Factor = itk::Math::pi / VRadius; }; /** @@ -85,12 +82,10 @@ class ITK_TEMPLATE_EXPORT WelchWindowFunction inline TOutput operator()(const TInput & A) const { - return static_cast(1.0 - A * m_Factor * A); + /** Equal to \f$ \frac{1}{m^2} \f$ */ + static constexpr double factor = 1.0 / (VRadius * VRadius); + return static_cast(1.0 - A * factor * A); } - -private: - /** Equal to \f$ \frac{1}{m^2} \f$ */ - static constexpr double m_Factor = 1.0 / (VRadius * VRadius); }; /** @@ -113,13 +108,11 @@ class ITK_TEMPLATE_EXPORT LanczosWindowFunction { return static_cast(1.0); } // namespace Function - const double z = m_Factor * A; + /** Equal to \f$ \frac{\pi}{m} \f$ */ + static constexpr double factor = Math::pi / VRadius; + const double z = factor * A; return static_cast(std::sin(z) / z); } // namespace itk - -private: - /** Equal to \f$ \frac{\pi}{m} \f$ */ - static constexpr double m_Factor = itk::Math::pi / VRadius; }; /** @@ -136,17 +129,16 @@ class ITK_TEMPLATE_EXPORT BlackmanWindowFunction inline TOutput operator()(const TInput & A) const { - return static_cast(0.42 + 0.5 * std::cos(A * m_Factor1) + 0.08 * std::cos(A * m_Factor2)); - } + /** Equal to \f$ \frac{\pi}{m} \f$ */ + static constexpr double factor1 = Math::pi / VRadius; -private: - /** Equal to \f$ \frac{\pi}{m} \f$ */ - static constexpr double m_Factor1 = itk::Math::pi / VRadius; - - /** Equal to \f$ \frac{2 \pi}{m} \f$ */ - static constexpr double m_Factor2 = 2.0 * itk::Math::pi / VRadius; + /** Equal to \f$ \frac{2 \pi}{m} \f$ */ + static constexpr double factor2 = 2.0 * Math::pi / VRadius; + return static_cast(0.42 + 0.5 * std::cos(A * factor1) + 0.08 * std::cos(A * factor2)); + } }; } // namespace Function +// clang-format on /** * \class WindowedSincInterpolateImageFunction @@ -356,7 +348,7 @@ class ITK_TEMPLATE_EXPORT WindowedSincInterpolateImageFunction inline double Sinc(double x) const { - const double px = itk::Math::pi * x; + const double px = Math::pi * x; return (x == 0.0) ? 1.0 : std::sin(px) / px; } From 1e0de796fb2bbc2935d539ca0d6a7ab188dee276 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Fri, 27 Dec 2024 14:29:29 -0600 Subject: [PATCH 3/3] STYLE: Prefer static member function Sinc Member function can be made static. Redundant 'inline' specifier on a function declared entirely inside a class definition. --- .../include/itkWindowedSincInterpolateImageFunction.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h index 1c98fbc40cd..dc55964d335 100644 --- a/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h +++ b/Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h @@ -345,11 +345,10 @@ class ITK_TEMPLATE_EXPORT WindowedSincInterpolateImageFunction unsigned int m_WeightOffsetTable[m_OffsetTableSize][ImageDimension]{}; /** The sinc function */ - inline double - Sinc(double x) const + static double + Sinc(const double x) { const double px = Math::pi * x; - return (x == 0.0) ? 1.0 : std::sin(px) / px; } };