35#include <vnl/vnl_math.h>
42#include <vxl_version.h>
106#define itkTemplateFloatingToIntegerMacro(name) \
107 template <typename TReturn, typename TInput> \
108 inline TReturn name(TInput x) \
110 if constexpr (sizeof(TReturn) <= 4) \
112 return static_cast<TReturn>(Detail::name##_32(x)); \
114 else if constexpr (sizeof(TReturn) <= 8) \
116 return static_cast<TReturn>(Detail::name##_64(x)); \
120 return static_cast<TReturn>(Detail::name##_base<TReturn, TInput>(x)); \
177template <typename TReturn, typename TInput>
181 return RoundHalfIntegerUp<TReturn, TInput>(x);
210#undef itkTemplateFloatingToIntegerMacro
212template <
typename TReturn,
typename TInput>
216#ifdef ITK_USE_CONCEPT_CHECKING
221 auto ret =
static_cast<TReturn
>(x);
222 if constexpr (
sizeof(TReturn) >
sizeof(TInput) &&
229 else if constexpr (
sizeof(TReturn) >=
sizeof(TInput))
233 itk::RangeError _e(__FILE__, __LINE__);
237 else if (
static_cast<TInput
>(ret) != x ||
240 itk::RangeError _e(__FILE__, __LINE__);
275 return representOutput.
asFloat;
318 const T absDifference =
std::abs(x1 - x2);
319 if (absDifference <= maxAbsoluteDifference)
328 if (std::signbit(x1) != std::signbit(x2))
334 return ulps <= maxUlps;
348 template <
typename TFloatType1,
typename TFloatType2>
352 return FloatAlmostEqual<double>(x1, x2);
355 template <
typename TFloatType1,
typename TFloatType2>
359 return FloatAlmostEqual<double>(x1, x2);
362 template <
typename TFloatType1,
typename TFloatType2>
366 return FloatAlmostEqual<float>(x1, x2);
369 template <
typename TFloatType1,
typename TFloatType2>
373 return FloatAlmostEqual<float>(x1, x2);
376 template <
typename TFloatType1,
typename TFloatType2>
380 return FloatAlmostEqual<float>(x1, x2);
386 template <
typename TFloatType,
typename TIntType>
390 return FloatAlmostEqual<TFloatType>(floatingVariable, integerVariable);
396 template <
typename TIntType,
typename TFloatType>
406 template <
typename TSignedInt,
typename TUn
signedInt>
410 if (signedVariable < 0)
418 return signedVariable ==
static_cast<TSignedInt
>(unsignedVariable);
424 template <
typename TUn
signedInt,
typename TSignedInt>
434 template <
typename TIntegerType1,
typename TIntegerType2>
445template <
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
460struct AlmostEqualsFunctionSelector<false, true, true, true>
467struct AlmostEqualsFunctionSelector<false, true, true, false>
474struct AlmostEqualsFunctionSelector<true, false, false, true>
481struct AlmostEqualsFunctionSelector<true, true, false, true>
488struct AlmostEqualsFunctionSelector<true, true, true, false>
495struct AlmostEqualsFunctionSelector<true, false, true, true>
502struct AlmostEqualsFunctionSelector<true, true, true, true>
509struct AlmostEqualsFunctionSelector<true, false, true, false>
517template <
typename TInputType1,
typename TInputType2>
518struct AlmostEqualsScalarImplementer
520 static constexpr bool TInputType1IsInteger = std::is_integral_v<TInputType1>;
521 static constexpr bool TInputType1IsSigned = std::is_signed_v<TInputType1>;
522 static constexpr bool TInputType2IsInteger = std::is_integral_v<TInputType2>;
523 static constexpr bool TInputType2IsSigned = std::is_signed_v<TInputType2>;
525 using SelectedVersion =
typename AlmostEqualsFunctionSelector<TInputType1IsInteger,
527 TInputType2IsInteger,
528 TInputType2IsSigned>::SelectedVersion;
534template <
typename TScalarType1,
typename TScalarType2>
536AlmostEqualsScalarComparer(TScalarType1 x1, TScalarType2 x2)
538 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion::
539 template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
546struct AlmostEqualsScalarVsScalar
548 template <
typename TScalarType1,
typename TScalarType2>
550 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
552 return AlmostEqualsScalarComparer(x1, x2);
558struct AlmostEqualsComplexVsComplex
560 template <
typename TComplexType1,
typename TComplexType2>
562 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
564 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer(x1.imag(), x2.imag());
571struct AlmostEqualsScalarVsComplex
573 template <
typename TScalarType,
typename TComplexType>
575 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
581 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
585struct AlmostEqualsComplexVsScalar
587 template <
typename TComplexType,
typename TScalarType>
589 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
591 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
598template <
bool T1IsComplex,
bool T2IsComplex>
599struct AlmostEqualsComplexChooser
601 using ChosenVersion = AlmostEqualsScalarVsScalar;
605struct AlmostEqualsComplexChooser<true, true>
607 using ChosenVersion = AlmostEqualsComplexVsComplex;
611struct AlmostEqualsComplexChooser<false, true>
613 using ChosenVersion = AlmostEqualsScalarVsComplex;
617struct AlmostEqualsComplexChooser<true, false>
619 using ChosenVersion = AlmostEqualsComplexVsScalar;
627template <
typename T1,
typename T2>
628struct AlmostEqualsComplexImplementer
633 using ChosenVersion =
typename AlmostEqualsComplexChooser<T1IsComplex, T2IsComplex>::ChosenVersion;
682template <
typename T1,
typename T2>
686 return Detail::AlmostEqualsComplexImplementer<T1, T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
690template <
typename T1,
typename T2>
720template <
typename TInput1,
typename TInput2>
725 ITK_GCC_SUPPRESS_Wfloat_equal
731template <
typename TInput1,
typename TInput2>
755ITKCommon_EXPORT
unsigned short
757ITKCommon_EXPORT
unsigned int
759ITKCommon_EXPORT
unsigned long
761ITKCommon_EXPORT
unsigned long long
769template <
typename TReturnType = u
intmax_t>
773 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedProduct only supports unsigned return types");
777 return (a == 0) || (b == 0) ||
778 (((
static_cast<TReturnType
>(a * b) / a) == b) && ((
static_cast<TReturnType
>(a * b) / b) == a))
779 ?
static_cast<TReturnType
>(a * b)
780 : (assert(!
"UnsignedProduct overflow!"), 0);
791template <
typename TReturnType = u
intmax_t>
795 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedPower only supports unsigned return types");
799 return (exponent == 0)
800 ? (assert(base > 0), 1)
801 : (exponent == 1) ? base
802 : UnsignedProduct<TReturnType>(UnsignedPower<TReturnType>(base, exponent / 2),
803 UnsignedPower<TReturnType>(base, (exponent + 1) / 2));
817using vnl_math::angle_0_to_2pi;
818using vnl_math::angle_minuspi_to_pi;
819using vnl_math::rnd_halfinttoeven;
820using vnl_math::rnd_halfintup;
822using vnl_math::floor;
826using vnl_math::remainder_truncated;
827using vnl_math::remainder_floored;
830using vnl_math::squared_magnitude;
852 return x < 0 ? static_cast<unsigned char>(-x) : x;
857 return static_cast<unsigned char>(x);
862 return x < 0 ? static_cast<unsigned short>(-x) : x;
880inline unsigned long long
Define additional traits for native types such as int or float.
static bool IsPositive(T val)
static constexpr bool IsComplex
#define itkConceptMacro(name, concept)
#define itkTemplateFloatingToIntegerMacro(name)
RoundHalfIntegerToEven(TInput x) template< TReturn
Round towards nearest integer.
Detail::FloatIEEE< T >::IntType FloatDifferenceULP(T x1, T x2)
Return the signed distance in ULPs (units in the last place) between two floats.
bool AlmostEquals(T1 x1, T2 x2)
Provide consistent equality checks between values of potentially different scalar or complex types.
static constexpr double sqrt2pi
static constexpr double sqrt1_2
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potentially different types.
ITKCommon_EXPORT unsigned short GreatestPrimeFactor(unsigned short n)
static constexpr double eps
bool NotAlmostEquals(T1 x1, T2 x2)
TInput Ceil(TInput x) template< typename TReturn
static constexpr double pi_over_2
constexpr TReturnType UnsignedPower(const uintmax_t base, const uintmax_t exponent) noexcept
TInput TInput TReturn CastWithRangeCheck(TInput x)
static constexpr double one_over_sqrt2pi
static constexpr double pi_over_4
TInput RoundHalfIntegerUp(TInput x) template< typename TReturn
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
static constexpr double sqrteps
static constexpr double two_over_sqrtpi
unsigned long long abs(unsigned long long x)
static constexpr double one_over_pi
T FloatAddULP(T x, typename Detail::FloatIEEE< T >::IntType ulps)
Add the given ULPs (units in the last place) to a float.
constexpr TReturnType UnsignedProduct(const uintmax_t a, const uintmax_t b) noexcept
static constexpr double ln2
Floor(TInput x) template< TReturn
Round towards minus infinity.
static constexpr double euler
euler constant
static constexpr float float_sqrteps
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
static constexpr float float_eps
static constexpr double ln10
static constexpr double twopi
ITKCommon_EXPORT bool IsPrime(unsigned short n)
static constexpr double sqrt2
bool FloatAlmostEqual(T x1, T x2, typename Detail::FloatIEEE< T >::IntType maxUlps=4, typename Detail::FloatIEEE< T >::FloatType maxAbsoluteDifference=0.1 *itk::NumericTraits< T >::epsilon())
Compare two floats and return if they are effectively equal.
static constexpr double pi
static constexpr double deg_per_rad
static constexpr double two_over_pi
static constexpr double e
static constexpr double sqrt1_3
static constexpr double pi_over_180
static constexpr double log10e
static constexpr double log2e
TInput TInput TReturn Round(TInput x)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
static bool AlmostEqualsFunction(double x1, double x2)
static bool AlmostEqualsFunction(float x1, double x2)
static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
static bool AlmostEqualsFunction(double x1, float x2)
static bool AlmostEqualsFunction(float x1, float x2)
static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
AlmostEqualsPlainOldEquals SelectedVersion
static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
static bool AlmostEqualsFunction(TSignedInt signedVariable, TUnsignedInt unsignedVariable)
static bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
typename FloatIEEETraits< T >::IntType IntType