Skip to content

Commit

Permalink
inline MathUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Oct 30, 2024
1 parent 31d7b7c commit 5348e7b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 69 deletions.
55 changes: 2 additions & 53 deletions native/cocos/editor-support/spine/MathUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
#include "SpinePluginPrivatePCH.h"
#endif

#include <math.h>
#include <spine/MathUtil.h>
#include <stdlib.h>
#include <string.h>

// Required for division by 0 in _isNaN on MSVC
#ifdef _MSC_VER
Expand All @@ -46,11 +46,6 @@ RTTI_IMPL_NOPARENT(Interpolation)
RTTI_IMPL(PowInterpolation, Interpolation)
RTTI_IMPL(PowOutInterpolation, Interpolation)

const float MathUtil::Pi = 3.1415926535897932385f;
const float MathUtil::Pi_2 = 3.1415926535897932385f * 2;
const float MathUtil::Deg_Rad = (3.1415926535897932385f / 180.0f);
const float MathUtil::Rad_Deg = (180.0f / 3.1415926535897932385f);

float MathUtil::abs(float v) {
return ((v) < 0 ? -(v) : (v));
}
Expand All @@ -64,46 +59,8 @@ float MathUtil::clamp(float x, float min, float max) {
return ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)));
}

float MathUtil::fmod(float a, float b) {
return (float)::fmod(a, b);
}

/// Returns atan2 in radians, faster but less accurate than Math.Atan2. Average error of 0.00231 radians (0.1323
/// degrees), largest error of 0.00488 radians (0.2796 degrees).
float MathUtil::atan2(float y, float x) {
return (float)::atan2(y, x);
}

/// Returns the cosine in radians from a lookup table.
float MathUtil::cos(float radians) {
return (float)::cos(radians);
}

/// Returns the sine in radians from a lookup table.
float MathUtil::sin(float radians) {
return (float)::sin(radians);
}

float MathUtil::sqrt(float v) {
return (float)::sqrt(v);
}

float MathUtil::acos(float v) {
return (float)::acos(v);
}

/// Returns the sine in radians from a lookup table.
float MathUtil::sinDeg(float degrees) {
return (float)::sin(degrees * MathUtil::Deg_Rad);
}

/// Returns the cosine in radians from a lookup table.
float MathUtil::cosDeg(float degrees) {
return (float)::cos(degrees * MathUtil::Deg_Rad);
}

/* Need to pass 0 as an argument, so VC++ doesn't error with C2124 */
static bool _isNan(float value, float zero) {
static inline bool _isNan(float value, float zero) {
float _nan = (float)0.0 / zero;
return 0 == memcmp((void *)&value, (void *)&_nan, sizeof(value));
}
Expand All @@ -112,14 +69,6 @@ bool MathUtil::isNan(float v) {
return _isNan(v, 0);
}

float MathUtil::random() {
return ::rand() / (float)RAND_MAX;
}

float MathUtil::randomTriangular(float min, float max) {
return randomTriangular(min, max, (min + max) * 0.5f);
}

float MathUtil::randomTriangular(float min, float max, float mode) {
float u = random();
float d = max - min;
Expand Down
52 changes: 36 additions & 16 deletions native/cocos/editor-support/spine/MathUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@

#include <spine/SpineObject.h>
#include <spine/RTTI.h>
#include <string.h>
#include <math.h>

namespace spine {

class SP_API MathUtil : public SpineObject {
class SP_API MathUtil {
private:
MathUtil();

public:
static const float Pi;
static const float Pi_2;
static const float Deg_Rad;
static const float Rad_Deg;
static constexpr float Pi = 3.1415926535897932385f;
static constexpr float Pi_2 = 3.1415926535897932385f * 2;
static constexpr float Deg_Rad = (3.1415926535897932385f / 180.0f);
static constexpr float Rad_Deg = (180.0f / 3.1415926535897932385f);

template <typename T>
static inline T min(T a, T b) { return a < b ? a : b; }
Expand All @@ -59,32 +59,52 @@ class SP_API MathUtil : public SpineObject {
static float abs(float v);

/// Returns the sine in radians from a lookup table.
static float sin(float radians);
static inline float sin(float radians) {
return ::sin(radians);
}

/// Returns the cosine in radians from a lookup table.
static float cos(float radians);
static inline float cos(float radians) {
return ::cos(radians);
}

/// Returns the sine in radians from a lookup table.
static float sinDeg(float degrees);
static inline float sinDeg(float degrees) {
return ::sin(degrees * MathUtil::Deg_Rad);
}

/// Returns the cosine in radians from a lookup table.
static float cosDeg(float degrees);
static inline float cosDeg(float degrees) {
return ::cos(degrees * MathUtil::Deg_Rad);
}

/// Returns atan2 in radians, faster but less accurate than Math.Atan2. Average error of 0.00231 radians (0.1323
/// degrees), largest error of 0.00488 radians (0.2796 degrees).
static float atan2(float y, float x);
static inline float atan2(float y, float x) {
return ::atan2(y, x);
}

static float acos(float v);
static inline float acos(float v) {
return ::acos(v);
}

static float sqrt(float v);
static inline float sqrt(float v) {
return ::sqrt(v);
}

static float fmod(float a, float b);
static inline float fmod(float a, float b) {
return ::fmod(a, b);
}

static bool isNan(float v);

static float random();
static inline float random() {
return ::rand() / static_cast<float>(RAND_MAX);
}

static float randomTriangular(float min, float max);
static inline float randomTriangular(float min, float max) {
return randomTriangular(min, max, (min + max) * 0.5f);
}

static float randomTriangular(float min, float max, float mode);

Expand Down

0 comments on commit 5348e7b

Please sign in to comment.