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 "glimports.h"
46 #include "monoChain.h"
47 #include "quicksort.h"
48 #include "searchTree.h"
52 #define max(a,b) ((a>b)? a:b)
55 #define min(a,b) ((a>b)? b:a)
58 extern Int
isCusp(directedLine
*v
);
59 extern Int
deleteRepeatDiagonals(Int num_diagonals
, directedLine
** diagonal_vertices
, directedLine
** new_vertices
);
61 //for debug purpose only
63 static void drawDiagonals(Int num_diagonals
, directedLine
** diagonal_vertices
)
66 for(i
=0; i
<num_diagonals
; i
++)
69 glVertex2fv(diagonal_vertices
[2*i
]->head());
70 glVertex2fv(diagonal_vertices
[2*i
+1]->head());
76 /*given (x_1, y_1) and (x_2, y_2), and y
77 *return x such that (x,y) is on the line
79 inline Real
intersectHoriz(Real x1
, Real y1
, Real x2
, Real y2
, Real y
)
81 return ((y2
==y1
)? (x1
+x2
)*0.5 : x1
+ ((y
-y1
)/(y2
-y1
)) * (x2
-x1
));
84 //compare the heads of the two chains
85 static int compChainHeadInY(monoChain
* mc1
, monoChain
* mc2
)
87 return compV2InY(mc1
->getHead()->head(), mc2
->getHead()->head());
90 monoChain::monoChain(directedLine
* cHead
, directedLine
* cTail
)
99 //compute bounding box
101 minX
= maxX
= chainTail
->head()[0];
102 minY
= maxY
= chainTail
->head()[1];
104 for(temp
=chainHead
; temp
!=cTail
; temp
= temp
->getNext())
106 if(temp
->head()[0] < minX
)
107 minX
= temp
->head()[0];
108 if(temp
->head()[0] > maxX
)
109 maxX
= temp
->head()[0];
111 if(temp
->head()[1] < minY
)
112 minY
= temp
->head()[1];
113 if(temp
->head()[1] > maxY
)
114 maxY
= temp
->head()[1];
117 //check whether the chain is increasing or decreasing
118 if(chainHead
->compInY(chainTail
) <0)
123 //initilize currrent, this is used for accelerating search
132 //insert a new line between prev and this
133 void monoChain::insert(monoChain
* nc
)
141 void monoChain::deleteLoop()
143 monoChain
*temp
, *tempNext
;
145 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
147 tempNext
= temp
->next
;
152 void monoChain::deleteLoopList()
154 monoChain
*temp
, *tempNext
;
155 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
157 tempNext
= temp
->nextPolygon
;
162 Int
monoChain::toArraySingleLoop(monoChain
** array
, Int index
)
165 array
[index
++] = this;
166 for(temp
= next
; temp
!= this; temp
= temp
->next
)
168 array
[index
++] = temp
;
173 monoChain
** monoChain::toArrayAllLoops(Int
& num_chains
)
175 num_chains
= numChainsAllLoops();
176 monoChain
**ret
= (monoChain
**) malloc(sizeof(monoChain
*) * num_chains
);
180 for(temp
= this; temp
!= NULL
; temp
=temp
->nextPolygon
){
181 index
= temp
->toArraySingleLoop(ret
, index
);
186 Int
monoChain::numChainsSingleLoop()
190 if(next
== this) return 1;
192 for(temp
=next
; temp
!= this; temp
= temp
->next
)
197 Int
monoChain::numChainsAllLoops()
201 for(temp
=this; temp
!= NULL
; temp
= temp
->nextPolygon
)
202 ret
+= temp
->numChainsSingleLoop();
207 Real
monoChain::chainIntersectHoriz(Real y
)
212 for(temp
= current
; temp
!= chainTail
; temp
= temp
->getNext())
214 if(temp
->head()[1] > y
)
217 current
= temp
->getPrev();
221 for(temp
= current
; temp
!= chainHead
; temp
= temp
->getPrev())
223 if(temp
->head()[1] > y
)
226 current
= temp
->getNext();
228 return intersectHoriz(current
->head()[0], current
->head()[1], current
->tail()[0], current
->tail()[1], y
);
231 monoChain
* directedLineLoopToMonoChainLoop(directedLine
* loop
)
236 //find the first cusp
237 directedLine
*prevCusp
=NULL
;
238 directedLine
*firstCusp
;
244 for(temp
= loop
->getNext(); temp
!= loop
; temp
= temp
->getNext())
249 firstCusp
= prevCusp
;
250 //printf("first cusp is (%f,%f), (%f,%f), (%f,%f)\n", prevCusp->getPrev()->head()[0], prevCusp->getPrev()->head()[1], prevCusp->head()[0], prevCusp->head()[1], prevCusp->tail()[0], prevCusp->tail()[1]);
252 for(temp
= prevCusp
->getNext(); temp
!= loop
; temp
= temp
->getNext())
256 //printf("the cusp is (%f,%f), (%f,%f), (%f,%f)\n", temp->getPrev()->head()[0], temp->getPrev()->head()[1], temp->head()[0], temp->head()[1], temp->tail()[0], temp->tail()[1]);
259 ret
= new monoChain(prevCusp
, temp
);
262 ret
->insert(new monoChain(prevCusp
, temp
));
266 ret
->insert(new monoChain(prevCusp
, firstCusp
));
271 monoChain
* directedLineLoopListToMonoChainLoopList(directedLine
* list
)
276 mc
= directedLineLoopToMonoChainLoop(list
);
278 for(temp
= list
->getNextPolygon(); temp
!= NULL
; temp
= temp
->getNextPolygon())
280 monoChain
*newLoop
= directedLineLoopToMonoChainLoop(temp
);
281 mcEnd
->setNextPolygon(newLoop
);
287 /*compare two edges of a polygon.
288 *edge A < edge B if there is a horizontal line so that the intersection
289 *with A is to the left of the intersection with B.
290 *This function is used in sweepY for the dynamic search tree insertion to
292 * Implementation: (x_1,y_1) and (x_2, y_2)
294 static Int
compEdges(directedLine
*e1
, directedLine
*e2
)
296 Real
* head1
= e1
->head();
297 Real
* tail1
= e1
->tail();
298 Real
* head2
= e2
->head();
299 Real
* tail2
= e2
->tail();
310 Real e1_Ymax
, e1_Ymin
, e2_Ymax
, e2_Ymin
;
331 if(head1
[1]>tail1
[1]) {
340 if(head2
[1]>tail2
[1]) {
350 /*Real e1_Ymax = max(head1[1], tail1[1]);*/ /*max(e1->head()[1], e1->tail()[1]);*/
351 /*Real e1_Ymin = min(head1[1], tail1[1]);*/ /*min(e1->head()[1], e1->tail()[1]);*/
352 /*Real e2_Ymax = max(head2[1], tail2[1]);*/ /*max(e2->head()[1], e2->tail()[1]);*/
353 /*Real e2_Ymin = min(head2[1], tail2[1]);*/ /*min(e2->head()[1], e2->tail()[1]);*/
355 Real Ymax
= min(e1_Ymax
, e2_Ymax
);
356 Real Ymin
= max(e1_Ymin
, e2_Ymin
);
358 Real y
= 0.5*(Ymax
+ Ymin
);
360 /* Real x1 = intersectHoriz(e1->head()[0], e1->head()[1], e1->tail()[0], e1->tail()[1], y);
361 Real x2 = intersectHoriz(e2->head()[0], e2->head()[1], e2->tail()[0], e2->tail()[1], y);
364 Real x1 = intersectHoriz(h10, h11, t10, t11, y);
365 Real x2 = intersectHoriz(h20, h21, t20, t21, y);
367 Real x1
= intersectHoriz(head1
[0], head1
[1], tail1
[0], tail1
[1], y
);
368 Real x2
= intersectHoriz(head2
[0], head2
[1], tail2
[0], tail2
[1], y
);
370 if(x1
<= x2
) return -1;
374 Int
compChains(monoChain
* mc1
, monoChain
* mc2
)
377 assert(mc1
->isKey
|| mc2
->isKey
);
382 directedLine
*d1
= mc1
->find(y
);
383 directedLine
*d2
= mc2
->find(y
);
385 // Real x1 = mc1->chainIntersectHoriz(y);
386 // Real x2 = mc2->chainIntersectHoriz(y);
387 return compEdges(d1
, d2
);
390 //this function modifies current for efficiency
391 directedLine
* monoChain::find(Real y
)
395 assert(current
->head()[1] <= y
);
398 assert(chainTail
->head()[1] >=y
);
399 for(temp
=current
; temp
!=chainTail
; temp
= temp
->getNext())
401 if(temp
->head()[1] > y
)
404 current
= temp
->getPrev();
409 for(temp
=current
; temp
!= chainHead
; temp
= temp
->getPrev())
411 if(temp
->head()[1] > y
)
414 current
= temp
->getNext();
420 void monoChain::printOneChain()
423 for(temp
= chainHead
; temp
!= chainTail
; temp
= temp
->getNext())
425 printf("(%f,%f) ", temp
->head()[0], temp
->head()[1]);
427 printf("(%f,%f) \n", chainTail
->head()[0], chainTail
->head()[1]);
430 void monoChain::printChainLoop()
433 this->printOneChain();
434 for(temp
= next
; temp
!= this; temp
= temp
->next
)
436 temp
->printOneChain();
441 void monoChain::printAllLoops()
444 for(temp
=this; temp
!= NULL
; temp
= temp
->nextPolygon
)
445 temp
->printChainLoop();
448 //return 1 if error occures
449 Int
MC_sweepY(Int nVertices
, monoChain
** sortedVertices
, sweepRange
** ret_ranges
)
454 //printf("enter MC_sweepY\n");
455 //printf("nVertices=%i\n", nVertices);
456 /*for each vertex in the sorted list, update the binary search tree.
457 *and store the range information for each vertex.
459 treeNode
* searchTree
= NULL
;
460 //printf("nVertices=%i\n", nVertices);
461 for(i
=0; i
<nVertices
; i
++)
463 monoChain
* vert
= sortedVertices
[i
];
464 keyY
= vert
->getHead()->head()[1]; //the sweep line
465 directedLine
*dline
= vert
->getHead();
466 directedLine
*dlinePrev
= dline
->getPrev();
467 if(isBelow(dline
, dline
) && isBelow(dline
, dlinePrev
))
469 //printf("case 1\n");
470 //this<v and prev < v
474 treeNode
* thisNode
= TreeNodeFind(searchTree
, vert
, (Int (*) (void *, void *))compChains
);
477 vert
->getPrev()->isKey
= 1;
478 vert
->getPrev()->keyY
= keyY
;
479 treeNode
* prevNode
= TreeNodeFind(searchTree
, vert
->getPrev(), (Int (*) (void *, void *))compChains
);
480 vert
->getPrev()->isKey
= 0;
482 if(cuspType(dline
) == 1)//interior cusp
485 treeNode
* leftEdge
= TreeNodePredecessor(prevNode
);
486 treeNode
* rightEdge
= TreeNodeSuccessor(thisNode
);
487 if(leftEdge
== NULL
|| rightEdge
== NULL
)
493 directedLine
* leftEdgeDline
= ((monoChain
* ) leftEdge
->key
)->find(keyY
);
497 directedLine
* rightEdgeDline
= ((monoChain
* ) rightEdge
->key
)->find(keyY
);
499 ret_ranges
[i
] = sweepRangeMake(leftEdgeDline
, 1, rightEdgeDline
, 1);
501 else /*exterior cusp*/
503 ret_ranges
[i
] = sweepRangeMake( dline
, 1, dlinePrev
, 1);
506 searchTree
= TreeNodeDeleteSingleNode(searchTree
, thisNode
);
507 searchTree
= TreeNodeDeleteSingleNode(searchTree
, prevNode
);
510 else if(isAbove(dline
, dline
) && isAbove(dline
, dlinePrev
))
512 //printf("case 2\n");
514 treeNode
* thisNode
= TreeNodeMake(vert
);
515 treeNode
* prevNode
= TreeNodeMake(vert
->getPrev());
519 searchTree
= TreeNodeInsert(searchTree
, thisNode
, (Int (*) (void *, void *))compChains
);
522 vert
->getPrev()->isKey
= 1;
523 vert
->getPrev()->keyY
= keyY
;
524 searchTree
= TreeNodeInsert(searchTree
, prevNode
, (Int (*) (void *, void *))compChains
);
525 vert
->getPrev()->isKey
= 0;
527 if(cuspType(dline
) == 1) //interior cusp
529 //printf("cuspType is 1\n");
530 treeNode
* leftEdge
= TreeNodePredecessor(thisNode
);
531 treeNode
* rightEdge
= TreeNodeSuccessor(prevNode
);
532 if(leftEdge
== NULL
|| rightEdge
== NULL
)
537 //printf("leftEdge is %i, rightEdge is %i\n", leftEdge, rightEdge);
538 directedLine
* leftEdgeDline
= ((monoChain
*) leftEdge
->key
)->find(keyY
);
539 directedLine
* rightEdgeDline
= ((monoChain
*) rightEdge
->key
)->find(keyY
);
540 ret_ranges
[i
] = sweepRangeMake( leftEdgeDline
, 1, rightEdgeDline
, 1);
544 //printf("cuspType is not 1\n");
545 ret_ranges
[i
] = sweepRangeMake(dlinePrev
, 1, dline
, 1);
550 //printf("%i,%i\n", isAbove(dline, dline), isAbove(dline, dlinePrev));
554 fprintf(stderr
, "error in MC_sweepY\n");
560 //finally clean up space: delete the search tree
561 TreeNodeDeleteWholeTree(searchTree
);
565 void MC_findDiagonals(Int total_num_edges
, monoChain
** sortedVertices
,
566 sweepRange
** ranges
, Int
& num_diagonals
,
567 directedLine
** diagonal_vertices
)
571 //reset 'current' of all the monoChains
572 for(i
=0; i
<total_num_edges
; i
++)
573 sortedVertices
[i
]->resetCurrent();
575 for(i
=0; i
<total_num_edges
; i
++)
577 directedLine
* vert
= sortedVertices
[i
]->getHead();
578 directedLine
* thisEdge
= vert
;
579 directedLine
* prevEdge
= vert
->getPrev();
580 if(isBelow(vert
, thisEdge
) && isBelow(vert
, prevEdge
) && compEdges(prevEdge
, thisEdge
)<0)
582 //this is an upward interior cusp
583 diagonal_vertices
[k
++] = vert
;
585 directedLine
* leftEdge
= ranges
[i
]->left
;
586 directedLine
* rightEdge
= ranges
[i
]->right
;
588 directedLine
* leftVert
= leftEdge
;
589 directedLine
* rightVert
= rightEdge
->getNext();
590 assert(leftVert
->head()[1] >= vert
->head()[1]);
591 assert(rightVert
->head()[1] >= vert
->head()[1]);
592 directedLine
* minVert
= (leftVert
->head()[1] <= rightVert
->head()[1])?leftVert
:rightVert
;
594 for(j
=i
+1; j
<total_num_edges
; j
++)
596 if(sortedVertices
[j
]->getHead()->head()[1] > minVert
->head()[1])
599 if(sweepRangeEqual(ranges
[i
], ranges
[j
]))
607 diagonal_vertices
[k
++] = sortedVertices
[j
]->getHead();
609 diagonal_vertices
[k
++] = minVert
;
611 else if(isAbove(vert
, thisEdge
) && isAbove(vert
, prevEdge
) && compEdges(prevEdge
, thisEdge
)>0)
613 //downward interior cusp
614 diagonal_vertices
[k
++] = vert
;
615 directedLine
* leftEdge
= ranges
[i
]->left
;
616 directedLine
* rightEdge
= ranges
[i
]->right
;
617 directedLine
* leftVert
= leftEdge
->getNext();
618 directedLine
* rightVert
= rightEdge
;
619 assert(leftVert
->head()[1] <= vert
->head()[1]);
620 assert(rightVert
->head()[1] <= vert
->head()[1]);
621 directedLine
* maxVert
= (leftVert
->head()[1] > rightVert
->head()[1])? leftVert
:rightVert
;
623 for(j
=i
-1; j
>=0; j
--)
625 if(sortedVertices
[j
]->getHead()->head()[1] < maxVert
->head()[1])
627 if(sweepRangeEqual(ranges
[i
], ranges
[j
]))
634 diagonal_vertices
[k
++] = sortedVertices
[j
]->getHead();
636 diagonal_vertices
[k
++] = maxVert
;
645 directedLine
* MC_partitionY(directedLine
*polygons
, sampledLine
**retSampledLines
)
647 //printf("enter mc_partitionY\n");
648 Int total_num_chains
= 0;
649 monoChain
* loopList
= directedLineLoopListToMonoChainLoopList(polygons
);
650 monoChain
** array
= loopList
->toArrayAllLoops(total_num_chains
);
652 if(total_num_chains
<=2) //there is just one single monotone polygon
654 loopList
->deleteLoopList();
656 *retSampledLines
= NULL
;
660 //loopList->printAllLoops();
661 //printf("total_num_chains=%i\n", total_num_chains);
662 quicksort( (void**)array
, 0, total_num_chains
-1, (Int (*)(void*, void*))compChainHeadInY
);
663 //printf("after quicksort\n");
665 sweepRange
** ranges
= (sweepRange
**)malloc(sizeof(sweepRange
*) * (total_num_chains
));
668 if(MC_sweepY(total_num_chains
, array
, ranges
))
670 loopList
->deleteLoopList();
672 *retSampledLines
= NULL
;
675 //printf("after MC_sweepY\n");
679 /*number diagonals is < total_num_edges*total_num_edges*/
680 directedLine
** diagonal_vertices
= (directedLine
**) malloc(sizeof(directedLine
*) * total_num_chains
*2/*total_num_edges*/);
681 assert(diagonal_vertices
);
683 //printf("before call MC_findDiagonales\n");
685 MC_findDiagonals(total_num_chains
, array
, ranges
, num_diagonals
, diagonal_vertices
);
686 //printf("after call MC_findDia, num_diagnla=%i\n", num_diagonals);
688 directedLine
* ret_polygons
= polygons
;
689 sampledLine
* newSampledLines
= NULL
;
692 num_diagonals
=deleteRepeatDiagonals(num_diagonals
, diagonal_vertices
, diagonal_vertices
);
696 //drawDiagonals(num_diagonals, diagonal_vertices);
697 //printf("diagoanls are \n");
698 //for(i=0; i<num_diagonals; i++)
700 // printf("(%f,%f)\n", diagonal_vertices[2*i]->head()[0], diagonal_vertices[2*i]->head()[1]);
701 // printf("**(%f,%f)\n", diagonal_vertices[2*i+1]->head()[0], diagonal_vertices[2*i+1]->head()[1]);
704 Int
*removedDiagonals
=(Int
*)malloc(sizeof(Int
) * num_diagonals
);
705 for(i
=0; i
<num_diagonals
; i
++)
706 removedDiagonals
[i
] = 0;
707 // printf("first pass\n");
710 for(i
=0,k
=0; i
<num_diagonals
; i
++,k
+=2)
714 directedLine
* v1
=diagonal_vertices
[k
];
715 directedLine
* v2
=diagonal_vertices
[k
+1];
716 directedLine
* ret_p1
;
717 directedLine
* ret_p2
;
719 /*we ahve to determine whether v1 and v2 belong to the same polygon before
720 *their structure are modified by connectDiagonal().
723 directedLine *root1 = v1->findRoot();
724 directedLine *root2 = v2->findRoot();
729 directedLine
* root1
= v1
->rootLinkFindRoot();
730 directedLine
* root2
= v2
->rootLinkFindRoot();
735 removedDiagonals
[i
] = 1;
736 sampledLine
* generatedLine
;
740 v1
->connectDiagonal(v1
,v2
, &ret_p1
, &ret_p2
, &generatedLine
, ret_polygons
);
744 newSampledLines
= generatedLine
->insert(newSampledLines
);
746 ret_polygons = ret_polygons->cutoffPolygon(root1);
748 ret_polygons = ret_polygons->cutoffPolygon(root2);
749 ret_polygons = ret_p1->insertPolygon(ret_polygons);
750 root1->rootLinkSet(ret_p1);
751 root2->rootLinkSet(ret_p1);
752 ret_p1->rootLinkSet(NULL);
753 ret_p2->rootLinkSet(ret_p1);
755 ret_polygons
= ret_polygons
->cutoffPolygon(root2
);
759 root2
->rootLinkSet(root1
);
760 ret_p1
->rootLinkSet(root1
);
761 ret_p2
->rootLinkSet(root1
);
763 /*now that we have connected the diagonal v1 and v2,
764 *we have to check those unprocessed diagonals which
765 *have v1 or v2 as an end point. Notice that the head of v1
766 *has the same coodinates as the head of v2->prev, and the head of
767 *v2 has the same coordinate as the head of v1->prev.
768 *Suppose these is a diagonal (v1, x). If (v1,x) is still a valid
769 *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
770 *replaced by (v2->prev, x), that is, x is on the left of
771 * v2->prev->prev->head, v2->prev->head, v2->prev->tail.
774 for(ii
=0, kk
=0; ii
<num_diagonals
; ii
++, kk
+=2)
775 if( removedDiagonals
[ii
]==0)
777 directedLine
* d1
=diagonal_vertices
[kk
];
778 directedLine
* d2
=diagonal_vertices
[kk
+1];
779 /*check d1, and replace diagonal_vertices[kk] if necessary*/
781 /*check if d2 is to left of v1->prev->head:v1->head:v1->tail*/
782 if(! pointLeft2Lines(v1
->getPrev()->head(),
783 v1
->head(), v1
->tail(), d2
->head()))
786 assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
787 v2->getPrev()->head(),
788 v2->getPrev()->tail(), d2->head()));
790 diagonal_vertices
[kk
] = v2
->getPrev();
794 /*check if d2 is to left of v2->prev->head:v2->head:v2->tail*/
795 if(! pointLeft2Lines(v2
->getPrev()->head(),
796 v2
->head(), v2
->tail(), d2
->head()))
799 assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
800 v1->getPrev()->head(),
801 v1->getPrev()->tail(), d2->head()));
803 diagonal_vertices
[kk
] = v1
->getPrev();
806 /*check d2 and replace diagonal_vertices[k+1] if necessary*/
808 /*check if d1 is to left of v1->prev->head:v1->head:v1->tail*/
809 if(! pointLeft2Lines(v1
->getPrev()->head(),
810 v1
->head(), v1
->tail(), d1
->head()))
812 /* assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
813 v2->getPrev()->head(),
814 v2->getPrev()->tail(), d1->head()));
816 diagonal_vertices
[kk
+1] = v2
->getPrev();
820 /*check if d1 is to left of v2->prev->head:v2->head:v2->tail*/
821 if(! pointLeft2Lines(v2
->getPrev()->head(),
822 v2
->head(), v2
->tail(), d1
->head()))
824 /* assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
825 v1->getPrev()->head(),
826 v1->getPrev()->tail(), d1->head()));
828 diagonal_vertices
[kk
+1] = v1
->getPrev();
832 }/*end if (root1 not equal to root 2)*/
835 /*second pass, now all diagoals should belong to the same polygon*/
836 //printf("second pass: \n");
838 // for(i=0; i<num_diagonals; i++)
839 // printf("%i ", removedDiagonals[i]);
842 for(i
=0,k
=0; i
<num_diagonals
; i
++, k
+= 2)
843 if(removedDiagonals
[i
] == 0)
847 directedLine
* v1
=diagonal_vertices
[k
];
848 directedLine
* v2
=diagonal_vertices
[k
+1];
852 directedLine
* ret_p1
;
853 directedLine
* ret_p2
;
855 /*we ahve to determine whether v1 and v2 belong to the same polygon before
856 *their structure are modified by connectDiagonal().
858 directedLine
*root1
= v1
->findRoot();
860 directedLine *root2 = v2->findRoot();
866 assert(root1 == root2);
868 sampledLine
* generatedLine
;
872 v1
->connectDiagonal(v1
,v2
, &ret_p1
, &ret_p2
, &generatedLine
, ret_polygons
);
873 newSampledLines
= generatedLine
->insert(newSampledLines
);
875 ret_polygons
= ret_polygons
->cutoffPolygon(root1
);
877 ret_polygons
= ret_p1
->insertPolygon(ret_polygons
);
879 ret_polygons
= ret_p2
->insertPolygon(ret_polygons
);
883 for(Int j
=i
+1; j
<num_diagonals
; j
++)
885 if(removedDiagonals
[j
] ==0)
888 directedLine
* temp1
=diagonal_vertices
[2*j
];
889 directedLine
* temp2
=diagonal_vertices
[2*j
+1];
890 if(temp1
==v1
|| temp1
==v2
|| temp2
==v1
|| temp2
==v2
)
891 if(! temp1
->samePolygon(temp1
, temp2
))
893 /*if temp1 and temp2 are in different polygons,
894 *then one of them must be v1 or v2.
899 assert(temp1
==v1
|| temp1
== v2
|| temp2
==v1
|| temp2
==v2
);
902 diagonal_vertices
[2*j
] = v2
->getPrev();
906 diagonal_vertices
[2*j
+1] = v2
->getPrev();
910 diagonal_vertices
[2*j
] = v1
->getPrev();
914 diagonal_vertices
[2*j
+1] = v1
->getPrev();
924 loopList
->deleteLoopList();
927 free(diagonal_vertices
);
928 free(removedDiagonals
);
930 *retSampledLines
= newSampledLines
;