Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STYLE: Prefer compile-time constexpr #5110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace itk
{
// clang-format off
namespace Function
{
/**
Expand All @@ -41,12 +42,10 @@ class ITK_TEMPLATE_EXPORT CosineWindowFunction
inline TOutput
operator()(const TInput & A) const
{
return static_cast<TOutput>(std::cos(A * m_Factor));
/** Equal to \f$ \frac{\pi}{2 m} \f$ */
static constexpr double factor = Math::pi / (2 * VRadius);
return static_cast<TOutput>(std::cos(A * factor));
}

private:
/** Equal to \f$ \frac{\pi}{2 m} \f$ */
static const double m_Factor;
};

/**
Expand All @@ -63,12 +62,10 @@ class ITK_TEMPLATE_EXPORT HammingWindowFunction
inline TOutput
operator()(const TInput & A) const
{
return static_cast<TOutput>(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<TOutput>(0.54 + 0.46 * std::cos(A * factor));
}

private:
/** Equal to \f$ \frac{\pi}{m} \f$ */
static const double m_Factor;
};

/**
Expand All @@ -85,12 +82,10 @@ class ITK_TEMPLATE_EXPORT WelchWindowFunction
inline TOutput
operator()(const TInput & A) const
{
return static_cast<TOutput>(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<TOutput>(1.0 - A * factor * A);
}

private:
/** Equal to \f$ \frac{1}{m^2} \f$ */
static const double m_Factor;
};

/**
Expand All @@ -112,14 +107,12 @@ class ITK_TEMPLATE_EXPORT LanczosWindowFunction
if (A == 0.0)
{
return static_cast<TOutput>(1.0);
}
const double z = m_Factor * A;
} // namespace Function
/** Equal to \f$ \frac{\pi}{m} \f$ */
static constexpr double factor = Math::pi / VRadius;
const double z = factor * A;
return static_cast<TOutput>(std::sin(z) / z);
}

private:
/** Equal to \f$ \frac{\pi}{m} \f$ */
static const double m_Factor;
} // namespace itk
};

/**
Expand All @@ -136,17 +129,16 @@ class ITK_TEMPLATE_EXPORT BlackmanWindowFunction
inline TOutput
operator()(const TInput & A) const
{
return static_cast<TOutput>(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 const double m_Factor1;

/** Equal to \f$ \frac{2 \pi}{m} \f$ */
static const double m_Factor2;
/** Equal to \f$ \frac{2 \pi}{m} \f$ */
static constexpr double factor2 = 2.0 * Math::pi / VRadius;
return static_cast<TOutput>(0.42 + 0.5 * std::cos(A * factor1) + 0.08 * std::cos(A * factor2));
}
};
} // namespace Function
// clang-format on

/**
* \class WindowedSincInterpolateImageFunction
Expand Down Expand Up @@ -353,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 = itk::Math::pi * x;

const double px = Math::pi * x;
return (x == 0.0) ? 1.0 : std::sin(px) / px;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,6 @@

namespace itk
{

// Constant definitions for functions
namespace Function
{
template <unsigned int VRadius, typename TInput, typename TOutput>
const double CosineWindowFunction<VRadius, TInput, TOutput>::m_Factor = itk::Math::pi / (2 * VRadius);

template <unsigned int VRadius, typename TInput, typename TOutput>
const double HammingWindowFunction<VRadius, TInput, TOutput>::m_Factor = itk::Math::pi / VRadius;

template <unsigned int VRadius, typename TInput, typename TOutput>
const double WelchWindowFunction<VRadius, TInput, TOutput>::m_Factor = 1.0 / (VRadius * VRadius);

template <unsigned int VRadius, typename TInput, typename TOutput>
const double LanczosWindowFunction<VRadius, TInput, TOutput>::m_Factor = itk::Math::pi / VRadius;

template <unsigned int VRadius, typename TInput, typename TOutput>
const double BlackmanWindowFunction<VRadius, TInput, TOutput>::m_Factor1 = itk::Math::pi / VRadius;

template <unsigned int VRadius, typename TInput, typename TOutput>
const double BlackmanWindowFunction<VRadius, TInput, TOutput>::m_Factor2 = 2.0 * itk::Math::pi / VRadius;
} // end namespace Function

template <typename TInputImage,
unsigned int VRadius,
typename TWindowFunction,
Expand Down
Loading