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))
338 return ulps <= maxUlps;
352 template <
typename TFloatType1,
typename TFloatType2>
356 return FloatAlmostEqual<double>(x1, x2);
359 template <
typename TFloatType1,
typename TFloatType2>
363 return FloatAlmostEqual<double>(x1, x2);
366 template <
typename TFloatType1,
typename TFloatType2>
370 return FloatAlmostEqual<float>(x1, x2);
373 template <
typename TFloatType1,
typename TFloatType2>
377 return FloatAlmostEqual<float>(x1, x2);
380 template <
typename TFloatType1,
typename TFloatType2>
384 return FloatAlmostEqual<float>(x1, x2);
390 template <
typename TFloatType,
typename TIntType>
394 return FloatAlmostEqual<TFloatType>(floatingVariable, integerVariable);
400 template <
typename TIntType,
typename TFloatType>
410 template <
typename TSignedInt,
typename TUn
signedInt>
414 if (signedVariable < 0)
422 return signedVariable ==
static_cast<TSignedInt
>(unsignedVariable);
428 template <
typename TUn
signedInt,
typename TSignedInt>
438 template <
typename TIntegerType1,
typename TIntegerType2>
449template <
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
464struct AlmostEqualsFunctionSelector<false, true, true, true>
471struct AlmostEqualsFunctionSelector<false, true, true, false>
478struct AlmostEqualsFunctionSelector<true, false, false, true>
485struct AlmostEqualsFunctionSelector<true, true, false, true>
492struct AlmostEqualsFunctionSelector<true, true, true, false>
499struct AlmostEqualsFunctionSelector<true, false, true, true>
506struct AlmostEqualsFunctionSelector<true, true, true, true>
513struct AlmostEqualsFunctionSelector<true, false, true, false>
521template <
typename TInputType1,
typename TInputType2>
522struct AlmostEqualsScalarImplementer
524 static constexpr bool TInputType1IsInteger = std::is_integral_v<TInputType1>;
525 static constexpr bool TInputType1IsSigned = std::is_signed_v<TInputType1>;
526 static constexpr bool TInputType2IsInteger = std::is_integral_v<TInputType2>;
527 static constexpr bool TInputType2IsSigned = std::is_signed_v<TInputType2>;
529 using SelectedVersion =
typename AlmostEqualsFunctionSelector<TInputType1IsInteger,
531 TInputType2IsInteger,
532 TInputType2IsSigned>::SelectedVersion;
538template <
typename TScalarType1,
typename TScalarType2>
540AlmostEqualsScalarComparer(TScalarType1 x1, TScalarType2 x2)
542 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion::
543 template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
550struct AlmostEqualsScalarVsScalar
552 template <
typename TScalarType1,
typename TScalarType2>
554 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
556 return AlmostEqualsScalarComparer(x1, x2);
562struct AlmostEqualsComplexVsComplex
564 template <
typename TComplexType1,
typename TComplexType2>
566 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
568 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer(x1.imag(), x2.imag());
575struct AlmostEqualsScalarVsComplex
577 template <
typename TScalarType,
typename TComplexType>
579 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
585 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
589struct AlmostEqualsComplexVsScalar
591 template <
typename TComplexType,
typename TScalarType>
593 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
595 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
602template <
bool T1IsComplex,
bool T2IsComplex>
603struct AlmostEqualsComplexChooser
605 using ChosenVersion = AlmostEqualsScalarVsScalar;
609struct AlmostEqualsComplexChooser<true, true>
611 using ChosenVersion = AlmostEqualsComplexVsComplex;
615struct AlmostEqualsComplexChooser<false, true>
617 using ChosenVersion = AlmostEqualsScalarVsComplex;
621struct AlmostEqualsComplexChooser<true, false>
623 using ChosenVersion = AlmostEqualsComplexVsScalar;
631template <
typename T1,
typename T2>
632struct AlmostEqualsComplexImplementer
637 using ChosenVersion =
typename AlmostEqualsComplexChooser<T1IsComplex, T2IsComplex>::ChosenVersion;
686template <
typename T1,
typename T2>
690 return Detail::AlmostEqualsComplexImplementer<T1, T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
694template <
typename T1,
typename T2>
724template <
typename TInput1,
typename TInput2>
729 ITK_GCC_SUPPRESS_Wfloat_equal
735template <
typename TInput1,
typename TInput2>
759ITKCommon_EXPORT
unsigned short
761ITKCommon_EXPORT
unsigned int
763ITKCommon_EXPORT
unsigned long
765ITKCommon_EXPORT
unsigned long long
773template <
typename TReturnType = u
intmax_t>
777 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedProduct only supports unsigned return types");
781 return (a == 0) || (b == 0) ||
782 (((
static_cast<TReturnType
>(a * b) / a) == b) && ((
static_cast<TReturnType
>(a * b) / b) == a))
783 ?
static_cast<TReturnType
>(a * b)
784 : (assert(!
"UnsignedProduct overflow!"), 0);
795template <
typename TReturnType = u
intmax_t>
799 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedPower only supports unsigned return types");
803 return (exponent == 0)
804 ? (assert(base > 0), 1)
805 : (exponent == 1) ? base
806 : UnsignedProduct<TReturnType>(UnsignedPower<TReturnType>(base, exponent / 2),
807 UnsignedPower<TReturnType>(base, (exponent + 1) / 2));
821using vnl_math::angle_0_to_2pi;
822using vnl_math::angle_minuspi_to_pi;
823using vnl_math::rnd_halfinttoeven;
824using vnl_math::rnd_halfintup;
826using vnl_math::floor;
830using vnl_math::remainder_truncated;
831using vnl_math::remainder_floored;
834using vnl_math::squared_magnitude;
856 return x < 0 ? static_cast<unsigned char>(-x) : x;
861 return static_cast<unsigned char>(x);
866 return x < 0 ? static_cast<unsigned short>(-x) : x;
884inline 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