8 |
|
* sm_del.c |
9 |
|
*/ |
10 |
|
#include "standard.h" |
11 |
< |
|
11 |
> |
#include "sm_flag.h" |
12 |
|
#include "sm_list.h" |
13 |
|
#include "sm_geom.h" |
14 |
+ |
#include "sm_qtree.h" |
15 |
+ |
#include "sm_stree.h" |
16 |
|
#include "sm.h" |
17 |
|
|
18 |
< |
static EDGE Edges[MAX_EDGES]; |
18 |
> |
static int Max_edges=200; |
19 |
> |
static EDGE *Edges=NULL; |
20 |
|
static int Ecnt=0; |
21 |
|
|
19 |
– |
|
20 |
– |
smLocator_remove_tri(sm,t_id) |
21 |
– |
SM *sm; |
22 |
– |
int t_id; |
23 |
– |
{ |
24 |
– |
STREE *st; |
25 |
– |
char found; |
26 |
– |
TRI *t; |
27 |
– |
FVECT p0,p1,p2; |
28 |
– |
|
29 |
– |
st = SM_LOCATOR(sm); |
30 |
– |
|
31 |
– |
t = SM_NTH_TRI(sm,t_id); |
32 |
– |
|
33 |
– |
smDir(sm,p0,T_NTH_V(t,0)); |
34 |
– |
smDir(sm,p1,T_NTH_V(t,1)); |
35 |
– |
smDir(sm,p2,T_NTH_V(t,2)); |
36 |
– |
|
37 |
– |
found = stRemove_tri(st,t_id,p0,p1,p2); |
38 |
– |
|
39 |
– |
return(found); |
40 |
– |
} |
41 |
– |
|
22 |
|
smFree_tri(sm,id) |
23 |
|
SM *sm; |
24 |
|
int id; |
25 |
|
{ |
26 |
< |
TRI *tri; |
26 |
> |
TRI *tri,*t; |
27 |
|
|
28 |
|
tri = SM_NTH_TRI(sm,id); |
29 |
|
/* Add to the free_list */ |
30 |
+ |
|
31 |
|
T_NEXT_FREE(tri) = SM_FREE_TRIS(sm); |
32 |
|
SM_FREE_TRIS(sm) = id; |
33 |
|
T_VALID_FLAG(tri) = -1; |
41 |
|
int t_id; |
42 |
|
{ |
43 |
|
|
63 |
– |
|
44 |
|
/* NOTE: Assumes that a new triangle adjacent to each vertex |
45 |
|
has been added- before the deletion: replacing |
46 |
|
the old tri- and therefore dont need to dereference any pointers |
47 |
|
to id because the vertices can no longer |
48 |
|
point to tri id as being the first triangle pointer |
49 |
|
*/ |
50 |
+ |
SM_SAMPLE_TRIS(sm)--; |
51 |
|
if(!SM_IS_NTH_T_BASE(sm,t_id)) |
52 |
|
{ |
53 |
< |
SM_NUM_TRIS(sm)--; |
53 |
> |
|
54 |
> |
#if 0 |
55 |
|
if(SM_IS_NTH_T_NEW(sm,t_id)) |
56 |
|
smNew_tri_cnt--; |
57 |
+ |
#endif |
58 |
|
} |
59 |
|
smClear_tri_flags(sm,t_id); |
60 |
|
|
61 |
|
smFree_tri(sm,t_id); |
79 |
– |
|
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
+ |
int |
66 |
+ |
eNew_edge() |
67 |
+ |
{ |
68 |
+ |
if(!Edges) |
69 |
+ |
if(!(Edges = (EDGE *)realloc(NULL,(Max_edges+1)*sizeof(EDGE)))) |
70 |
+ |
goto memerr; |
71 |
|
|
72 |
< |
LIST |
73 |
< |
*smVertex_star_polygon(sm,id) |
72 |
> |
if(Ecnt >= Max_edges) |
73 |
> |
{ |
74 |
> |
if(Max_edges > 10000) |
75 |
> |
error(CONSISTENCY,"Too many edges in vertex loop\n"); |
76 |
> |
Max_edges += 100; |
77 |
> |
if(!(Edges = (EDGE *)realloc(Edges,(Max_edges+1)*sizeof(EDGE)))) |
78 |
> |
goto memerr; |
79 |
> |
} |
80 |
> |
return(++Ecnt); |
81 |
> |
|
82 |
> |
memerr: |
83 |
> |
error(SYSTEM,"eNew_edge(): Unable to allocate memory"); |
84 |
> |
} |
85 |
> |
|
86 |
> |
/* Return list of edges defining polygon formed by boundary of triangles |
87 |
> |
adjacent to id. Return set of triangles adjacent to id to delete in delptr |
88 |
> |
*/ |
89 |
> |
LIST |
90 |
> |
*smVertexPolygon(sm,id,del_ptr) |
91 |
|
SM *sm; |
92 |
|
int id; |
93 |
+ |
LIST **del_ptr; |
94 |
|
{ |
95 |
|
TRI *tri,*t_next; |
96 |
< |
LIST *elist,*end,*tlist; |
97 |
< |
int t_id,v_next,t_next_id; |
92 |
< |
int e; |
96 |
> |
LIST *elist,*end; |
97 |
> |
int e,t_id,v_next,t_next_id,b_id,v_id; |
98 |
|
|
99 |
+ |
eClear_edges(); |
100 |
|
elist = end = NULL; |
101 |
+ |
|
102 |
|
/* Get the first triangle adjacent to vertex id */ |
103 |
|
t_id = SM_NTH_VERT(sm,id); |
104 |
|
tri = SM_NTH_TRI(sm,t_id); |
105 |
|
|
106 |
< |
|
107 |
< |
if((e = eNew_edge()) == SM_INVALID) |
108 |
< |
{ |
109 |
< |
#ifdef DEBUG |
110 |
< |
eputs("smVertex_star_polygon():Too many edges\n"); |
111 |
< |
#endif |
112 |
< |
return(NULL); |
113 |
< |
} |
106 |
> |
e = eNew_edge(); |
107 |
> |
/* Get the next vertex on the polygon boundary */ |
108 |
> |
v_id = T_WHICH_V(tri,id); |
109 |
> |
b_id = (v_id + 1)%3; |
110 |
> |
/* Create an edge */ |
111 |
> |
SET_E_NTH_VERT(e,0,T_NTH_V(tri,b_id)); |
112 |
> |
SET_E_NTH_TRI(e,0,INVALID); |
113 |
> |
SET_E_NTH_TRI(e,1,T_NTH_NBR(tri,v_id)); |
114 |
> |
v_next = T_NTH_V(tri,(b_id+1)%3); |
115 |
> |
SET_E_NTH_VERT(e,1,v_next); |
116 |
|
elist = add_data_to_circular_list(elist,&end,e); |
108 |
– |
v_next = (T_WHICH_V(tri,id)+1)%3; |
109 |
– |
SET_E_NTH_VERT(e,0,T_NTH_V(tri,v_next)); |
110 |
– |
SET_E_NTH_TRI(e,0,SM_INVALID); |
111 |
– |
SET_E_NTH_TRI(e,1,T_NTH_NBR(tri,v_next)); |
112 |
– |
v_next = (T_WHICH_V(tri,id)+2)%3; |
113 |
– |
SET_E_NTH_VERT(e,1,T_NTH_V(tri,v_next)); |
114 |
– |
|
115 |
– |
|
117 |
|
t_next_id = t_id; |
118 |
|
t_next = tri; |
119 |
|
|
120 |
< |
tlist = push_data(NULL,t_id); |
120 |
> |
*del_ptr = push_data(*del_ptr,t_id); |
121 |
> |
/* Create a set to hold all of the triangles for deletion later */ |
122 |
|
|
123 |
< |
while((t_next_id = smTri_next_ccw_nbr(sm,t_next,id)) != t_id) |
122 |
< |
{ |
123 |
< |
if((e = eNew_edge()) == SM_INVALID) |
124 |
< |
{ |
125 |
< |
#ifdef DEBUG |
126 |
< |
eputs("smVertex_star_polygon():Too many edges\n"); |
127 |
< |
#endif |
128 |
< |
return(NULL); |
129 |
< |
} |
130 |
< |
elist = add_data_to_circular_list(elist,&end,e); |
131 |
< |
t_next = SM_NTH_TRI(sm,t_next_id); |
132 |
< |
v_next = (T_WHICH_V(t_next,id)+1)%3; |
133 |
< |
SET_E_NTH_VERT(e,0,T_NTH_V(t_next,v_next)); |
134 |
< |
SET_E_NTH_TRI(e,0,SM_INVALID); |
135 |
< |
SET_E_NTH_TRI(e,1,T_NTH_NBR(t_next,v_next)); |
136 |
< |
v_next = (T_WHICH_V(t_next,id)+2)%3; |
137 |
< |
SET_E_NTH_VERT(e,1,T_NTH_V(t_next,v_next)); |
138 |
< |
tlist = push_data(tlist,t_next_id); |
139 |
< |
} |
140 |
< |
while(tlist) |
123 |
> |
while((t_next_id = T_NTH_NBR(t_next,b_id)) != t_id) |
124 |
|
{ |
125 |
< |
t_id = (int)pop_list(&tlist); |
126 |
< |
/* first remove from point location structure */ |
127 |
< |
smLocator_remove_tri(sm,t_id); |
128 |
< |
smDelete_tri(sm,t_id); |
125 |
> |
e = eNew_edge(); |
126 |
> |
t_next = SM_NTH_TRI(sm,t_next_id); |
127 |
> |
SET_E_NTH_VERT(e,0,v_next); |
128 |
> |
SET_E_NTH_TRI(e,0,INVALID); |
129 |
> |
v_id = T_WHICH_V(t_next,id); |
130 |
> |
b_id = (v_id + 1)%3; |
131 |
> |
SET_E_NTH_TRI(e,1,T_NTH_NBR(t_next,v_id)); |
132 |
> |
v_next = T_NTH_V(t_next,(b_id+1)%3); |
133 |
> |
SET_E_NTH_VERT(e,1,v_next); |
134 |
> |
elist = add_data_to_circular_list(elist,&end,e); |
135 |
> |
*del_ptr = push_data(*del_ptr,t_next_id); |
136 |
|
} |
137 |
|
return(elist); |
138 |
|
} |
139 |
|
|
140 |
+ |
|
141 |
|
int |
142 |
< |
smEdge_intersect_polygon(sm,v0,v1,l) |
142 |
> |
smTriangulate_add_tri(sm,id0,id1,id2,e0,e1,e2ptr) |
143 |
|
SM *sm; |
144 |
< |
FVECT v0,v1; |
154 |
< |
LIST *l; |
144 |
> |
int id0,id1,id2,e0,e1,*e2ptr; |
145 |
|
{ |
146 |
< |
FVECT e0,e1; |
147 |
< |
int e,id_e0,id_e1; |
158 |
< |
LIST *el,*eptr; |
159 |
< |
|
160 |
< |
/* Test the edges in l against v0v1 to see if v0v1 intersects |
161 |
< |
any other edges |
162 |
< |
*/ |
163 |
< |
|
164 |
< |
el = l; |
146 |
> |
int t_id; |
147 |
> |
int e2; |
148 |
|
|
149 |
< |
while(el) |
150 |
< |
{ |
151 |
< |
e = (int)LIST_DATA(el); |
169 |
< |
id_e0 = E_NTH_VERT(e,0); |
170 |
< |
id_e1 = E_NTH_VERT(e,1); |
171 |
< |
|
172 |
< |
smDir(sm,e0,id_e0); |
173 |
< |
smDir(sm,e1,id_e1); |
174 |
< |
if(spherical_polygon_edge_intersect(v0,v1,e0,e1)) |
175 |
< |
return(TRUE); |
176 |
< |
|
177 |
< |
el = LIST_NEXT(el); |
178 |
< |
if(el == l) |
179 |
< |
break; |
180 |
< |
} |
181 |
< |
return(FALSE); |
182 |
< |
} |
183 |
< |
|
184 |
< |
int |
185 |
< |
smFind_next_convex_vertex(sm,id0,id1,v0,v1,l) |
186 |
< |
SM *sm; |
187 |
< |
int id0,id1; |
188 |
< |
FVECT v0,v1; |
189 |
< |
LIST *l; |
190 |
< |
{ |
191 |
< |
int e,id; |
192 |
< |
LIST *el; |
193 |
< |
FVECT v; |
194 |
< |
|
195 |
< |
/* starting with the end of edge at head of l, search sequentially for |
196 |
< |
vertex v such that v0v1v is a convex angle, and the edge v1v does |
197 |
< |
not intersect any other edges |
198 |
< |
*/ |
199 |
< |
id = SM_INVALID; |
200 |
< |
el = l; |
201 |
< |
while(id != id0) |
202 |
< |
{ |
203 |
< |
e = (int)LIST_DATA(el); |
204 |
< |
id = E_NTH_VERT(e,1); |
205 |
< |
|
206 |
< |
smDir(sm,v,id); |
207 |
< |
|
208 |
< |
if(convex_angle(v0,v1,v) && !smEdge_intersect_polygon(sm,v1,v,l)) |
209 |
< |
return(id); |
210 |
< |
|
211 |
< |
el = LIST_NEXT(el); |
212 |
< |
if(el == l) |
213 |
< |
break; |
214 |
< |
} |
215 |
< |
return(SM_INVALID); |
216 |
< |
} |
217 |
< |
|
218 |
< |
int |
219 |
< |
split_edge_list(id0,id_new,l,lnew) |
220 |
< |
int id0,id_new; |
221 |
< |
LIST **l,**lnew; |
222 |
< |
{ |
223 |
< |
LIST *list,*lptr,*end; |
224 |
< |
int e,e1,e2,new_e; |
225 |
< |
|
226 |
< |
e2 = SM_INVALID; |
227 |
< |
list = lptr = *l; |
228 |
< |
|
229 |
< |
if((new_e = eNew_edge())==SM_INVALID) |
230 |
< |
{ |
231 |
< |
#ifdef DEBUG |
232 |
< |
eputs("split_edge_list():Too many edges\n"); |
149 |
> |
#ifdef DEBUG |
150 |
> |
if(id0 == INVALID || id1==INVALID || id2==INVALID) |
151 |
> |
error(CONSISTENCY,"bad id- smTriangulate_add_tri()\n"); |
152 |
|
#endif |
153 |
< |
return(FALSE); |
154 |
< |
} |
155 |
< |
SET_E_NTH_VERT(new_e,0,id0); |
156 |
< |
SET_E_NTH_VERT(new_e,1,id_new); |
157 |
< |
SET_E_NTH_TRI(new_e,0,SM_INVALID); |
158 |
< |
SET_E_NTH_TRI(new_e,1,SM_INVALID); |
159 |
< |
|
160 |
< |
while(e2 != id_new) |
161 |
< |
{ |
162 |
< |
lptr = LIST_NEXT(lptr); |
163 |
< |
e = (int)LIST_DATA(lptr); |
164 |
< |
e2 = E_NTH_VERT(e,1); |
165 |
< |
if(lptr == list) |
247 |
< |
{ |
248 |
< |
#ifdef DEBUG |
249 |
< |
eputs("split_edge_list():cant find vertex\n"); |
250 |
< |
#endif |
251 |
< |
*lnew = NULL; |
252 |
< |
return(FALSE); |
253 |
< |
} |
153 |
> |
t_id = smAdd_tri(sm,id0,id1,id2); |
154 |
> |
if(*e2ptr == 0) |
155 |
> |
{ |
156 |
> |
e2 = eNew_edge(); |
157 |
> |
SET_E_NTH_VERT(e2,0,id2); |
158 |
> |
SET_E_NTH_VERT(e2,1,id0); |
159 |
> |
} |
160 |
> |
else |
161 |
> |
e2 = *e2ptr; |
162 |
> |
/* set appropriate tri for each edge*/ |
163 |
> |
SET_E_NTH_TRI(e0,0,t_id); |
164 |
> |
SET_E_NTH_TRI(e1,0,t_id); |
165 |
> |
SET_E_NTH_TRI(e2,0,t_id); |
166 |
|
|
167 |
< |
} |
168 |
< |
end = lptr; |
257 |
< |
lptr = LIST_NEXT(lptr); |
258 |
< |
list = add_data_to_circular_list(list,&end,-new_e); |
259 |
< |
*lnew = list; |
260 |
< |
|
261 |
< |
/* now follow other cycle */ |
262 |
< |
|
263 |
< |
list = lptr; |
264 |
< |
e2 = SM_INVALID; |
265 |
< |
while(e2 != id0) |
266 |
< |
{ |
267 |
< |
lptr = LIST_NEXT(lptr); |
268 |
< |
e = (int)LIST_DATA(lptr); |
269 |
< |
e2 = E_NTH_VERT(e,1); |
270 |
< |
if(lptr == list) |
271 |
< |
{ |
272 |
< |
#ifdef DEBUG |
273 |
< |
eputs("split_edge_list():cant find intial vertex\n"); |
274 |
< |
#endif |
275 |
< |
*l = NULL; |
276 |
< |
return(FALSE); |
277 |
< |
} |
278 |
< |
|
279 |
< |
} |
280 |
< |
end = lptr; |
281 |
< |
list = add_data_to_circular_list(list,&end,new_e); |
282 |
< |
*l = list; |
283 |
< |
return(TRUE); |
167 |
> |
*e2ptr = e2; |
168 |
> |
return(t_id); |
169 |
|
} |
170 |
|
|
171 |
< |
|
172 |
< |
|
288 |
< |
LIST |
289 |
< |
*smTriangulate_convex(sm,plist) |
171 |
> |
int |
172 |
> |
smTriangulateConvex(sm,plist,add_ptr) |
173 |
|
SM *sm; |
174 |
< |
LIST *plist; |
174 |
> |
LIST *plist,**add_ptr; |
175 |
|
{ |
293 |
– |
TRI *tri; |
176 |
|
int t_id,e_id0,e_id1,e_id2; |
177 |
|
int v_id0,v_id1,v_id2; |
178 |
|
LIST *lptr; |
297 |
– |
int cnt; |
179 |
|
|
180 |
|
lptr = plist; |
181 |
|
e_id0 = (int)LIST_DATA(lptr); |
186 |
|
e_id1 = (int)LIST_DATA(lptr); |
187 |
|
v_id1 = E_NTH_VERT(e_id1,0); |
188 |
|
v_id2 = E_NTH_VERT(e_id1,1); |
308 |
– |
/* form a triangle for each triple of with v0 as base of star */ |
309 |
– |
t_id = smAdd_tri(sm,v_id0,v_id1,v_id2,&tri); |
310 |
– |
smLocator_add_tri(sm,t_id,v_id0,v_id1,v_id2); |
311 |
– |
/* add which pointer?*/ |
312 |
– |
|
189 |
|
lptr = LIST_NEXT(lptr); |
190 |
|
|
191 |
< |
if(LIST_NEXT(lptr) != plist) |
192 |
< |
{ |
317 |
< |
e_id2 = eNew_edge(); |
318 |
< |
SET_E_NTH_VERT(e_id2,0,v_id2); |
319 |
< |
SET_E_NTH_VERT(e_id2,1,v_id0); |
320 |
< |
} |
191 |
> |
if(LIST_NEXT(lptr) != plist) |
192 |
> |
e_id2 = 0; |
193 |
|
else |
194 |
|
e_id2 = (int)LIST_DATA(lptr); |
195 |
< |
|
196 |
< |
/* set appropriate tri for each edge*/ |
325 |
< |
SET_E_NTH_TRI(e_id0,0,t_id); |
326 |
< |
SET_E_NTH_TRI(e_id1,0,t_id); |
327 |
< |
SET_E_NTH_TRI(e_id2,0,t_id); |
328 |
< |
|
195 |
> |
t_id = smTriangulate_add_tri(sm,v_id0,v_id1,v_id2,e_id0,e_id1,&e_id2); |
196 |
> |
*add_ptr = push_data(*add_ptr,t_id); |
197 |
|
e_id0 = -e_id2; |
198 |
|
} |
199 |
|
free_list(plist); |
200 |
+ |
return(TRUE); |
201 |
|
} |
202 |
+ |
#ifdef TEST_DRIVER |
203 |
+ |
FVECT Norm[500],B_V[500]; |
204 |
+ |
int Ncnt,Bcnt,Del=0; |
205 |
+ |
#endif |
206 |
|
|
207 |
< |
smTriangulate_elist(sm,plist) |
207 |
> |
|
208 |
> |
/* Triangulate the polygon defined by plist, and generating vertex p_id. |
209 |
> |
Return list of added triangles in list add_ptr. Returns TRUE if |
210 |
> |
successful, FALSE otherwise. This is NOT a general triangulation routine, |
211 |
> |
assumes polygon star relative to id |
212 |
> |
*/ |
213 |
> |
|
214 |
> |
int |
215 |
> |
smTriangulate(sm,id,plist,add_ptr) |
216 |
|
SM *sm; |
217 |
< |
LIST *plist; |
217 |
> |
int id; |
218 |
> |
LIST *plist,**add_ptr; |
219 |
|
{ |
220 |
< |
LIST *l,*el1; |
221 |
< |
FVECT v0,v1,v2; |
222 |
< |
int id0,id1,id2,e,id_next; |
223 |
< |
char flipped; |
224 |
< |
int debug = TRUE; |
220 |
> |
LIST *l,*prev,*t; |
221 |
> |
FVECT v0,v1,v2,n,p; |
222 |
> |
int is_tri,is_convex,cut,t_id,id0,id1,id2,e2,e1,enew; |
223 |
> |
double dp; |
224 |
> |
static int debug=0; |
225 |
|
|
226 |
< |
l = plist; |
227 |
< |
|
226 |
> |
VSUB(p,SM_NTH_WV(sm,id),SM_VIEW_CENTER(sm)); |
227 |
> |
enew = 0; |
228 |
> |
is_convex = TRUE; |
229 |
> |
cut = is_tri= FALSE; |
230 |
> |
l = prev = plist; |
231 |
> |
|
232 |
> |
/* get v0,v1 */ |
233 |
> |
e1 = (int)LIST_DATA(l); |
234 |
> |
id0 = E_NTH_VERT(e1,0); |
235 |
> |
id1 = E_NTH_VERT(e1,1); |
236 |
> |
VSUB(v0,SM_NTH_WV(sm,id0),SM_VIEW_CENTER(sm)); |
237 |
> |
VSUB(v1,SM_NTH_WV(sm,id1),SM_VIEW_CENTER(sm)); |
238 |
> |
#ifdef TEST_DRIVER |
239 |
> |
Del = TRUE; |
240 |
> |
VCOPY(B_V[0],v0); |
241 |
> |
VCOPY(B_V[1],v1); |
242 |
> |
Bcnt = 2; |
243 |
> |
Ncnt = 0; |
244 |
> |
#endif |
245 |
|
while(l) |
246 |
|
{ |
348 |
– |
/* get v0,v1,v2 */ |
349 |
– |
e = (int)LIST_DATA(l); |
350 |
– |
id0 = E_NTH_VERT(e,0); |
351 |
– |
id1 = E_NTH_VERT(e,1); |
247 |
|
l = LIST_NEXT(l); |
248 |
< |
e = (int)LIST_DATA(l); |
249 |
< |
id2 = E_NTH_VERT(e,1); |
248 |
> |
/* Get v2 */ |
249 |
> |
e2 = (int)LIST_DATA(l); |
250 |
> |
id2 = E_NTH_VERT(e2,1); |
251 |
> |
VSUB(v2,SM_NTH_WV(sm,id2),SM_VIEW_CENTER(sm)); |
252 |
> |
#ifdef TEST_DRIVER |
253 |
> |
VCOPY(B_V[Bcnt++],v2); |
254 |
> |
#endif |
255 |
> |
if(LIST_NEXT(LIST_NEXT(l)) == prev) |
256 |
> |
{/* Check if have a triangle */ |
257 |
> |
is_tri = TRUE; |
258 |
> |
break; |
259 |
> |
} |
260 |
|
|
261 |
< |
smDir(sm,v0,id0); |
262 |
< |
smDir(sm,v1,id1); |
263 |
< |
smDir(sm,v2,id2); |
264 |
< |
/* determine if convex (left turn), or concave(right turn) angle */ |
360 |
< |
if(convex_angle(v0,v1,v2)) |
261 |
> |
/* determine if v0-v1-v2 is convex:defined clockwise on the sphere- |
262 |
> |
so switch orientation |
263 |
> |
*/ |
264 |
> |
if(convex_angle(v2,v1,v0)) |
265 |
|
{ |
266 |
< |
if(l == plist) |
267 |
< |
break; |
268 |
< |
else |
266 |
> |
/* test if safe to cut off v0-v1-v2 by testing if p lies outside of |
267 |
> |
triangle v0-v1-v2: if so, because plist is the star polygon around p, |
268 |
> |
the new edge v2-v0 cannot intersect any existing edges |
269 |
> |
*/ |
270 |
> |
VCROSS(n,v0,v2); |
271 |
> |
dp = DOT(n,p); |
272 |
> |
if(dp <= 0.0) |
273 |
> |
{ |
274 |
> |
/* remove edges e1,e2 and add triangle id0,id1,id2 */ |
275 |
> |
enew = 0; |
276 |
> |
t_id = smTriangulate_add_tri(sm,id0,id1,id2,e1,e2,&enew); |
277 |
> |
cut = TRUE; |
278 |
> |
*add_ptr = push_data(*add_ptr,t_id); |
279 |
> |
/* Insert edge enew into the list, reuse prev list element */ |
280 |
> |
LIST_NEXT(prev) = LIST_NEXT(l); |
281 |
> |
LIST_DATA(prev) = e1 = -enew; |
282 |
> |
/* If removing head of list- reset plist pointer */ |
283 |
> |
if(l== plist) |
284 |
> |
plist = prev; |
285 |
> |
/* free list element for e2 */ |
286 |
> |
LIST_NEXT(l)=NULL; |
287 |
> |
free_list(l); |
288 |
> |
l = prev; |
289 |
> |
VCOPY(v1,v2); |
290 |
> |
id1 = id2; |
291 |
|
continue; |
292 |
+ |
} |
293 |
|
} |
294 |
< |
/* if concave: add edge and recurse on two sub polygons */ |
295 |
< |
id_next = smFind_next_convex_vertex(sm,id0,id1,v0,v1,LIST_NEXT(l)); |
296 |
< |
if(id_next == SM_INVALID) |
294 |
> |
else |
295 |
> |
is_convex = FALSE; |
296 |
> |
VCOPY(v0,v1); |
297 |
> |
VCOPY(v1,v2); |
298 |
> |
id0 = id1; |
299 |
> |
id1 = id2; |
300 |
> |
e1 = e2; |
301 |
> |
/* check if gone around circular list without adding any |
302 |
> |
triangles: prevent infinite loop */ |
303 |
> |
if(l == plist) |
304 |
|
{ |
305 |
+ |
if(LIST_NEXT(LIST_NEXT(l)) == prev) |
306 |
+ |
{/* Check if have a triangle */ |
307 |
+ |
is_tri = TRUE; |
308 |
+ |
break; |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
if(is_convex) |
312 |
+ |
break; |
313 |
+ |
if(!cut) |
314 |
+ |
{ |
315 |
|
#ifdef DEBUG |
316 |
< |
eputs("smTriangulate_elist():Unable to find convex vertex\n"); |
316 |
> |
eputs("smTriangulate():Unable to triangulate\n"); |
317 |
|
#endif |
318 |
< |
return; |
318 |
> |
free_list(l); |
319 |
> |
while(*add_ptr) |
320 |
> |
{ |
321 |
> |
t_id = pop_list(add_ptr); |
322 |
> |
smDelete_tri(sm,t_id); |
323 |
> |
} |
324 |
> |
return(FALSE); |
325 |
> |
} |
326 |
> |
|
327 |
> |
cut = FALSE; |
328 |
> |
is_convex = TRUE; |
329 |
|
} |
330 |
< |
/* add edge */ |
377 |
< |
el1 = NULL; |
378 |
< |
/* Split edge list l into two lists: one from id1-id_next-id1, |
379 |
< |
and the next from id2-id_next-id2 |
380 |
< |
*/ |
381 |
< |
debug = split_edge_list(id1,id_next,&l,&el1); |
382 |
< |
/* Recurse and triangulate the two edge lists */ |
383 |
< |
if(debug) |
384 |
< |
debug = smTriangulate_elist(sm,l); |
385 |
< |
if(debug) |
386 |
< |
debug = smTriangulate_elist(sm,el1); |
387 |
< |
|
388 |
< |
return(debug); |
330 |
> |
prev = l; |
331 |
|
} |
332 |
< |
smTriangulate_convex(sm,plist); |
333 |
< |
return(TRUE); |
334 |
< |
} |
332 |
> |
if(is_tri) |
333 |
> |
{ |
334 |
> |
l = LIST_NEXT(l); |
335 |
> |
enew = (int)LIST_DATA(l); |
336 |
> |
t_id = smTriangulate_add_tri(sm,id0,id1,id2,e1,e2,&enew); |
337 |
> |
*add_ptr = push_data(*add_ptr,t_id); |
338 |
> |
free_list(l); |
339 |
> |
} |
340 |
> |
else |
341 |
> |
if(!smTriangulateConvex(sm,l,add_ptr)) |
342 |
> |
return(FALSE); |
343 |
|
|
344 |
< |
int |
345 |
< |
smTriangulate_polygon(sm,plist) |
396 |
< |
SM *sm; |
397 |
< |
LIST *plist; |
398 |
< |
{ |
399 |
< |
int e,id_t0,id_t1,e0,e1; |
400 |
< |
TRI *t0,*t1; |
401 |
< |
int test; |
402 |
< |
|
403 |
< |
test = smTriangulate_elist(sm,plist,NULL); |
404 |
< |
|
405 |
< |
if(!test) |
406 |
< |
return(test); |
407 |
< |
FOR_ALL_EDGES(e) |
344 |
> |
/* Set triangle adjacencies based on edge adjacencies */ |
345 |
> |
FOR_ALL_EDGES(enew) |
346 |
|
{ |
347 |
< |
id_t0 = E_NTH_TRI(e,0); |
348 |
< |
id_t1 = E_NTH_TRI(e,1); |
411 |
< |
if((id_t0==SM_INVALID) || (id_t1==SM_INVALID)) |
412 |
< |
{ |
413 |
< |
#ifdef DEBUG |
414 |
< |
eputs("smTriangulate_polygon(): Unassigned edge neighbor\n"); |
415 |
< |
#endif |
416 |
< |
continue; |
417 |
< |
} |
418 |
< |
t0 = SM_NTH_TRI(sm,id_t0); |
419 |
< |
t1 = SM_NTH_TRI(sm,id_t1); |
347 |
> |
id0 = E_NTH_TRI(enew,0); |
348 |
> |
id1 = E_NTH_TRI(enew,1); |
349 |
|
|
350 |
< |
e0 = T_WHICH_V(t0,E_NTH_VERT(e,0)); |
351 |
< |
T_NTH_NBR(t0,e0) = id_t1; |
352 |
< |
|
353 |
< |
e1 = T_WHICH_V(t1,E_NTH_VERT(e,1)); |
354 |
< |
T_NTH_NBR(t1,e1) = id_t0; |
350 |
> |
e1 = (T_WHICH_V(SM_NTH_TRI(sm,id0),E_NTH_VERT(enew,0))+2)%3; |
351 |
> |
T_NTH_NBR(SM_NTH_TRI(sm,id0),e1) = id1; |
352 |
> |
|
353 |
> |
e2 = (T_WHICH_V(SM_NTH_TRI(sm,id1),E_NTH_VERT(enew,1))+2)%3; |
354 |
> |
T_NTH_NBR(SM_NTH_TRI(sm,id1),e2) = id0; |
355 |
|
} |
356 |
< |
return(test); |
356 |
> |
return(TRUE); |
357 |
|
} |
358 |
|
|
359 |
|
eIn_tri(e,t) |
368 |
|
return(T_NTH_V(t,0)==E_NTH_VERT(e,1)||T_NTH_V(t,2)==E_NTH_VERT(e,1)); |
369 |
|
else if(T_NTH_V(t,2)==E_NTH_VERT(e,0)) |
370 |
|
return(T_NTH_V(t,0)==E_NTH_VERT(e,1)||T_NTH_V(t,1)==E_NTH_VERT(e,1)); |
371 |
+ |
|
372 |
|
return(FALSE); |
373 |
|
} |
374 |
< |
smFix_edges(sm) |
374 |
> |
|
375 |
> |
/* Test the new set of triangles for Delaunay condition. 'Edges' contains |
376 |
> |
all of the new edges added. The CCW triangle assoc with each edge is |
377 |
> |
tested against the opposite vertex of the CW triangle. If the vertex |
378 |
> |
lies inside the circle defined by the CCW triangle- the edge is swapped |
379 |
> |
for the opposite diagonal |
380 |
> |
*/ |
381 |
> |
smFixEdges(sm,add_list) |
382 |
|
SM *sm; |
383 |
+ |
LIST *add_list; |
384 |
|
{ |
385 |
< |
int e,id_t0,id_t1,e_new,e0,e1,e0_next,e1_next; |
386 |
< |
TRI *t0,*t1,*nt0,*nt1; |
449 |
< |
int i,id_v0,id_v1,id_v2,id_p,nid_t0,nid_t1; |
385 |
> |
int e,t0_id,t1_id,e_new,e0,e1,e0_next,e1_next; |
386 |
> |
int i,v0_id,v1_id,v2_id,p_id,t0_nid,t1_nid; |
387 |
|
FVECT v0,v1,v2,p,np,v; |
388 |
< |
LIST *add,*del; |
389 |
< |
|
453 |
< |
add = del = NULL; |
388 |
> |
TRI *t0,*t1; |
389 |
> |
|
390 |
|
FOR_ALL_EDGES(e) |
391 |
|
{ |
392 |
< |
id_t0 = E_NTH_TRI(e,0); |
393 |
< |
id_t1 = E_NTH_TRI(e,1); |
394 |
< |
if((id_t0==SM_INVALID) || (id_t1==SM_INVALID)) |
392 |
> |
t0_id = E_NTH_TRI(e,0); |
393 |
> |
t1_id = E_NTH_TRI(e,1); |
394 |
> |
if((t0_id==INVALID) || (t1_id==INVALID)) |
395 |
|
{ |
396 |
|
#ifdef DEBUG |
397 |
< |
eputs("smFix_edges: Unassigned edge nbr\n"); |
397 |
> |
error(CONSISTENCY,"smFix_edges: Unassigned edge nbr\n"); |
398 |
|
#endif |
463 |
– |
continue; |
399 |
|
} |
400 |
< |
t0 = SM_NTH_TRI(sm,id_t0); |
401 |
< |
t1 = SM_NTH_TRI(sm,id_t1); |
402 |
< |
|
403 |
< |
e0 = T_WHICH_V(t0,E_NTH_VERT(e,0)); |
469 |
< |
e1 = T_WHICH_V(t1,E_NTH_VERT(-e,0)); |
470 |
< |
e0_next = (e0+2)%3; |
471 |
< |
e1_next = (e1+2)%3; |
472 |
< |
id_v0 = E_NTH_VERT(e,0); |
473 |
< |
id_v1 = E_NTH_VERT(e,1); |
474 |
< |
id_v2 = T_NTH_V(t0,e0_next); |
475 |
< |
id_p = T_NTH_V(t1,e1_next); |
400 |
> |
t0 = SM_NTH_TRI(sm,t0_id); |
401 |
> |
t1 = SM_NTH_TRI(sm,t1_id); |
402 |
> |
e0 = T_NTH_NBR_PTR(t1_id,t0); |
403 |
> |
e1 = T_NTH_NBR_PTR(t0_id,t1); |
404 |
|
|
405 |
< |
smDir(sm,v0,id_v0); |
406 |
< |
smDir(sm,v1,id_v1); |
407 |
< |
smDir(sm,v2,id_v2); |
405 |
> |
v0_id = E_NTH_VERT(e,0); |
406 |
> |
v1_id = E_NTH_VERT(e,1); |
407 |
> |
v2_id = T_NTH_V(t0,e0); |
408 |
> |
p_id = T_NTH_V(t1,e1); |
409 |
> |
|
410 |
> |
smDir_in_cone(sm,v0,v0_id); |
411 |
> |
smDir_in_cone(sm,v1,v1_id); |
412 |
> |
smDir_in_cone(sm,v2,v2_id); |
413 |
|
|
414 |
< |
VCOPY(p,SM_NTH_WV(sm,id_p)); |
414 |
> |
VCOPY(p,SM_NTH_WV(sm,p_id)); |
415 |
|
VSUB(p,p,SM_VIEW_CENTER(sm)); |
416 |
|
if(point_in_cone(p,v0,v1,v2)) |
417 |
|
{ |
418 |
< |
smTris_swap_edge(sm,id_t0,id_t1,e0,e1,&nid_t0,&nid_t1,&add,&del); |
418 |
> |
smTris_swap_edge(sm,t0_id,t1_id,e0,e1,&t0_nid,&t1_nid,&add_list); |
419 |
|
|
420 |
< |
nt0 = SM_NTH_TRI(sm,nid_t0); |
421 |
< |
nt1 = SM_NTH_TRI(sm,nid_t1); |
420 |
> |
/* Adjust the triangle pointers of the remaining edges to be |
421 |
> |
processed |
422 |
> |
*/ |
423 |
|
FOR_ALL_EDGES_FROM(e,i) |
424 |
|
{ |
425 |
< |
if(E_NTH_TRI(i,0)==id_t0 || E_NTH_TRI(i,0)==id_t1) |
425 |
> |
if(E_NTH_TRI(i,0)==t0_id || E_NTH_TRI(i,0)==t1_id) |
426 |
|
{ |
427 |
< |
if(eIn_tri(i,nt0)) |
428 |
< |
SET_E_NTH_TRI(i,0,nid_t0); |
427 |
> |
if(eIn_tri(i,SM_NTH_TRI(sm,t0_nid))) |
428 |
> |
SET_E_NTH_TRI(i,0,t0_nid); |
429 |
|
else |
430 |
< |
SET_E_NTH_TRI(i,0,nid_t1); |
430 |
> |
SET_E_NTH_TRI(i,0,t1_nid); |
431 |
|
} |
432 |
|
|
433 |
< |
if(E_NTH_TRI(i,1)==id_t0 || E_NTH_TRI(i,1)==id_t1) |
433 |
> |
if(E_NTH_TRI(i,1)==t0_id || E_NTH_TRI(i,1)==t1_id) |
434 |
|
{ |
435 |
< |
if(eIn_tri(i,nt0)) |
436 |
< |
SET_E_NTH_TRI(i,1,nid_t0); |
435 |
> |
if(eIn_tri(i,SM_NTH_TRI(sm,t0_nid))) |
436 |
> |
SET_E_NTH_TRI(i,1,t0_nid); |
437 |
|
else |
438 |
< |
SET_E_NTH_TRI(i,1,nid_t1); |
438 |
> |
SET_E_NTH_TRI(i,1,t1_nid); |
439 |
|
} |
440 |
|
} |
441 |
< |
id_t0 = nid_t0; |
442 |
< |
id_t1 = nid_t1; |
441 |
> |
t0_id = t0_nid; |
442 |
> |
t1_id = t1_nid; |
443 |
|
e_new = eNew_edge(); |
444 |
< |
SET_E_NTH_VERT(e_new,0,id_p); |
445 |
< |
SET_E_NTH_VERT(e_new,1,id_v2); |
446 |
< |
SET_E_NTH_TRI(e_new,0,id_t0); |
447 |
< |
SET_E_NTH_TRI(e_new,1,id_t1); |
444 |
> |
SET_E_NTH_VERT(e_new,0,p_id); |
445 |
> |
SET_E_NTH_VERT(e_new,1,v2_id); |
446 |
> |
SET_E_NTH_TRI(e_new,0,t0_id); |
447 |
> |
SET_E_NTH_TRI(e_new,1,t1_id); |
448 |
|
} |
449 |
|
} |
450 |
< |
smUpdate_locator(sm,add,del); |
450 |
> |
/* Add/Delete the appropriate triangles from the stree */ |
451 |
> |
smUpdate_locator(sm,add_list); |
452 |
|
} |
453 |
|
|
454 |
+ |
/* Remove vertex "id" from the mesh- and retriangulate the resulting |
455 |
+ |
hole: Returns TRUE if successful, FALSE otherwise. |
456 |
+ |
*/ |
457 |
|
int |
458 |
< |
smMesh_remove_vertex(sm,id) |
458 |
> |
smRemoveVertex(sm,id) |
459 |
|
SM *sm; |
460 |
|
int id; |
461 |
|
{ |
462 |
< |
int tri; |
463 |
< |
LIST *elist; |
464 |
< |
int cnt,debug; |
465 |
< |
/* generate list of vertices that form the boundary of the |
466 |
< |
star polygon formed by vertex id and all of its adjacent |
467 |
< |
triangles |
462 |
> |
LIST *b_list,*add_list,*del_list; |
463 |
> |
int t_id,i; |
464 |
> |
static int cnt=0; |
465 |
> |
OBJECT *optr,*os; |
466 |
> |
/* generate list of edges that form the boundary of the |
467 |
> |
polygon formed by the triangles adjacent to vertex 'id' |
468 |
|
*/ |
469 |
< |
eClear_edges(); |
470 |
< |
elist = smVertex_star_polygon(sm,id); |
533 |
< |
if(!elist) |
534 |
< |
return(FALSE); |
469 |
> |
del_list = NULL; |
470 |
> |
b_list = smVertexPolygon(sm,id,&del_list); |
471 |
|
|
472 |
< |
/* Triangulate spherical polygon */ |
473 |
< |
debug = smTriangulate_polygon(sm,elist); |
472 |
> |
add_list = NULL; |
473 |
> |
/* Triangulate polygonal hole */ |
474 |
> |
if(!smTriangulate(sm,id,b_list,&add_list)) |
475 |
> |
{ |
476 |
> |
free_list(del_list); |
477 |
> |
return(FALSE); |
478 |
> |
} |
479 |
> |
else |
480 |
> |
{ |
481 |
> |
#ifdef DEBUG |
482 |
> |
b_list = del_list; |
483 |
> |
while(b_list) |
484 |
> |
{ |
485 |
> |
t_id = LIST_DATA(b_list); |
486 |
> |
b_list = LIST_NEXT(b_list); |
487 |
> |
T_VALID_FLAG(SM_NTH_TRI(sm,t_id))=-1; |
488 |
> |
} |
489 |
> |
#endif |
490 |
> |
while(del_list) |
491 |
> |
{ |
492 |
> |
t_id = pop_list(&del_list); |
493 |
> |
smDelete_tri(sm,t_id); |
494 |
> |
} |
495 |
> |
} |
496 |
> |
/* Fix up new triangles to be Delaunay-delnode contains set of |
497 |
> |
triangles to delete,add_list is the set of new triangles to add |
498 |
> |
*/ |
499 |
> |
smFixEdges(sm,add_list); |
500 |
|
|
539 |
– |
|
540 |
– |
/* Fix up new triangles to be Delaunay */ |
541 |
– |
if(debug) |
542 |
– |
smFix_edges(sm); |
543 |
– |
|
501 |
|
return(TRUE); |
502 |
|
} |
503 |
|
|
547 |
– |
/* Remove point from samples, and from mesh. Delete any triangles |
548 |
– |
adjacent to the point and re-triangulate the hole |
549 |
– |
Return TRUE is point found , FALSE otherwise |
550 |
– |
*/ |
551 |
– |
int |
552 |
– |
smDelete_point(sm,id) |
553 |
– |
SM *sm; |
554 |
– |
int id; |
555 |
– |
{ |
504 |
|
|
557 |
– |
/* Remove the corresponding vertex from the mesh */ |
558 |
– |
smMesh_remove_vertex(sm,id); |
559 |
– |
/* Free the sample point */ |
560 |
– |
smDelete_sample(sm,id); |
561 |
– |
return(TRUE); |
562 |
– |
} |
505 |
|
|
506 |
< |
|
565 |
< |
|
506 |
> |
|
507 |
|
|
508 |
|
|
509 |
|
|