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
133 //insert a new line between prev and this
134 void monoChain::insert(monoChain
* nc
)
142 void monoChain::deleteLoop()
144 monoChain
*temp
, *tempNext
;
146 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
148 tempNext
= temp
->next
;
153 void monoChain::deleteLoopList()
155 monoChain
*temp
, *tempNext
;
156 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
158 tempNext
= temp
->nextPolygon
;
163 Int
monoChain::toArraySingleLoop(monoChain
** array
, Int index
)
166 array
[index
++] = this;
167 for(temp
= next
; temp
!= this; temp
= temp
->next
)
169 array
[index
++] = temp
;
174 monoChain
** monoChain::toArrayAllLoops(Int
& num_chains
)
176 num_chains
= numChainsAllLoops();
177 monoChain
**ret
= (monoChain
**) malloc(sizeof(monoChain
*) * num_chains
);
181 for(temp
= this; temp
!= NULL
; temp
=temp
->nextPolygon
){
182 index
= temp
->toArraySingleLoop(ret
, index
);
187 Int
monoChain::numChainsSingleLoop()
191 if(next
== this) return 1;
193 for(temp
=next
; temp
!= this; temp
= temp
->next
)
198 Int
monoChain::numChainsAllLoops()
202 for(temp
=this; temp
!= NULL
; temp
= temp
->nextPolygon
)
203 ret
+= temp
->numChainsSingleLoop();
208 Real
monoChain::chainIntersectHoriz(Real y
)
213 for(temp
= current
; temp
!= chainTail
; temp
= temp
->getNext())
215 if(temp
->head()[1] > y
)
218 current
= temp
->getPrev();
222 for(temp
= current
; temp
!= chainHead
; temp
= temp
->getPrev())
224 if(temp
->head()[1] > y
)
227 current
= temp
->getNext();
229 return intersectHoriz(current
->head()[0], current
->head()[1], current
->tail()[0], current
->tail()[1], y
);
232 monoChain
* directedLineLoopToMonoChainLoop(directedLine
* loop
)
237 //find the first cusp
238 directedLine
*prevCusp
=NULL
;
239 directedLine
*firstCusp
;
245 for(temp
= loop
->getNext(); temp
!= loop
; temp
= temp
->getNext())
250 firstCusp
= prevCusp
;
251 //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]);
253 for(temp
= prevCusp
->getNext(); temp
!= loop
; temp
= temp
->getNext())
257 //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]);
260 ret
= new monoChain(prevCusp
, temp
);
263 ret
->insert(new monoChain(prevCusp
, temp
));
268 ret
->insert(new monoChain(prevCusp
, firstCusp
));
273 monoChain
* directedLineLoopListToMonoChainLoopList(directedLine
* list
)
278 mc
= directedLineLoopToMonoChainLoop(list
);
280 for(temp
= list
->getNextPolygon(); temp
!= NULL
; temp
= temp
->getNextPolygon())
282 monoChain
*newLoop
= directedLineLoopToMonoChainLoop(temp
);
283 mcEnd
->setNextPolygon(newLoop
);
289 /*compare two edges of a polygon.
290 *edge A < edge B if there is a horizontal line so that the intersection
291 *with A is to the left of the intersection with B.
292 *This function is used in sweepY for the dynamic search tree insertion to
294 * Implementation: (x_1,y_1) and (x_2, y_2)
296 static Int
compEdges(directedLine
*e1
, directedLine
*e2
)
298 Real
* head1
= e1
->head();
299 Real
* tail1
= e1
->tail();
300 Real
* head2
= e2
->head();
301 Real
* tail2
= e2
->tail();
312 Real e1_Ymax
, e1_Ymin
, e2_Ymax
, e2_Ymin
;
333 if(head1
[1]>tail1
[1]) {
342 if(head2
[1]>tail2
[1]) {
352 /*Real e1_Ymax = max(head1[1], tail1[1]);*/ /*max(e1->head()[1], e1->tail()[1]);*/
353 /*Real e1_Ymin = min(head1[1], tail1[1]);*/ /*min(e1->head()[1], e1->tail()[1]);*/
354 /*Real e2_Ymax = max(head2[1], tail2[1]);*/ /*max(e2->head()[1], e2->tail()[1]);*/
355 /*Real e2_Ymin = min(head2[1], tail2[1]);*/ /*min(e2->head()[1], e2->tail()[1]);*/
357 Real Ymax
= min(e1_Ymax
, e2_Ymax
);
358 Real Ymin
= max(e1_Ymin
, e2_Ymin
);
360 Real y
= 0.5*(Ymax
+ Ymin
);
362 /* Real x1 = intersectHoriz(e1->head()[0], e1->head()[1], e1->tail()[0], e1->tail()[1], y);
363 Real x2 = intersectHoriz(e2->head()[0], e2->head()[1], e2->tail()[0], e2->tail()[1], y);
366 Real x1 = intersectHoriz(h10, h11, t10, t11, y);
367 Real x2 = intersectHoriz(h20, h21, t20, t21, y);
369 Real x1
= intersectHoriz(head1
[0], head1
[1], tail1
[0], tail1
[1], y
);
370 Real x2
= intersectHoriz(head2
[0], head2
[1], tail2
[0], tail2
[1], y
);
372 if(x1
<= x2
) return -1;
376 Int
compChains(monoChain
* mc1
, monoChain
* mc2
)
379 assert(mc1
->isKey
|| mc2
->isKey
);
384 directedLine
*d1
= mc1
->find(y
);
385 directedLine
*d2
= mc2
->find(y
);
387 // Real x1 = mc1->chainIntersectHoriz(y);
388 // Real x2 = mc2->chainIntersectHoriz(y);
389 return compEdges(d1
, d2
);
392 //this function modifies current for efficiency
393 directedLine
* monoChain::find(Real y
)
397 assert(current
->head()[1] <= y
);
400 assert(chainTail
->head()[1] >=y
);
401 for(temp
=current
; temp
!=chainTail
; temp
= temp
->getNext())
403 if(temp
->head()[1] > y
)
406 current
= temp
->getPrev();
411 for(temp
=current
; temp
!= chainHead
; temp
= temp
->getPrev())
413 if(temp
->head()[1] > y
)
416 current
= temp
->getNext();
422 void monoChain::printOneChain()
425 for(temp
= chainHead
; temp
!= chainTail
; temp
= temp
->getNext())
427 printf("(%f,%f) ", temp
->head()[0], temp
->head()[1]);
429 printf("(%f,%f) \n", chainTail
->head()[0], chainTail
->head()[1]);
432 void monoChain::printChainLoop()
435 this->printOneChain();
436 for(temp
= next
; temp
!= this; temp
= temp
->next
)
438 temp
->printOneChain();
443 void monoChain::printAllLoops()
446 for(temp
=this; temp
!= NULL
; temp
= temp
->nextPolygon
)
447 temp
->printChainLoop();
450 //return 1 if error occures
451 Int
MC_sweepY(Int nVertices
, monoChain
** sortedVertices
, sweepRange
** ret_ranges
)
456 //printf("enter MC_sweepY\n");
457 //printf("nVertices=%i\n", nVertices);
458 /*for each vertex in the sorted list, update the binary search tree.
459 *and store the range information for each vertex.
461 treeNode
* searchTree
= NULL
;
462 //printf("nVertices=%i\n", nVertices);
463 for(i
=0; i
<nVertices
; i
++)
465 monoChain
* vert
= sortedVertices
[i
];
466 keyY
= vert
->getHead()->head()[1]; //the sweep line
467 directedLine
*dline
= vert
->getHead();
468 directedLine
*dlinePrev
= dline
->getPrev();
469 if(isBelow(dline
, dline
) && isBelow(dline
, dlinePrev
))
471 //printf("case 1\n");
472 //this<v and prev < v
476 treeNode
* thisNode
= TreeNodeFind(searchTree
, vert
, (Int (*) (void *, void *))compChains
);
479 vert
->getPrev()->isKey
= 1;
480 vert
->getPrev()->keyY
= keyY
;
481 treeNode
* prevNode
= TreeNodeFind(searchTree
, vert
->getPrev(), (Int (*) (void *, void *))compChains
);
482 vert
->getPrev()->isKey
= 0;
484 if(cuspType(dline
) == 1)//interior cusp
487 treeNode
* leftEdge
= TreeNodePredecessor(prevNode
);
488 treeNode
* rightEdge
= TreeNodeSuccessor(thisNode
);
489 if(leftEdge
== NULL
|| rightEdge
== NULL
)
495 directedLine
* leftEdgeDline
= ((monoChain
* ) leftEdge
->key
)->find(keyY
);
499 directedLine
* rightEdgeDline
= ((monoChain
* ) rightEdge
->key
)->find(keyY
);
501 ret_ranges
[i
] = sweepRangeMake(leftEdgeDline
, 1, rightEdgeDline
, 1);
503 else /*exterior cusp*/
505 ret_ranges
[i
] = sweepRangeMake( dline
, 1, dlinePrev
, 1);
508 searchTree
= TreeNodeDeleteSingleNode(searchTree
, thisNode
);
509 searchTree
= TreeNodeDeleteSingleNode(searchTree
, prevNode
);
512 else if(isAbove(dline
, dline
) && isAbove(dline
, dlinePrev
))
514 //printf("case 2\n");
516 treeNode
* thisNode
= TreeNodeMake(vert
);
517 treeNode
* prevNode
= TreeNodeMake(vert
->getPrev());
521 searchTree
= TreeNodeInsert(searchTree
, thisNode
, (Int (*) (void *, void *))compChains
);
524 vert
->getPrev()->isKey
= 1;
525 vert
->getPrev()->keyY
= keyY
;
526 searchTree
= TreeNodeInsert(searchTree
, prevNode
, (Int (*) (void *, void *))compChains
);
527 vert
->getPrev()->isKey
= 0;
529 if(cuspType(dline
) == 1) //interior cusp
531 //printf("cuspType is 1\n");
532 treeNode
* leftEdge
= TreeNodePredecessor(thisNode
);
533 treeNode
* rightEdge
= TreeNodeSuccessor(prevNode
);
534 if(leftEdge
== NULL
|| rightEdge
== NULL
)
539 //printf("leftEdge is %i, rightEdge is %i\n", leftEdge, rightEdge);
540 directedLine
* leftEdgeDline
= ((monoChain
*) leftEdge
->key
)->find(keyY
);
541 directedLine
* rightEdgeDline
= ((monoChain
*) rightEdge
->key
)->find(keyY
);
542 ret_ranges
[i
] = sweepRangeMake( leftEdgeDline
, 1, rightEdgeDline
, 1);
546 //printf("cuspType is not 1\n");
547 ret_ranges
[i
] = sweepRangeMake(dlinePrev
, 1, dline
, 1);
552 //printf("%i,%i\n", isAbove(dline, dline), isAbove(dline, dlinePrev));
556 fprintf(stderr
, "error in MC_sweepY\n");
562 //finally clean up space: delete the search tree
563 TreeNodeDeleteWholeTree(searchTree
);
567 void MC_findDiagonals(Int total_num_edges
, monoChain
** sortedVertices
,
568 sweepRange
** ranges
, Int
& num_diagonals
,
569 directedLine
** diagonal_vertices
)
573 //reset 'current' of all the monoChains
574 for(i
=0; i
<total_num_edges
; i
++)
575 sortedVertices
[i
]->resetCurrent();
577 for(i
=0; i
<total_num_edges
; i
++)
579 directedLine
* vert
= sortedVertices
[i
]->getHead();
580 directedLine
* thisEdge
= vert
;
581 directedLine
* prevEdge
= vert
->getPrev();
582 if(isBelow(vert
, thisEdge
) && isBelow(vert
, prevEdge
) && compEdges(prevEdge
, thisEdge
)<0)
584 //this is an upward interior cusp
585 diagonal_vertices
[k
++] = vert
;
587 directedLine
* leftEdge
= ranges
[i
]->left
;
588 directedLine
* rightEdge
= ranges
[i
]->right
;
590 directedLine
* leftVert
= leftEdge
;
591 directedLine
* rightVert
= rightEdge
->getNext();
592 assert(leftVert
->head()[1] >= vert
->head()[1]);
593 assert(rightVert
->head()[1] >= vert
->head()[1]);
594 directedLine
* minVert
= (leftVert
->head()[1] <= rightVert
->head()[1])?leftVert
:rightVert
;
596 for(j
=i
+1; j
<total_num_edges
; j
++)
598 if(sortedVertices
[j
]->getHead()->head()[1] > minVert
->head()[1])
601 if(sweepRangeEqual(ranges
[i
], ranges
[j
]))
609 diagonal_vertices
[k
++] = sortedVertices
[j
]->getHead();
611 diagonal_vertices
[k
++] = minVert
;
613 else if(isAbove(vert
, thisEdge
) && isAbove(vert
, prevEdge
) && compEdges(prevEdge
, thisEdge
)>0)
615 //downward interior cusp
616 diagonal_vertices
[k
++] = vert
;
617 directedLine
* leftEdge
= ranges
[i
]->left
;
618 directedLine
* rightEdge
= ranges
[i
]->right
;
619 directedLine
* leftVert
= leftEdge
->getNext();
620 directedLine
* rightVert
= rightEdge
;
621 assert(leftVert
->head()[1] <= vert
->head()[1]);
622 assert(rightVert
->head()[1] <= vert
->head()[1]);
623 directedLine
* maxVert
= (leftVert
->head()[1] > rightVert
->head()[1])? leftVert
:rightVert
;
625 for(j
=i
-1; j
>=0; j
--)
627 if(sortedVertices
[j
]->getHead()->head()[1] < maxVert
->head()[1])
629 if(sweepRangeEqual(ranges
[i
], ranges
[j
]))
636 diagonal_vertices
[k
++] = sortedVertices
[j
]->getHead();
638 diagonal_vertices
[k
++] = maxVert
;
647 directedLine
* MC_partitionY(directedLine
*polygons
, sampledLine
**retSampledLines
)
649 //printf("enter mc_partitionY\n");
650 Int total_num_chains
= 0;
651 monoChain
* loopList
= directedLineLoopListToMonoChainLoopList(polygons
);
652 monoChain
** array
= loopList
->toArrayAllLoops(total_num_chains
);
654 if(total_num_chains
<=2) //there is just one single monotone polygon
656 loopList
->deleteLoopList();
658 *retSampledLines
= NULL
;
662 //loopList->printAllLoops();
663 //printf("total_num_chains=%i\n", total_num_chains);
664 quicksort( (void**)array
, 0, total_num_chains
-1, (Int (*)(void*, void*))compChainHeadInY
);
665 //printf("after quicksort\n");
667 sweepRange
** ranges
= (sweepRange
**)malloc(sizeof(sweepRange
*) * (total_num_chains
));
670 if(MC_sweepY(total_num_chains
, array
, ranges
))
672 loopList
->deleteLoopList();
674 *retSampledLines
= NULL
;
677 //printf("after MC_sweepY\n");
681 /*number diagonals is < total_num_edges*total_num_edges*/
682 directedLine
** diagonal_vertices
= (directedLine
**) malloc(sizeof(directedLine
*) * total_num_chains
*2/*total_num_edges*/);
683 assert(diagonal_vertices
);
685 //printf("before call MC_findDiagonales\n");
687 MC_findDiagonals(total_num_chains
, array
, ranges
, num_diagonals
, diagonal_vertices
);
688 //printf("after call MC_findDia, num_diagnla=%i\n", num_diagonals);
690 directedLine
* ret_polygons
= polygons
;
691 sampledLine
* newSampledLines
= NULL
;
694 num_diagonals
=deleteRepeatDiagonals(num_diagonals
, diagonal_vertices
, diagonal_vertices
);
698 //drawDiagonals(num_diagonals, diagonal_vertices);
699 //printf("diagoanls are \n");
700 //for(i=0; i<num_diagonals; i++)
702 // printf("(%f,%f)\n", diagonal_vertices[2*i]->head()[0], diagonal_vertices[2*i]->head()[1]);
703 // printf("**(%f,%f)\n", diagonal_vertices[2*i+1]->head()[0], diagonal_vertices[2*i+1]->head()[1]);
706 Int
*removedDiagonals
=(Int
*)malloc(sizeof(Int
) * num_diagonals
);
707 for(i
=0; i
<num_diagonals
; i
++)
708 removedDiagonals
[i
] = 0;
709 // printf("first pass\n");
712 for(i
=0,k
=0; i
<num_diagonals
; i
++,k
+=2)
716 directedLine
* v1
=diagonal_vertices
[k
];
717 directedLine
* v2
=diagonal_vertices
[k
+1];
718 directedLine
* ret_p1
;
719 directedLine
* ret_p2
;
721 /*we ahve to determine whether v1 and v2 belong to the same polygon before
722 *their structure are modified by connectDiagonal().
725 directedLine *root1 = v1->findRoot();
726 directedLine *root2 = v2->findRoot();
731 directedLine
* root1
= v1
->rootLinkFindRoot();
732 directedLine
* root2
= v2
->rootLinkFindRoot();
737 removedDiagonals
[i
] = 1;
738 sampledLine
* generatedLine
;
742 v1
->connectDiagonal(v1
,v2
, &ret_p1
, &ret_p2
, &generatedLine
, ret_polygons
);
746 newSampledLines
= generatedLine
->insert(newSampledLines
);
748 ret_polygons = ret_polygons->cutoffPolygon(root1);
750 ret_polygons = ret_polygons->cutoffPolygon(root2);
751 ret_polygons = ret_p1->insertPolygon(ret_polygons);
752 root1->rootLinkSet(ret_p1);
753 root2->rootLinkSet(ret_p1);
754 ret_p1->rootLinkSet(NULL);
755 ret_p2->rootLinkSet(ret_p1);
757 ret_polygons
= ret_polygons
->cutoffPolygon(root2
);
761 root2
->rootLinkSet(root1
);
762 ret_p1
->rootLinkSet(root1
);
763 ret_p2
->rootLinkSet(root1
);
765 /*now that we have connected the diagonal v1 and v2,
766 *we have to check those unprocessed diagonals which
767 *have v1 or v2 as an end point. Notice that the head of v1
768 *has the same coodinates as the head of v2->prev, and the head of
769 *v2 has the same coordinate as the head of v1->prev.
770 *Suppose these is a diagonal (v1, x). If (v1,x) is still a valid
771 *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
772 *replaced by (v2->prev, x), that is, x is on the left of
773 * v2->prev->prev->head, v2->prev->head, v2->prev->tail.
776 for(ii
=0, kk
=0; ii
<num_diagonals
; ii
++, kk
+=2)
777 if( removedDiagonals
[ii
]==0)
779 directedLine
* d1
=diagonal_vertices
[kk
];
780 directedLine
* d2
=diagonal_vertices
[kk
+1];
781 /*check d1, and replace diagonal_vertices[kk] if necessary*/
783 /*check if d2 is to left of v1->prev->head:v1->head:v1->tail*/
784 if(! pointLeft2Lines(v1
->getPrev()->head(),
785 v1
->head(), v1
->tail(), d2
->head()))
788 assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
789 v2->getPrev()->head(),
790 v2->getPrev()->tail(), d2->head()));
792 diagonal_vertices
[kk
] = v2
->getPrev();
796 /*check if d2 is to left of v2->prev->head:v2->head:v2->tail*/
797 if(! pointLeft2Lines(v2
->getPrev()->head(),
798 v2
->head(), v2
->tail(), d2
->head()))
801 assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
802 v1->getPrev()->head(),
803 v1->getPrev()->tail(), d2->head()));
805 diagonal_vertices
[kk
] = v1
->getPrev();
808 /*check d2 and replace diagonal_vertices[k+1] if necessary*/
810 /*check if d1 is to left of v1->prev->head:v1->head:v1->tail*/
811 if(! pointLeft2Lines(v1
->getPrev()->head(),
812 v1
->head(), v1
->tail(), d1
->head()))
814 /* assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
815 v2->getPrev()->head(),
816 v2->getPrev()->tail(), d1->head()));
818 diagonal_vertices
[kk
+1] = v2
->getPrev();
822 /*check if d1 is to left of v2->prev->head:v2->head:v2->tail*/
823 if(! pointLeft2Lines(v2
->getPrev()->head(),
824 v2
->head(), v2
->tail(), d1
->head()))
826 /* assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
827 v1->getPrev()->head(),
828 v1->getPrev()->tail(), d1->head()));
830 diagonal_vertices
[kk
+1] = v1
->getPrev();
834 }/*end if (root1 not equal to root 2)*/
837 /*second pass, now all diagoals should belong to the same polygon*/
838 //printf("second pass: \n");
840 // for(i=0; i<num_diagonals; i++)
841 // printf("%i ", removedDiagonals[i]);
844 for(i
=0,k
=0; i
<num_diagonals
; i
++, k
+= 2)
845 if(removedDiagonals
[i
] == 0)
849 directedLine
* v1
=diagonal_vertices
[k
];
850 directedLine
* v2
=diagonal_vertices
[k
+1];
854 directedLine
* ret_p1
;
855 directedLine
* ret_p2
;
857 /*we ahve to determine whether v1 and v2 belong to the same polygon before
858 *their structure are modified by connectDiagonal().
860 directedLine
*root1
= v1
->findRoot();
862 directedLine *root2 = v2->findRoot();
868 assert(root1 == root2);
870 sampledLine
* generatedLine
;
874 v1
->connectDiagonal(v1
,v2
, &ret_p1
, &ret_p2
, &generatedLine
, ret_polygons
);
875 newSampledLines
= generatedLine
->insert(newSampledLines
);
877 ret_polygons
= ret_polygons
->cutoffPolygon(root1
);
879 ret_polygons
= ret_p1
->insertPolygon(ret_polygons
);
881 ret_polygons
= ret_p2
->insertPolygon(ret_polygons
);
885 for(Int j
=i
+1; j
<num_diagonals
; j
++)
887 if(removedDiagonals
[j
] ==0)
890 directedLine
* temp1
=diagonal_vertices
[2*j
];
891 directedLine
* temp2
=diagonal_vertices
[2*j
+1];
892 if(temp1
==v1
|| temp1
==v2
|| temp2
==v1
|| temp2
==v2
)
893 if(! temp1
->samePolygon(temp1
, temp2
))
895 /*if temp1 and temp2 are in different polygons,
896 *then one of them must be v1 or v2.
901 assert(temp1
==v1
|| temp1
== v2
|| temp2
==v1
|| temp2
==v2
);
904 diagonal_vertices
[2*j
] = v2
->getPrev();
908 diagonal_vertices
[2*j
+1] = v2
->getPrev();
912 diagonal_vertices
[2*j
] = v1
->getPrev();
916 diagonal_vertices
[2*j
+1] = v1
->getPrev();
926 loopList
->deleteLoopList();
929 free(diagonal_vertices
);
930 free(removedDiagonals
);
932 *retSampledLines
= newSampledLines
;