35 #include <vnl/vnl_math.h> 42 #include <vxl_version.h> 43 #if VXL_VERSION_DATE_FULL <= 20121114 44 # error "VXL version must support vnl_math:: namespace versions of fuctions" 108 #define itkTemplateFloatingToIntegerMacro(name) \ 109 template< typename TReturn, typename TInput > \ 110 inline TReturn name(TInput x) \ 113 if ( sizeof( TReturn ) <= 4 ) \ 115 return static_cast< TReturn >( Detail::name##_32(x) ); \ 117 else if ( sizeof( TReturn ) <= 8 ) \ 119 return static_cast< TReturn >( Detail::name##_64(x) ); \ 123 return static_cast< TReturn >( Detail::name##_base< TReturn, TInput >(x) ); \ 180 template<
typename TReturn,
typename TInput >
181 inline TReturn
Round(TInput x) {
return RoundHalfIntegerUp< TReturn, TInput >(x); }
209 #undef itkTemplateFloatingToIntegerMacro 211 template<
typename TReturn,
typename TInput >
214 #ifdef ITK_USE_CONCEPT_CHECKING 217 #endif // ITK_USE_CONCEPT_CHECKING 219 TReturn ret =
static_cast< TReturn
>( x );
220 if (
sizeof( TReturn ) >
sizeof( TInput )
227 else if (
sizeof( TReturn ) >=
sizeof( TInput ) )
235 else if ( static_cast< TInput >( ret ) != x
251 template <
typename T>
267 template <
typename T>
273 return representOutput.
asFloat;
306 template <
typename T>
314 const T absDifference =
std::abs(x1 - x2);
315 if ( absDifference <= maxAbsoluteDifference )
320 #if defined(__APPLE__) && (__clang_major__ == 3) && (__clang_minor__ == 0) && defined(NDEBUG) && defined(__x86_64__) 323 double x1fAsULP =
static_cast<double>(x1f.
AsULP());
324 double x2fAsULP =
static_cast<double>(x2f.
AsULP());
325 double ulps = x1fAsULP - x2fAsULP;
330 return ulps <= static_cast<double>(maxUlps);
338 return ulps <= maxUlps;
353 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>
393 return FloatAlmostEqual<TFloatType> (floatingVariable, integerVariable);
399 template <
typename TIntType,
typename TFloatType>
408 template <
typename TSignedInt,
typename TUn
signedInt>
411 if(signedVariable < 0)
return false;
413 return signedVariable ==
static_cast< TSignedInt
>(unsignedVariable);
419 template <
typename TUn
signedInt,
typename TSignedInt>
428 template <
typename TIntegerType1,
typename TIntegerType2>
438 template<
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
510 template<
typename TInputType1,
typename TInputType2>
511 struct AlmostEqualsScalarImplementer
525 template <
typename TScalarType1,
typename TScalarType2>
527 AlmostEqualsScalarComparer( TScalarType1 x1, TScalarType2 x2 )
529 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion:: template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
536 struct AlmostEqualsScalarVsScalar
538 template <
typename TScalarType1,
typename TScalarType2>
542 return AlmostEqualsScalarComparer(x1, x2);
548 struct AlmostEqualsComplexVsComplex
550 template <
typename TComplexType1,
typename TComplexType2>
554 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer( x1.imag(), x2.imag() );
561 struct AlmostEqualsScalarVsComplex
563 template <
typename TScalarType,
typename TComplexType>
571 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
575 struct AlmostEqualsComplexVsScalar
577 template <
typename TComplexType,
typename TScalarType>
581 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
588 template <
bool T1IsComplex,
bool T2IsComplex >
589 struct AlmostEqualsComplexChooser
591 typedef AlmostEqualsScalarVsScalar ChosenVersion;
595 struct AlmostEqualsComplexChooser< true, true >
597 typedef AlmostEqualsComplexVsComplex ChosenVersion;
601 struct AlmostEqualsComplexChooser< false, true >
603 typedef AlmostEqualsScalarVsComplex ChosenVersion;
607 struct AlmostEqualsComplexChooser< true, false>
609 typedef AlmostEqualsComplexVsScalar ChosenVersion;
617 template <
typename T1,
typename T2>
618 struct AlmostEqualsComplexImplementer
623 typedef typename AlmostEqualsComplexChooser< T1IsComplex, T2IsComplex >::ChosenVersion ChosenVersion;
672 template <
typename T1,
typename T2>
676 return Detail::AlmostEqualsComplexImplementer<T1,T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
680 template <
typename T1,
typename T2>
710 template <
typename TInput1,
typename TInput2>
715 CLANG_SUPPRESS_Wfloat_equal
721 template <
typename TInput1,
typename TInput2>
733 ITKCommon_EXPORT
bool IsPrime(
unsigned short n );
734 ITKCommon_EXPORT
bool IsPrime(
unsigned int n );
735 ITKCommon_EXPORT
bool IsPrime(
unsigned long n );
736 ITKCommon_EXPORT
bool IsPrime(
unsigned long long n );
752 #if ITK_COMPILED_CXX_VERSION >= 201103L 758 #define ITK_PERFECT_FORWARD_MACRO(new_name, old_name) \ 759 template <typename... TArgs> \ 760 auto new_name(TArgs&&... args) -> decltype(old_name(std::forward<TArgs>(args)...)) { \ 761 return old_name(std::forward<TArgs>(args)...); \ 768 ITK_PERFECT_FORWARD_MACRO(isnormal,std::isnormal);
788 #undef ITK_PERFECT_FORWARD_MACRO 794 template<
typename T> T
cbrt(
const T value) {
return vnl_math::cuberoot(value); }
808 inline bool abs(
const bool x) {
return x; }
809 inline unsigned char abs(
const unsigned char x) {
return x; }
813 inline unsigned short abs(
const unsigned short x) {
return x; }
815 inline unsigned int abs(
const unsigned int x) {
return x; }
817 inline unsigned long abs(
const unsigned long x) {
return x; }
818 #if VCL_HAS_LONG_LONG 819 inline unsigned long long abs(
const long long x) {
return vnl_math::abs(x); }
820 inline unsigned long long abs(
const unsigned long long x) {
return x; }
824 inline long double abs(
const long double x) {
return std::abs(x); }
831 #if VCL_HAS_LONG_LONG 833 inline unsigned long long sqr(
const unsigned long long x) {
return vnl_math::sqr(x); }
843 #if VCL_HAS_LONG_LONG 845 inline unsigned long long cube(
const unsigned long long x) {
return vnl_math::cube(x); }
856 #if VCL_HAS_LONG_LONG 864 #endif //If not C++11 features 869 #endif // end of itkMath.h static bool AlmostEqualsFunction(double x1, float x2)
static ITK_CONSTEXPR double two_over_pi
static bool AlmostEqualsFunction(TSignedInt signedVariable, TUnsignedInt unsignedVariable)
static bool AlmostEqualsFunction(float x1, double x2)
double cube(const double x)
TReturn Round(TInput x)
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
static ITK_CONSTEXPR double euler
euler constant
RoundHalfIntegerUp(TInput x)
Round towards nearest integer.
bool isnan(const T value)
static ITK_CONSTEXPR double deg_per_rad
T remainder_truncated(const T x, const T y)
static ITK_CONSTEXPR double twopi
static ITK_CONSTEXPR double one_over_pi
static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
static ITK_CONSTEXPR float float_sqrteps
static ITK_CONSTEXPR double eps
Floor(TInput x)
Round towards minus infinity.
bool isinf(const T value)
long double abs(const long double x)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes...
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potetially different types...
TReturn CastWithRangeCheck(TInput x)
ITKCommon_EXPORT unsigned short GreatestPrimeFactor(unsigned short n)
static bool IsPositive(T val)
static ITK_CONSTEXPR float float_eps
static bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
static ITK_CONSTEXPR double ln10
static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
static ITK_CONSTEXPR double log10e
static ITK_CONSTEXPR double sqrt2
static ITK_CONSTEXPR double one_over_sqrt2pi
T angle_minuspi_to_pi(const T angle)
static ITK_CONSTEXPR double e
The base of the natural logarithm or Euler's number
Detail::FloatIEEE< T >::IntType FloatDifferenceULP(T x1, T x2)
Return the signed distance in ULPs (units in the last place) between two floats.
int rnd_halfinttoeven(const T x)
static ITK_CONSTEXPR double sqrt1_3
static ITK_CONSTEXPR double sqrt2pi
#define itkTemplateFloatingToIntegerMacro(name)
T FloatAddULP(T x, typename Detail::FloatIEEE< T >::IntType ulps)
Add the given ULPs (units in the last place) to a float.
static ITK_CONSTEXPR double pi_over_2
T remainder_floored(const T x, const T y)
FloatIEEETraits< T >::IntType IntType
static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
static ITK_CONSTEXPR double log2e
double sqr(const double x)
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
static ITK_CONSTEXPR double sqrteps
Ceil(TInput x)
Round towards plus infinity.
static ITK_CONSTEXPR double pi_over_180
static bool AlmostEqualsFunction(float x1, float x2)
long double squared_magnitude(const long double x)
T angle_0_to_2pi(const T angle)
bool AlmostEquals(T1 x1, T2 x2)
Provide consistent equality checks between values of potentially different scalar or complex types...
unsigned int squared_magnitude(const char x)
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 ITK_CONSTEXPR double ln2
static ITK_CONSTEXPR double two_over_sqrtpi
AlmostEqualsPlainOldEquals SelectedVersion
static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
static ITK_CONSTEXPR double pi_over_4
Define additional traits for native types such as int or float.
bool NotAlmostEquals(T1 x1, T2 x2)
T hypot(const T value1, const T value2)
static bool AlmostEqualsFunction(double x1, double x2)
RoundHalfIntegerToEven(TInput x)
Round towards nearest integer.
#define itkConceptMacro(name, concept)
ITKCommon_EXPORT bool IsPrime(unsigned short n)
bool isfinite(const T value)
static ITK_CONSTEXPR double pi
int rnd_halfintup(const T x)
static ITK_CONSTEXPR double sqrt1_2