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)
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;
75 Real* vert = v->head();
76 if( compVertInY(e->head(), vert) != 1
77 && compVertInY(e->tail(), vert) != 1
91 Real* vert = v->head();
92 if( compVertInY(e->head(), vert) != -1
93 && compVertInY(e->tail(), vert) != -1
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()))
124 Real* A = v->getPrev()->head();
133 if(Bx*Cy - Cx*By < 0)
return 1;
144 if(! isCusp(v))
return 0;
145 else if(isReflex(v))
return 1;
156 ret->leftType = leftType;
158 ret->rightType = rightType;
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)
190 if(src1->left == src2->left ||
191 src1->left == src2->left->getPrev()
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)
216 if(src1->right == src2->right ||
217 src1->right == src2->right->getPrev()
224 return (leftEqual == 1 || rightEqual == 1);
230 inline 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));
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]) {
307 Real Ymax = min(e1_Ymax, e2_Ymax);
308 Real Ymin = max(e1_Ymin, e2_Ymin);
310 Real y = Real(0.5)*(Ymax + Ymin);
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;
330 return v1->compInY(v2);
339 for(i=0; i<total_num_edges; i++)
349 if(isBelow(vert, thisEdge) && isBelow(vert, prevEdge) && compEdges(prevEdge, thisEdge)<0)
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)
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];
392 for(i=0,k=0; i<num_diagonals; i++, k+=2)
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];
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);
434 sweepY(total_num_edges, array, ranges);
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;
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);
459 sweepY(total_num_edges, array, ranges);
474 assert(diagonal_vertices);
478 findDiagonals(total_num_edges, array, ranges, num_diagonals, diagonal_vertices);
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)
523 removedDiagonals[i] = 1;
528 v1->connectDiagonal(v1,v2, &ret_p1, &ret_p2, &generatedLine, ret_polygons);
532 newSampledLines = generatedLine->insert(newSampledLines);
543 ret_polygons = ret_polygons->cutoffPolygon(root2);
547 root2->rootLinkSet(root1);
548 ret_p1->rootLinkSet(root1);
549 ret_p2->rootLinkSet(root1);
562 for(ii=0, kk=0; ii<num_diagonals; ii++, kk+=2)
563 if( removedDiagonals[ii]==0)
570 if(! pointLeft2Lines(v1->getPrev()->head(),
571 v1->head(), v1->tail(), d2->head()))
578 diagonal_vertices[kk] = v2->getPrev();
583 if(! pointLeft2Lines(v2->getPrev()->head(),
584 v2->head(), v2->tail(), d2->head()))
591 diagonal_vertices[kk] = v1->getPrev();
597 if(! pointLeft2Lines(v1->getPrev()->head(),
598 v1->head(), v1->tail(), d1->head()))
604 diagonal_vertices[kk+1] = v2->getPrev();
609 if(! pointLeft2Lines(v2->getPrev()->head(),
610 v2->head(), v2->tail(), d1->head()))
616 diagonal_vertices[kk+1] = v1->getPrev();
627 for(i=0,k=0; i<num_diagonals; i++, k += 2)
628 if(removedDiagonals[i] == 0)
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)
675 if(temp1==v1 || temp1==v2 || temp2==v1 || temp2==v2)
676 if(! temp1->samePolygon(temp1, temp2))
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;
729 for(i=0; i<nVertices;i++)
737 if(isBelow(vert, thisEdge) && isAbove(vert, prevEdge))
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))
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))
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)
787 treeNode* leftEdge = TreeNodePredecessor(thisNode);
788 treeNode* rightEdge = TreeNodeSuccessor(prevNode);
789 ret_ranges[i] = sweepRangeMake( (
directedLine*) leftEdge->key, 1,
796 ret_ranges[i] = sweepRangeMake( prevEdge, 1, thisEdge, 1);
799 else if(isBelow(vert, thisEdge) && isBelow(vert, prevEdge))
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)
807 treeNode* leftEdge = TreeNodePredecessor(prevNode);
808 treeNode* rightEdge = TreeNodeSuccessor(thisNode);
809 ret_ranges[i] = sweepRangeMake( (
directedLine*) leftEdge->key, 1,
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();
835 TreeNodeDeleteWholeTree(searchTree);