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) \
111 if (sizeof(TReturn) <= 4) \
113 return static_cast<TReturn>(Detail::name##_32(x)); \
115 else if (sizeof(TReturn) <= 8) \
117 return static_cast<TReturn>(Detail::name##_64(x)); \
121 return static_cast<TReturn>(Detail::name##_base<TReturn, TInput>(x)); \
178 template <
typename TReturn,
typename TInput>
182 return RoundHalfIntegerUp<TReturn, TInput>(x);
211 #undef itkTemplateFloatingToIntegerMacro
213 template <
typename TReturn,
typename TInput>
217 #ifdef ITK_USE_CONCEPT_CHECKING
220 #endif // ITK_USE_CONCEPT_CHECKING
222 auto ret = static_cast<TReturn>(x);
223 if (
sizeof(TReturn) >
sizeof(TInput) &&
230 else if (
sizeof(TReturn) >=
sizeof(TInput))
234 itk::RangeError _e(__FILE__, __LINE__);
238 else if (static_cast<TInput>(ret) != x ||
241 itk::RangeError _e(__FILE__, __LINE__);
254 template <
typename T>
270 template <
typename T>
276 return representOutput.
asFloat;
309 template <
typename T>
319 const T absDifference =
std::abs(x1 - x2);
320 if (absDifference <= maxAbsoluteDifference)
329 if (std::signbit(x1) != std::signbit(x2))
339 return ulps <= maxUlps;
353 template <
typename TFloatType1,
typename TFloatType2>
357 return FloatAlmostEqual<double>(x1, x2);
360 template <
typename TFloatType1,
typename TFloatType2>
364 return FloatAlmostEqual<double>(x1, x2);
367 template <
typename TFloatType1,
typename TFloatType2>
371 return FloatAlmostEqual<float>(x1, x2);
374 template <
typename TFloatType1,
typename TFloatType2>
378 return FloatAlmostEqual<float>(x1, x2);
381 template <
typename TFloatType1,
typename TFloatType2>
385 return FloatAlmostEqual<float>(x1, x2);
391 template <
typename TFloatType,
typename TIntType>
395 return FloatAlmostEqual<TFloatType>(floatingVariable, integerVariable);
401 template <
typename TIntType,
typename TFloatType>
411 template <
typename TSignedInt,
typename TUn
signedInt>
415 if (signedVariable < 0)
423 return signedVariable == static_cast<TSignedInt>(unsignedVariable);
429 template <
typename TUn
signedInt,
typename TSignedInt>
439 template <
typename TIntegerType1,
typename TIntegerType2>
450 template <
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
465 struct AlmostEqualsFunctionSelector<false, true, true, true>
472 struct AlmostEqualsFunctionSelector<false, true, true, false>
479 struct AlmostEqualsFunctionSelector<true, false, false, true>
486 struct AlmostEqualsFunctionSelector<true, true, false, true>
493 struct AlmostEqualsFunctionSelector<true, true, true, false>
500 struct AlmostEqualsFunctionSelector<true, false, true, true>
507 struct AlmostEqualsFunctionSelector<true, true, true, true>
514 struct AlmostEqualsFunctionSelector<true, false, true, false>
522 template <
typename TInputType1,
typename TInputType2>
523 struct AlmostEqualsScalarImplementer
525 static constexpr
bool TInputType1IsInteger = std::is_integral_v<TInputType1>;
526 static constexpr
bool TInputType1IsSigned = std::is_signed_v<TInputType1>;
527 static constexpr
bool TInputType2IsInteger = std::is_integral_v<TInputType2>;
528 static constexpr
bool TInputType2IsSigned = std::is_signed_v<TInputType2>;
530 using SelectedVersion =
typename AlmostEqualsFunctionSelector<TInputType1IsInteger,
532 TInputType2IsInteger,
533 TInputType2IsSigned>::SelectedVersion;
539 template <
typename TScalarType1,
typename TScalarType2>
541 AlmostEqualsScalarComparer(TScalarType1 x1, TScalarType2 x2)
543 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion::
544 template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
551 struct AlmostEqualsScalarVsScalar
553 template <
typename TScalarType1,
typename TScalarType2>
555 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
557 return AlmostEqualsScalarComparer(x1, x2);
563 struct AlmostEqualsComplexVsComplex
565 template <
typename TComplexType1,
typename TComplexType2>
567 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
569 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer(x1.imag(), x2.imag());
576 struct AlmostEqualsScalarVsComplex
578 template <
typename TScalarType,
typename TComplexType>
580 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
582 if (!AlmostEqualsScalarComparer(
583 complexVariable.imag(),
588 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
592 struct AlmostEqualsComplexVsScalar
594 template <
typename TComplexType,
typename TScalarType>
596 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
598 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
605 template <
bool T1IsComplex,
bool T2IsComplex>
606 struct AlmostEqualsComplexChooser
608 using ChosenVersion = AlmostEqualsScalarVsScalar;
612 struct AlmostEqualsComplexChooser<true, true>
614 using ChosenVersion = AlmostEqualsComplexVsComplex;
618 struct AlmostEqualsComplexChooser<false, true>
620 using ChosenVersion = AlmostEqualsScalarVsComplex;
624 struct AlmostEqualsComplexChooser<true, false>
626 using ChosenVersion = AlmostEqualsComplexVsScalar;
634 template <
typename T1,
typename T2>
635 struct AlmostEqualsComplexImplementer
640 using ChosenVersion =
typename AlmostEqualsComplexChooser<T1IsComplex, T2IsComplex>::ChosenVersion;
689 template <
typename T1,
typename T2>
693 return Detail::AlmostEqualsComplexImplementer<T1, T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
697 template <
typename T1,
typename T2>
727 template <
typename TInput1,
typename TInput2>
732 CLANG_SUPPRESS_Wfloat_equal
return x1 == x2;
737 template <
typename TInput1,
typename TInput2>
749 ITKCommon_EXPORT
bool
751 ITKCommon_EXPORT
bool
753 ITKCommon_EXPORT
bool
755 ITKCommon_EXPORT
bool
761 ITKCommon_EXPORT
unsigned short
763 ITKCommon_EXPORT
unsigned int
765 ITKCommon_EXPORT
unsigned long
767 ITKCommon_EXPORT
unsigned long long
775 template <
typename TReturnType = u
intmax_t>
776 constexpr TReturnType
779 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedProduct only supports unsigned return types");
783 return (a == 0) || (b == 0) ||
784 (((static_cast<TReturnType>(a * b) / a) == b) && ((static_cast<TReturnType>(a * b) / b) == a))
785 ? static_cast<TReturnType>(a * b)
786 : (assert(!
"UnsignedProduct overflow!"), 0);
797 template <
typename TReturnType = u
intmax_t>
798 constexpr TReturnType
801 static_assert(std::is_unsigned_v<TReturnType>,
"UnsignedPower only supports unsigned return types");
805 return (exponent == 0)
806 ? (assert(base > 0), 1)
807 : (exponent == 1) ? base
808 : UnsignedProduct<TReturnType>(UnsignedPower<TReturnType>(base, exponent / 2),
809 UnsignedPower<TReturnType>(base, (exponent + 1) / 2));
823 using vnl_math::angle_0_to_2pi;
824 using vnl_math::angle_minuspi_to_pi;
825 using vnl_math::rnd_halfinttoeven;
826 using vnl_math::rnd_halfintup;
828 using vnl_math::floor;
829 using vnl_math::ceil;
831 using vnl_math::sgn0;
832 using vnl_math::remainder_truncated;
833 using vnl_math::remainder_floored;
835 using vnl_math::cube;
836 using vnl_math::squared_magnitude;
858 return x < 0 ? static_cast<unsigned char>(-x) : x;
863 return static_cast<unsigned char>(x);
865 inline unsigned short
868 return x < 0 ? static_cast<unsigned short>(-x) : x;
870 inline unsigned short
886 inline unsigned long long
897 #endif // end of itkMath.h