Debian Codegraph @

debian/external_deps/fetk/sg/src/glu/tess.c

raw
1 /* $Id: tess.c,v 1.2 2000/11/02 00:28:54 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.0
6 * Copyright (C) 1995-1996 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24 * This file is part of the polygon tesselation code contributed by
25 * Bogdan Sikorski
26 */
27
28
29 #include <math.h>
30 #include <stdlib.h>
31 #include "tess.h"
32
33
34
35 extern void tess_test_polygon(GLUtriangulatorObj *);
36 extern void tess_find_contour_hierarchies(GLUtriangulatorObj *);
37 extern void tess_handle_holes(GLUtriangulatorObj *);
38 extern void tess_tesselate(GLUtriangulatorObj *);
39 extern void tess_tesselate_with_edge_flag(GLUtriangulatorObj *);
40 static void delete_contours(GLUtriangulatorObj *);
41
42 void init_callbacks(tess_callbacks *callbacks)
43 {
44 callbacks->begin = ( void (*)(GLenum) ) 0;
45 callbacks->edgeFlag = ( void (*)(GLboolean) ) 0;
46 callbacks->vertex = ( void (*)(void*) ) 0;
47 callbacks->end = ( void (*)(void) ) 0;
48 callbacks->error = ( void (*)(GLenum) ) 0;
49 }
50
51 void tess_call_user_error(GLUtriangulatorObj *tobj,
52 GLenum gluerr)
53 {
54 if(tobj->error==GLU_NO_ERROR)
55 tobj->error=gluerr;
56 if(tobj->callbacks.error!=NULL)
57 (tobj->callbacks.error)(gluerr);
58 }
59
60 GLUtriangulatorObj* gluNewTess( void )
61 {
62 GLUtriangulatorObj *tobj;
63
64 if((tobj=(GLUtriangulatorObj *)
65 malloc(sizeof(struct GLUtriangulatorObj)))==NULL)
66 return NULL;
67 tobj->contours=tobj->last_contour=NULL;
68 init_callbacks(&tobj->callbacks);
69 tobj->error=GLU_NO_ERROR;
70 tobj->current_polygon=NULL;
71 tobj->contour_cnt=0;
72 return tobj;
73 }
74
75
76 void gluTessCallback( GLUtriangulatorObj *tobj, GLenum which,
77 void (*fn)(GLenum) )
78 {
79 switch(which)
80 {
81 case GLU_BEGIN:
82 tobj->callbacks.begin = (void (*)(GLenum)) fn;
83 break;
84 case GLU_EDGE_FLAG:
85 tobj->callbacks.edgeFlag = (void (*)(GLboolean)) fn;
86 break;
87 case GLU_VERTEX:
88 tobj->callbacks.vertex = (void (*)(void *)) fn;
89 break;
90 case GLU_END:
91 tobj->callbacks.end = (void (*)(void)) fn;
92 break;
93 case GLU_ERROR:
94 tobj->callbacks.error = (void (*)(GLenum)) fn;
95 break;
96 default:
97 tobj->error=GLU_INVALID_ENUM;
98 break;
99 }
100 }
101
102
103
104 void gluDeleteTess( GLUtriangulatorObj *tobj )
105 {
106 if(tobj->error==GLU_NO_ERROR && tobj->contour_cnt)
107 /* was gluEndPolygon called? */
108 tess_call_user_error(tobj,GLU_TESS_ERROR1);
109 /* delete all internal structures */
110 delete_contours(tobj);
111 free(tobj);
112 }
113
114
115 void gluBeginPolygon( GLUtriangulatorObj *tobj )
116 {
117 /*
118 if(tobj->error!=GLU_NO_ERROR)
119 return;
120 */
121 tobj->error = GLU_NO_ERROR;
122 if(tobj->current_polygon!=NULL)
123 {
124 /* gluEndPolygon was not called */
125 tess_call_user_error(tobj,GLU_TESS_ERROR1);
126 /* delete all internal structures */
127 delete_contours(tobj);
128 }
129 else
130 {
131 if((tobj->current_polygon=
132 (tess_polygon *)malloc(sizeof(tess_polygon)))==NULL)
133 {
134 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
135 return;
136 }
137 tobj->current_polygon->vertex_cnt=0;
138 tobj->current_polygon->vertices=
139 tobj->current_polygon->last_vertex=NULL;
140 }
141 }
142
143
144 void gluEndPolygon( GLUtriangulatorObj *tobj )
145 {
146 /*tess_contour *contour_ptr;*/
147
148 /* there was an error */
149 if(tobj->error!=GLU_NO_ERROR) goto end;
150
151 /* check if gluBeginPolygon was called */
152 if(tobj->current_polygon==NULL)
153 {
154 tess_call_user_error(tobj,GLU_TESS_ERROR2);
155 return;
156 }
157 tess_test_polygon(tobj);
158 /* there was an error */
159 if(tobj->error!=GLU_NO_ERROR) goto end;
160
161 /* any real contours? */
162 if(tobj->contour_cnt==0)
163 {
164 /* delete all internal structures */
165 delete_contours(tobj);
166 return;
167 }
168 tess_find_contour_hierarchies(tobj);
169 /* there was an error */
170 if(tobj->error!=GLU_NO_ERROR) goto end;
171
172 tess_handle_holes(tobj);
173 /* there was an error */
174 if(tobj->error!=GLU_NO_ERROR) goto end;
175
176 /* if no callbacks, nothing to do */
177 if(tobj->callbacks.begin!=NULL && tobj->callbacks.vertex!=NULL &&
178 tobj->callbacks.end!=NULL)
179 {
180 if(tobj->callbacks.edgeFlag==NULL)
181 tess_tesselate(tobj);
182 else
183 tess_tesselate_with_edge_flag(tobj);
184 }
185
186 end:
187 /* delete all internal structures */
188 delete_contours(tobj);
189 }
190
191
192 void gluNextContour( GLUtriangulatorObj *tobj, GLenum type )
193 {
194 if(tobj->error!=GLU_NO_ERROR)
195 return;
196 if(tobj->current_polygon==NULL)
197 {
198 tess_call_user_error(tobj,GLU_TESS_ERROR2);
199 return;
200 }
201 /* first contour? */
202 if(tobj->current_polygon->vertex_cnt)
203 tess_test_polygon(tobj);
204 }
205
206
207 void gluTessVertex( GLUtriangulatorObj *tobj, GLdouble v[3], void *data )
208 {
209 tess_polygon *polygon=tobj->current_polygon;
210 tess_vertex *last_vertex_ptr;
211
212 if(tobj->error!=GLU_NO_ERROR)
213 return;
214 if(polygon==NULL)
215 {
216 tess_call_user_error(tobj,GLU_TESS_ERROR2);
217 return;
218 }
219 last_vertex_ptr=polygon->last_vertex;
220 if(last_vertex_ptr==NULL)
221 {
222 if((last_vertex_ptr=(tess_vertex *)
223 malloc(sizeof(tess_vertex)))==NULL)
224 {
225 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
226 return;
227 }
228 polygon->vertices=last_vertex_ptr;
229 polygon->last_vertex=last_vertex_ptr;
230 last_vertex_ptr->data=data;
231 last_vertex_ptr->location[0]=v[0];
232 last_vertex_ptr->location[1]=v[1];
233 last_vertex_ptr->location[2]=v[2];
234 last_vertex_ptr->next=NULL;
235 last_vertex_ptr->previous=NULL;
236 ++(polygon->vertex_cnt);
237 }
238 else
239 {
240 tess_vertex *vertex_ptr;
241
242 /* same point twice? */
243 if(fabs(last_vertex_ptr->location[0]-v[0]) < EPSILON &&
244 fabs(last_vertex_ptr->location[1]-v[1]) < EPSILON &&
245 fabs(last_vertex_ptr->location[2]-v[2]) < EPSILON)
246 {
247 tess_call_user_error(tobj,GLU_TESS_ERROR6);
248 return;
249 }
250 if((vertex_ptr=(tess_vertex *)
251 malloc(sizeof(tess_vertex)))==NULL)
252 {
253 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
254 return;
255 }
256 vertex_ptr->data=data;
257 vertex_ptr->location[0]=v[0];
258 vertex_ptr->location[1]=v[1];
259 vertex_ptr->location[2]=v[2];
260 vertex_ptr->next=NULL;
261 vertex_ptr->previous=last_vertex_ptr;
262 ++(polygon->vertex_cnt);
263 last_vertex_ptr->next=vertex_ptr;
264 polygon->last_vertex=vertex_ptr;
265 }
266 }
267
268
269 static void delete_contours(GLUtriangulatorObj *tobj)
270 {
271 tess_polygon *polygon=tobj->current_polygon;
272 tess_contour *contour,*contour_tmp;
273 tess_vertex *vertex,*vertex_tmp;
274
275 /* remove current_polygon list - if exists due to detected error */
276 if(polygon!=NULL)
277 {
278 if (polygon->vertices)
279 {
280 for(vertex=polygon->vertices;vertex!=polygon->last_vertex;)
281 {
282 vertex_tmp=vertex->next;
283 free(vertex);
284 vertex=vertex_tmp;
285 }
286 free(vertex);
287 }
288 free(polygon);
289 tobj->current_polygon=NULL;
290 }
291 /* remove all contour data */
292 for(contour=tobj->contours;contour!=NULL;)
293 {
294 for(vertex=contour->vertices;vertex!=contour->last_vertex;)
295 {
296 vertex_tmp=vertex->next;
297 free(vertex);
298 vertex=vertex_tmp;
299 }
300 free(vertex);
301 contour_tmp=contour->next;
302 free(contour);
303 contour=contour_tmp;
304 }
305 tobj->contours=tobj->last_contour=NULL;
306 tobj->contour_cnt=0;
307 }
308
309
310