#include #include #include #include #include "Triangulate.h" #define EPSILON 0.0000000001f float Triangulate::area(const VecVertex *contour, int offset, int n) { float A=0.0f; for(int p=n-1,q=offset; q= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f)); }; bool Triangulate::snip(const VecVertex *contour, int offset, int n, int u, int v, int w, int *V) { int p; float Ax, Ay, Bx, By, Cx, Cy, Px, Py; Ax = contour[V[u]].x; Ay = contour[V[u]].y; Bx = contour[V[v]].x; By = contour[V[v]].y; Cx = contour[V[w]].x; Cy = contour[V[w]].y; if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) { return false; } for (p = offset; p < n; p++) { if ( (p == u) || (p == v) || (p == w) ) { continue; } Px = contour[V[p]].x; Py = contour[V[p]].y; if ( insideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py) ) { return false; } } return true; } bool Triangulate::process(const VecVertex *contour, int offset, int n, std::vector &result) { /* allocate and initialize list of Vertices in polygon */ if ( n < 3 ) return false; int *V = new int[n]; /* we want a counter-clockwise polygon in V */ if ( 0.0f < area(contour, offset, n) ) for (int v=0; v2; ) { /* if we loop, it is probably a non-simple polygon */ if (0 >= (count--)) { //** Triangulate: ERROR - probable bad polygon! delete []V; return false; } /* three consecutive vertices in current polygon, */ 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(contour, offset, nv, u, v, w, V) ) { int a,b,c,s,t; /* true names of the vertices */ a = V[u]; b = V[v]; c = V[w]; /* output Triangle */ result.push_back( a ); result.push_back( b ); result.push_back( c ); m++; /* remove v from remaining polygon */ for(s=v,t=v+1;t