8 #ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED
9 #define OPENVDB_MATH_HAS_BEEN_INCLUDED
22 #include <type_traits>
29 #if defined(__INTEL_COMPILER)
30 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
31 _Pragma("warning (push)") \
32 _Pragma("warning (disable:1572)")
33 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
34 _Pragma("warning (pop)")
35 #elif defined(__clang__)
36 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
37 PRAGMA(clang diagnostic push) \
38 PRAGMA(clang diagnostic ignored "-Wfloat-equal")
39 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
40 PRAGMA(clang diagnostic pop)
49 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
50 #define OPENVDB_NO_FP_EQUALITY_WARNING_END
57 #define OPENVDB_IS_POD(Type) \
58 static_assert(std::is_standard_layout<Type>::value, \
59 #Type" must be a POD type (satisfy StandardLayoutType.)"); \
60 static_assert(std::is_trivial<Type>::value, \
61 #Type" must be a POD type (satisfy TrivialType.)");
71 template<
typename T>
inline constexpr
T zeroVal() {
return T(0); }
73 template<>
inline std::string zeroVal<std::string>() {
return ""; }
93 inline std::string
operator+(
const std::string&
s,
bool) {
return s; }
94 inline std::string
operator+(
const std::string&
s,
int) {
return s; }
95 inline std::string
operator+(
const std::string&
s,
float) {
return s; }
96 inline std::string
operator+(
const std::string&
s,
double) {
return s; }
100 template<
typename Type1,
typename Type2>
109 template<
typename Type1,
typename Type2>
118 template<
typename Type1,
typename Type2>
130 template <
typename T>
inline constexpr
T pi() {
return 3.141592653589793238462643383279502884e+00; }
131 template <>
inline constexpr
float pi() {
return 3.141592653589793238462643383279502884e+00F; }
132 template <>
inline constexpr
double pi() {
return 3.141592653589793238462643383279502884e+00; }
133 template <>
inline constexpr
long double pi() {
return 3.141592653589793238462643383279502884e+00L; }
142 #if defined(_MSC_VER)
143 #pragma warning(push)
144 #pragma warning(disable:4146)
147 #if defined(_MSC_VER)
167 template<
typename T>
struct Delta {
static T value() {
return zeroVal<T>(); } };
170 template<>
struct Delta<double> {
static double value() {
return 1e-9; } };
178 template<
typename FloatType =
double,
typename EngineType = std::mt19937>
183 std::uniform_real_distribution<FloatType> mRand;
194 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
199 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
203 const EngineType&
engine()
const {
return mEngine; }
214 template<
typename IntType =
int,
typename EngineType = std::mt19937>
218 using Distr = std::uniform_int_distribution<IntType>;
228 mRand(std::
min(imin, imax), std::
max(imin, imax))
234 RandInt(
unsigned int seed, IntType imin, IntType imax):
235 mEngine(static_cast<typename EngineType::result_type>(seed)),
236 mRand(std::
min(imin, imax), std::
max(imin, imax))
248 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
252 const EngineType&
engine()
const {
return mEngine; }
262 return mRand(mEngine,
typename Distr::param_type(lo, hi));
272 template<
typename Type>
277 return x > min ? x < max ? x : max :
min;
282 template<
typename Type>
288 template<
typename Type>
292 if (x >=
Type(0) && x <=
Type(1))
return false;
298 template<
typename Type>
302 return x > 0 ? x < 1 ? (3-2*
x)*x*x :
Type(1) :
Type(0);
307 template<
typename Type>
322 inline int64_t
Abs(int64_t i)
324 static_assert(
sizeof(decltype(
std::abs(i))) ==
sizeof(int64_t),
325 "std::abs(int64) broken");
328 inline float Abs(
float x) {
return std::fabs(x); }
329 inline double Abs(
double x) {
return std::fabs(x); }
330 inline long double Abs(
long double x) {
return std::fabs(x); }
331 inline uint32_t
Abs(uint32_t i) {
return i; }
332 inline uint64_t
Abs(uint64_t i) {
return i; }
333 inline bool Abs(
bool b) {
return b; }
335 template <
typename T>
348 template<
typename Type>
353 return x == zeroVal<Type>();
360 template<
typename Type>
365 return !(x > tolerance) && !(x < -tolerance);
369 template<
typename Type>
373 return !(x > tolerance) && !(x < -tolerance);
378 template<
typename Type>
416 isNan(
const float x) {
return std::isnan(x); }
425 isNan(
const Type&
x) {
return std::isnan(static_cast<double>(x)); }
429 template<
typename Type>
438 template<
typename Type>
446 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
447 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
448 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
457 template<typename
Type>
461 return (b - a < tolerance);
466 template<
typename T0,
typename T1>
476 template<
typename Type>
482 if (!(
Abs(a - b) > absTol))
return true;
489 relError =
Abs((a - b) / b);
491 relError =
Abs((a - b) / a);
493 return (relError <= relTol);
507 static_assert(
sizeof(int32_t) ==
sizeof f,
"`float` has an unexpected size.");
509 std::memcpy(&ret, &f,
sizeof(int32_t));
517 static_assert(
sizeof(int64_t) ==
sizeof d,
"`double` has an unexpected size.");
519 std::memcpy(&ret, &d,
sizeof(int64_t));
528 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
533 longLeft = INT64_C(0x8000000000000000) - longLeft;
539 longRight = INT64_C(0x8000000000000000) - longRight;
542 int64_t difference =
Abs(longLeft - longRight);
543 return (difference <= aUnitsInLastPlace);
547 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
552 intLeft = 0x80000000 - intLeft;
558 intRight = 0x80000000 - intRight;
561 int32_t difference =
Abs(intLeft - intRight);
562 return (difference <= aUnitsInLastPlace);
572 template<
typename Type>
576 template<
typename Type>
580 template<
typename Type>
584 template<
typename Type>
593 while (n--) ans *=
x;
618 OPENVDB_ASSERT( b >= 0.0 &&
"Pow(double,double): base is negative" );
631 return a < b ? b :
a;
634 template<
typename Type>
644 template<
typename Type>
652 template<
typename Type>
660 template<
typename Type>
668 template<
typename Type>
676 template<
typename Type>
684 template<
typename Type>
693 template<
typename Type>
709 return b < a ? b :
a;
712 template<
typename Type>
722 template<
typename Type>
727 template<
typename Type>
735 template<
typename Type>
743 template<
typename Type>
751 template<
typename Type>
759 template<
typename Type>
768 template<
typename Type>
780 template<
typename Type>
787 inline float Sin(
const float&
x) {
return std::sin(x); }
789 inline double Sin(
const double&
x) {
return std::sin(x); }
796 inline float Cos(
const float&
x) {
return std::cos(x); }
798 inline double Cos(
const double&
x) {
return std::cos(x); }
806 template <
typename Type>
812 template <
typename Type>
816 return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
822 template <
typename Type>
848 inline int Mod(
int x,
int y) {
return (x % y); }
849 inline float Mod(
float x,
float y) {
return std::fmod(x, y); }
850 inline double Mod(
double x,
double y) {
return std::fmod(x, y); }
851 inline long double Mod(
long double x,
long double y) {
return std::fmod(x, y); }
863 template<
typename Type>
868 return remainder ? x-remainder+base :
x;
879 template<
typename Type>
884 return remainder ? x-remainder :
x;
898 template<
typename Type>
904 template<
typename Type>
912 template<
typename Type>
934 template<
typename Type>
939 template<
typename Type>
943 Type tenth =
static_cast<Type>(
Pow(
size_t(10), digits));
963 template<
typename Type>
990 template <
typename S,
typename T,
typename = std::enable_if_t<openvdb::is_arithmetic_v<S>&& openvdb::is_arithmetic_v<T>>>
992 using type =
typename std::common_type_t<S,T>;
1000 template<
typename Vec3T>
1005 for (
size_t i = 1; i < 3; ++i) {
1007 if (v[i] <= v[r]) r = i;
1016 template<
typename Vec3T>
1021 for (
size_t i = 1; i < 3; ++i) {
1023 if (v[i] >= v[r]) r = i;
1032 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
Type Inv(Type x)
Return the inverse of x.
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
int Ceil(float x)
Return the ceiling of x.
static math::half value()
Simple generator of random numbers over the range [0, 1)
bool cwiseGreaterThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
static math::half value()
Type Pow(Type x, int n)
Return xn.
Type IntegerPart(Type x)
Return the integer part of x.
bool ZeroCrossing(const Type &a, const Type &b)
Return true if the interval [a, b] includes zero, i.e., if either a or b is zero or if they have diff...
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
imath_half_bits_t half
if we're in a C-only context, alias the half bits type to half
Type Pow2(Type x)
Return x2.
IMATH_HOSTDEVICE constexpr int floor(T x) IMATH_NOEXCEPT
T negative(const T &val)
Return the unary negation of the given value.
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Type FractionalPart(Type x)
Return the fractional part of x.
GLsizei const GLfloat * value
Mat3< Type1 > cwiseAdd(const Mat3< Type1 > &m, const Type2 s)
vfloat4 sqrt(const vfloat4 &a)
GLboolean GLboolean GLboolean GLboolean a
#define OPENVDB_USE_VERSION_NAMESPACE
const EngineType & engine() const
Return a const reference to the random number generator.
float Cos(const float &x)
Return cos x.
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Rand01(unsigned int seed)
Initialize the generator.
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
float RoundDown(float x)
Return x rounded down to the nearest integer.
RandInt(const EngineType &engine, IntType imin, IntType imax)
Initialize the generator.
bool isNegative(const Type &x)
Return true if x is less than zero.
Tolerance for floating-point comparison.
float RoundUp(float x)
Return x rounded up to the nearest integer.
float Cbrt(float x)
Return the cube root of a floating-point value.
Simple random integer generator.
#define OPENVDB_ASSERT(X)
ImageBuf OIIO_API pow(const ImageBuf &A, cspan< float > B, ROI roi={}, int nthreads=0)
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
bool isNegative< bool >(const bool &)
constexpr bool isInfinity() const noexcept
Return true if a positive or a negative infinity.
GLint GLint GLsizei GLint GLenum GLenum type
const math::half & max_impl(const math::half &a, const math::half &b)
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Coord Abs(const Coord &xyz)
float Round(float x)
Return x rounded to the nearest integer.
bool isInfinite(const float x)
Return true if x is an infinity value (either positive infinity or negative infinity).
Type Pow4(Type x)
Return x4.
Delta for small floating-point offsets.
float Sqrt(float x)
Return the square root of a floating-point value.
constexpr bool is_arithmetic_v
bool isNan(const float x)
Return true if x is a NaN (Not-A-Number) value.
Type Pow3(Type x)
Return x3.
bool cwiseLessThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
bool isUlpsEqual(const double aLeft, const double aRight, const int64_t aUnitsInLastPlace)
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
GLboolean GLboolean GLboolean b
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
GLfloat GLfloat GLfloat GLfloat h
IMATH_HOSTDEVICE constexpr int ceil(T x) IMATH_NOEXCEPT
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
Library and file format version numbers.
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
int32_t floatToInt32(const float f)
int64_t doubleToInt64(const double d)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
constexpr bool isNan() const noexcept
Return true if NAN.
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
const EngineType & engine() const
Return a const reference to the random number generator.
Type Chop(Type x, Type delta)
Return x if it is greater or equal in magnitude than delta. Otherwise, return zero.
constexpr T pi()
Pi constant taken from Boost to match old behaviour.
Type EuclideanRemainder(Type x)
Vec3< typename promote< T, typename Coord::ValueType >::type > operator+(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be added to or subtracted from a Vec3.
int Floor(float x)
Return the floor of x.
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE constexpr T abs(T a) IMATH_NOEXCEPT
constexpr bool zeroVal< bool >()
Return the bool value that corresponds to zero.
IntType operator()()
Return a randomly-generated integer in the current range.
float Sin(const float &x)
Return sin x.
const math::half & min_impl(const math::half &a, const math::half &b)
Rand01(const EngineType &engine)
Initialize the generator.
Type SmoothUnitStep(Type x)
Return 0 if x < 0, 1 if x > 1 or else (3 − 2 x) x .
constexpr bool isFinite() const noexcept
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Type Remainder(Type x, Type y)
Return the remainder of x / y.
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
int Mod(int x, int y)
Return the remainder of x / y.
auto PrintCast(const T &val) -> typename std::enable_if<!std::is_same< T, int8_t >::value &&!std::is_same< T, uint8_t >::value, const T & >::type
8-bit integer values print to std::ostreams as characters. Cast them so that they print as integers i...
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
bool isFinite(const float x)
Return true if x is finite.
Type Exp(const Type &x)
Return ex.
IntType operator()(IntType imin, IntType imax)
Return a randomly-generated integer in the new range [imin, imax], without changing the current range...
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Type Clamp01(Type x)
Return x clamped to [0, 1].
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
bool isApproxLarger(const Type &a, const Type &b, const Type &tolerance)
Return true if a is larger than b to within the given tolerance, i.e., if b - a < tolerance...