34 #include <vnl/vnl_math.h>
41 #include <vxl_version.h>
105 #define itkTemplateFloatingToIntegerMacro(name) \
106 template <typename TReturn, typename TInput> \
107 inline TReturn name(TInput x) \
110 if (sizeof(TReturn) <= 4) \
112 return static_cast<TReturn>(Detail::name##_32(x)); \
114 else if (sizeof(TReturn) <= 8) \
116 return static_cast<TReturn>(Detail::name##_64(x)); \
120 return static_cast<TReturn>(Detail::name##_base<TReturn, TInput>(x)); \
177 template <
typename TReturn,
typename TInput>
181 return RoundHalfIntegerUp<TReturn, TInput>(x);
210 #undef itkTemplateFloatingToIntegerMacro
212 template <
typename TReturn,
typename TInput>
216 #ifdef ITK_USE_CONCEPT_CHECKING
219 #endif // ITK_USE_CONCEPT_CHECKING
221 auto ret = static_cast<TReturn>(x);
222 if (
sizeof(TReturn) >
sizeof(TInput) &&
229 else if (
sizeof(TReturn) >=
sizeof(TInput))
233 itk::RangeError _e(__FILE__, __LINE__);
237 else if (static_cast<TInput>(ret) != x ||
240 itk::RangeError _e(__FILE__, __LINE__);
253 template <
typename T>
269 template <
typename T>
275 return representOutput.
asFloat;
308 template <
typename T>
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>
449 template <
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
464 struct AlmostEqualsFunctionSelector<false, true, true, true>
471 struct AlmostEqualsFunctionSelector<false, true, true, false>
478 struct AlmostEqualsFunctionSelector<true, false, false, true>
485 struct AlmostEqualsFunctionSelector<true, true, false, true>
492 struct AlmostEqualsFunctionSelector<true, true, true, false>
499 struct AlmostEqualsFunctionSelector<true, false, true, true>
506 struct AlmostEqualsFunctionSelector<true, true, true, true>
513 struct AlmostEqualsFunctionSelector<true, false, true, false>
521 template <
typename TInputType1,
typename TInputType2>
522 struct 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;
538 template <
typename TScalarType1,
typename TScalarType2>
540 AlmostEqualsScalarComparer(TScalarType1 x1, TScalarType2 x2)
542 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion::
543 template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
550 struct AlmostEqualsScalarVsScalar
552 template <
typename TScalarType1,
typename TScalarType2>
554 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
556 return AlmostEqualsScalarComparer(x1, x2);
562 struct 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());
575 struct AlmostEqualsScalarVsComplex
577 template <
typename TScalarType,
typename TComplexType>
579 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
581 if (!AlmostEqualsScalarComparer(
582 complexVariable.imag(),
587 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
591 struct AlmostEqualsComplexVsScalar
593 template <
typename TComplexType,
typename TScalarType>
595 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
597 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
604 template <
bool T1IsComplex,
bool T2IsComplex>
605 struct AlmostEqualsComplexChooser
607 using ChosenVersion = AlmostEqualsScalarVsScalar;
611 struct AlmostEqualsComplexChooser<true, true>
613 using ChosenVersion = AlmostEqualsComplexVsComplex;
617 struct AlmostEqualsComplexChooser<false, true>
619 using ChosenVersion = AlmostEqualsScalarVsComplex;
623 struct AlmostEqualsComplexChooser<true, false>
625 using ChosenVersion = AlmostEqualsComplexVsScalar;
633 template <
typename T1,
typename T2>
634 struct AlmostEqualsComplexImplementer
639 using ChosenVersion =
typename AlmostEqualsComplexChooser<T1IsComplex, T2IsComplex>::ChosenVersion;
688 template <
typename T1,
typename T2>
692 return Detail::AlmostEqualsComplexImplementer<T1, T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
696 template <
typename T1,
typename T2>
726 template <
typename TInput1,
typename TInput2>
731 CLANG_SUPPRESS_Wfloat_equal
return x1 == x2;
736 template <
typename TInput1,
typename TInput2>
748 ITKCommon_EXPORT
bool
750 ITKCommon_EXPORT
bool
752 ITKCommon_EXPORT
bool
754 ITKCommon_EXPORT
bool
760 ITKCommon_EXPORT
unsigned short
762 ITKCommon_EXPORT
unsigned int
764 ITKCommon_EXPORT
unsigned long
766 ITKCommon_EXPORT
unsigned long long
775 # define ITK_X_ASSERT(CHECK) void(0)
777 # define ITK_X_ASSERT(CHECK) ((CHECK) ? void(0) : [] { assert(!#CHECK); }())
783 template <
typename TReturnType = u
intmax_t>
784 constexpr TReturnType
787 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedProduct only supports unsigned return types");
791 return (a == 0) || (b == 0) ||
792 (((static_cast<TReturnType>(a * b) / a) == b) && ((static_cast<TReturnType>(a * b) / b) == a))
793 ? static_cast<TReturnType>(a * b)
805 template <
typename TReturnType = u
intmax_t>
806 constexpr TReturnType
809 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedPower only supports unsigned return types");
813 return (exponent == 0)
815 : (exponent == 1) ? base
816 : UnsignedProduct<TReturnType>(UnsignedPower<TReturnType>(base, exponent / 2),
817 UnsignedPower<TReturnType>(base, (exponent + 1) / 2));
833 using vnl_math::angle_0_to_2pi;
834 using vnl_math::angle_minuspi_to_pi;
835 using vnl_math::rnd_halfinttoeven;
836 using vnl_math::rnd_halfintup;
838 using vnl_math::floor;
839 using vnl_math::ceil;
841 using vnl_math::sgn0;
842 using vnl_math::remainder_truncated;
843 using vnl_math::remainder_floored;
845 using vnl_math::cube;
846 using vnl_math::squared_magnitude;
868 return x < 0 ? static_cast<unsigned char>(-x) : x;
873 return static_cast<unsigned char>(x);
875 inline unsigned short
878 return x < 0 ? static_cast<unsigned short>(-x) : x;
880 inline unsigned short
896 inline unsigned long long
907 #endif // end of itkMath.h