From e1daddebf4538ceb5adc18be08ecd51312acae3d Mon Sep 17 00:00:00 2001 From: badlogic Date: Wed, 6 Jun 2018 16:24:46 +0200 Subject: [PATCH] [cpp] Fixed constants in MathUtil. --- spine-cpp/spine-cpp/include/spine/MathUtil.h | 10 ++--- spine-cpp/spine-cpp/src/spine/Bone.cpp | 28 +++++++------- .../spine-cpp/src/spine/IkConstraint.cpp | 8 ++-- spine-cpp/spine-cpp/src/spine/MathUtil.cpp | 9 ++++- .../spine-cpp/src/spine/PathConstraint.cpp | 10 ++--- .../spine-cpp/src/spine/PointAttachment.cpp | 2 +- .../spine-cpp/src/spine/SkeletonJson.cpp | 2 +- spine-cpp/spine-cpp/src/spine/Skin.cpp | 4 +- .../src/spine/TransformConstraint.cpp | 38 +++++++++---------- .../spine-cpp/src/spine/VertexEffect.cpp | 2 +- 10 files changed, 59 insertions(+), 54 deletions(-) diff --git a/spine-cpp/spine-cpp/include/spine/MathUtil.h b/spine-cpp/spine-cpp/include/spine/MathUtil.h index 31f9a7a83..fc09c2475 100644 --- a/spine-cpp/spine-cpp/include/spine/MathUtil.h +++ b/spine-cpp/spine-cpp/include/spine/MathUtil.h @@ -33,20 +33,20 @@ #include -#include #include namespace spine { -static const float PI = 3.1415926535897932385f; -static const float PI_2 = PI * 2; -static const float DEG_RAD = (PI / 180.0f); -static const float RAD_DEG = (180.0f / PI); class MathUtil : public SpineObject { private: MathUtil(); public: + static const float Pi; + static const float Pi_2; + static const float Deg_Rad; + static const float Rad_Deg; + template static inline T min(T a, T b) { return a < b ? a : b; } diff --git a/spine-cpp/spine-cpp/src/spine/Bone.cpp b/spine-cpp/spine-cpp/src/spine/Bone.cpp index 44d1d07bb..178db7454 100644 --- a/spine-cpp/spine-cpp/src/spine/Bone.cpp +++ b/spine-cpp/spine-cpp/src/spine/Bone.cpp @@ -164,11 +164,11 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float s = MathUtil::abs(pa * pd - pb * pc) / s; pb = pc * s; pd = pa * s; - prx = MathUtil::atan2(pc, pa) * RAD_DEG; + prx = MathUtil::atan2(pc, pa) * MathUtil::Rad_Deg; } else { pa = 0; pc = 0; - prx = 90 - MathUtil::atan2(pd, pb) * RAD_DEG; + prx = 90 - MathUtil::atan2(pd, pb) * MathUtil::Rad_Deg; } rx = rotation + shearX - prx; ry = rotation + shearY - prx + 90; @@ -195,7 +195,7 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float za *= s; zc *= s; s = MathUtil::sqrt(za * za + zc * zc); - r = PI / 2 + MathUtil::atan2(zc, za); + r = MathUtil::Pi / 2 + MathUtil::atan2(zc, za); zb = MathUtil::cos(r) * s; zd = MathUtil::sin(r) * s; la = MathUtil::cosDeg(shearX) * scaleX; @@ -258,14 +258,14 @@ float Bone::worldToLocalRotation(float worldRotation) { float sin = MathUtil::sinDeg(worldRotation); float cos = MathUtil::cosDeg(worldRotation); - return MathUtil::atan2(_a * sin - _c * cos, _d * cos - _b * sin) * RAD_DEG; + return MathUtil::atan2(_a * sin - _c * cos, _d * cos - _b * sin) * MathUtil::Rad_Deg; } float Bone::localToWorldRotation(float localRotation) { float sin = MathUtil::sinDeg(localRotation); float cos = MathUtil::cosDeg(localRotation); - return MathUtil::atan2(cos * _c + sin * _d, cos * _a + sin * _b) * RAD_DEG; + return MathUtil::atan2(cos * _c + sin * _d, cos * _a + sin * _b) * MathUtil::Rad_Deg; } void Bone::rotateWorld(float degrees) { @@ -298,7 +298,7 @@ float Bone::getWorldToLocalRotationX() { float a = _a; float c = _c; - return MathUtil::atan2(pa * c - pc * a, pd * a - pb * c) * RAD_DEG; + return MathUtil::atan2(pa * c - pc * a, pd * a - pb * c) * MathUtil::Rad_Deg; } float Bone::getWorldToLocalRotationY() { @@ -314,7 +314,7 @@ float Bone::getWorldToLocalRotationY() { float b = _b; float d = _d; - return MathUtil::atan2(pa * d - pc * b, pd * b - pb * d) * RAD_DEG; + return MathUtil::atan2(pa * d - pc * b, pd * b - pb * d) * MathUtil::Rad_Deg; } BoneData &Bone::getData() { @@ -494,11 +494,11 @@ void Bone::setWorldY(float inValue) { } float Bone::getWorldRotationX() { - return MathUtil::atan2(_c, _a) * RAD_DEG; + return MathUtil::atan2(_c, _a) * MathUtil::MathUtil::Rad_Deg; } float Bone::getWorldRotationY() { - return MathUtil::atan2(_d, _b) * RAD_DEG; + return MathUtil::atan2(_d, _b) * MathUtil::Rad_Deg; } float Bone::getWorldScaleX() { @@ -522,11 +522,11 @@ void Bone::updateAppliedTransform() { if (!parent) { _ax = _worldX; _ay = _worldY; - _arotation = MathUtil::atan2(_c, _a) * RAD_DEG; + _arotation = MathUtil::atan2(_c, _a) * MathUtil::Rad_Deg; _ascaleX = MathUtil::sqrt(_a * _a + _c * _c); _ascaleY = MathUtil::sqrt(_b * _b + _d * _d); _ashearX = 0; - _ashearY = MathUtil::atan2(_a * _b + _c * _d, _a * _d - _b * _c) * RAD_DEG; + _ashearY = MathUtil::atan2(_a * _b + _c * _d, _a * _d - _b * _c) * MathUtil::Rad_Deg; } else { float pa = parent->_a, pb = parent->_b, pc = parent->_c, pd = parent->_d; float pid = 1 / (pa * pd - pb * pc); @@ -546,13 +546,13 @@ void Bone::updateAppliedTransform() { if (_ascaleX > 0.0001f) { float det = ra * rd - rb * rc; _ascaleY = det / _ascaleX; - _ashearY = MathUtil::atan2(ra * rb + rc * rd, det) * RAD_DEG; - _arotation = MathUtil::atan2(rc, ra) * RAD_DEG; + _ashearY = MathUtil::atan2(ra * rb + rc * rd, det) * MathUtil::Rad_Deg; + _arotation = MathUtil::atan2(rc, ra) * MathUtil::Rad_Deg; } else { _ascaleX = 0; _ascaleY = MathUtil::sqrt(rb * rb + rd * rd); _ashearY = 0; - _arotation = 90 - MathUtil::atan2(rd, rb) * RAD_DEG; + _arotation = 90 - MathUtil::atan2(rd, rb) * MathUtil::Rad_Deg; } } } diff --git a/spine-cpp/spine-cpp/src/spine/IkConstraint.cpp b/spine-cpp/spine-cpp/src/spine/IkConstraint.cpp index 1dc743f76..3302ad5bf 100644 --- a/spine-cpp/spine-cpp/src/spine/IkConstraint.cpp +++ b/spine-cpp/spine-cpp/src/spine/IkConstraint.cpp @@ -52,7 +52,7 @@ void IkConstraint::apply(Bone &bone, float targetX, float targetY, float alpha) x = targetX - p->_worldX, y = targetY - p->_worldY; tx = (x * p->_d - y * p->_b) * id - bone._ax; ty = (y * p->_a - x * p->_c) * id - bone._ay; - rotationIK = MathUtil::atan2(ty, tx) * RAD_DEG - bone._ashearX - bone._arotation; + rotationIK = MathUtil::atan2(ty, tx) * MathUtil::Rad_Deg - bone._ashearX - bone._arotation; if (bone._ascaleX < 0) rotationIK += 180; if (rotationIK > 180) rotationIK -= 360; else if (rotationIK < -180) rotationIK += 360; @@ -148,7 +148,7 @@ void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY } } { - float minAngle = PI, minX = l1 - a, minDist = minX * minX, minY = 0; + float minAngle = MathUtil::Pi, minX = l1 - a, minDist = minX * minX, minY = 0; float maxAngle = 0, maxX = l1 + a, maxDist = maxX * maxX, maxY = 0; c0 = -a * l1 / (aa - bb); if (c0 >= -1 && c0 <= 1) { @@ -181,11 +181,11 @@ void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY break_outer: { float os = MathUtil::atan2(cy, cx) * s2; - a1 = (a1 - os) * RAD_DEG + o1 - parent._arotation; + a1 = (a1 - os) * MathUtil::Rad_Deg + o1 - parent._arotation; if (a1 > 180) a1 -= 360; else if (a1 < -180) a1 += 360; parent.updateWorldTransform(px, py, parent._rotation + a1 * alpha, parent._ascaleX, parent._ascaleY, 0, 0); - a2 = ((a2 + os) * RAD_DEG - child._ashearX) * s2 + o2 - child._arotation; + a2 = ((a2 + os) * MathUtil::Rad_Deg - child._ashearX) * s2 + o2 - child._arotation; if (a2 > 180) a2 -= 360; else if (a2 < -180) a2 += 360; child.updateWorldTransform(cx, cy, child._arotation + a2 * alpha, child._ascaleX, child._ascaleY, diff --git a/spine-cpp/spine-cpp/src/spine/MathUtil.cpp b/spine-cpp/spine-cpp/src/spine/MathUtil.cpp index a13a83822..32ae5702a 100644 --- a/spine-cpp/spine-cpp/src/spine/MathUtil.cpp +++ b/spine-cpp/spine-cpp/src/spine/MathUtil.cpp @@ -39,6 +39,11 @@ using namespace spine; +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)); } @@ -81,12 +86,12 @@ float MathUtil::acos(float v) { /// Returns the sine in radians from a lookup table. float MathUtil::sinDeg(float degrees) { - return (float)::sin(degrees * DEG_RAD); + 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 * DEG_RAD); + return (float)::cos(degrees * MathUtil::Deg_Rad); } /* Need to pass 0 as an argument, so VC++ doesn't error with C2124 */ diff --git a/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp b/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp index f12f123f3..5fe75cce9 100644 --- a/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp +++ b/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp @@ -148,7 +148,7 @@ void PathConstraint::update() { } else { tip = false; Bone &p = _target->getBone(); - offsetRotation *= p.getA() * p.getD() - p.getB() * p.getC() > 0 ? DEG_RAD : -DEG_RAD; + offsetRotation *= p.getA() * p.getD() - p.getB() * p.getC() > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad; } for (size_t i = 0, p = 3; i < boneCount; i++, p += 3) { @@ -194,10 +194,10 @@ void PathConstraint::update() { r += offsetRotation; } - if (r > PI) { - r -= PI_2; - } else if (r < -PI) { - r += PI_2; + if (r > MathUtil::Pi) { + r -= MathUtil::Pi_2; + } else if (r < -MathUtil::Pi) { + r += MathUtil::Pi_2; } r *= rotateMix; diff --git a/spine-cpp/spine-cpp/src/spine/PointAttachment.cpp b/spine-cpp/spine-cpp/src/spine/PointAttachment.cpp index 4da3e0d27..39c76236d 100644 --- a/spine-cpp/spine-cpp/src/spine/PointAttachment.cpp +++ b/spine-cpp/spine-cpp/src/spine/PointAttachment.cpp @@ -55,7 +55,7 @@ float PointAttachment::computeWorldRotation(Bone &bone) { float ix = cos * bone._a + sin * bone._b; float iy = cos * bone._c + sin * bone._d; - return MathUtil::atan2(iy, ix) * RAD_DEG; + return MathUtil::atan2(iy, ix) * MathUtil::Rad_Deg; } float PointAttachment::getX() { diff --git a/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp b/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp index 417ec3586..febbe1c13 100644 --- a/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp +++ b/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp @@ -443,7 +443,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { Json *attachmentMap; for (attachmentMap = attachmentsMap->_child; attachmentMap; attachmentMap = attachmentMap->_next) { - Attachment *attachment; + Attachment *attachment = NULL; const char *skinAttachmentName = attachmentMap->_name; const char *attachmentName = Json::getString(attachmentMap, "name", skinAttachmentName); const char *attachmentPath = Json::getString(attachmentMap, "path", attachmentName); diff --git a/spine-cpp/spine-cpp/src/spine/Skin.cpp b/spine-cpp/spine-cpp/src/spine/Skin.cpp index 89ce5c06b..a1be7d8b8 100644 --- a/spine-cpp/spine-cpp/src/spine/Skin.cpp +++ b/spine-cpp/spine-cpp/src/spine/Skin.cpp @@ -138,8 +138,8 @@ void Skin::attachAll(Skeleton &skeleton, Skin &oldSkin) { Slot *slot = slots[slotIndex]; if (slot->getAttachment() == entry._attachment) { - Attachment *attachment = NULL; - if ((attachment = getAttachment(slotIndex, entry._name))) { + Attachment *attachment = getAttachment(slotIndex, entry._name); + if (attachment) { slot->setAttachment(attachment); } } diff --git a/spine-cpp/spine-cpp/src/spine/TransformConstraint.cpp b/spine-cpp/spine-cpp/src/spine/TransformConstraint.cpp index 6fe032213..28bb5fc26 100644 --- a/spine-cpp/spine-cpp/src/spine/TransformConstraint.cpp +++ b/spine-cpp/spine-cpp/src/spine/TransformConstraint.cpp @@ -140,7 +140,7 @@ void TransformConstraint::applyAbsoluteWorld() { float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix; Bone &target = *_target; float ta = target._a, tb = target._b, tc = target._c, td = target._d; - float degRadReflect = ta * td - tb * tc > 0 ? DEG_RAD : -DEG_RAD; + float degRadReflect = ta * td - tb * tc > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad; float offsetRotation = _data._offsetRotation * degRadReflect, offsetShearY = _data._offsetShearY * degRadReflect; for (size_t i = 0; i < _bones.size(); ++i) { @@ -152,10 +152,10 @@ void TransformConstraint::applyAbsoluteWorld() { if (rotateMix != 0) { float a = bone._a, b = bone._b, c = bone._c, d = bone._d; float r = MathUtil::atan2(tc, ta) - MathUtil::atan2(c, a) + offsetRotation; - if (r > PI) { - r -= PI_2; - } else if (r < -PI) { - r += PI_2; + if (r > MathUtil::Pi) { + r -= MathUtil::Pi_2; + } else if (r < -MathUtil::Pi) { + r += MathUtil::Pi_2; } r *= rotateMix; @@ -197,10 +197,10 @@ void TransformConstraint::applyAbsoluteWorld() { float b = bone._b, d = bone._d; float by = MathUtil::atan2(d, b); float r = MathUtil::atan2(td, tb) - MathUtil::atan2(tc, ta) - (by - MathUtil::atan2(bone._c, bone._a)); - if (r > PI) { - r -= PI_2; - } else if (r < -PI) { - r += PI_2; + if (r > MathUtil::Pi) { + r -= MathUtil::Pi_2; + } else if (r < -MathUtil::Pi) { + r += MathUtil::Pi_2; } r = by + (r + offsetShearY) * shearMix; @@ -220,7 +220,7 @@ void TransformConstraint::applyRelativeWorld() { float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix; Bone &target = *_target; float ta = target._a, tb = target._b, tc = target._c, td = target._d; - float degRadReflect = ta * td - tb * tc > 0 ? DEG_RAD : -DEG_RAD; + float degRadReflect = ta * td - tb * tc > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad; float offsetRotation = _data._offsetRotation * degRadReflect, offsetShearY = _data._offsetShearY * degRadReflect; for (size_t i = 0; i < _bones.size(); ++i) { Bone *item = _bones[i]; @@ -231,10 +231,10 @@ void TransformConstraint::applyRelativeWorld() { if (rotateMix != 0) { float a = bone._a, b = bone._b, c = bone._c, d = bone._d; float r = MathUtil::atan2(tc, ta) + offsetRotation; - if (r > PI) { - r -= PI_2; - } else if (r < -PI) { - r += PI_2; + if (r > MathUtil::Pi) { + r -= MathUtil::Pi_2; + } else if (r < -MathUtil::Pi) { + r += MathUtil::Pi_2; } r *= rotateMix; @@ -266,14 +266,14 @@ void TransformConstraint::applyRelativeWorld() { if (shearMix > 0) { float r = MathUtil::atan2(td, tb) - MathUtil::atan2(tc, ta); - if (r > PI) { - r -= PI_2; - } else if (r < -PI) { - r += PI_2; + if (r > MathUtil::Pi) { + r -= MathUtil::Pi_2; + } else if (r < -MathUtil::Pi) { + r += MathUtil::Pi_2; } float b = bone._b, d = bone._d; - r = MathUtil::atan2(d, b) + (r - PI / 2 + offsetShearY) * shearMix; + r = MathUtil::atan2(d, b) + (r - MathUtil::Pi / 2 + offsetShearY) * shearMix; float s = MathUtil::sqrt(b * b + d * d); bone._b = MathUtil::cos(r) * s; bone._d = MathUtil::sin(r) * s; diff --git a/spine-cpp/spine-cpp/src/spine/VertexEffect.cpp b/spine-cpp/spine-cpp/src/spine/VertexEffect.cpp index c67abef4a..18bf150ab 100644 --- a/spine-cpp/spine-cpp/src/spine/VertexEffect.cpp +++ b/spine-cpp/spine-cpp/src/spine/VertexEffect.cpp @@ -136,7 +136,7 @@ float SwirlVertexEffect::getRadius() { } void SwirlVertexEffect::setAngle(float angle) { - _angle = angle * spine::DEG_RAD; + _angle = angle * MathUtil::Deg_Rad; } float SwirlVertexEffect::getAngle() {