From be7437e0a3fdad103e77f741229460d7c7361d17 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 15 Apr 2024 11:57:26 +0900 Subject: [PATCH] common: code refactoring introduced mathDeg2Rad() and mathRad2Deg() for a common implementation. --- src/common/tvgLines.cpp | 2 +- src/common/tvgMath.h | 12 ++++++++++++ src/loaders/lottie/tvgLottieBuilder.cpp | 10 +++++----- src/loaders/lottie/tvgLottieModel.cpp | 4 ++-- src/loaders/svg/tvgSvgLoader.cpp | 8 ++++---- src/loaders/svg/tvgSvgPath.cpp | 2 +- src/renderer/tvgShape.cpp | 4 ++-- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/common/tvgLines.cpp b/src/common/tvgLines.cpp index e794e47f..cf711937 100644 --- a/src/common/tvgLines.cpp +++ b/src/common/tvgLines.cpp @@ -238,7 +238,7 @@ float bezAngleAt(const Bezier& bz, float t) pt.x *= 3; pt.y *= 3; - return atan2(pt.x, pt.y) * 180.0f / 3.141592f; + return mathRad2Deg(atan2(pt.x, pt.y)); } diff --git a/src/common/tvgMath.h b/src/common/tvgMath.h index 32f4e6b7..60ea1d21 100644 --- a/src/common/tvgMath.h +++ b/src/common/tvgMath.h @@ -44,6 +44,18 @@ bool mathIdentity(const Matrix* m); void mathMultiply(Point* pt, const Matrix* transform); +static inline float mathDeg2Rad(float degree) +{ + return degree * (MATH_PI / 180.0f); +} + + +static inline float mathRad2Deg(float radian) +{ + return radian * (180.0f / MATH_PI); +} + + static inline bool mathZero(float a) { return (fabsf(a) < FLT_EPSILON) ? true : false; diff --git a/src/loaders/lottie/tvgLottieBuilder.cpp b/src/loaders/lottie/tvgLottieBuilder.cpp index 3405bf78..8461481a 100644 --- a/src/loaders/lottie/tvgLottieBuilder.cpp +++ b/src/loaders/lottie/tvgLottieBuilder.cpp @@ -104,7 +104,7 @@ static Shape* _draw(LottieGroup* parent, RenderContext* ctx); static void _rotateX(Matrix* m, float degree) { if (degree == 0.0f) return; - auto radian = degree / 180.0f * MATH_PI; + auto radian = mathDeg2Rad(degree); m->e22 *= cosf(radian); } @@ -112,7 +112,7 @@ static void _rotateX(Matrix* m, float degree) static void _rotateY(Matrix* m, float degree) { if (degree == 0.0f) return; - auto radian = degree / 180.0f * MATH_PI; + auto radian = mathDeg2Rad(degree); m->e11 *= cosf(radian); } @@ -120,7 +120,7 @@ static void _rotateY(Matrix* m, float degree) static void _rotationZ(Matrix* m, float degree) { if (degree == 0.0f) return; - auto radian = degree / 180.0f * MATH_PI; + auto radian = mathDeg2Rad(degree); m->e11 = cosf(radian); m->e12 = -sinf(radian); m->e21 = sinf(radian); @@ -617,7 +617,7 @@ static void _updateStar(LottieGroup* parent, LottiePolyStar* star, Matrix* trans auto innerRoundness = star->innerRoundness(frameNo) * 0.01f; auto outerRoundness = star->outerRoundness(frameNo) * 0.01f; - auto angle = -90.0f * MATH_PI / 180.0f; + auto angle = mathDeg2Rad(-90.0f); auto partialPointRadius = 0.0f; auto anglePerPoint = (2.0f * MATH_PI / ptsCnt); auto halfAnglePerPoint = anglePerPoint * 0.5f; @@ -724,7 +724,7 @@ static void _updatePolygon(LottieGroup* parent, LottiePolyStar* star, Matrix* tr auto radius = star->outerRadius(frameNo); auto roundness = star->outerRoundness(frameNo) * 0.01f; - auto angle = -90.0f * MATH_PI / 180.0f; + auto angle = mathDeg2Rad(-90.0f); auto anglePerPoint = 2.0f * MATH_PI / float(ptsCnt); auto direction = (star->direction == 0) ? 1.0f : -1.0f; auto hasRoundness = false; diff --git a/src/loaders/lottie/tvgLottieModel.cpp b/src/loaders/lottie/tvgLottieModel.cpp index 0f518785..85352c02 100644 --- a/src/loaders/lottie/tvgLottieModel.cpp +++ b/src/loaders/lottie/tvgLottieModel.cpp @@ -115,8 +115,8 @@ Fill* LottieGradient::fill(float frameNo) P(static_cast(fill))->radial(sx, sy, r, sx, sy, 0.0f); } else { if (mathEqual(progress, 1.0f)) progress = 0.99f; - auto startAngle = atan2(ey - sy, ex - sx) * 180.0f / MATH_PI; - auto angle = (startAngle + this->angle(frameNo)) * (MATH_PI / 180.0f); + auto startAngle = mathRad2Deg(atan2(ey - sy, ex - sx)); + auto angle = mathDeg2Rad((startAngle + this->angle(frameNo))); auto fx = sx + cos(angle) * progress * r; auto fy = sy + sin(angle) * progress * r; // Lottie dosen't have any focal radius concept diff --git a/src/loaders/svg/tvgSvgLoader.cpp b/src/loaders/svg/tvgSvgLoader.cpp index 19b89afe..7062945e 100644 --- a/src/loaders/svg/tvgSvgLoader.cpp +++ b/src/loaders/svg/tvgSvgLoader.cpp @@ -853,8 +853,8 @@ static Matrix* _parseTransformationMatrix(const char* value) //Transform to signed. points[0] = fmodf(points[0], 360.0f); if (points[0] < 0) points[0] += 360.0f; - auto c = cosf(points[0] * (MATH_PI / 180.0f)); - auto s = sinf(points[0] * (MATH_PI / 180.0f)); + auto c = cosf(mathDeg2Rad(points[0])); + auto s = sinf(mathDeg2Rad(points[0])); if (ptCount == 1) { Matrix tmp = { c, -s, 0, s, c, 0, 0, 0, 1 }; *matrix = mathMultiply(matrix, &tmp); @@ -877,12 +877,12 @@ static Matrix* _parseTransformationMatrix(const char* value) *matrix = mathMultiply(matrix, &tmp); } else if (state == MatrixState::SkewX) { if (ptCount != 1) goto error; - auto deg = tanf(points[0] * (MATH_PI / 180.0f)); + auto deg = tanf(mathDeg2Rad(points[0])); Matrix tmp = { 1, deg, 0, 0, 1, 0, 0, 0, 1 }; *matrix = mathMultiply(matrix, &tmp); } else if (state == MatrixState::SkewY) { if (ptCount != 1) goto error; - auto deg = tanf(points[0] * (MATH_PI / 180.0f)); + auto deg = tanf(mathDeg2Rad(points[0])); Matrix tmp = { 1, 0, 0, deg, 1, 0, 0, 0, 1 }; *matrix = mathMultiply(matrix, &tmp); } diff --git a/src/loaders/svg/tvgSvgPath.cpp b/src/loaders/svg/tvgSvgPath.cpp index d7a38ce4..63e9ce53 100644 --- a/src/loaders/svg/tvgSvgPath.cpp +++ b/src/loaders/svg/tvgSvgPath.cpp @@ -126,7 +126,7 @@ void _pathAppendArcTo(Array* cmds, Array* pts, Point* cur, P rx = fabsf(rx); ry = fabsf(ry); - angle = angle * MATH_PI / 180.0f; + angle = mathDeg2Rad(angle); cosPhi = cosf(angle); sinPhi = sinf(angle); dx2 = (sx - x) / 2.0f; diff --git a/src/renderer/tvgShape.cpp b/src/renderer/tvgShape.cpp index 33841f33..da1fc7f0 100644 --- a/src/renderer/tvgShape.cpp +++ b/src/renderer/tvgShape.cpp @@ -145,8 +145,8 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa //just circle if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius); - startAngle = (startAngle * MATH_PI) / 180.0f; - sweep = sweep * MATH_PI / 180.0f; + startAngle = mathDeg2Rad(startAngle); + sweep = mathDeg2Rad(sweep); auto nCurves = ceil(fabsf(sweep / MATH_PI2)); auto sweepSign = (sweep < 0 ? -1 : 1);