2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 ** http://oss.sgi.com/projects/FreeB
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
43 #include "partitionY.h"
44 #include "searchTree.h"
45 #include "quicksort.h"
49 #define max(a,b) ((a>b)? a:b)
50 #define min(a,b) ((a>b)? b:a)
54 *-1: if A < B (Ya<Yb) || (Ya==Yb)
58 static Int compVertInY(Real A[2], Real B[2])
60 if( (A[1] < B[1]) || (A[1]==B[1] && A[0]<B[0]))
63 ( A[1] == B[1] && A[0] == B[0]) return 0;
68 /*v is a vertex: the head of en edge,
70 *return 1 if e is below v: assume v1 and v2 are the two endpoints of e:
73 Int isBelow(directedLine *v, directedLine *e)
75 Real* vert = v->head();
76 if( compVertInY(e->head(), vert) != 1
77 && compVertInY(e->tail(), vert) != 1
84 /*v is a vertex: the head of en edge,
86 *return 1 if e is below v: assume v1 and v2 are the two endpoints of e:
89 Int isAbove(directedLine *v, directedLine *e)
91 Real* vert = v->head();
92 if( compVertInY(e->head(), vert) != -1
93 && compVertInY(e->tail(), vert) != -1
100 Int isCusp(directedLine *v)
102 Real *A=v->getPrev()->head();
105 if(A[1] < B[1] && B[1] < C[1])
107 else if(A[1] > B[1] && B[1] > C[1])
109 else if(A[1] < B[1] && C[1] < B[1])
111 else if(A[1] > B[1] && C[1] > B[1])
114 if((isAbove(v, v) && isAbove(v, v->getPrev())) ||
115 (isBelow(v, v) && isBelow(v, v->getPrev())))
121 /*crossproduct is strictly less than 0*/
122 Int isReflex(directedLine *v)
124 Real* A = v->getPrev()->head();
133 if(Bx*Cy - Cx*By < 0) return 1;
142 Int cuspType(directedLine *v)
144 if(! isCusp(v)) return 0;
145 else if(isReflex(v)) return 1;
150 sweepRange* sweepRangeMake(directedLine* left, Int leftType,
151 directedLine* right, Int rightType)
153 sweepRange* ret = (sweepRange*)malloc(sizeof(sweepRange));
156 ret->leftType = leftType;
158 ret->rightType = rightType;
162 void sweepRangeDelete(sweepRange* range)
167 Int sweepRangeEqual(sweepRange* src1, sweepRange* src2)
173 /*The case when both are vertices should not happen*/
174 assert(! (src1->leftType == 0 && src2->leftType == 0));
175 if(src1->leftType == 0 && src2->leftType == 1){
176 if(src1->left == src2->left ||
177 src1->left->getPrev() == src2->left
183 else if(src1->leftType == 1 && src2->leftType == 1){
184 if(src1->left == src2->left)
189 else /*src1->leftType == 1 && src2->leftType == 0*/{
190 if(src1->left == src2->left ||
191 src1->left == src2->left->getPrev()
198 /*the same thing for right*/
199 /*The case when both are vertices should not happen*/
200 assert(! (src1->rightType == 0 && src2->rightType == 0));
201 if(src1->rightType == 0 && src2->rightType == 1){
202 if(src1->right == src2->right ||
203 src1->right->getPrev() == src2->right
209 else if(src1->rightType == 1 && src2->rightType == 1){
210 if(src1->right == src2->right)
215 else /*src1->rightType == 1 && src2->rightType == 0*/{
216 if(src1->right == src2->right ||
217 src1->right == src2->right->getPrev()
224 return (leftEqual == 1 || rightEqual == 1);
227 /*given (x_1, y_1) and (x_2, y_2), and y
228 *return x such that (x,y) is on the line
230 inline/*static*/ Real intersectHoriz(Real x1, Real y1, Real x2, Real y2, Real y)
232 return ((y2==y1)? (x1+x2)*Real(0.5) : x1 + ((y-y1)/(y2-y1)) * (x2-x1));
234 if(y2 == y1) return (x1+x2)*0.5;
235 else return x1 + ((y-y1)/(y2-y1)) * (x2-x1);
239 /*compare two edges of a polygon.
240 *edge A < edge B if there is a horizontal line so that the intersection
241 *with A is to the left of the intersection with B.
242 *This function is used in sweepY for the dynamic search tree insertion to
244 * Implementation: (x_1,y_1) and (x_2, y_2)
246 static Int compEdges(directedLine *e1, directedLine *e2)
248 Real* head1 = e1->head();
249 Real* tail1 = e1->tail();
250 Real* head2 = e2->head();
251 Real* tail2 = e2->tail();
262 Real e1_Ymax, e1_Ymin, e2_Ymax, e2_Ymin;
283 if(head1[1]>tail1[1]) {
292 if(head2[1]>tail2[1]) {
302 /*Real e1_Ymax = max(head1[1], tail1[1]);*/ /*max(e1->head()[1], e1->tail()[1]);*/
303 /*Real e1_Ymin = min(head1[1], tail1[1]);*/ /*min(e1->head()[1], e1->tail()[1]);*/
304 /*Real e2_Ymax = max(head2[1], tail2[1]);*/ /*max(e2->head()[1], e2->tail()[1]);*/
305 /*Real e2_Ymin = min(head2[1], tail2[1]);*/ /*min(e2->head()[1], e2->tail()[1]);*/
307 Real Ymax = min(e1_Ymax, e2_Ymax);
308 Real Ymin = max(e1_Ymin, e2_Ymin);
310 Real y = Real(0.5)*(Ymax + Ymin);
312 /* Real x1 = intersectHoriz(e1->head()[0], e1->head()[1], e1->tail()[0], e1->tail()[1], y);
313 Real x2 = intersectHoriz(e2->head()[0], e2->head()[1], e2->tail()[0], e2->tail()[1], y);
316 Real x1 = intersectHoriz(h10, h11, t10, t11, y);
317 Real x2 = intersectHoriz(h20, h21, t20, t21, y);
319 Real x1 = intersectHoriz(head1[0], head1[1], tail1[0], tail1[1], y);
320 Real x2 = intersectHoriz(head2[0], head2[1], tail2[0], tail2[1], y);
322 if(x1<= x2) return -1;
326 /*used by sort precedures
328 static Int compInY(directedLine* v1, directedLine* v2)
330 return v1->compInY(v2);
333 void findDiagonals(Int total_num_edges, directedLine** sortedVertices, sweepRange** ranges, Int& num_diagonals, directedLine** diagonal_vertices)
339 for(i=0; i<total_num_edges; i++)
341 directedLine* vert =sortedVertices[i];
342 directedLine* thisEdge = vert;
343 directedLine* prevEdge = vert->getPrev();
345 printf("find i=%i\n", i);
346 printf("the vertex is\n");
349 if(isBelow(vert, thisEdge) && isBelow(vert, prevEdge) && compEdges(prevEdge, thisEdge)<0)
351 /*this is an upward interior cusp*/
352 diagonal_vertices[k++] = vert;
354 for(j=i+1; j<total_num_edges; j++)
355 if(sweepRangeEqual(ranges[i], ranges[j]))
357 diagonal_vertices[k++] = sortedVertices[j];
360 assert(j<total_num_edges);
364 else if(isAbove(vert, thisEdge) && isAbove(vert, prevEdge) && compEdges(prevEdge, thisEdge)>0)
366 /*this is an downward interior cusp*/
367 diagonal_vertices[k++] = vert;
368 for(j=i-1; j>=0; j--)
369 if(sweepRangeEqual(ranges[i], ranges[j]))
371 diagonal_vertices[k++] = sortedVertices[j];
374 /* printf("j=%i\n", j);*/
384 /*get rid of repeated diagonlas so that each diagonal appears only once in the array
386 Int deleteRepeatDiagonals(Int num_diagonals, directedLine** diagonal_vertices, directedLine** new_vertices)
392 for(i=0,k=0; i<num_diagonals; i++, k+=2)
395 /*check the diagonla (diagonal_vertice[k], diagonal_vertices[k+1])
398 for(j=0,l=0; j<index; j++, l+=2)
401 (diagonal_vertices[k] == new_vertices[l] &&
402 diagonal_vertices[k+1] == new_vertices[l+1]
406 diagonal_vertices[k] == new_vertices[l+1] &&
407 diagonal_vertices[k+1] == new_vertices[l]
417 new_vertices[index+index] = diagonal_vertices[k];
418 new_vertices[index+index+1] = diagonal_vertices[k+1];
426 directedLine** DBGfindDiagonals(directedLine *polygons, Int& num_diagonals)
428 Int total_num_edges = 0;
429 directedLine** array = polygons->toArrayAllPolygons(total_num_edges);
430 quicksort( (void**)array, 0, total_num_edges-1, (Int (*)(void*, void*)) compInY);
431 sweepRange** ranges = (sweepRange**) malloc(sizeof(sweepRange*) * total_num_edges);
434 sweepY(total_num_edges, array, ranges);
436 directedLine** diagonal_vertices = (directedLine**) malloc(sizeof(directedLine*) * total_num_edges);
437 assert(diagonal_vertices);
438 findDiagonals(total_num_edges, array, ranges, num_diagonals, diagonal_vertices);
440 num_diagonals=deleteRepeatDiagonals(num_diagonals, diagonal_vertices, diagonal_vertices);
441 return diagonal_vertices;
446 /*partition into Y-monotone polygons*/
447 directedLine* partitionY(directedLine *polygons, sampledLine **retSampledLines)
449 Int total_num_edges = 0;
450 directedLine** array = polygons->toArrayAllPolygons(total_num_edges);
452 quicksort( (void**)array, 0, total_num_edges-1, (Int (*)(void*, void*)) compInY);
454 sweepRange** ranges = (sweepRange**) malloc(sizeof(sweepRange*) * (total_num_edges));
459 sweepY(total_num_edges, array, ranges);
463 /*the diagonal vertices are stored as:
472 /*number diagonals is < total_num_edges*total_num_edges*/
473 directedLine** diagonal_vertices = (directedLine**) malloc(sizeof(directedLine*) * total_num_edges*2/*total_num_edges*/);
474 assert(diagonal_vertices);
478 findDiagonals(total_num_edges, array, ranges, num_diagonals, diagonal_vertices);
482 directedLine* ret_polygons = polygons;
483 sampledLine* newSampledLines = NULL;
486 num_diagonals=deleteRepeatDiagonals(num_diagonals, diagonal_vertices, diagonal_vertices);
490 Int *removedDiagonals=(Int*)malloc(sizeof(Int) * num_diagonals);
491 for(i=0; i<num_diagonals; i++)
492 removedDiagonals[i] = 0;
498 for(i=0,k=0; i<num_diagonals; i++,k+=2)
502 directedLine* v1=diagonal_vertices[k];
503 directedLine* v2=diagonal_vertices[k+1];
504 directedLine* ret_p1;
505 directedLine* ret_p2;
507 /*we ahve to determine whether v1 and v2 belong to the same polygon before
508 *their structure are modified by connectDiagonal().
511 directedLine *root1 = v1->findRoot();
512 directedLine *root2 = v2->findRoot();
517 directedLine* root1 = v1->rootLinkFindRoot();
518 directedLine* root2 = v2->rootLinkFindRoot();
523 removedDiagonals[i] = 1;
524 sampledLine* generatedLine;
528 v1->connectDiagonal(v1,v2, &ret_p1, &ret_p2, &generatedLine, ret_polygons);
532 newSampledLines = generatedLine->insert(newSampledLines);
534 ret_polygons = ret_polygons->cutoffPolygon(root1);
536 ret_polygons = ret_polygons->cutoffPolygon(root2);
537 ret_polygons = ret_p1->insertPolygon(ret_polygons);
538 root1->rootLinkSet(ret_p1);
539 root2->rootLinkSet(ret_p1);
540 ret_p1->rootLinkSet(NULL);
541 ret_p2->rootLinkSet(ret_p1);
543 ret_polygons = ret_polygons->cutoffPolygon(root2);
547 root2->rootLinkSet(root1);
548 ret_p1->rootLinkSet(root1);
549 ret_p2->rootLinkSet(root1);
551 /*now that we have connected the diagonal v1 and v2,
552 *we have to check those unprocessed diagonals which
553 *have v1 or v2 as an end point. Notice that the head of v1
554 *has the same coodinates as the head of v2->prev, and the head of
555 *v2 has the same coordinate as the head of v1->prev.
556 *Suppose these is a diagonal (v1, x). If (v1,x) is still a valid
557 *diagonal, then x should be on the left hand side of the directed line: *v1->prev->head -- v1->head -- v1->tail. Otherwise, (v1,x) should be
558 *replaced by (v2->prev, x), that is, x is on the left of
559 * v2->prev->prev->head, v2->prev->head, v2->prev->tail.
562 for(ii=0, kk=0; ii<num_diagonals; ii++, kk+=2)
563 if( removedDiagonals[ii]==0)
565 directedLine* d1=diagonal_vertices[kk];
566 directedLine* d2=diagonal_vertices[kk+1];
567 /*check d1, and replace diagonal_vertices[kk] if necessary*/
569 /*check if d2 is to left of v1->prev->head:v1->head:v1->tail*/
570 if(! pointLeft2Lines(v1->getPrev()->head(),
571 v1->head(), v1->tail(), d2->head()))
574 assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
575 v2->getPrev()->head(),
576 v2->getPrev()->tail(), d2->head()));
578 diagonal_vertices[kk] = v2->getPrev();
582 /*check if d2 is to left of v2->prev->head:v2->head:v2->tail*/
583 if(! pointLeft2Lines(v2->getPrev()->head(),
584 v2->head(), v2->tail(), d2->head()))
587 assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
588 v1->getPrev()->head(),
589 v1->getPrev()->tail(), d2->head()));
591 diagonal_vertices[kk] = v1->getPrev();
594 /*check d2 and replace diagonal_vertices[k+1] if necessary*/
596 /*check if d1 is to left of v1->prev->head:v1->head:v1->tail*/
597 if(! pointLeft2Lines(v1->getPrev()->head(),
598 v1->head(), v1->tail(), d1->head()))
600 /* assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
601 v2->getPrev()->head(),
602 v2->getPrev()->tail(), d1->head()));
604 diagonal_vertices[kk+1] = v2->getPrev();
608 /*check if d1 is to left of v2->prev->head:v2->head:v2->tail*/
609 if(! pointLeft2Lines(v2->getPrev()->head(),
610 v2->head(), v2->tail(), d1->head()))
612 /* assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
613 v1->getPrev()->head(),
614 v1->getPrev()->tail(), d1->head()));
616 diagonal_vertices[kk+1] = v1->getPrev();
620 }/*end if (root1 not equal to root 2)*/
623 /*second pass, now all diagoals should belong to the same polygon*/
627 for(i=0,k=0; i<num_diagonals; i++, k += 2)
628 if(removedDiagonals[i] == 0)
632 directedLine* v1=diagonal_vertices[k];
633 directedLine* v2=diagonal_vertices[k+1];
637 directedLine* ret_p1;
638 directedLine* ret_p2;
640 /*we ahve to determine whether v1 and v2 belong to the same polygon before
641 *their structure are modified by connectDiagonal().
643 directedLine *root1 = v1->findRoot();
645 directedLine *root2 = v2->findRoot();
651 assert(root1 == root2);
653 sampledLine* generatedLine;
657 v1->connectDiagonal(v1,v2, &ret_p1, &ret_p2, &generatedLine, ret_polygons);
658 newSampledLines = generatedLine->insert(newSampledLines);
660 ret_polygons = ret_polygons->cutoffPolygon(root1);
662 ret_polygons = ret_p1->insertPolygon(ret_polygons);
664 ret_polygons = ret_p2->insertPolygon(ret_polygons);
668 for(Int j=i+1; j<num_diagonals; j++)
670 if(removedDiagonals[j] ==0)
673 directedLine* temp1=diagonal_vertices[2*j];
674 directedLine* temp2=diagonal_vertices[2*j+1];
675 if(temp1==v1 || temp1==v2 || temp2==v1 || temp2==v2)
676 if(! temp1->samePolygon(temp1, temp2))
678 /*if temp1 and temp2 are in different polygons,
679 *then one of them must be v1 or v2.
684 assert(temp1==v1 || temp1 == v2 || temp2==v1 || temp2 ==v2);
687 diagonal_vertices[2*j] = v2->getPrev();
691 diagonal_vertices[2*j+1] = v2->getPrev();
695 diagonal_vertices[2*j] = v1->getPrev();
699 diagonal_vertices[2*j+1] = v1->getPrev();
710 free(diagonal_vertices);
711 free(removedDiagonals);
713 *retSampledLines = newSampledLines;
717 /*given a set of simple polygons where the interior
718 *is decided by left-hand principle,
719 *return a range (sight) for each vertex. This is called
722 void sweepY(Int nVertices, directedLine** sortedVertices, sweepRange** ret_ranges)
725 /*for each vertex in the sorted list, update the binary search tree.
726 *and store the range information for each vertex.
728 treeNode* searchTree = NULL;
729 for(i=0; i<nVertices;i++)
732 directedLine* vert = sortedVertices[i];
734 directedLine* thisEdge = vert;
735 directedLine* prevEdge = vert->getPrev();
737 if(isBelow(vert, thisEdge) && isAbove(vert, prevEdge))
740 /*case 1: this < v < prev
741 *the polygon is going down at v, the interior is to
742 *the right hand side.
743 * find the edge to the right of thisEdge for right range.
747 treeNode* thisNode = TreeNodeFind(searchTree, thisEdge, ( Int (*) (void *, void *))compEdges);
750 treeNode* succ = TreeNodeSuccessor(thisNode);
752 searchTree = TreeNodeDeleteSingleNode(searchTree, thisNode);
753 searchTree = TreeNodeInsert(searchTree, TreeNodeMake(prevEdge), ( Int (*) (void *, void *))compEdges);
756 ret_ranges[i] = sweepRangeMake(vert, 0, (directedLine*) (succ->key), 1);
759 else if(isAbove(vert, thisEdge) && isBelow(vert, prevEdge))
762 /*case 2: this > v > prev
763 *the polygon is going up at v, the interior is to
765 * find the edge to the left of thisEdge for left range.
769 treeNode* prevNode = TreeNodeFind(searchTree, prevEdge, ( Int (*) (void *, void *))compEdges);
771 treeNode* pred = TreeNodePredecessor(prevNode);
772 searchTree = TreeNodeDeleteSingleNode(searchTree, prevNode);
773 searchTree = TreeNodeInsert(searchTree, TreeNodeMake(thisEdge), ( Int (*) (void *, void *))compEdges);
774 ret_ranges[i] = sweepRangeMake((directedLine*)(pred->key), 1, vert, 0);
776 else if(isAbove(vert, thisEdge) && isAbove(vert, prevEdge))
779 /*case 3: insert both edges*/
780 treeNode* thisNode = TreeNodeMake(thisEdge);
781 treeNode* prevNode = TreeNodeMake(prevEdge);
782 searchTree = TreeNodeInsert(searchTree, thisNode, ( Int (*) (void *, void *))compEdges);
783 searchTree = TreeNodeInsert(searchTree, prevNode, ( Int (*) (void *, void *))compEdges);
784 if(compEdges(thisEdge, prevEdge)<0) /*interior cusp*/
787 treeNode* leftEdge = TreeNodePredecessor(thisNode);
788 treeNode* rightEdge = TreeNodeSuccessor(prevNode);
789 ret_ranges[i] = sweepRangeMake( (directedLine*) leftEdge->key, 1,
790 (directedLine*) rightEdge->key, 1
793 else /*exterior cusp*/
796 ret_ranges[i] = sweepRangeMake( prevEdge, 1, thisEdge, 1);
799 else if(isBelow(vert, thisEdge) && isBelow(vert, prevEdge))
802 /*case 4: delete both edges*/
803 treeNode* thisNode = TreeNodeFind(searchTree, thisEdge, ( Int (*) (void *, void *))compEdges);
804 treeNode* prevNode = TreeNodeFind(searchTree, prevEdge, ( Int (*) (void *, void *))compEdges);
805 if(compEdges(thisEdge, prevEdge)>0) /*interior cusp*/
807 treeNode* leftEdge = TreeNodePredecessor(prevNode);
808 treeNode* rightEdge = TreeNodeSuccessor(thisNode);
809 ret_ranges[i] = sweepRangeMake( (directedLine*) leftEdge->key, 1,
810 (directedLine*) rightEdge->key, 1
813 else /*exterior cusp*/
815 ret_ranges[i] = sweepRangeMake( thisEdge, 1, prevEdge, 1);
817 searchTree = TreeNodeDeleteSingleNode(searchTree, thisNode);
818 searchTree = TreeNodeDeleteSingleNode(searchTree, prevNode);
822 fprintf(stderr,"error in partitionY.C, invalid case\n");
825 printf("thisEdge is\n");
826 thisEdge->printSingle();
827 printf("prevEdge is\n");
828 prevEdge->printSingle();
834 /*finaly clean up space: delete the search tree*/
835 TreeNodeDeleteWholeTree(searchTree);