34 #include <vnl/vnl_math.h>
41 #include <vxl_version.h>
104 #define itkTemplateFloatingToIntegerMacro(name) \
105 template <typename TReturn, typename TInput> \
106 inline TReturn name(TInput x) \
109 if (sizeof(TReturn) <= 4) \
111 return static_cast<TReturn>(Detail::name##_32(x)); \
113 else if (sizeof(TReturn) <= 8) \
115 return static_cast<TReturn>(Detail::name##_64(x)); \
119 return static_cast<TReturn>(Detail::name##_base<TReturn, TInput>(x)); \
176 template <
typename TReturn,
typename TInput>
180 return RoundHalfIntegerUp<TReturn, TInput>(x);
209 #undef itkTemplateFloatingToIntegerMacro
211 template <
typename TReturn,
typename TInput>
215 #ifdef ITK_USE_CONCEPT_CHECKING
218 #endif // ITK_USE_CONCEPT_CHECKING
220 auto ret = static_cast<TReturn>(x);
221 if (
sizeof(TReturn) >
sizeof(TInput) &&
228 else if (
sizeof(TReturn) >=
sizeof(TInput))
232 itk::RangeError _e(__FILE__, __LINE__);
236 else if (static_cast<TInput>(ret) != x ||
239 itk::RangeError _e(__FILE__, __LINE__);
252 template <
typename T>
268 template <
typename T>
274 return representOutput.
asFloat;
307 template <
typename T>
317 const T absDifference = std::abs(x1 - x2);
318 if (absDifference <= maxAbsoluteDifference)
327 if (std::signbit(x1) != std::signbit(x2))
337 return ulps <= maxUlps;
351 template <
typename TFloatType1,
typename TFloatType2>
355 return FloatAlmostEqual<double>(x1, x2);
358 template <
typename TFloatType1,
typename TFloatType2>
362 return FloatAlmostEqual<double>(x1, x2);
365 template <
typename TFloatType1,
typename TFloatType2>
369 return FloatAlmostEqual<float>(x1, x2);
372 template <
typename TFloatType1,
typename TFloatType2>
376 return FloatAlmostEqual<float>(x1, x2);
379 template <
typename TFloatType1,
typename TFloatType2>
383 return FloatAlmostEqual<float>(x1, x2);
389 template <
typename TFloatType,
typename TIntType>
393 return FloatAlmostEqual<TFloatType>(floatingVariable, integerVariable);
399 template <
typename TIntType,
typename TFloatType>
409 template <
typename TSignedInt,
typename TUn
signedInt>
413 if (signedVariable < 0)
417 return signedVariable == static_cast<TSignedInt>(unsignedVariable);
423 template <
typename TUn
signedInt,
typename TSignedInt>
433 template <
typename TIntegerType1,
typename TIntegerType2>
444 template <
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
459 struct AlmostEqualsFunctionSelector<false, true, true, true>
466 struct AlmostEqualsFunctionSelector<false, true, true, false>
473 struct AlmostEqualsFunctionSelector<true, false, false, true>
480 struct AlmostEqualsFunctionSelector<true, true, false, true>
487 struct AlmostEqualsFunctionSelector<true, true, true, false>
494 struct AlmostEqualsFunctionSelector<true, false, true, true>
501 struct AlmostEqualsFunctionSelector<true, true, true, true>
508 struct AlmostEqualsFunctionSelector<true, false, true, false>
516 template <
typename TInputType1,
typename TInputType2>
517 struct AlmostEqualsScalarImplementer
524 using SelectedVersion =
typename AlmostEqualsFunctionSelector<TInputType1IsInteger,
526 TInputType2IsInteger,
527 TInputType2IsSigned>::SelectedVersion;
533 template <
typename TScalarType1,
typename TScalarType2>
535 AlmostEqualsScalarComparer(TScalarType1 x1, TScalarType2 x2)
537 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion::
538 template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
545 struct AlmostEqualsScalarVsScalar
547 template <
typename TScalarType1,
typename TScalarType2>
549 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
551 return AlmostEqualsScalarComparer(x1, x2);
557 struct AlmostEqualsComplexVsComplex
559 template <
typename TComplexType1,
typename TComplexType2>
561 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
563 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer(x1.imag(), x2.imag());
570 struct AlmostEqualsScalarVsComplex
572 template <
typename TScalarType,
typename TComplexType>
574 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
576 if (!AlmostEqualsScalarComparer(
577 complexVariable.imag(),
582 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
586 struct AlmostEqualsComplexVsScalar
588 template <
typename TComplexType,
typename TScalarType>
590 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
592 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
599 template <
bool T1IsComplex,
bool T2IsComplex>
600 struct AlmostEqualsComplexChooser
602 using ChosenVersion = AlmostEqualsScalarVsScalar;
606 struct AlmostEqualsComplexChooser<true, true>
608 using ChosenVersion = AlmostEqualsComplexVsComplex;
612 struct AlmostEqualsComplexChooser<false, true>
614 using ChosenVersion = AlmostEqualsScalarVsComplex;
618 struct AlmostEqualsComplexChooser<true, false>
620 using ChosenVersion = AlmostEqualsComplexVsScalar;
628 template <
typename T1,
typename T2>
629 struct AlmostEqualsComplexImplementer
634 using ChosenVersion =
typename AlmostEqualsComplexChooser<T1IsComplex, T2IsComplex>::ChosenVersion;
683 template <
typename T1,
typename T2>
687 return Detail::AlmostEqualsComplexImplementer<T1, T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
691 template <
typename T1,
typename T2>
721 template <
typename TInput1,
typename TInput2>
726 CLANG_SUPPRESS_Wfloat_equal
return x1 == x2;
731 template <
typename TInput1,
typename TInput2>
743 ITKCommon_EXPORT
bool
745 ITKCommon_EXPORT
bool
747 ITKCommon_EXPORT
bool
749 ITKCommon_EXPORT
bool
755 ITKCommon_EXPORT
unsigned short
757 ITKCommon_EXPORT
unsigned int
759 ITKCommon_EXPORT
unsigned long
761 ITKCommon_EXPORT
unsigned long long
770 # define ITK_X_ASSERT(CHECK) void(0)
772 # define ITK_X_ASSERT(CHECK) ((CHECK) ? void(0) : [] { assert(!#CHECK); }())
778 template <
typename TReturnType = std::u
intmax_t>
779 constexpr TReturnType
782 static_assert(std::is_unsigned<TReturnType>::value,
"UnsignedProduct only supports unsigned return types");
786 return (a == 0) || (b == 0) ||
787 (((static_cast<TReturnType>(a * b) / a) == b) && ((static_cast<TReturnType>(a * b) / b) == a))
788 ? static_cast<TReturnType>(a * b)
800 template <
typename TReturnType = std::u
intmax_t>
801 constexpr TReturnType
804 static_assert(std::is_unsigned<TReturnType>::value,
"UnsignedPower only supports unsigned return types");
808 return (exponent == 0)
810 : (exponent == 1) ? base
811 : UnsignedProduct<TReturnType>(UnsignedPower<TReturnType>(base, exponent / 2),
812 UnsignedPower<TReturnType>(base, (exponent + 1) / 2));
828 using vnl_math::angle_0_to_2pi;
829 using vnl_math::angle_minuspi_to_pi;
830 using vnl_math::rnd_halfinttoeven;
831 using vnl_math::rnd_halfintup;
833 using vnl_math::floor;
834 using vnl_math::ceil;
836 using vnl_math::sgn0;
837 using vnl_math::remainder_truncated;
838 using vnl_math::remainder_floored;
841 using vnl_math::cube;
842 using vnl_math::squared_magnitude;
847 #endif // end of itkMath.h