Skip to content

fix concave polygon bug in DrawNode #19641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 115 additions & 10 deletions cocos/2d/CCDrawNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,120 @@ static inline Tex2F __t(const Vec2 &v)
return *(Tex2F*)&v;
}

static const float EPSILON=0.0000000001f;
float Triangulate::Area(const Vec2 *verts,int n)
{
float A=0.0f;
for(int p=n-1,q=0; q<n; p=q++)
{
A+= verts[p].x*verts[q].y - verts[q].x*verts[p].y;
}
return A*0.5f;
}

/*
InsideTriangle decides if a point P is Inside of the triangle
defined by A, B, C.
*/
bool Triangulate::InsideTriangle(float Ax, float Ay,
float Bx, float By,
float Cx, float Cy,
float Px, float Py)

{
float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
float cCROSSap, bCROSScp, aCROSSbp;
ax = Cx - Bx; ay = Cy - By;
bx = Ax - Cx; by = Ay - Cy;
cx = Bx - Ax; cy = By - Ay;
apx= Px - Ax; apy= Py - Ay;
bpx= Px - Bx; bpy= Py - By;
cpx= Px - Cx; cpy= Py - Cy;

aCROSSbp = ax*bpy - ay*bpx;
cCROSSap = cx*apy - cy*apx;
bCROSScp = bx*cpy - by*cpx;

return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
};

bool Triangulate::Snip(const Vec2 *verts,int u,int v,int w,int n,int *V)
{
int p;
float Ax, Ay, Bx, By, Cx, Cy, Px, Py;

Ax = verts[V[u]].x;
Ay = verts[V[u]].y;

Bx = verts[V[v]].x;
By = verts[V[v]].y;

Cx = verts[V[w]].x;
Cy = verts[V[w]].y;

if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false;

for (p=0;p<n;p++)
{
if( (p == u) || (p == v) || (p == w) ) continue;
Px = verts[V[p]].x;
Py = verts[V[p]].y;
if (InsideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false;
}

return true;
}

V2F_C4B_T2F_Triangle * Triangulate::Process(const Vec2 *verts,V2F_C4B_T2F_Triangle * triangles,int n,const Color4F &fillColor)
{
if ( n < 3 ) return triangles;

int *V = new int[n];
/* we want a counter-clockwise polygon in V */
if ( 0.0f < Area(verts,n) )
for (int v=0; v<n; v++) V[v] = v;
else
for(int v=0; v<n; v++) V[v] = (n-1)-v;

int nv = n;
/* remove nv-2 Vertices, creating 1 triangle every time */
int count = 2*nv; /* error detection */
for(int m=0, v=nv-1; nv>2; )
{
/* if we loop, it is probably a non-simple polygon */
if (0 >= (count--))
{
//** Triangulate: ERROR - probable bad polygon!
return triangles;
}
/* three consecutive vertices in current polygon, <u,v,w> */
int u = v ; if (nv <= u) u = 0; /* previous */
v = u+1; if (nv <= v) v = 0; /* new v */
int w = v+1; if (nv <= w) w = 0; /* next */

if ( Snip(verts,u,v,w,nv,V) )
{
int a,b,c,s,t;
/* true names of the vertices */
a = V[u]; b = V[v]; c = V[w];

V2F_C4B_T2F_Triangle tmp = {
{verts[a], Color4B(fillColor), __t(v2fzero)},
{verts[b], Color4B(fillColor), __t(v2fzero)},
{verts[c], Color4B(fillColor), __t(v2fzero)},
};
*triangles++ = tmp;
m++;
/* remove v from remaining polygon */
for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--;
/* resest error detection counter */
count = 2*nv;
}
}
delete []V;
return triangles;
}

// implementation of DrawNode

DrawNode::DrawNode(GLfloat lineWidth)
Expand Down Expand Up @@ -764,16 +878,7 @@ void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColo
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
V2F_C4B_T2F_Triangle *cursor = triangles;

for (int i = 0; i < count-2; i++)
{
V2F_C4B_T2F_Triangle tmp = {
{verts[0], Color4B(fillColor), __t(v2fzero)},
{verts[i+1], Color4B(fillColor), __t(v2fzero)},
{verts[i+2], Color4B(fillColor), __t(v2fzero)},
};

*cursor++ = tmp;
}
cursor = Triangulate::Process(verts,cursor,count,fillColor);

if(outline)
{
Expand Down
22 changes: 22 additions & 0 deletions cocos/2d/CCDrawNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ NS_CC_BEGIN

static const int DEFAULT_LINE_WIDTH = 2;

class Triangulate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to add the url of the codes comes from.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The url is http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml.
I wrote the url in the first comment up there. Do you mean that I need write the url into the source code's comment ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, i think it is better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've added in the new commit.

{
public:

// triangulate a contour/polygon, places results in STL vector
// as series of triangles.
static V2F_C4B_T2F_Triangle * Process(const Vec2 *verts,V2F_C4B_T2F_Triangle * triangles,int n,const Color4F &fillColor);

// compute area of a contour/polygon
static float Area(const Vec2 *verts,int n);

// decide if point Px/Py is inside triangle defined by
// (Ax,Ay) (Bx,By) (Cx,Cy)
static bool InsideTriangle(float Ax, float Ay,
float Bx, float By,
float Cx, float Cy,
float Px, float Py);

private:
static bool Snip(const Vec2 *verts,int u,int v,int w,int n,int *V);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With cocos2d-x coding style, please use lower case for the first character of the function name, and please use verb for first word, for example

Area -> computeArea

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I have changed the style in the new commit .
And yes, it is compatible.

};

class PointArray;
/**
* @addtogroup _2d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ DrawNodeTest::DrawNodeTest()
Vec2 points[] = { Vec2(s.height/4,0), Vec2(s.width,s.height/5), Vec2(s.width/3*2,s.height) };
draw->drawPolygon(points, sizeof(points)/sizeof(points[0]), Color4F(1,0,0,0.5), 4, Color4F(0,0,1,0.5));

// Draw concave polygons
Vec2 concavePoints[] = { Vec2(0,130), Vec2(140,130), Vec2(140,160),Vec2(120,145),Vec2(30,145),Vec2(0,160) };
draw->drawPolygon(concavePoints, sizeof(concavePoints)/sizeof(concavePoints[0]), Color4F(1,0,0,0.5), 4, Color4F(0,0,1,0.5));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to add a separate test case, or it is hard to catch the error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,I added a test class : Issue19641Test in the new commit .And I moved that code into Issue19641Test.

// star poly (triggers buggs)
{
const float o=80;
Expand Down