Saga3D API Documentation
1.0-RC4
|
Go to the documentation of this file.
5 #ifndef __IRR_MATH_H_INCLUDED__
6 #define __IRR_MATH_H_INCLUDED__
8 #include "SagaConfig.h"
14 #include <glm/vec3.hpp>
15 #include <glm/gtx/norm.hpp>
27 const std::int64_t ROUNDING_ERROR_S64 = 0;
32 #ifdef PI // make sure we don't collide with a define
35 const float PI = 3.14159265359f;
44 #ifdef PI64 // make sure we don't collide with a define
47 const double PI64 = 3.1415926535897932384626433832795028841971693993751;
65 inline bool isBetweenPoints(
const glm::vec3& point,
const glm::vec3& begin,
const glm::vec3& end)
67 const auto f = glm::length2(end - begin);
68 return glm::distance2(point, begin) <= f && glm::distance2(point, end) <= f;
75 const double tmp = (atan2((
double)vec.x, (
double)vec.z) *
RADTODEG64);
83 const double z1 = glm::sqrt(vec.x*vec.x + vec.z*vec.z);
85 angle.x = (atan2((
double)z1, (
double)vec.y) *
RADTODEG64 - 90.0);
104 const double srsp = sr*sp;
105 const double crsp = cr*sp;
107 const double pseudoMatrix[] = {
108 ( cp*cy ), ( cp*sy ), ( -sp ),
109 ( srsp*cy-cr*sy ), ( srsp*sy+cr*cy ), ( sr*cp ),
110 ( crsp*cy+sr*sy ), ( crsp*sy-sr*cy ), ( cr*cp )};
113 (forwards.x * pseudoMatrix[0] +
114 forwards.y * pseudoMatrix[3] +
115 forwards.z * pseudoMatrix[6]),
116 (forwards.x * pseudoMatrix[1] +
117 forwards.y * pseudoMatrix[4] +
118 forwards.z * pseudoMatrix[7]),
119 (forwards.x * pseudoMatrix[2] +
120 forwards.y * pseudoMatrix[5] +
121 forwards.z * pseudoMatrix[8])
163 inline const T&
min_(
const T& a,
const T& b)
165 return a < b ? a : b;
170 inline const T&
min_(
const T& a,
const T& b,
const T& c)
172 return a < b ?
min_(a, c) :
min_(b, c);
177 inline const T&
max_(
const T& a,
const T& b)
179 return a < b ? b : a;
184 inline const T&
max_(
const T& a,
const T& b,
const T& c)
186 return a < b ?
max_(b, c) :
max_(a, c);
193 return a < (T)0 ? -a : a;
199 inline T
lerp(
const T& a,
const T& b,
const float t)
201 return (T)(a*(1.f-t)) + (b*t);
206 inline const T
clamp (
const T& value,
const T& low,
const T& high)
208 return min_ (
max_(value,low), high);
216 template <
class T1,
class T2>
255 return ROUNDING_ERROR_S64;
261 return ROUNDING_ERROR_S64;
285 inline bool equals(
const T a,
const T b,
const T tolerance = roundingError<T>())
287 return (a + tolerance >= b) && (a - tolerance <= b);
294 inline bool equalsRelative(
const T a,
const T b,
const T factor = relativeErrorFactor<T>())
298 const T maxi =
max_(a, b);
299 const T mini =
min_(a, b);
300 const T maxMagnitude =
max_(maxi, -mini);
302 return (maxMagnitude*factor + maxi) == (maxMagnitude*factor + mini);
309 bool sign()
const {
return (
i >> 31) != 0; }
338 int ulpsDiff =
abs_(fa.
i- fb.
i);
339 if (ulpsDiff <= maxUlpDiff)
348 return fabs(a) <= tolerance;
354 return fabsf(a) <= tolerance;
360 return fabsf(a) > tolerance;
364 inline bool iszero(
const std::int32_t a,
const std::int32_t tolerance = 0)
366 return (a & 0x7ffffff) <= tolerance;
370 inline bool iszero(
const std::uint32_t a,
const std::uint32_t tolerance = 0)
372 return a <= tolerance;
376 inline bool iszero(
const std::int64_t a,
const std::int64_t tolerance = 0)
379 return abs_(a) <= tolerance;
383 inline std::int32_t
s32_min(std::int32_t a, std::int32_t b)
385 const std::int32_t mask = (a - b) >> 31;
386 return (a & mask) | (b & ~mask);
389 inline std::int32_t
s32_max(std::int32_t a, std::int32_t b)
391 const std::int32_t mask = (a - b) >> 31;
392 return (b & mask) | (a & ~mask);
395 inline std::int32_t
s32_clamp(std::int32_t value, std::int32_t low, std::int32_t high)
413 typedef union { std::uint32_t
u; std::int32_t s;
float f; }
inttofloat;
415 #define F32_AS_S32(f) (*((std::int32_t *) &(f)))
416 #define F32_AS_U32(f) (*((std::uint32_t *) &(f)))
417 #define F32_AS_U32_POINTER(f) (((std::uint32_t *) &(f)))
419 #define F32_VALUE_0 0x00000000
420 #define F32_VALUE_1 0x3f800000
421 #define F32_SIGN_BIT 0x80000000U
422 #define F32_EXPON_MANTISSA 0x7FFFFFFFU
426 #ifdef IRRLICHT_FAST_MATH
427 #define IR(x) ((std::uint32_t&)(x))
433 #define AIR(x) (IR(x)&0x7fffffff)
436 #ifdef IRRLICHT_FAST_MATH
437 #define FR(x) ((float&)(x))
444 #define IEEE_1_0 0x3f800000
445 #define IEEE_255_0 0x437f0000
448 #ifdef IRRLICHT_FAST_MATH
449 #define F32_LOWER_0(f) (F32_AS_U32(f) > F32_SIGN_BIT)
450 #define F32_LOWER_EQUAL_0(f) (F32_AS_S32(f) <= F32_VALUE_0)
451 #define F32_GREATER_0(f) (F32_AS_S32(f) > F32_VALUE_0)
452 #define F32_GREATER_EQUAL_0(f) (F32_AS_U32(f) <= F32_SIGN_BIT)
453 #define F32_EQUAL_1(f) (F32_AS_U32(f) == F32_VALUE_1)
454 #define F32_EQUAL_0(f) ((F32_AS_U32(f) & F32_EXPON_MANTISSA) == F32_VALUE_0)
457 #define F32_A_GREATER_B(a,b) (F32_AS_S32((a)) > F32_AS_S32((b)))
461 #define F32_LOWER_0(n) ((n) < 0.0f)
462 #define F32_LOWER_EQUAL_0(n) ((n) <= 0.0f)
463 #define F32_GREATER_0(n) ((n) > 0.0f)
464 #define F32_GREATER_EQUAL_0(n) ((n) >= 0.0f)
465 #define F32_EQUAL_1(n) ((n) == 1.0f)
466 #define F32_EQUAL_0(n) ((n) == 0.0f)
467 #define F32_A_GREATER_B(a,b) ((a) > (b))
473 #define REALINLINE __forceinline
475 #define REALINLINE inline
479 #if defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
486 return ((-condition >> 7) & (a ^ b)) ^ b;
492 return (-condition >> 31) & a;
499 return ((-condition >> 31) & (a ^ b)) ^ b;
505 return ((-condition >> 15) & (a ^ b)) ^ b;
511 return (-condition >> 31) & a;
522 state ^= ((-condition >> 31) ^ state) & mask;
527 return floorf(x + 0.5f);
532 #ifdef IRRLICHT_FAST_MATH
535 feclearexcept(FE_ALL_EXCEPT);
536 #elif defined(_MSC_VER)
538 #elif defined(__GNUC__) && defined(__x86__)
539 __asm__ __volatile__ (
"fclex \n\t");
559 return static_cast<std::int32_t
>(
squareroot(
static_cast<float>(f)));
566 return static_cast<std::int64_t
>(
squareroot(
static_cast<double>(f)));
573 return 1.0 / sqrt(x);
579 #if defined (IRRLICHT_FAST_MATH)
580 #if defined(_MSC_VER)
584 __asm rsqrtss xmm0, f
585 __asm movss recsqrt, xmm0
595 return 1.f / sqrtf(f);
597 #else // no fast math
598 return 1.f / sqrtf(f);
611 #if defined (IRRLICHT_FAST_MATH)
617 #if defined(_MSC_VER)
621 __asm mulss xmm1, xmm0
622 __asm mulss xmm1, xmm0
623 __asm addss xmm0, xmm0
624 __asm subss xmm0, xmm1
626 __asm movss rec, xmm0
628 #else // no support yet for other compilers
638 #else // no fast math
653 #if defined(IRRLICHT_FAST_MATH)
659 #if defined(_MSC_VER)
663 __asm mulss xmm1, xmm0
664 __asm mulss xmm1, xmm0
665 __asm addss xmm0, xmm0
666 __asm subss xmm0, xmm1
668 __asm movss rec, xmm0
670 #else // no support yet for other compilers
686 #else // no fast math
694 #ifdef IRRLICHT_FAST_MATH
695 const float h = 0.5f;
699 #if defined(_MSC_VER)
706 #elif defined(__GNUC__)
707 __asm__ __volatile__ (
715 return (std::int32_t) floorf (x);
718 #else // no fast math
719 return (std::int32_t) floorf (x);
726 #ifdef IRRLICHT_FAST_MATH
727 const float h = 0.5f;
731 #if defined(_MSC_VER)
738 #elif defined(__GNUC__)
739 __asm__ __volatile__ (
747 return (std::int32_t) ceilf (x);
750 #else // not fast math
751 return (std::int32_t) ceilf (x);
759 #if defined(IRRLICHT_FAST_MATH)
762 #if defined(_MSC_VER)
768 #elif defined(__GNUC__)
769 __asm__ __volatile__ (
776 return (std::int32_t)
round_(x);
779 #else // no fast math
780 return (std::int32_t)
round_(x);
784 inline float float_max3(
const float a,
const float b,
const float c)
786 return a > b ? (a > c ? a : c) : (b > c ? b : c);
789 inline float float_min3(
const float a,
const float b,
const float c)
791 return a < b ? (a < c ? a : c) : (b < c ? b : c);
796 return x - floorf (x);
802 #ifndef IRRLICHT_FAST_MATH
bool equalsRelative(const T a, const T b, const T factor=relativeErrorFactor< T >())
const double RECIPROCAL_PI64
Constant for 64bit reciprocal of PI.
REALINLINE float reciprocal_approxim(const float f)
std::uint32_t IR(float x)
REALINLINE std::int32_t floor32(float x)
T abs_(const T &a)
returns abs of two values. Own implementation to get rid of STL (VS6 problems)
const float DEGTORAD
32bit Constant for converting from degrees to radians
const double RADTODEG64
64bit constant for converting from radians to degrees
std::int32_t s32_max(std::int32_t a, std::int32_t b)
glm::vec3 getHorizontalAngle(const glm::vec3 &vec)
REALINLINE std::int32_t round32(float x)
REALINLINE float squareroot(const float f)
REALINLINE double reciprocal_squareroot(const double x)
const double PI64
Constant for 64bit PI.
std::int32_t s32_clamp(std::int32_t value, std::int32_t low, std::int32_t high)
bool isnotzero(const float a, const float tolerance=ROUNDING_ERROR_float)
returns if a equals not zero, taking rounding errors into account
REALINLINE std::uint32_t if_c_a_else_b(const std::int32_t condition, const std::uint32_t a, const std::uint32_t b)
conditional set based on mask and arithmetic shift
REALINLINE std::uint32_t if_c_a_else_0(const std::int32_t condition, const std::uint32_t a)
conditional set based on mask and arithmetic shift
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
REALINLINE std::int32_t ceil32(float x)
REALINLINE void clearFPUException()
bool isBetweenPoints(const glm::vec3 &point, const glm::vec3 &begin, const glm::vec3 &end)
const float ROUNDING_ERROR_float
REALINLINE float reciprocal(const float f)
const std::int32_t ROUNDING_ERROR_S32
Rounding error constant often used when comparing float values.
float float_max3(const float a, const float b, const float c)
const double ROUNDING_ERROR_double
float degToRad(float degrees)
Utility function to convert a degrees value to radians.
T lerp(const T &a, const T &b, const float t)
float float_min3(const float a, const float b, const float c)
const double DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
REALINLINE void setbit_cond(std::uint32_t &state, std::int32_t condition, std::uint32_t mask)
const T & max_(const T &a, const T &b)
returns maximum of two values. Own implementation to get rid of the STL (VS6 problems)
const float HALF_PI
Constant for half of PI.
bool equalsByUlp(float a, float b, int maxUlpDiff)
We compare the difference in ULP's (spacing between floating-point numbers, aka ULP=1 means there exi...
FloatIntUnion32(float f1=0.0f)
bool iszero(const double a, const double tolerance=ROUNDING_ERROR_double)
returns if a equals zero, taking rounding errors into account
float radToDeg(float radians)
Utility function to convert a radian value to degrees.
const float RECIPROCAL_PI
Constant for reciprocal of PI.
glm::vec3 rotationToDirection(const glm::vec3 &vec, const glm::vec3 &forwards={0, 0, 1})
const T & min_(const T &a, const T &b)
returns minimum of two values. Own implementation to get rid of the STL (VS6 problems)
const float RADTODEG
32bit constant for converting from radians to degrees (formally known as GRAD_PI)
float FR(std::uint32_t x)
Floating-point representation of an integer value.
std::int32_t s32_min(std::int32_t a, std::int32_t b)
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
const float PI
Constant for PI.
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters