5 #include "ParaMathUtility.h" 7 #if !defined(WIN32) && !defined(INT64_C) 8 # define INT64_C(c) static_cast<long long>(c ## LL) 9 # define UINT64_C(c) static_cast<unsigned long long>(c ## ULL) 35 static const float POS_INFINITY;
36 static const float NEG_INFINITY;
37 static const float PI;
38 static const float TWO_PI;
39 static const float HALF_PI;
40 static const float fDeg2Rad;
41 static const float fRad2Deg;
52 static float* mSinTable;
53 static float* mTanTable;
59 static float SinTable (
float fValue);
60 static float TanTable (
float fValue);
67 Math(
unsigned int trigTableSize = 4096);
73 static inline int IAbs (
int iValue) {
return ( iValue >= 0 ? iValue : -iValue ); }
74 static inline int ICeil (
float fValue) {
return int(ceil(fValue)); }
75 static inline int IFloor (
float fValue) {
return int(floor(fValue)); }
76 static int ISign (
int iValue);
78 static inline float Abs (
float fValue) {
return float(fabs(fValue)); }
79 static inline double Abs(
double fValue) {
return fabs(fValue); }
80 static inline int Abs(
int nValue) {
return nValue >= 0 ? nValue : -nValue; }
81 static inline Degree Abs (
const Degree& dValue) {
return Degree(fabs(dValue.valueDegrees())); }
82 static inline Radian Abs (
const Radian& rValue) {
return Radian(fabs(rValue.valueRadians())); }
83 static Radian ACos (
float fValue);
84 static Radian ASin (
float fValue);
85 static inline Radian ATan (
float fValue) {
return Radian(atan(fValue)); }
86 static inline Radian ATan2 (
float fY,
float fX) {
return Radian(atan2(fY,fX)); }
87 static inline float Ceil (
float fValue) {
return float(ceil(fValue)); }
96 static inline float Cos (
const Radian& fValue,
bool useTables =
false) {
97 return (!useTables) ? float(cos(fValue.valueRadians())) : SinTable(fValue.valueRadians() + HALF_PI);
106 static inline float Cos (
float fValue,
bool useTables =
false) {
107 return (!useTables) ? float(cos(fValue)) : SinTable(fValue + HALF_PI);
110 static inline float Exp (
float fValue) {
return float(exp(fValue)); }
112 static inline float Floor (
float fValue) {
return float(floor(fValue)); }
114 static inline float Log (
float fValue) {
return float(log(fValue)); }
116 static inline float Pow (
float fBase,
float fExponent) {
return float(pow(fBase,fExponent)); }
118 static float Sign (
float fValue);
121 return Radian(Sign(rValue.valueRadians()));
125 return Degree(Sign(dValue.valueDegrees()));
135 static inline float Sin (
const Radian& fValue,
bool useTables =
false) {
136 return (!useTables) ? float(sin(fValue.valueRadians())) : SinTable(fValue.valueRadians());
145 static inline float Sin (
float fValue,
bool useTables =
false) {
146 return (!useTables) ? float(sin(fValue)) : SinTable(fValue);
149 static inline float Sqr (
float fValue) {
return fValue*fValue; }
150 static inline float Sqrt (
float fValue) {
return float(sqrt(fValue)); }
152 static inline double Sqr(
double fValue) {
return fValue*fValue; }
153 static inline double Sqrt(
double fValue) {
return sqrt(fValue); }
155 static inline Radian Sqrt (
const Radian& fValue) {
return Radian(sqrt(fValue.valueRadians())); }
157 static inline Degree Sqrt (
const Degree& fValue) {
return Degree(sqrt(fValue.valueDegrees())); }
162 static float InvSqrt(
float fValue);
164 static float UnitRandom ();
166 static float RangeRandom (
float fLow,
float fHigh);
168 static float SymmetricRandom ();
177 static inline float Tan (
const Radian& fValue,
bool useTables =
false) {
178 return (!useTables) ? float(tan(fValue.valueRadians())) : TanTable(fValue.valueRadians());
187 static inline float Tan (
float fValue,
bool useTables =
false) {
188 return (!useTables) ? float(tan(fValue)) : TanTable(fValue);
191 static inline float DegreesToRadians(
float degrees) {
return degrees * fDeg2Rad; }
192 static inline float RadiansToDegrees(
float radians) {
return radians * fRad2Deg; }
268 bool discardInside =
true);
296 float* d1,
float* d2);
324 bool positiveSide =
true,
bool negativeSide =
true);
348 bool positiveSide =
true,
bool negativeSide =
true);
362 const Ray& ray,
const std::vector<Plane>& planeList,
363 bool normalIsOutside);
370 const Ray& ray,
const std::list<Plane>& planeList,
371 bool normalIsOutside);
381 float tolerance = std::numeric_limits<float>::epsilon());
386 float u1,
float v1,
float u2,
float v2,
float u3,
float v3);
408 return X < Min ? Min : X < Max ? X :
Max;
412 const Matrix4* reflectMatrix = 0);
417 static inline bool IsEquivalent(
float f1,
float f2) {
return f1 - f2 < FLT_TOLERANCE && f2 - f1 < FLT_TOLERANCE; }
423 static inline bool IsEquivalent(
float f1,
float f2,
float tolerance) {
return f1 - f2 < tolerance && f2 - f1 < tolerance; }
428 static inline bool IsEquivalent(
double f1,
double f2) {
return f1 - f2 < DBL_TOLERANCE && f2 - f1 < DBL_TOLERANCE; }
434 static inline bool IsEquivalent(
double f1,
double f2,
double tolerance) {
return f1 - f2 < tolerance && f2 - f1 < tolerance; }
443 static inline bool IsEquivalent(
const T* al,
size_t acount,
const T* bl,
size_t bcount)
445 if (acount != bcount)
return false;
446 bool equivalent =
true;
447 for (
size_t i = 0; i < acount && equivalent; ++i)
455 static int log2 (
unsigned int x);
462 static inline T
Max(
const T A,
const T B)
464 return (A >= B) ? A : B;
468 static inline T
Min(
const T A,
const T B)
470 return (A <= B) ? A : B;
489 return (
float)
Trunc(F);
503 static inline float GridSnap(
float Location,
float Grid)
505 if (Grid == 0.f)
return Location;
508 return Floor((Location + 0.5f*Grid) / Grid)*Grid;
517 return (Dividend + Divisor - 1) / Divisor;
522 static inline T
Max3(
const T A,
const T B,
const T C)
529 static inline T
Min3(
const T A,
const T B,
const T C)
535 template<
class T,
class U >
536 static inline T
Lerp(
const T& A,
const T& B,
const U& Alpha)
538 return (T)(A + Alpha * (B - A));
542 static inline int32
Rand() {
return rand(); }
545 static inline void RandInit(int32 Seed) { srand(Seed); }
548 static inline float FRand() {
return rand() / (float)RAND_MAX; }
554 static float SRand();
570 static inline float FloatSelect(
float Comparand,
float ValueGEZero,
float ValueLTZero)
572 return Comparand >= 0.f ? ValueGEZero : ValueLTZero;
576 static bool SmoothMoveFloat1(
float& src,
const float dest,
const float fMaxStep);
579 static bool SmoothMoveAngle1(
float& src,
const float dest,
const float fMaxStep);
597 static bool MatchString(
const std::string& matchPattern,
const std::string& matchStr);
609 static inline int Round(
float fValue){
610 return int(fValue > 0.0f ? fValue + 0.5f : fValue - 0.5f);
622 return (a.x == b.x && a.z == b.z);
627 static bool CompareXZ(
const T& a,
const T& b,
float epsilon)
629 return (fabs(a.x - b.x) + fabs(a.z - b.z)) < epsilon;
632 static inline bool FuzzyCompare(
double p1,
double p2)
634 return (Math::Abs(p1 - p2) * 1000000000000. <=
Math::Min(Math::Abs(p1), Math::Abs(p2)));
637 static bool FuzzyCompare(
float p1,
float p2)
639 return (Math::Abs(p1 - p2) * 100000.f <=
Math::Min(Math::Abs(p1), Math::Abs(p2)));
642 static inline bool FuzzyIsNull(
double d)
644 return Math::Abs(d) <= 0.000000000001;
647 static inline bool FuzzyIsNull(
float f)
649 return Math::Abs(f) <= 0.00001f;
652 static inline bool IsNull(
double d)
660 return (val.u & UINT64_C(0x7fffffffffffffff)) == 0;
668 static bool IsNull(
float f)
676 return (val.u & 0x7fffffff) == 0;
683 return value != value;
690 return std::numeric_limits<T>::has_infinity &&
691 value == std::numeric_limits<T>::infinity();
static bool IsEquivalent(float f1, float f2, float tolerance)
Returns whether two floating-point values are equivalent within a given tolerance.
Definition: ParaMath.h:423
static float RadiansToAngleUnits(float radians)
Convert from radians to the current AngleUnit .
Definition: ParaMath.cpp:184
static int Round(float fValue)
get the closest integer near the specified float number.
Definition: ParaMath.h:609
static float GridSnap(float Location, float Grid)
Snaps a value to the nearest grid multiple.
Definition: ParaMath.h:503
Wrapper class which indicates a given angle value is in Radians.
Definition: ParaAngle.h:10
static Matrix4 buildReflectionMatrix(const Plane &p)
Build a reflection matrix for the passed in plane.
Definition: ParaMath.cpp:893
static float Sin(float fValue, bool useTables=false)
Sine function.
Definition: ParaMath.h:145
3-dimensional vector with double precision.
Definition: ParaDVector3.h:17
static float Sin(const Radian &fValue, bool useTables=false)
Sine function.
Definition: ParaMath.h:135
static int log2(unsigned int x)
this is a fast version of log2.
Definition: ParaMath.cpp:978
static bool CompareXZ(const T &a, const T &b)
return true if equal.
Definition: ParaMath.h:620
4-dimensional homogeneous vector.
Definition: ParaVector4.h:10
static bool IsEquivalent(double f1, double f2, double tolerance)
Returns whether two double-sized floating-point values are equivalent within a given tolerance...
Definition: ParaMath.h:434
static T Min3(const T A, const T B, const T C)
Returns lowest of 3 values.
Definition: ParaMath.h:529
static bool IsEquivalent(double f1, double f2)
Returns whether two double-sized floating-point values are equivalent.
Definition: ParaMath.h:428
static Vector3 calculateBasicFaceNormalWithoutNormalize(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
Calculate a face normal without normalize, no w-information.
Definition: ParaMath.cpp:923
static Vector3 calculateBasicFaceNormal(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
Calculate a face normal, no w-information.
Definition: ParaMath.cpp:909
static float FloatSelect(float Comparand, float ValueGEZero, float ValueLTZero)
Returns value based on comparand.
Definition: ParaMath.h:570
different physics engine has different winding order.
Definition: EventBinding.h:32
static int mTrigTableSize
Size of the trig tables as determined by constructor.
Definition: ParaMath.h:48
static std::pair< bool, float > intersects(const Ray &ray, const Plane &plane)
Ray / plane intersection, returns boolean result and distance.
Definition: ParaMath.cpp:316
Math(unsigned int trigTableSize=4096)
Default constructor.
Definition: ParaMath.cpp:37
static int NextPowerOf2(int x)
get nearest power of two of x
Definition: ParaMath.cpp:1304
static T Clamp(const T X, const T Min, const T Max)
Clamps X to be between Min and Max, inclusive.
Definition: ParaMath.h:406
A sphere primitive, mostly used for bounds checking.
Definition: ParaSphere.h:46
static float mTrigTableFactor
Radian -> index factor value ( mTrigTableSize / 2 * PI )
Definition: ParaMath.h:51
static bool RealEqual(float a, float b, float tolerance=std::numeric_limits< float >::epsilon())
Compare 2 reals, using tolerance for inaccuracies.
Definition: ParaMath.cpp:307
static float Tan(const Radian &fValue, bool useTables=false)
Tangent function.
Definition: ParaMath.h:177
static bool CompareXZ(const T &a, const T &b, float epsilon)
return true if equal.
Definition: ParaMath.h:627
Implementation of a Quaternion, i.e.
Definition: ParaQuaternion.h:10
static bool SmoothMoveVec3(Vector3 *result, const Vector3 &vPosTarget, const Vector3 &vPos, FLOAT fIncrease, FLOAT fTolerance)
linearly (smoothly) move vPos to vPosTarget by the amount fIncrease return true if we have reached th...
Definition: ParaMath.cpp:1051
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
static bool pointInTri2D(const Vector2 &p, const Vector2 &a, const Vector2 &b, const Vector2 &c)
Checks whether a given point is inside a triangle, in a 2-dimensional (Cartesian) space...
Definition: ParaMath.cpp:211
Definition: minilua.c:225
Wrapper class which indicates a given angle value is in Degrees.
Definition: ParaAngle.h:56
static bool pointInTri3D(const Vector3 &p, const Vector3 &a, const Vector3 &b, const Vector3 &c, const Vector3 &normal)
Checks whether a given 3D point is inside a triangle.
Definition: ParaMath.cpp:259
static int32 Trunc(float F)
Converts a float to an integer with truncation towards zero.
Definition: ParaMath.h:478
static void CameraRotMatrixYawPitch(Matrix4 &out, float fYaw, float fPitch)
Definition: ParaMath.cpp:1289
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
static void setAngleUnit(AngleUnit unit)
These functions used to set the assumed angle units (radians or degrees) expected when using the Angl...
Definition: ParaMath.cpp:165
static float Cos(float fValue, bool useTables=false)
Cosine function.
Definition: ParaMath.h:106
static int32 Rand()
Returns a random integer between 0 and RAND_MAX, inclusive.
Definition: ParaMath.h:542
Definition: ShapeSphere.h:6
void buildTrigTables()
Private function to build trig tables.
Definition: ParaMath.cpp:58
static bool IsEquivalent(const T *al, size_t acount, const T *bl, size_t bcount)
Returns whether two constant-sized arrays are equivalent.
Definition: ParaMath.h:443
static float Tan(float fValue, bool useTables=false)
Tangent function.
Definition: ParaMath.h:187
static float Fractional(float Value)
Returns the fractional part of a float.
Definition: ParaMath.h:497
Representation of a ray in space, i.e.
Definition: ParaRay.h:41
static float AngleUnitsToDegrees(float units)
Convert from the current AngleUnit to degrees.
Definition: ParaMath.cpp:193
AngleUnit
The angular units used by the API.
Definition: ParaMath.h:29
static bool SmoothMoveFloat1(float &src, const float dest, const float fMaxStep)
change src from src to dest, by a maximum of fMaxStep.
Definition: ParaMath.cpp:1015
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
static float DegreesToAngleUnits(float degrees)
Convert from degrees to the current AngleUnit.
Definition: ParaMath.cpp:202
static T Min(const T A, const T B)
Returns lower value in a generic way.
Definition: ParaMath.h:468
Defines a plane in 3D space.
Definition: ParaPlane.h:23
static float AngleUnitsToRadians(float units)
Convert from the current AngleUnit to radians.
Definition: ParaMath.cpp:175
static float ToStandardAngle(float fAngle)
make sure that the angle is in the range (-Pi,Pi]
Definition: ParaMath.cpp:1094
Definition: enum_maker.hpp:46
static bool IsEquivalent(float f1, float f2)
Returns whether two floating-point values are equivalent within a given tolerance.
Definition: ParaMath.h:417
static bool is_nan(T value)
whether value is not a number, T is usually float or double
Definition: ParaMath.h:681
static float Cos(const Radian &fValue, bool useTables=false)
Cosine function.
Definition: ParaMath.h:96
static T DivideAndRoundUp(T Dividend, T Divisor)
Divides two integers and rounds up.
Definition: ParaMath.h:515
static bool MatchString(const std::string &matchPattern, const std::string &matchStr)
check if the matchStr matches the string pattern in matchPattern
Definition: ParaMath.cpp:1116
~Math()
Default destructor.
Definition: ParaMath.cpp:51
static float SRand()
Returns a seeded random float in the range [0,1), using the seed from SRandInit().
Definition: ParaMath.cpp:1003
static float InvSqrt(float fValue)
Inverse square root i.e.
Definition: ParaMath.cpp:142
static AngleUnit getAngleUnit(void)
Get the unit being used for angles.
Definition: ParaMath.cpp:170
static Vector3 calculateTangentSpaceVector(const Vector3 &position1, const Vector3 &position2, const Vector3 &position3, float u1, float v1, float u2, float v2, float u3, float v3)
Calculates the tangent space vector for a given set of positions / texture coords.
Definition: ParaMath.cpp:855
static bool is_infinity(T value)
whether value is infinity, T is usually float or double
Definition: ParaMath.h:688
static bool ComputeFacingTarget(const Vector3 &target, const Vector3 &source, FLOAT &fFacing)
facing target in xz plane fFacing: [out] it is in the range [0, 2PI] return true if target!=source(a...
Definition: ParaMath.cpp:1142
static bool SmoothMoveAngle1(float &src, const float dest, const float fMaxStep)
change src from src to dest, by a maximum of fMaxStep.
Definition: ParaMath.cpp:1032
static float MinVec3(const Vector3 &v)
Returns MIN(x, y, z);.
Definition: ParaMath.cpp:1200
static T Lerp(const T &A, const T &B, const U &Alpha)
Performs a linear interpolation between two values, Alpha ranges from 0-1.
Definition: ParaMath.h:536
A 3D box aligned with the x/y/z axes.
Definition: ParaAxisAlignedBox.h:16
static Vector4 calculateFaceNormal(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
Calculate a face normal, including the w component which is the offset from the origin.
Definition: ParaMath.cpp:902
static float gaussianDistribution(float x, float offset=0.0f, float scale=1.0f)
Generates a value based on the Gaussian (normal) distribution function with the given offset and scal...
Definition: ParaMath.cpp:929
static float FRand()
Returns a random float between 0 and 1, inclusive.
Definition: ParaMath.h:548
static T Max3(const T A, const T B, const T C)
Returns highest of 3 values.
Definition: ParaMath.h:522
static void SRandInit(int32 Seed)
Seeds future calls to SRand()
Definition: ParaMath.cpp:998
static Vector4 calculateFaceNormalWithoutNormalize(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
Calculate a face normal without normalize, including the w component which is the offset from the ori...
Definition: ParaMath.cpp:916
static Matrix4 * CreateBillboardMatrix(Matrix4 *pOut, const Matrix4 *matModelview, const Vector3 *vBillboardPos, bool bAxisAligned=false)
create a billboard matrix
Definition: ParaMath.cpp:1221
standard math lib
Definition: ParaMath.h:21
static void RandInit(int32 Seed)
Seeds global random number functions Rand() and FRand()
Definition: ParaMath.h:545
static int log2_ceil(unsigned int x)
this is a fast version of log2.
Definition: ParaMath.h:458
static float TruncFloat(float F)
Converts a float to an integer value with truncation towards zero.
Definition: ParaMath.h:487
static float MaxVec3(const Vector3 &v)
Returns MAX(x, y, z);.
Definition: ParaMath.cpp:1205
static T Max(const T A, const T B)
Returns higher value in a generic way.
Definition: ParaMath.h:462
static void GetMatrixScaling(const Matrix4 &globalMat, float *fScalingX, float *fScalingY, float *fScalingZ)
Get the scaling factor from globalMat.
Definition: ParaMath.cpp:1210
a min max box.
Definition: ShapeAABB.h:190