gl_engine: merged point/line data structure with common

this also contains a code clean up.

issue: https://github.com/thorvg/thorvg/issues/2313
This commit is contained in:
Hermet Park 2024-10-17 18:54:14 +09:00 committed by Hermet Park
parent 81cb7da9f3
commit a26a386ccd
5 changed files with 453 additions and 732 deletions

View file

@ -222,6 +222,15 @@ Point normal(const Point& p1, const Point& p2)
} }
void normalize(Point& pt)
{
if (zero(pt)) return; //prevent zero division
auto ilength = 1.0f / sqrtf((pt.x * pt.x) + (pt.y * pt.y));
pt.x *= ilength;
pt.y *= ilength;
}
float Line::length() const float Line::length() const
{ {
return _lineLength(pt1, pt2); return _lineLength(pt1, pt2);

View file

@ -173,6 +173,8 @@ static inline void log(const Matrix& m)
void operator*=(Point& pt, const Matrix& m); void operator*=(Point& pt, const Matrix& m);
Point operator*(const Point& pt, const Matrix& m); Point operator*(const Point& pt, const Matrix& m);
Point normal(const Point& p1, const Point& p2); Point normal(const Point& p1, const Point& p2);
void normalize(Point& pt);
static inline bool zero(const Point& p) static inline bool zero(const Point& p)
{ {
@ -216,13 +218,31 @@ static inline Point operator-(const Point& lhs, const Point& rhs)
} }
static inline Point operator-(const Point& lhs, const float rhs)
{
return {lhs.x - rhs, lhs.y - rhs};
}
static inline Point operator+(const Point& lhs, const Point& rhs) static inline Point operator+(const Point& lhs, const Point& rhs)
{ {
return {lhs.x + rhs.x, lhs.y + rhs.y}; return {lhs.x + rhs.x, lhs.y + rhs.y};
} }
static inline Point operator*(const Point& lhs, float rhs) static inline Point operator+(const Point& lhs, const float rhs)
{
return {lhs.x + rhs, lhs.y + rhs};
}
static inline Point operator*(const Point& lhs, const Point& rhs)
{
return {lhs.x * rhs.x, lhs.y * rhs.y};
}
static inline Point operator*(const Point& lhs, const float rhs)
{ {
return {lhs.x * rhs, lhs.y * rhs}; return {lhs.x * rhs, lhs.y * rhs};
} }
@ -234,6 +254,12 @@ static inline Point operator*(const float& lhs, const Point& rhs)
} }
static inline Point operator/(const Point& lhs, const Point& rhs)
{
return {lhs.x / rhs.x, lhs.y / rhs.y};
}
static inline Point operator/(const Point& lhs, const float rhs) static inline Point operator/(const Point& lhs, const float rhs)
{ {
return {lhs.x / rhs, lhs.y / rhs}; return {lhs.x / rhs, lhs.y / rhs};

View file

@ -80,106 +80,6 @@
mat4[15] = mat3.e33; \ mat4[15] = mat3.e33; \
} while (false) } while (false)
class GlPoint
{
public:
float x = 0.0f;
float y = 0.0f;
GlPoint() = default;
GlPoint(float pX, float pY):x(pX), y(pY)
{
}
GlPoint(const Point& rhs):GlPoint(rhs.x, rhs.y)
{
}
GlPoint(const GlPoint& rhs) = default;
GlPoint(GlPoint&& rhs) = default;
GlPoint& operator= (const GlPoint& rhs) = default;
GlPoint& operator= (GlPoint&& rhs) = default;
GlPoint& operator= (const Point& rhs)
{
x = rhs.x;
y = rhs.y;
return *this;
}
bool operator== (const GlPoint& rhs) const
{
if (&rhs == this) return true;
if (rhs.x == this->x && rhs.y == this->y) return true;
return false;
}
bool operator!= (const GlPoint& rhs) const
{
if (&rhs == this) return true;
if (rhs.x != this->x || rhs.y != this->y) return true;
return false;
}
GlPoint operator+ (const GlPoint& rhs) const
{
return GlPoint(x + rhs.x, y + rhs.y);
}
GlPoint operator+ (const float c) const
{
return GlPoint(x + c, y + c);
}
GlPoint operator- (const GlPoint& rhs) const
{
return GlPoint(x - rhs.x, y - rhs.y);
}
GlPoint operator- (const float c) const
{
return GlPoint(x - c, y - c);
}
GlPoint operator* (const GlPoint& rhs) const
{
return GlPoint(x * rhs.x, y * rhs.y);
}
GlPoint operator* (const float c) const
{
return GlPoint(x * c, y * c);
}
GlPoint operator/ (const GlPoint& rhs) const
{
return GlPoint(x / rhs.x, y / rhs.y);
}
GlPoint operator/ (const float c) const
{
return GlPoint(x / c, y / c);
}
void mod()
{
x = fabsf(x);
y = fabsf(y);
}
void normalize()
{
auto length = sqrtf((x * x) + (y * y));
if (length != 0.0f) {
const auto inverseLen = 1.0f / length;
x *= inverseLen;
y *= inverseLen;
}
}
};
class GlStageBuffer; class GlStageBuffer;
class GlRenderTask; class GlRenderTask;

