1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: triangulate.c,v 2.4 2016/12/22 18:48:36 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* triangulate.c |
6 |
* |
7 |
* Adapted by Greg Ward on 1/23/14. |
8 |
* Fixes for polygons with seams/holes and co-linear vertices added |
9 |
* by Nathaniel Jones on 12/21/16. |
10 |
* Copyright 2016 Anyhere Software. All rights reserved. |
11 |
* |
12 |
*/ |
13 |
|
14 |
/* COTD Entry submitted by John W. Ratcliff [[email protected]] |
15 |
|
16 |
// ** THIS IS A CODE SNIPPET WHICH WILL EFFICIEINTLY TRIANGULATE ANY |
17 |
// ** POLYGON/CONTOUR (without holes) AS A STATIC CLASS. THIS SNIPPET |
18 |
// ** IS COMPRISED OF 3 FILES, TRIANGULATE.H, THE HEADER FILE FOR THE |
19 |
// ** TRIANGULATE BASE CLASS, TRIANGULATE.CPP, THE IMPLEMENTATION OF |
20 |
// ** THE TRIANGULATE BASE CLASS, AND TEST.CPP, A SMALL TEST PROGRAM |
21 |
// ** DEMONSTRATING THE USAGE OF THE TRIANGULATOR. THE TRIANGULATE |
22 |
// ** BASE CLASS ALSO PROVIDES TWO USEFUL HELPER METHODS, ONE WHICH |
23 |
// ** COMPUTES THE AREA OF A POLYGON, AND ANOTHER WHICH DOES AN EFFICENT |
24 |
// ** POINT IN A TRIANGLE TEST. |
25 |
// ** SUBMITTED BY JOHN W. RATCLIFF ([email protected]) July 22, 2000 |
26 |
*/ |
27 |
|
28 |
#include <stdio.h> |
29 |
#include <stdlib.h> |
30 |
#include "triangulate.h" |
31 |
|
32 |
#ifndef true |
33 |
#define true 1 |
34 |
#define false 0 |
35 |
#endif |
36 |
|
37 |
static const double EPSILON = 0.0000000001; |
38 |
|
39 |
static int |
40 |
polySnip(const Vert2_list *contour, int u, int v, int w, int n, int *V) |
41 |
{ |
42 |
int p; |
43 |
double Ax, Ay, Bx, By, Cx, Cy, Px, Py, cross; |
44 |
|
45 |
Ax = contour->v[V[u]].mX; |
46 |
Ay = contour->v[V[u]].mY; |
47 |
|
48 |
Bx = contour->v[V[v]].mX; |
49 |
By = contour->v[V[v]].mY; |
50 |
|
51 |
Cx = contour->v[V[w]].mX; |
52 |
Cy = contour->v[V[w]].mY; |
53 |
|
54 |
cross = ((Bx - Ax)*(Cy - Ay)) - ((By - Ay)*(Cx - Ax)); |
55 |
if (EPSILON > cross) return EPSILON > -cross ? -1 : false; /* Negative if colinear points */ |
56 |
|
57 |
for (p=0;p<n;p++) |
58 |
{ |
59 |
if( (p == u) | (p == v) | (p == w) ) continue; |
60 |
Px = contour->v[V[p]].mX; |
61 |
Py = contour->v[V[p]].mY; |
62 |
if ((Px == Ax && Py == Ay) || (Px == Bx && Py == By) || |
63 |
(Px == Cx && Py == Cy)) continue; /* Handle donuts */ |
64 |
if (insideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false; |
65 |
} |
66 |
|
67 |
return true; |
68 |
} |
69 |
|
70 |
Vert2_list * |
71 |
polyAlloc(int nv) |
72 |
{ |
73 |
Vert2_list *pnew; |
74 |
|
75 |
if (nv < 3) return NULL; |
76 |
|
77 |
pnew = (Vert2_list *)malloc(sizeof(Vert2_list) + sizeof(Vert2)*(nv-3)); |
78 |
if (pnew == NULL) return NULL; |
79 |
pnew->nv = nv; |
80 |
pnew->p = NULL; |
81 |
|
82 |
return pnew; |
83 |
} |
84 |
|
85 |
double |
86 |
polyArea(const Vert2_list *contour) |
87 |
{ |
88 |
double A=0.0; |
89 |
int p, q; |
90 |
|
91 |
for(p = contour->nv-1, q = 0; q < contour->nv; p=q++) |
92 |
{ |
93 |
A += contour->v[p].mX*contour->v[q].mY - contour->v[q].mX*contour->v[p].mY; |
94 |
} |
95 |
return A*0.5; |
96 |
} |
97 |
|
98 |
/* |
99 |
InsideTriangle decides if a point P is Inside of the triangle |
100 |
defined by A, B, C. |
101 |
*/ |
102 |
int |
103 |
insideTriangle(double Ax, double Ay, |
104 |
double Bx, double By, |
105 |
double Cx, double Cy, |
106 |
double Px, double Py) |
107 |
|
108 |
{ |
109 |
double ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy; |
110 |
double cCROSSap, bCROSScp, aCROSSbp; |
111 |
|
112 |
ax = Cx - Bx; ay = Cy - By; |
113 |
bx = Ax - Cx; by = Ay - Cy; |
114 |
cx = Bx - Ax; cy = By - Ay; |
115 |
apx= Px - Ax; apy= Py - Ay; |
116 |
bpx= Px - Bx; bpy= Py - By; |
117 |
cpx= Px - Cx; cpy= Py - Cy; |
118 |
|
119 |
aCROSSbp = ax*bpy - ay*bpx; |
120 |
cCROSSap = cx*apy - cy*apx; |
121 |
bCROSScp = bx*cpy - by*cpx; |
122 |
|
123 |
return ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0)); |
124 |
}; |
125 |
|
126 |
int |
127 |
polyTriangulate(const Vert2_list *contour, tri_out_t *cb) |
128 |
{ |
129 |
/* allocate and initialize list of Vertices in polygon */ |
130 |
|
131 |
int nv, m, u, v, w, count, result; |
132 |
int *V; |
133 |
|
134 |
if ( contour->nv < 3 ) return false; |
135 |
|
136 |
V = (int *)malloc(sizeof(int)*contour->nv); |
137 |
if (V == NULL) return false; |
138 |
|
139 |
/* we want a counter-clockwise polygon in V */ |
140 |
|
141 |
if ( 0.0 < polyArea(contour) ) |
142 |
for (v=0; v<contour->nv; v++) V[v] = v; |
143 |
else |
144 |
for(v=0; v<contour->nv; v++) V[v] = (contour->nv-1)-v; |
145 |
|
146 |
nv = contour->nv; |
147 |
|
148 |
/* remove nv-2 Vertices, creating 1 triangle every time */ |
149 |
count = 2*nv; /* error detection */ |
150 |
|
151 |
for(m=0, v=nv-1; nv>2; ) |
152 |
{ |
153 |
/* if we loop, it is probably a non-simple polygon */ |
154 |
if (0 >= count--) |
155 |
{ |
156 |
/* Triangulate: ERROR - probable bad polygon */ |
157 |
return false; |
158 |
} |
159 |
|
160 |
/* three consecutive vertices in current polygon, <u,v,w> */ |
161 |
u = v ; if (nv <= u) u = 0; /* previous */ |
162 |
v = u+1; if (nv <= v) v = 0; /* new v */ |
163 |
w = v+1; if (nv <= w) w = 0; /* next */ |
164 |
|
165 |
result = polySnip(contour, u, v, w, nv, V); |
166 |
if (result > 0) /* successfully found a triangle */ |
167 |
{ |
168 |
int a,b,c; |
169 |
|
170 |
/* true names of the vertices */ |
171 |
a = V[u]; b = V[v]; c = V[w]; |
172 |
|
173 |
/* output Triangle */ |
174 |
if (!(*cb)(contour, a, b, c)) return false; |
175 |
|
176 |
m++; |
177 |
} |
178 |
if (result) /* successfully found a triangle or three consecutive colinear points */ |
179 |
{ |
180 |
int s,t; |
181 |
|
182 |
/* remove v from remaining polygon */ |
183 |
for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--; |
184 |
|
185 |
/* reset error detection counter */ |
186 |
count = 2*nv; |
187 |
} |
188 |
} |
189 |
|
190 |
free(V); |
191 |
|
192 |
return true; |
193 |
} |