38 |
|
polySnip(const Vert2_list *contour, int u, int v, int w, int n, int *V) |
39 |
|
{ |
40 |
|
int p; |
41 |
< |
double Ax, Ay, Bx, By, Cx, Cy, Px, Py; |
41 |
> |
double Ax, Ay, Bx, By, Cx, Cy, Px, Py, cross; |
42 |
|
|
43 |
|
Ax = contour->v[V[u]].mX; |
44 |
|
Ay = contour->v[V[u]].mY; |
49 |
|
Cx = contour->v[V[w]].mX; |
50 |
|
Cy = contour->v[V[w]].mY; |
51 |
|
|
52 |
< |
if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false; |
52 |
> |
cross = ((Bx - Ax)*(Cy - Ay)) - ((By - Ay)*(Cx - Ax)); |
53 |
> |
if (EPSILON > cross) return EPSILON > -cross ? -1 : false; /* Negative if colinear points */ |
54 |
|
|
55 |
|
for (p=0;p<n;p++) |
56 |
|
{ |
57 |
< |
if( (p == u) || (p == v) || (p == w) ) continue; |
57 |
> |
if( (p == u) | (p == v) | (p == w) ) continue; |
58 |
|
Px = contour->v[V[p]].mX; |
59 |
|
Py = contour->v[V[p]].mY; |
60 |
+ |
if ((Px == Ax && Py == Ay) || (Px == Bx && Py == By) || |
61 |
+ |
(Px == Cx && Py == Cy)) continue; /* Handle donuts */ |
62 |
|
if (insideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false; |
63 |
|
} |
64 |
|
|
126 |
|
{ |
127 |
|
/* allocate and initialize list of Vertices in polygon */ |
128 |
|
|
129 |
< |
int nv, m, u, v, w, count; |
129 |
> |
int nv, m, u, v, w, count, result; |
130 |
|
int *V; |
131 |
|
|
132 |
|
if ( contour->nv < 3 ) return false; |
149 |
|
for(m=0, v=nv-1; nv>2; ) |
150 |
|
{ |
151 |
|
/* if we loop, it is probably a non-simple polygon */ |
152 |
< |
if (0 >= (count--)) |
152 |
> |
if (0 >= count--) |
153 |
|
{ |
154 |
< |
/* Triangulate: ERROR - probable bad polygon! */ |
154 |
> |
/* Triangulate: ERROR - probable bad polygon */ |
155 |
|
return false; |
156 |
|
} |
157 |
|
|
160 |
|
v = u+1; if (nv <= v) v = 0; /* new v */ |
161 |
|
w = v+1; if (nv <= w) w = 0; /* next */ |
162 |
|
|
163 |
< |
if ( polySnip(contour,u,v,w,nv,V) ) |
163 |
> |
result = polySnip(contour, u, v, w, nv, V); |
164 |
> |
if (result > 0) /* successfully found a triangle */ |
165 |
|
{ |
166 |
< |
int a,b,c,s,t; |
166 |
> |
int a,b,c; |
167 |
|
|
168 |
|
/* true names of the vertices */ |
169 |
|
a = V[u]; b = V[v]; c = V[w]; |
172 |
|
if (!(*cb)(contour, a, b, c)) return false; |
173 |
|
|
174 |
|
m++; |
175 |
+ |
} |
176 |
+ |
if (result) /* successfully found a triangle or three consecutive colinear points */ |
177 |
+ |
{ |
178 |
+ |
int s,t; |
179 |
|
|
180 |
|
/* remove v from remaining polygon */ |
181 |
|
for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--; |
182 |
|
|
183 |
< |
/* resest error detection counter */ |
183 |
> |
/* reset error detection counter */ |
184 |
|
count = 2*nv; |
185 |
|
} |
186 |
|
} |