File diff suppressed because it is too large Load diff

View file

@ -24,101 +24,72 @@
#define _TVG_GL_TESSELLATOR_H_ #define _TVG_GL_TESSELLATOR_H_
#include <cstdint> #include <cstdint>
#include "tvgGlGeometry.h" #include "tvgGlGeometry.h"
#include "tvgMath.h" #include "tvgMath.h"
namespace tvg namespace tvg
{ {
namespace detail
{
class ObjectHeap; class ObjectHeap;
struct Vertex; struct Vertex;
struct Edge; struct Edge;
struct Polygon; struct Polygon;
struct MonotonePolygon; struct MonotonePolygon;
struct VertexList; struct VertexList;
struct ActiveEdgeList; struct ActiveEdgeList;
} // namespace detail
struct RenderShape; struct RenderShape;
class Tessellator final class Tessellator final
{ {
public: public:
Tessellator(Array<float>* points, Array<uint32_t>* indices); Tessellator(Array<float>* points, Array<uint32_t>* indices);
~Tessellator(); ~Tessellator();
bool tessellate(const RenderShape *rshape, bool antialias = false); bool tessellate(const RenderShape *rshape, bool antialias = false);
void tessellate(const Array<const RenderShape*> &shapes); void tessellate(const Array<const RenderShape*> &shapes);
private: private:
void visitShape(const PathCommand *cmds, uint32_t cmd_count, const Point *pts, uint32_t pts_count); void visitShape(const PathCommand *cmds, uint32_t cmd_count, const Point *pts, uint32_t pts_count);
void buildMesh(); void buildMesh();
void mergeVertices(); void mergeVertices();
bool simplifyMesh(); bool simplifyMesh();
bool tessMesh(); bool tessMesh();
bool matchFillRule(int32_t winding); bool matchFillRule(int32_t winding);
Edge *makeEdge(Vertex* p1, Vertex* p2);
detail::Edge *makeEdge(detail::Vertex* p1, detail::Vertex* p2); bool checkIntersection(Edge* left, Edge* right, ActiveEdgeList* ael, Vertex** current);
bool splitEdge(Edge* edge, Vertex* v, ActiveEdgeList* ael, Vertex** current);
bool checkIntersection(detail::Edge* left, detail::Edge* right, detail::ActiveEdgeList* ael, bool intersectPairEdge(Edge* left, Edge* right, ActiveEdgeList* ael, Vertex** current);
detail::Vertex** current); Polygon *makePoly(Vertex* v, int32_t winding);
void emitPoly(MonotonePolygon* poly);
bool splitEdge(detail::Edge* edge, detail::Vertex* v, detail::ActiveEdgeList* ael, detail::Vertex** current); void emitTriangle(Vertex* p1, Vertex* p2, Vertex* p3);
bool intersectPairEdge(detail::Edge* left, detail::Edge* right, detail::ActiveEdgeList* ael,
detail::Vertex** current);
detail::Polygon *makePoly(detail::Vertex* v, int32_t winding);
void emitPoly(detail::MonotonePolygon* poly);
void emitTriangle(detail::Vertex* p1, detail::Vertex* p2, detail::Vertex* p3);
private: private:
FillRule fillRule = FillRule::Winding; FillRule fillRule = FillRule::Winding;
std::unique_ptr<detail::ObjectHeap> pHeap; std::unique_ptr<ObjectHeap> pHeap;
Array<detail::VertexList*> outlines; Array<VertexList*> outlines;
detail::VertexList* pMesh; VertexList* pMesh;
detail::Polygon* pPolygon; Polygon* pPolygon;
Array<float>* resGlPoints; Array<float>* resGlPoints;
Array<uint32_t>* resIndices; Array<uint32_t>* resIndices;
}; };
class Stroker final class Stroker final
{ {
struct State struct State
{ {
GlPoint firstPt = {}; Point firstPt;
GlPoint firstPtDir = {}; Point firstPtDir;
GlPoint prevPt = {}; Point prevPt;
GlPoint prevPtDir = {}; Point prevPtDir;
bool hasMove = false; bool hasMove = false;
}; };
public: public:
Stroker(Array<float>* points, Array<uint32_t>* indices, const Matrix& matrix); Stroker(Array<float>* points, Array<uint32_t>* indices, const Matrix& matrix);
~Stroker() = default; ~Stroker() = default;
void stroke(const RenderShape *rshape); void stroke(const RenderShape *rshape);
RenderRegion bounds() const; RenderRegion bounds() const;
private: private:
void doStroke(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count); void doStroke(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count);
void doDashStroke(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count, void doDashStroke(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count, uint32_t dash_count, const float* dash_pattern);
uint32_t dash_count, const float* dash_pattern);
float strokeRadius() const float strokeRadius() const
{ {
@ -126,25 +97,16 @@ private:
} }
void strokeCap(); void strokeCap();
void strokeLineTo(const Point& curr);
void strokeLineTo(const GlPoint &curr); void strokeCubicTo(const Point& cnt1, const Point& cnt2, const Point& end);
void strokeCubicTo(const GlPoint &cnt1, const GlPoint &cnt2, const GlPoint &end);
void strokeClose(); void strokeClose();
void strokeJoin(const Point& dir);
void strokeRound(const Point& prev, const Point& curr, const Point& center);
void strokeMiter(const Point& prev, const Point& curr, const Point& center);
void strokeBevel(const Point& prev, const Point& curr, const Point& center);
void strokeSquare(const Point& p, const Point& outDir);
void strokeRound(const Point& p, const Point& outDir);
void strokeJoin(const GlPoint &dir);
void strokeRound(const GlPoint &prev, const GlPoint &curr, const GlPoint &center);
void strokeMiter(const GlPoint &prev, const GlPoint &curr, const GlPoint &center);
void strokeBevel(const GlPoint &prev, const GlPoint &curr, const GlPoint &center);
void strokeSquare(const GlPoint& p, const GlPoint& outDir);
void strokeRound(const GlPoint& p, const GlPoint& outDir);
private:
Array<float>* mResGlPoints; Array<float>* mResGlPoints;
Array<uint32_t>* mResIndices; Array<uint32_t>* mResIndices;
Matrix mMatrix; Matrix mMatrix;
@ -153,26 +115,23 @@ private:
StrokeCap mStrokeCap = StrokeCap::Square; StrokeCap mStrokeCap = StrokeCap::Square;
StrokeJoin mStrokeJoin = StrokeJoin::Bevel; StrokeJoin mStrokeJoin = StrokeJoin::Bevel;
State mStrokeState = {}; State mStrokeState = {};
GlPoint mLeftTop = {}; Point mLeftTop;
GlPoint mRightBottom = {}; Point mRightBottom;
}; };
class DashStroke class DashStroke
{ {
public: public:
DashStroke(Array<PathCommand>* cmds, Array<Point>* pts, uint32_t dash_count, const float* dash_pattern); DashStroke(Array<PathCommand>* cmds, Array<Point>* pts, uint32_t dash_count, const float* dash_pattern);
~DashStroke() = default; ~DashStroke() = default;
void doStroke(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count); void doStroke(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count);
private: private:
void dashLineTo(const GlPoint &pt); void dashLineTo(const Point& pt);
void dashCubicTo(const GlPoint &pt1, const GlPoint &pt2, const GlPoint &pt3); void dashCubicTo(const Point& pt1, const Point& pt2, const Point& pt3);
void moveTo(const Point& pt);
void moveTo(const GlPoint &pt); void lineTo(const Point& pt);
void lineTo(const GlPoint &pt); void cubicTo(const Point& pt1, const Point& pt2, const Point& pt3);
void cubicTo(const GlPoint &pt1, const GlPoint &pt2, const GlPoint &pt3);
private: private:
Array<PathCommand>* mCmds; Array<PathCommand>* mCmds;
@ -182,8 +141,8 @@ private:
float mCurrLen; float mCurrLen;
int32_t mCurrIdx; int32_t mCurrIdx;
bool mCurOpGap; bool mCurOpGap;
GlPoint mPtStart; Point mPtStart;
GlPoint mPtCur; Point mPtCur;
}; };
class PathTrim class PathTrim
@ -199,7 +158,6 @@ private:
float pathLength(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count); float pathLength(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count);
void trimPath(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count, float start, float end); void trimPath(const PathCommand* cmds, uint32_t cmd_count, const Point* pts, uint32_t pts_count, float start, float end);
private:
Array<PathCommand> mCmds; Array<PathCommand> mCmds;
Array<Point> mPts; Array<Point> mPts;
}; };
@ -209,20 +167,17 @@ class BWTessellator
public: public:
BWTessellator(Array<float>* points, Array<uint32_t>* indices); BWTessellator(Array<float>* points, Array<uint32_t>* indices);
~BWTessellator() = default; ~BWTessellator() = default;
void tessellate(const RenderShape *rshape, const Matrix& matrix); void tessellate(const RenderShape *rshape, const Matrix& matrix);
RenderRegion bounds() const; RenderRegion bounds() const;
private: private:
uint32_t pushVertex(float x, float y); uint32_t pushVertex(float x, float y);
void pushTriangle(uint32_t a, uint32_t b, uint32_t c); void pushTriangle(uint32_t a, uint32_t b, uint32_t c);
private:
Array<float>* mResPoints; Array<float>* mResPoints;
Array<uint32_t>* mResIndices; Array<uint32_t>* mResIndices;
GlPoint mLeftTop = {}; Point mLeftTop;
GlPoint mRightBottom = {}; Point mRightBottom;
}; };
} // namespace tvg } // namespace tvg