From 9e55eefe13fed17bbdbd59c919384cd30cc78097 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 20 Feb 2024 16:59:27 +0900 Subject: [PATCH] common: code refactoring refactor common code to consolidate Bezier and line function implementations into a single location. --- src/common/meson.build | 4 +-- src/common/{tvgBezier.cpp => tvgLines.cpp} | 21 +++++++++++++- src/common/{tvgBezier.h => tvgLines.h} | 16 +++++++++-- src/loaders/lottie/tvgLottieProperty.h | 2 +- src/renderer/sw_engine/tvgSwShape.cpp | 33 ++-------------------- 5 files changed, 39 insertions(+), 37 deletions(-) rename src/common/{tvgBezier.cpp => tvgLines.cpp} (93%) rename src/common/{tvgBezier.h => tvgLines.h} (87%) diff --git a/src/common/meson.build b/src/common/meson.build index 41193656..6c2e37e5 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -1,14 +1,14 @@ source_file = [ 'tvgArray.h', - 'tvgBezier.h', 'tvgCompressor.h', 'tvgFormat.h', 'tvgInlist.h', + 'tvgLines.h', 'tvgLock.h', 'tvgMath.h', 'tvgStr.h', - 'tvgBezier.cpp', 'tvgCompressor.cpp', + 'tvgLines.cpp', 'tvgMath.cpp', 'tvgStr.cpp' ] diff --git a/src/common/tvgBezier.cpp b/src/common/tvgLines.cpp similarity index 93% rename from src/common/tvgBezier.cpp rename to src/common/tvgLines.cpp index 068004bc..e794e47f 100644 --- a/src/common/tvgBezier.cpp +++ b/src/common/tvgLines.cpp @@ -21,7 +21,7 @@ */ #include "tvgMath.h" -#include "tvgBezier.h" +#include "tvgLines.h" #define BEZIER_EPSILON 1e-4f @@ -101,6 +101,25 @@ float _bezAt(const Bezier& bz, float at, float length, LengthFunc lineLengthFunc namespace tvg { +float lineLength(const Point& pt1, const Point& pt2) +{ + return _lineLength(pt1, pt2); +} + + +void lineSplitAt(const Line& cur, float at, Line& left, Line& right) +{ + auto len = lineLength(cur.pt1, cur.pt2); + auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at; + auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at; + left.pt1 = cur.pt1; + left.pt2.x = left.pt1.x + dx; + left.pt2.y = left.pt1.y + dy; + right.pt1 = left.pt2; + right.pt2 = cur.pt2; +} + + void bezSplit(const Bezier& cur, Bezier& left, Bezier& right) { auto c = (cur.ctrl1.x + cur.ctrl2.x) * 0.5f; diff --git a/src/common/tvgBezier.h b/src/common/tvgLines.h similarity index 87% rename from src/common/tvgBezier.h rename to src/common/tvgLines.h index 80a19925..d900782b 100644 --- a/src/common/tvgBezier.h +++ b/src/common/tvgLines.h @@ -20,14 +20,24 @@ * SOFTWARE. */ -#ifndef _TVG_BEZIER_H_ -#define _TVG_BEZIER_H_ +#ifndef _TVG_LINES_H_ +#define _TVG_LINES_H_ #include "tvgCommon.h" namespace tvg { +struct Line +{ + Point pt1; + Point pt2; +}; + +float lineLength(const Point& pt1, const Point& pt2); +void lineSplitAt(const Line& cur, float at, Line& left, Line& right); + + struct Bezier { Point start; @@ -48,4 +58,4 @@ float bezLengthApprox(const Bezier& cur); float bezAtApprox(const Bezier& bz, float at, float length); } -#endif //_TVG_BEZIER_H_ +#endif //_TVG_LINES_H_ diff --git a/src/loaders/lottie/tvgLottieProperty.h b/src/loaders/lottie/tvgLottieProperty.h index b0e3fd7c..a62957c3 100644 --- a/src/loaders/lottie/tvgLottieProperty.h +++ b/src/loaders/lottie/tvgLottieProperty.h @@ -26,7 +26,7 @@ #include "tvgCommon.h" #include "tvgArray.h" #include "tvgMath.h" -#include "tvgBezier.h" +#include "tvgLines.h" #include "tvgLottieInterpolator.h" struct PathSet diff --git a/src/renderer/sw_engine/tvgSwShape.cpp b/src/renderer/sw_engine/tvgSwShape.cpp index 8cad462f..27d91530 100644 --- a/src/renderer/sw_engine/tvgSwShape.cpp +++ b/src/renderer/sw_engine/tvgSwShape.cpp @@ -22,39 +22,12 @@ #include "tvgSwCommon.h" #include "tvgMath.h" -#include "tvgBezier.h" +#include "tvgLines.h" /************************************************************************/ /* Internal Class Implementation */ /************************************************************************/ -struct Line -{ - Point pt1; - Point pt2; -}; - - -static float _lineLength(const Point& pt1, const Point& pt2) -{ - Point diff = {pt2.x - pt1.x, pt2.y - pt1.y}; - return sqrtf(diff.x * diff.x + diff.y * diff.y); -} - - -static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right) -{ - auto len = _lineLength(cur.pt1, cur.pt2); - auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at; - auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at; - left.pt1 = cur.pt1; - left.pt2.x = left.pt1.x + dx; - left.pt2.y = left.pt1.y + dy; - right.pt1 = left.pt2; - right.pt2 = cur.pt2; -} - - static void _outlineEnd(SwOutline& outline) { if (outline.pts.empty()) return; @@ -115,7 +88,7 @@ static void _outlineClose(SwOutline& outline) static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* transform) { Line cur = {dash.ptCur, *to}; - auto len = _lineLength(cur.pt1, cur.pt2); + auto len = lineLength(cur.pt1, cur.pt2); if (mathZero(len)) { _outlineMoveTo(*dash.outline, &dash.ptCur, transform); @@ -133,7 +106,7 @@ static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* trans Line left, right; if (dash.curLen > 0) { len -= dash.curLen; - _lineSplitAt(cur, dash.curLen, left, right); + lineSplitAt(cur, dash.curLen, left, right); if (!dash.curOpGap) { if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) { _outlineMoveTo(*dash.outline, &left.pt1, transform);