Debian Codegraph @

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

raw
1 /* $Id: polytest.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 "gluP.h"
32 #include "tess.h"
33
34
35
36 static GLenum store_polygon_as_contour(GLUtriangulatorObj *);
37 static void free_current_polygon(tess_polygon *);
38 static void prepare_projection_info(GLUtriangulatorObj *);
39 static GLdouble twice_the_polygon_area(tess_vertex *,tess_vertex *);
40 static GLenum verify_edge_vertex_intersections(GLUtriangulatorObj *);
41 void tess_find_contour_hierarchies(GLUtriangulatorObj *);
42 static GLenum test_for_overlapping_contours(GLUtriangulatorObj *);
43 static GLenum contours_overlap(tess_contour *, tess_polygon *);
44 static GLenum is_contour_contained_in(tess_contour *,tess_contour *);
45 static void add_new_exterior(GLUtriangulatorObj *,tess_contour *);
46 static void add_new_interior(GLUtriangulatorObj *,tess_contour *,
47 tess_contour *);
48 static void add_interior_with_hierarchy_check(GLUtriangulatorObj *,
49 tess_contour *,tess_contour *);
50 static void reverse_hierarchy_and_add_exterior(GLUtriangulatorObj *,
51 tess_contour *,tess_contour *);
52 static GLboolean point_in_polygon(tess_contour *,GLdouble,GLdouble);
53 static void shift_interior_to_exterior(GLUtriangulatorObj *,tess_contour *);
54 static void add_exterior_with_check(GLUtriangulatorObj *,tess_contour *,
55 tess_contour *);
56 static GLenum cut_out_hole(GLUtriangulatorObj *,tess_contour *,
57 tess_contour *);
58 static GLenum merge_hole_with_contour(GLUtriangulatorObj *,
59 tess_contour *,tess_contour *,tess_vertex *,
60 tess_vertex *);
61
62 static GLenum
63 find_normal(GLUtriangulatorObj *tobj)
64 {
65 tess_polygon *polygon=tobj->current_polygon;
66 tess_vertex *va,*vb,*vc;
67 GLdouble A,B,C;
68 GLdouble A0,A1,A2,B0,B1,B2;
69
70 va=polygon->vertices;
71 vb=va->next;
72 A0=vb->location[0]-va->location[0];
73 A1=vb->location[1]-va->location[1];
74 A2=vb->location[2]-va->location[2];
75 for(vc=vb->next;vc!=va;vc=vc->next)
76 {
77 B0=vc->location[0]-va->location[0];
78 B1=vc->location[1]-va->location[1];
79 B2=vc->location[2]-va->location[2];
80 A=A1*B2-A2*B1;
81 B=A2*B0-A0*B2;
82 C=A0*B1-A1*B0;
83 if(fabs(A)>EPSILON || fabs(B)>EPSILON || fabs(C)>EPSILON)
84 {
85 polygon->A=A;
86 polygon->B=B;
87 polygon->C=C;
88 polygon->D= -A*va->location[0]-B*va->location[1]-C*va->location[2];
89 return GLU_NO_ERROR;
90 }
91 }
92 tess_call_user_error(tobj,GLU_TESS_ERROR7);
93 return GLU_ERROR;
94 }
95
96 void
97 tess_test_polygon( GLUtriangulatorObj *tobj )
98 {
99 tess_polygon *polygon=tobj->current_polygon;
100
101 /* any vertices defined? */
102 if(polygon->vertex_cnt<3)
103 {
104 free_current_polygon(polygon);
105 return;
106 }
107 /* wrap pointers */
108 polygon->last_vertex->next=polygon->vertices;
109 polygon->vertices->previous=polygon->last_vertex;
110 /* determine the normal */
111 if(find_normal(tobj)==GLU_ERROR)
112 return;
113 /* compare the normals of previously defined contours and this one */
114 /* first contour define ? */
115 if(tobj->contours==NULL)
116 {
117 tobj->A=polygon->A;
118 tobj->B=polygon->B;
119 tobj->C=polygon->C;
120 tobj->D=polygon->D;
121 /* determine the best projection to use */
122 if(fabs(polygon->A) > fabs(polygon->B))
123 if(fabs(polygon->A) > fabs(polygon->C))
124 tobj->projection=OYZ;
125 else
126 tobj->projection=OXY;
127 else
128 if(fabs(polygon->B) > fabs(polygon->C))
129 tobj->projection=OXZ;
130 else
131 tobj->projection=OXY;
132 }
133 else
134 {
135 GLdouble a[3],b[3];
136 tess_vertex *vertex=polygon->vertices;
137
138 a[0]=tobj->A;
139 a[1]=tobj->B;
140 a[2]=tobj->C;
141 b[0]=polygon->A;
142 b[1]=polygon->B;
143 b[2]=polygon->C;
144
145 /* compare the normals */
146 if( fabs(a[1]*b[2]-a[2]*b[1]) > EPSILON ||
147 fabs(a[2]*b[0]-a[0]*b[2]) > EPSILON ||
148 fabs(a[0]*b[1]-a[1]*b[0]) > EPSILON)
149 {
150 /* not coplanar */
151 tess_call_user_error(tobj,GLU_TESS_ERROR9);
152 return;
153 }
154 /* the normals are parallel - test for plane equation */
155 if(fabs(a[0]*vertex->location[0]+a[1]*vertex->location[1]+
156 a[2]*vertex->location[2]+tobj->D) > EPSILON)
157 {
158 /* not the same plane */
159 tess_call_user_error(tobj,GLU_TESS_ERROR9);
160 return;
161 }
162 }
163 prepare_projection_info(tobj);
164 if(verify_edge_vertex_intersections(tobj)==GLU_ERROR)
165 return;
166 if(test_for_overlapping_contours(tobj)==GLU_ERROR)
167 return;
168 if(store_polygon_as_contour(tobj)==GLU_ERROR)
169 return;
170 }
171
172 static GLenum test_for_overlapping_contours(GLUtriangulatorObj *tobj)
173 {
174 tess_contour *contour;
175 tess_polygon *polygon;
176
177 polygon=tobj->current_polygon;
178 for(contour=tobj->contours;contour!=NULL;contour=contour->next)
179 if(contours_overlap(contour,polygon)!=GLU_NO_ERROR)
180 {
181 tess_call_user_error(tobj,GLU_TESS_ERROR5);
182 return GLU_ERROR;
183 }
184 return GLU_NO_ERROR;
185 }
186
187 static GLenum store_polygon_as_contour(GLUtriangulatorObj *tobj)
188 {
189 tess_polygon *polygon=tobj->current_polygon;
190 tess_contour *contour=tobj->contours;
191
192 /* the first contour defined */
193 if(contour==NULL)
194 {
195 if((contour=(tess_contour *)malloc(
196 sizeof(tess_contour)))==NULL)
197 {
198 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
199 free_current_polygon(polygon);
200 return GLU_ERROR;
201 }
202 tobj->contours=tobj->last_contour=contour;
203 contour->next=contour->previous=NULL;
204 }
205 else
206 {
207 if((contour=(tess_contour *)malloc(
208 sizeof(tess_contour)))==NULL)
209 {
210 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
211 free_current_polygon(polygon);
212 return GLU_ERROR;
213 }
214 contour->previous=tobj->last_contour;
215 tobj->last_contour->next=contour;
216 tobj->last_contour=contour;
217 contour->next=NULL;
218 }
219 /* mark all vertices in new contour as not special */
220 /* and all are boundary edges */
221 {
222 tess_vertex *vertex;
223 GLuint vertex_cnt,i;
224
225 for(vertex=polygon->vertices , i=0 , vertex_cnt=polygon->vertex_cnt;
226 i<vertex_cnt;
227 vertex=vertex->next , i++)
228 {
229 vertex->shadow_vertex=NULL;
230 vertex->edge_flag=GL_TRUE;
231 }
232 }
233 contour->vertex_cnt=polygon->vertex_cnt;
234 contour->area=polygon->area;
235 contour->orientation=polygon->orientation;
236 contour->type=GLU_UNKNOWN;
237 contour->vertices=polygon->vertices;
238 contour->last_vertex=polygon->last_vertex;
239 polygon->vertices=polygon->last_vertex=NULL;
240 polygon->vertex_cnt=0;
241 ++(tobj->contour_cnt);
242 return GLU_NO_ERROR;
243 }
244
245 static void free_current_polygon(tess_polygon *polygon)
246 {
247 tess_vertex *vertex,*vertex_tmp;
248
249 /* free current_polygon structures */
250 for(vertex=polygon->vertices;vertex!=polygon->last_vertex;)
251 {
252 vertex_tmp=vertex->next;
253 free(vertex);
254 vertex=vertex_tmp;
255 }
256 polygon->vertices=polygon->last_vertex=NULL;
257 polygon->vertex_cnt=0;
258 }
259
260 static void prepare_projection_info(GLUtriangulatorObj *tobj)
261 {
262 tess_polygon *polygon=tobj->current_polygon;
263 tess_vertex *vertex,*last_vertex_ptr;
264 GLdouble area;
265
266 last_vertex_ptr=polygon->last_vertex;
267 switch(tobj->projection)
268 {
269 case OXY:
270 for(vertex=polygon->vertices;vertex!=last_vertex_ptr;
271 vertex=vertex->next)
272 {
273 vertex->x=vertex->location[0];
274 vertex->y=vertex->location[1];
275 }
276 last_vertex_ptr->x=last_vertex_ptr->location[0];
277 last_vertex_ptr->y=last_vertex_ptr->location[1];
278 break;
279 case OXZ:
280 for(vertex=polygon->vertices;vertex!=last_vertex_ptr;
281 vertex=vertex->next)
282 {
283 vertex->x=vertex->location[0];
284 vertex->y=vertex->location[2];
285 }
286 last_vertex_ptr->x=last_vertex_ptr->location[0];
287 last_vertex_ptr->y=last_vertex_ptr->location[2];
288 break;
289 case OYZ:
290 for(vertex=polygon->vertices;vertex!=last_vertex_ptr;
291 vertex=vertex->next)
292 {
293 vertex->x=vertex->location[1];
294 vertex->y=vertex->location[2];
295 }
296 last_vertex_ptr->x=last_vertex_ptr->location[1];
297 last_vertex_ptr->y=last_vertex_ptr->location[2];
298 break;
299 }
300 area=twice_the_polygon_area(polygon->vertices,polygon->last_vertex);
301 if(area >= 0.0)
302 {
303 polygon->orientation=GLU_CCW;
304 polygon->area=area;
305 }
306 else
307 {
308 polygon->orientation=GLU_CW;
309 polygon->area= -area;
310 }
311 }
312
313 static GLdouble twice_the_polygon_area(tess_vertex *vertex,
314 tess_vertex *last_vertex)
315 {
316 tess_vertex *next;
317 GLdouble area,x,y;
318
319 area=0.0;
320 x=vertex->x;
321 y=vertex->y;
322 vertex=vertex->next;
323 for(; vertex!=last_vertex; vertex=vertex->next)
324 {
325 next=vertex->next;
326 area+=(vertex->x - x)*(next->y - y) - (vertex->y - y)*(next->x - x);
327 }
328 return area;
329 }
330
331 /* test if edges ab and cd intersect */
332 /* if not return GLU_NO_ERROR, else if cross return GLU_TESS_ERROR8, */
333 /* else if adjacent return GLU_TESS_ERROR4 */
334 static GLenum edge_edge_intersect(
335 tess_vertex *a,
336 tess_vertex *b,
337 tess_vertex *c,
338 tess_vertex *d)
339 {
340 GLdouble denom,r,s;
341 GLdouble xba,ydc,yba,xdc,yac,xac;
342
343 xba=b->x - a->x;
344 yba=b->y - a->y;
345 xdc=d->x - c->x;
346 ydc=d->y - c->y;
347 xac=a->x - c->x;
348 yac=a->y - c->y;
349 denom= xba*ydc - yba*xdc;
350 r = yac*xdc - xac*ydc;
351 /* parallel? */
352 if(fabs(denom) < EPSILON)
353 {
354 if(fabs(r) < EPSILON)
355 {
356 /* colinear */
357 if(fabs(xba) < EPSILON)
358 {
359 /* compare the Y coordinate */
360 if(yba > 0.0)
361 {
362 if((fabs(a->y - c->y)<EPSILON && fabs(c->y - b->y)<EPSILON)
363 ||
364 (fabs(a->y - d->y)<EPSILON && fabs(d->y - b->y)<EPSILON))
365 return GLU_TESS_ERROR4;
366
367 }
368 else
369 {
370 if((fabs(b->y - c->y)<EPSILON && fabs(c->y - a->y)<EPSILON)
371 ||
372 (fabs(b->y - d->y)<EPSILON && fabs(d->y - a->y)<EPSILON))
373 return GLU_TESS_ERROR4;
374 }
375 }
376 else
377 {
378 /* compare the X coordinate */
379 if(xba > 0.0)
380 {
381 if((fabs(a->x - c->x)<EPSILON && fabs(c->x - b->x)<EPSILON)
382 ||
383 (fabs(a->x - d->x)<EPSILON && fabs(d->x - b->x)<EPSILON))
384 return GLU_TESS_ERROR4;
385 }
386 else
387 {
388 if((fabs(b->x - c->x)<EPSILON && fabs(c->x - a->x)<EPSILON)
389 ||
390 (fabs(b->x - d->x)<EPSILON && fabs(d->x - a->x)<EPSILON))
391 return GLU_TESS_ERROR4;
392 }
393 }
394 }
395 return GLU_NO_ERROR;
396 }
397 r /= denom;
398 s = (yac*xba - xac*yba) / denom;
399 /* test if one vertex lies on other edge */
400 if(((fabs(r) < EPSILON || (r < 1.0+EPSILON && r > 1.0-EPSILON)) &&
401 s > -EPSILON && s < 1.0+EPSILON) ||
402 ((fabs(s) < EPSILON || (s < 1.0+EPSILON && s > 1.0-EPSILON)) &&
403 r > -EPSILON && r < 1.0+EPSILON))
404 {
405 return GLU_TESS_ERROR4;
406 }
407 /* test for crossing */
408 if(r > -EPSILON && r < 1.0+EPSILON &&
409 s > -EPSILON && s < 1.0+EPSILON)
410 {
411 return GLU_TESS_ERROR8;
412 }
413 return GLU_NO_ERROR;
414 }
415
416 static GLenum verify_edge_vertex_intersections(GLUtriangulatorObj *tobj)
417 {
418 tess_polygon *polygon=tobj->current_polygon;
419 tess_vertex *vertex1,*last_vertex,*vertex2;
420 GLenum test;
421
422 last_vertex=polygon->last_vertex;
423 vertex1=last_vertex;
424 for(vertex2=vertex1->next->next;
425 vertex2->next!=last_vertex;
426 vertex2=vertex2->next)
427 {
428 test=edge_edge_intersect(vertex1,vertex1->next,vertex2,
429 vertex2->next);
430 if(test!=GLU_NO_ERROR)
431 {
432 tess_call_user_error(tobj,test);
433 return GLU_ERROR;
434 }
435 }
436 for(vertex1=polygon->vertices;
437 vertex1->next->next!=last_vertex;
438 vertex1=vertex1->next)
439 {
440 for(vertex2=vertex1->next->next;
441 vertex2!=last_vertex;
442 vertex2=vertex2->next)
443 {
444 test=edge_edge_intersect(vertex1,vertex1->next,vertex2,
445 vertex2->next);
446 if(test!=GLU_NO_ERROR)
447 {
448 tess_call_user_error(tobj,test);
449 return GLU_ERROR;
450 }
451 }
452 }
453 return GLU_NO_ERROR;
454 }
455
456 static int area_compare(const void *a,const void *b)
457 {
458 GLdouble area1,area2;
459
460 area1=(*((tess_contour **)a))->area;
461 area2=(*((tess_contour **)b))->area;
462 if(area1 < area2)
463 return 1;
464 if(area1 > area2)
465 return -1;
466 return 0;
467 }
468
469 void tess_find_contour_hierarchies(GLUtriangulatorObj *tobj)
470 {
471 tess_contour **contours; /* dinamic array of pointers */
472 tess_contour *tmp_contour_ptr=tobj->contours;
473 GLuint cnt,i;
474 GLenum result;
475 GLboolean hierarchy_changed;
476
477 /* any contours? */
478 if(tobj->contour_cnt < 2)
479 {
480 tobj->contours->type=GLU_EXTERIOR;
481 return;
482 }
483 if((contours=(tess_contour **)
484 malloc(sizeof(tess_contour *)*(tobj->contour_cnt)))==NULL)
485 {
486 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
487 return;
488 }
489 for(tmp_contour_ptr=tobj->contours , cnt=0;
490 tmp_contour_ptr!=NULL;
491 tmp_contour_ptr=tmp_contour_ptr->next)
492 contours[cnt++]=tmp_contour_ptr;
493 /* now sort the contours in decreasing area size order */
494 qsort((void *)contours,(size_t)cnt,(size_t)sizeof(tess_contour *),area_compare);
495 /* we leave just the first contour - remove others from list */
496 tobj->contours=contours[0];
497 tobj->contours->next=tobj->contours->previous=NULL;
498 tobj->last_contour=tobj->contours;
499 tobj->contour_cnt=1;
500 /* first contour is the one with greatest area */
501 /* must be EXTERIOR */
502 tobj->contours->type=GLU_EXTERIOR;
503 tmp_contour_ptr=tobj->contours;
504 /* now we play! */
505 for(i=1;i<cnt;i++)
506 {
507 hierarchy_changed=GL_FALSE;
508 for(tmp_contour_ptr=tobj->contours;
509 tmp_contour_ptr!=NULL;
510 tmp_contour_ptr=tmp_contour_ptr->next)
511 {
512 if(tmp_contour_ptr->type==GLU_EXTERIOR)
513 {
514 /* check if contour completely contained in EXTERIOR */
515 result=is_contour_contained_in(tmp_contour_ptr,contours[i]);
516 switch(result)
517 {
518 case GLU_INTERIOR:
519 /* now we have to check if contour is inside interiors */
520 /* or not */
521 /* any interiors? */
522 if(tmp_contour_ptr->next!=NULL &&
523 tmp_contour_ptr->next->type==GLU_INTERIOR)
524 {
525 /* for all interior, check if inside any of them */
526 /* if not inside any of interiors, its another */
527 /* interior */
528 /* or it may contain some interiors, then change */
529 /* the contained interiors to exterior ones */
530 add_interior_with_hierarchy_check(tobj,
531 tmp_contour_ptr,contours[i]);
532 }
533 else
534 {
535 /* not in interior, add as new interior contour */
536 add_new_interior(tobj,tmp_contour_ptr,contours[i]);
537 }
538 hierarchy_changed=GL_TRUE;
539 break;
540 case GLU_EXTERIOR:
541 /* ooops, the marked as EXTERIOR (contours[i]) is */
542 /* actually an interior of tmp_contour_ptr */
543 /* reverse the local hierarchy */
544 reverse_hierarchy_and_add_exterior(tobj,tmp_contour_ptr,
545 contours[i]);
546 hierarchy_changed=GL_TRUE;
547 break;
548 case GLU_NO_ERROR:
549 break;
550 default:
551 abort();
552 }
553 }
554 if(hierarchy_changed)
555 break; /* break from for loop */
556 }
557 if(hierarchy_changed==GL_FALSE)
558 {
559 /* disjoint with all contours, add to contour list */
560 add_new_exterior(tobj,contours[i]);
561 }
562 }
563 free(contours);
564 }
565
566 /* returns GLU_INTERIOR if inner is completey enclosed within outer */
567 /* returns GLU_EXTERIOR if outer is completely enclosed within inner */
568 /* returns GLU_NO_ERROR if contours are disjoint */
569 static GLenum is_contour_contained_in(
570 tess_contour *outer,
571 tess_contour *inner)
572 {
573 GLenum relation_flag;
574
575 /* set relation_flag to relation of containment of first inner vertex */
576 /* regarding outer contour */
577 if(point_in_polygon(outer,inner->vertices->x,inner->vertices->y))
578 relation_flag=GLU_INTERIOR;
579 else
580 relation_flag=GLU_EXTERIOR;
581 if(relation_flag==GLU_INTERIOR)
582 return GLU_INTERIOR;
583 if(point_in_polygon(inner,outer->vertices->x,outer->vertices->y))
584 return GLU_EXTERIOR;
585 return GLU_NO_ERROR;
586 }
587
588 static GLboolean point_in_polygon(
589 tess_contour *contour,
590 GLdouble x,
591 GLdouble y)
592 {
593 tess_vertex *v1,*v2;
594 GLuint i,vertex_cnt;
595 GLdouble xp1,yp1,xp2,yp2;
596 GLboolean tst;
597
598 tst=GL_FALSE;
599 v1=contour->vertices;
600 v2=contour->vertices->previous;
601 for(i=0 , vertex_cnt=contour->vertex_cnt;
602 i < vertex_cnt;
603 i++)
604 {
605 xp1=v1->x;
606 yp1=v1->y;
607 xp2=v2->x;
608 yp2=v2->y;
609 if ((((yp1<=y) && (y<yp2)) || ((yp2<=y) && (y<yp1))) &&
610 (x < (xp2 - xp1) * (y - yp1) / (yp2 - yp1) + xp1))
611 tst = (tst==GL_FALSE ? GL_TRUE : GL_FALSE);
612 v2=v1;
613 v1=v1->next;
614 }
615 return tst;
616 }
617
618 static GLenum contours_overlap(
619 tess_contour *contour,
620 tess_polygon *polygon)
621 {
622 tess_vertex *vertex1,*vertex2;
623 GLuint vertex1_cnt,vertex2_cnt,i,j;
624 GLenum test;
625
626 vertex1=contour->vertices;
627 vertex2=polygon->vertices;
628 vertex1_cnt=contour->vertex_cnt;
629 vertex2_cnt=polygon->vertex_cnt;
630 for(i=0; i<vertex1_cnt; vertex1=vertex1->next , i++)
631 {
632 for(j=0; j<vertex2_cnt; vertex2=vertex2->next , j++)
633 if((test=edge_edge_intersect(vertex1,vertex1->next,vertex2,
634 vertex2->next))!=GLU_NO_ERROR)
635 return test;
636 }
637 return GLU_NO_ERROR;
638 }
639
640 static void add_new_exterior(
641 GLUtriangulatorObj *tobj,
642 tess_contour *contour)
643 {
644 contour->type=GLU_EXTERIOR;
645 contour->next=NULL;
646 contour->previous=tobj->last_contour;
647 tobj->last_contour->next=contour;
648 tobj->last_contour=contour;
649 }
650
651 static void add_new_interior(
652 GLUtriangulatorObj *tobj,
653 tess_contour *outer,
654 tess_contour *contour)
655 {
656 contour->type=GLU_INTERIOR;
657 contour->next=outer->next;
658 contour->previous=outer;
659 if(outer->next!=NULL)
660 outer->next->previous=contour;
661 outer->next=contour;
662 if(tobj->last_contour==outer)
663 tobj->last_contour=contour;
664 }
665
666 static void add_interior_with_hierarchy_check(
667 GLUtriangulatorObj *tobj,
668 tess_contour *outer,
669 tess_contour *contour)
670 {
671 tess_contour *ptr;
672
673 /* for all interiors of outer check if they are interior of contour */
674 /* if so, change that interior to exterior and move it of of the */
675 /* interior sequence */
676 if(outer->next!=NULL && outer->next->type==GLU_INTERIOR)
677 {
678 GLenum test;
679
680 for(ptr=outer->next;ptr!=NULL && ptr->type==GLU_INTERIOR;ptr=ptr->next)
681 {
682 test=is_contour_contained_in(ptr,contour);
683 switch(test)
684 {
685 case GLU_INTERIOR:
686 /* contour is contained in one of the interiors */
687 /* check if possibly contained in other exteriors */
688 /* move ptr to first EXTERIOR */
689 for(;ptr!=NULL && ptr->type==GLU_INTERIOR;ptr=ptr->next);
690 if(ptr==NULL)
691 /* another exterior */
692 add_new_exterior(tobj,contour);
693 else
694 add_exterior_with_check(tobj,ptr,contour);
695 return;
696 case GLU_EXTERIOR:
697 /* one of the interiors is contained in the contour */
698 /* change it to EXTERIOR, and shift it away from the */
699 /* interior sequence */
700 shift_interior_to_exterior(tobj,ptr);
701 break;
702 case GLU_NO_ERROR:
703 /* disjoint */
704 break;
705 default:
706 abort();
707 }
708 }
709 }
710 /* add contour to the interior sequence */
711 add_new_interior(tobj,outer,contour);
712 }
713
714 static void reverse_hierarchy_and_add_exterior(
715 GLUtriangulatorObj *tobj,
716 tess_contour *outer,
717 tess_contour *contour)
718 {
719 tess_contour *ptr;
720
721 /* reverse INTERIORS to EXTERIORS */
722 /* any INTERIORS? */
723 if(outer->next!=NULL && outer->next->type==GLU_INTERIOR)
724 for(ptr=outer->next;ptr!=NULL && ptr->type==GLU_INTERIOR;ptr=ptr->next)
725 ptr->type=GLU_EXTERIOR;
726 /* the outer now becomes inner */
727 outer->type=GLU_INTERIOR;
728 /* contour is the EXTERIOR */
729 contour->next=outer;
730 if(tobj->contours==outer)
731 {
732 /* first contour beeing reversed */
733 contour->previous=NULL;
734 tobj->contours=contour;
735 }
736 else
737 {
738 outer->previous->next=contour;
739 contour->previous=outer->previous;
740 }
741 outer->previous=contour;
742 }
743
744 static void shift_interior_to_exterior(
745 GLUtriangulatorObj *tobj,
746 tess_contour *contour)
747 {
748 contour->previous->next=contour->next;
749 if(contour->next!=NULL)
750 contour->next->previous=contour->previous;
751 else
752 tobj->last_contour=contour->previous;
753 }
754
755 static void add_exterior_with_check(
756 GLUtriangulatorObj *tobj,
757 tess_contour *outer,
758 tess_contour *contour)
759 {
760 GLenum test;
761
762 /* this contour might be interior to further exteriors - check */
763 /* if not, just add as a new exterior */
764 for(;outer!=NULL && outer->type==GLU_EXTERIOR;outer=outer->next)
765 {
766 test=is_contour_contained_in(outer,contour);
767 switch(test)
768 {
769 case GLU_INTERIOR:
770 /* now we have to check if contour is inside interiors */
771 /* or not */
772 /* any interiors? */
773 if(outer->next!=NULL && outer->next->type==GLU_INTERIOR)
774 {
775 /* for all interior, check if inside any of them */
776 /* if not inside any of interiors, its another */
777 /* interior */
778 /* or it may contain some interiors, then change */
779 /* the contained interiors to exterior ones */
780 add_interior_with_hierarchy_check(tobj,
781 outer,contour);
782 }
783 else
784 {
785 /* not in interior, add as new interior contour */
786 add_new_interior(tobj,outer,contour);
787 }
788 return;
789 case GLU_NO_ERROR:
790 /* disjoint */
791 break;
792 default:
793 abort();
794 }
795 }
796 /* add contour to the exterior sequence */
797 add_new_exterior(tobj,contour);
798 }
799
800 void tess_handle_holes(GLUtriangulatorObj *tobj)
801 {
802 tess_contour *contour,*hole;
803 GLenum exterior_orientation;
804
805 /* verify hole orientation */
806 for(contour=tobj->contours;contour!=NULL;)
807 {
808 exterior_orientation=contour->orientation;
809 for(contour=contour->next;
810 contour!=NULL && contour->type==GLU_INTERIOR;
811 contour=contour->next)
812 {
813 if(contour->orientation==exterior_orientation)
814 {
815 tess_call_user_error(tobj,GLU_TESS_ERROR5);
816 return;
817 }
818 }
819 }
820 /* now cut-out holes */
821 for(contour=tobj->contours;contour!=NULL;)
822 {
823 hole=contour->next;
824 while(hole!=NULL && hole->type==GLU_INTERIOR)
825 {
826 if(cut_out_hole(tobj,contour,hole)==GLU_ERROR)
827 return;
828 hole=contour->next;
829 }
830 contour=contour->next;
831 }
832 }
833
834 static GLenum cut_out_hole(
835 GLUtriangulatorObj *tobj,
836 tess_contour *contour,
837 tess_contour *hole)
838 {
839 tess_contour *tmp_hole;
840 tess_vertex *v1,*v2,*tmp_vertex;
841 GLuint vertex1_cnt,vertex2_cnt,tmp_vertex_cnt;
842 GLuint i,j,k;
843 GLenum test;
844
845 /* find an edge connecting contour and hole not intersecting any other */
846 /* edge belonging to either the contour or any of the other holes */
847 for(v1=contour->vertices , vertex1_cnt=contour->vertex_cnt , i=0;
848 i<vertex1_cnt;
849 i++ , v1=v1->next)
850 {
851 for(v2=hole->vertices , vertex2_cnt=hole->vertex_cnt , j=0;
852 j<vertex2_cnt;
853 j++ , v2=v2->next)
854 {
855 /* does edge (v1,v2) intersect any edge of contour */
856 for(tmp_vertex=contour->vertices , tmp_vertex_cnt=contour->vertex_cnt ,
857 k=0;
858 k<tmp_vertex_cnt;
859 tmp_vertex=tmp_vertex->next , k++)
860 {
861 /* skip edge tests for edges directly connected */
862 if(v1==tmp_vertex || v1==tmp_vertex->next)
863 continue;
864 test=edge_edge_intersect(v1,v2,tmp_vertex,tmp_vertex->next);
865 if(test!=GLU_NO_ERROR)
866 break;
867 }
868 if(test==GLU_NO_ERROR)
869 {
870 /* does edge (v1,v2) intersect any edge of hole */
871 for(tmp_vertex=hole->vertices ,
872 tmp_vertex_cnt=hole->vertex_cnt , k=0;
873 k<tmp_vertex_cnt;
874 tmp_vertex=tmp_vertex->next , k++)
875 {
876 /* skip edge tests for edges directly connected */
877 if(v2==tmp_vertex || v2==tmp_vertex->next)
878 continue;
879 test=edge_edge_intersect(v1,v2,tmp_vertex,tmp_vertex->next);
880 if(test!=GLU_NO_ERROR)
881 break;
882 }
883 if(test==GLU_NO_ERROR)
884 {
885 /* does edge (v1,v2) intersect any other hole? */
886 for(tmp_hole=hole->next;
887 tmp_hole!=NULL && tmp_hole->type==GLU_INTERIOR;
888 tmp_hole=tmp_hole->next)
889 {
890 /* does edge (v1,v2) intersect any edge of hole */
891 for(tmp_vertex=tmp_hole->vertices ,
892 tmp_vertex_cnt=tmp_hole->vertex_cnt , k=0;
893 k<tmp_vertex_cnt;
894 tmp_vertex=tmp_vertex->next , k++)
895 {
896 test=edge_edge_intersect(v1,v2,tmp_vertex,
897 tmp_vertex->next);
898 if(test!=GLU_NO_ERROR)
899 break;
900 }
901 if(test!=GLU_NO_ERROR)
902 break;
903 }
904 }
905 }
906 if(test==GLU_NO_ERROR)
907 {
908 /* edge (v1,v2) is good for eliminating the hole */
909 if(merge_hole_with_contour(tobj,contour,hole,v1,v2)
910 ==GLU_NO_ERROR)
911 return GLU_NO_ERROR;
912 else
913 return GLU_ERROR;
914 }
915 }
916 }
917 /* other holes are blocking all possible connections of hole */
918 /* with contour, we shift this hole as the last hole and retry */
919 for(tmp_hole=hole;
920 tmp_hole!=NULL && tmp_hole->type==GLU_INTERIOR;
921 tmp_hole=tmp_hole->next);
922 contour->next=hole->next;
923 hole->next->previous=contour;
924 if(tmp_hole==NULL)
925 {
926 /* last EXTERIOR contour, shift hole as last contour */
927 hole->next=NULL;
928 hole->previous=tobj->last_contour;
929 tobj->last_contour->next=hole;
930 tobj->last_contour=hole;
931 }
932 else
933 {
934 tmp_hole->previous->next=hole;
935 hole->previous=tmp_hole->previous;
936 tmp_hole->previous=hole;
937 hole->next=tmp_hole;
938 }
939 hole=contour->next;
940 /* try once again - recurse */
941 return cut_out_hole(tobj,contour,hole);
942 }
943
944 static GLenum merge_hole_with_contour(
945 GLUtriangulatorObj *tobj,
946 tess_contour *contour,
947 tess_contour *hole,
948 tess_vertex *v1,
949 tess_vertex *v2)
950 {
951 tess_vertex *v1_new,*v2_new;
952
953 /* make copies of v1 and v2, place them respectively after their originals */
954 if((v1_new=(tess_vertex *)malloc(sizeof(tess_vertex)))==NULL)
955 {
956 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
957 return GLU_ERROR;
958 }
959 if((v2_new=(tess_vertex *)malloc(sizeof(tess_vertex)))==NULL)
960 {
961 tess_call_user_error(tobj,GLU_OUT_OF_MEMORY);
962 return GLU_ERROR;
963 }
964 v1_new->edge_flag=GL_TRUE;
965 v1_new->data=v1->data;
966 v1_new->location[0]=v1->location[0];
967 v1_new->location[1]=v1->location[1];
968 v1_new->location[2]=v1->location[2];
969 v1_new->x=v1->x;
970 v1_new->y=v1->y;
971 v1_new->shadow_vertex=v1;
972 v1->shadow_vertex=v1_new;
973 v1_new->next=v1->next;
974 v1_new->previous=v1;
975 v1->next->previous=v1_new;
976 v1->next=v1_new;
977 v2_new->edge_flag=GL_TRUE;
978 v2_new->data=v2->data;
979 v2_new->location[0]=v2->location[0];
980 v2_new->location[1]=v2->location[1];
981 v2_new->location[2]=v2->location[2];
982 v2_new->x=v2->x;
983 v2_new->y=v2->y;
984 v2_new->shadow_vertex=v2;
985 v2->shadow_vertex=v2_new;
986 v2_new->next=v2->next;
987 v2_new->previous=v2;
988 v2->next->previous=v2_new;
989 v2->next=v2_new;
990 /* link together the two lists */
991 v1->next=v2_new;
992 v2_new->previous=v1;
993 v2->next=v1_new;
994 v1_new->previous=v2;
995 /* update the vertex count of the contour */
996 contour->vertex_cnt += hole->vertex_cnt+2;
997 /* remove the INTERIOR contour */
998 contour->next=hole->next;
999 if(hole->next!=NULL)
1000 hole->next->previous=contour;
1001 free(hole);
1002 /* update tobj structure */
1003 --(tobj->contour_cnt);
1004 if(contour->last_vertex==v1)
1005 contour->last_vertex=v1_new;
1006 /* mark two vertices with edge_flag */
1007 v2->edge_flag=GL_FALSE;
1008 v1->edge_flag=GL_FALSE;
1009 return GLU_NO_ERROR;
1010 }