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
{
return _lineLength(pt1, pt2);

View file

@ -173,6 +173,8 @@ static inline void log(const Matrix& m)
void operator*=(Point& pt, const Matrix& m);
Point operator*(const Point& pt, const Matrix& m);
Point normal(const Point& p1, const Point& p2);
void normalize(Point& pt);
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)
{
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};
}
@ -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)
{
return {lhs.x / rhs, lhs.y / rhs};

View file

@ -80,106 +80,6 @@
mat4[15] = mat3.e33; \
} 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 GlRenderTask;

File diff suppressed because it is too large Load diff

View file

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