common: code refactoring

refactor common code to consolidate Bezier and line
function implementations into a single location.
This commit is contained in:
Hermet Park 2024-02-20 16:59:27 +09:00
parent 71c636e33c
commit c08c11db7d
5 changed files with 39 additions and 37 deletions

View file

@ -1,13 +1,13 @@
source_file = [
'tvgArray.h',
'tvgBezier.h',
'tvgCompressor.h',
'tvgInlist.h',
'tvgLines.h',
'tvgLock.h',
'tvgMath.h',
'tvgStr.h',
'tvgBezier.cpp',
'tvgCompressor.cpp',
'tvgLines.cpp',
'tvgMath.cpp',
'tvgStr.cpp'
]

View file

@ -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;

View file

@ -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_

View file

@ -26,7 +26,7 @@
#include "tvgCommon.h"
#include "tvgArray.h"
#include "tvgMath.h"
#include "tvgBezier.h"
#include "tvgLines.h"
#include "tvgLottieInterpolator.h"
struct PathSet

View file

@ -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);