mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-14 12:04:29 +00:00
common: code refactoring
introduced mathDeg2Rad() and mathRad2Deg() for a common implementation.
This commit is contained in:
parent
bb793a2762
commit
be7437e0a3
7 changed files with 27 additions and 15 deletions
|
@ -238,7 +238,7 @@ float bezAngleAt(const Bezier& bz, float t)
|
||||||
pt.x *= 3;
|
pt.x *= 3;
|
||||||
pt.y *= 3;
|
pt.y *= 3;
|
||||||
|
|
||||||
return atan2(pt.x, pt.y) * 180.0f / 3.141592f;
|
return mathRad2Deg(atan2(pt.x, pt.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,18 @@ bool mathIdentity(const Matrix* m);
|
||||||
void mathMultiply(Point* pt, const Matrix* transform);
|
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)
|
static inline bool mathZero(float a)
|
||||||
{
|
{
|
||||||
return (fabsf(a) < FLT_EPSILON) ? true : false;
|
return (fabsf(a) < FLT_EPSILON) ? true : false;
|
||||||
|
|
|
@ -104,7 +104,7 @@ static Shape* _draw(LottieGroup* parent, RenderContext* ctx);
|
||||||
static void _rotateX(Matrix* m, float degree)
|
static void _rotateX(Matrix* m, float degree)
|
||||||
{
|
{
|
||||||
if (degree == 0.0f) return;
|
if (degree == 0.0f) return;
|
||||||
auto radian = degree / 180.0f * MATH_PI;
|
auto radian = mathDeg2Rad(degree);
|
||||||
m->e22 *= cosf(radian);
|
m->e22 *= cosf(radian);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ static void _rotateX(Matrix* m, float degree)
|
||||||
static void _rotateY(Matrix* m, float degree)
|
static void _rotateY(Matrix* m, float degree)
|
||||||
{
|
{
|
||||||
if (degree == 0.0f) return;
|
if (degree == 0.0f) return;
|
||||||
auto radian = degree / 180.0f * MATH_PI;
|
auto radian = mathDeg2Rad(degree);
|
||||||
m->e11 *= cosf(radian);
|
m->e11 *= cosf(radian);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ static void _rotateY(Matrix* m, float degree)
|
||||||
static void _rotationZ(Matrix* m, float degree)
|
static void _rotationZ(Matrix* m, float degree)
|
||||||
{
|
{
|
||||||
if (degree == 0.0f) return;
|
if (degree == 0.0f) return;
|
||||||
auto radian = degree / 180.0f * MATH_PI;
|
auto radian = mathDeg2Rad(degree);
|
||||||
m->e11 = cosf(radian);
|
m->e11 = cosf(radian);
|
||||||
m->e12 = -sinf(radian);
|
m->e12 = -sinf(radian);
|
||||||
m->e21 = 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 innerRoundness = star->innerRoundness(frameNo) * 0.01f;
|
||||||
auto outerRoundness = star->outerRoundness(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 partialPointRadius = 0.0f;
|
||||||
auto anglePerPoint = (2.0f * MATH_PI / ptsCnt);
|
auto anglePerPoint = (2.0f * MATH_PI / ptsCnt);
|
||||||
auto halfAnglePerPoint = anglePerPoint * 0.5f;
|
auto halfAnglePerPoint = anglePerPoint * 0.5f;
|
||||||
|
@ -724,7 +724,7 @@ static void _updatePolygon(LottieGroup* parent, LottiePolyStar* star, Matrix* tr
|
||||||
auto radius = star->outerRadius(frameNo);
|
auto radius = star->outerRadius(frameNo);
|
||||||
auto roundness = star->outerRoundness(frameNo) * 0.01f;
|
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 anglePerPoint = 2.0f * MATH_PI / float(ptsCnt);
|
||||||
auto direction = (star->direction == 0) ? 1.0f : -1.0f;
|
auto direction = (star->direction == 0) ? 1.0f : -1.0f;
|
||||||
auto hasRoundness = false;
|
auto hasRoundness = false;
|
||||||
|
|
|
@ -115,8 +115,8 @@ Fill* LottieGradient::fill(float frameNo)
|
||||||
P(static_cast<RadialGradient*>(fill))->radial(sx, sy, r, sx, sy, 0.0f);
|
P(static_cast<RadialGradient*>(fill))->radial(sx, sy, r, sx, sy, 0.0f);
|
||||||
} else {
|
} else {
|
||||||
if (mathEqual(progress, 1.0f)) progress = 0.99f;
|
if (mathEqual(progress, 1.0f)) progress = 0.99f;
|
||||||
auto startAngle = atan2(ey - sy, ex - sx) * 180.0f / MATH_PI;
|
auto startAngle = mathRad2Deg(atan2(ey - sy, ex - sx));
|
||||||
auto angle = (startAngle + this->angle(frameNo)) * (MATH_PI / 180.0f);
|
auto angle = mathDeg2Rad((startAngle + this->angle(frameNo)));
|
||||||
auto fx = sx + cos(angle) * progress * r;
|
auto fx = sx + cos(angle) * progress * r;
|
||||||
auto fy = sy + sin(angle) * progress * r;
|
auto fy = sy + sin(angle) * progress * r;
|
||||||
// Lottie dosen't have any focal radius concept
|
// Lottie dosen't have any focal radius concept
|
||||||
|
|
|
@ -853,8 +853,8 @@ static Matrix* _parseTransformationMatrix(const char* value)
|
||||||
//Transform to signed.
|
//Transform to signed.
|
||||||
points[0] = fmodf(points[0], 360.0f);
|
points[0] = fmodf(points[0], 360.0f);
|
||||||
if (points[0] < 0) points[0] += 360.0f;
|
if (points[0] < 0) points[0] += 360.0f;
|
||||||
auto c = cosf(points[0] * (MATH_PI / 180.0f));
|
auto c = cosf(mathDeg2Rad(points[0]));
|
||||||
auto s = sinf(points[0] * (MATH_PI / 180.0f));
|
auto s = sinf(mathDeg2Rad(points[0]));
|
||||||
if (ptCount == 1) {
|
if (ptCount == 1) {
|
||||||
Matrix tmp = { c, -s, 0, s, c, 0, 0, 0, 1 };
|
Matrix tmp = { c, -s, 0, s, c, 0, 0, 0, 1 };
|
||||||
*matrix = mathMultiply(matrix, &tmp);
|
*matrix = mathMultiply(matrix, &tmp);
|
||||||
|
@ -877,12 +877,12 @@ static Matrix* _parseTransformationMatrix(const char* value)
|
||||||
*matrix = mathMultiply(matrix, &tmp);
|
*matrix = mathMultiply(matrix, &tmp);
|
||||||
} else if (state == MatrixState::SkewX) {
|
} else if (state == MatrixState::SkewX) {
|
||||||
if (ptCount != 1) goto error;
|
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 tmp = { 1, deg, 0, 0, 1, 0, 0, 0, 1 };
|
||||||
*matrix = mathMultiply(matrix, &tmp);
|
*matrix = mathMultiply(matrix, &tmp);
|
||||||
} else if (state == MatrixState::SkewY) {
|
} else if (state == MatrixState::SkewY) {
|
||||||
if (ptCount != 1) goto error;
|
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 tmp = { 1, 0, 0, deg, 1, 0, 0, 0, 1 };
|
||||||
*matrix = mathMultiply(matrix, &tmp);
|
*matrix = mathMultiply(matrix, &tmp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,7 +126,7 @@ void _pathAppendArcTo(Array<PathCommand>* cmds, Array<Point>* pts, Point* cur, P
|
||||||
rx = fabsf(rx);
|
rx = fabsf(rx);
|
||||||
ry = fabsf(ry);
|
ry = fabsf(ry);
|
||||||
|
|
||||||
angle = angle * MATH_PI / 180.0f;
|
angle = mathDeg2Rad(angle);
|
||||||
cosPhi = cosf(angle);
|
cosPhi = cosf(angle);
|
||||||
sinPhi = sinf(angle);
|
sinPhi = sinf(angle);
|
||||||
dx2 = (sx - x) / 2.0f;
|
dx2 = (sx - x) / 2.0f;
|
||||||
|
|
|
@ -145,8 +145,8 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa
|
||||||
//just circle
|
//just circle
|
||||||
if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
|
if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
|
||||||
|
|
||||||
startAngle = (startAngle * MATH_PI) / 180.0f;
|
startAngle = mathDeg2Rad(startAngle);
|
||||||
sweep = sweep * MATH_PI / 180.0f;
|
sweep = mathDeg2Rad(sweep);
|
||||||
|
|
||||||
auto nCurves = ceil(fabsf(sweep / MATH_PI2));
|
auto nCurves = ceil(fabsf(sweep / MATH_PI2));
|
||||||
auto sweepSign = (sweep < 0 ? -1 : 1);
|
auto sweepSign = (sweep < 0 ? -1 : 1);
|
||||||
|
|
Loading…
Add table
Reference in a new issue