46 #pragma warning 391 10
47 #pragma warning 726 10
50 static Real area(Real A[2], Real B[2], Real C[2])
63 if(area(poly->head(), poly->tail(), poly->getNext()->tail()) < 0.00000)
65 for(temp = poly->getNext(); temp != poly; temp = temp->getNext())
67 if(area(temp->head(), temp->tail(), temp->getNext()->tail()) < 0.00000)
79 cur_sign = compV2InX(poly->tail(), poly->head());
81 n_changes = (compV2InX(poly->getPrev()->tail(), poly->getPrev()->head())
84 for(temp = poly->getNext(); temp != poly; temp = temp->getNext())
87 cur_sign = compV2InX(temp->tail(), temp->head());
89 if(cur_sign != prev_sign)
93 if(n_changes ==2)
return 1;
107 if( fabs(poly->head()[0] - poly->tail()[0]) <= fabs(poly->head()[1]-poly->tail()[1]))
108 V_count += poly->get_npoints();
110 U_count += poly->get_npoints();
115 for(temp = poly->getNext(); temp != poly; temp = temp->getNext())
117 if( fabs(temp->head()[0] - temp->tail()[0]) <= fabs(temp->head()[1]-temp->tail()[1]))
118 V_count += temp->get_npoints();
120 U_count += temp->get_npoints();
129 if(U_count > V_count)
return 1;
140 if(l1->getNext() == l2)
142 if(area(l1->head(), l1->tail(), l2->tail()) == 0)
144 if( (l1->tail()[0] - l1->head()[0])*(l2->tail()[0]-l2->head()[0]) +
145 (l1->tail()[1] - l1->head()[1])*(l2->tail()[1]-l2->head()[1]) >=0)
152 else if(l1->getPrev() == l2)
154 if(area(l2->head(), l2->tail(), l1->tail()) == 0)
156 if( (l2->tail()[0] - l2->head()[0])*(l1->tail()[0]-l1->head()[0]) +
157 (l2->tail()[1] - l2->head()[1])*(l1->tail()[1]-l1->head()[1]) >=0)
166 if((l1->head()[0] == l2->head()[0] &&
167 l1->head()[1] == l2->head()[1]) ||
168 (l1->tail()[0] == l2->tail()[0] &&
169 l1->tail()[1] == l2->tail()[1]))
177 area(l1->head(), l1->tail(), l2->head())
179 area(l1->head(), l1->tail(), l2->tail())
184 area(l2->head(), l2->tail(), l1->head())
185 *area(l2->head(), l2->tail(), l1->tail())
198 Int DBG_edgesIntersectGen(Real A[2], Real B[2], Real C[2], Real D[2])
202 area(A, B, C) * area(A,B,D) <0
206 area(C,D,A) * area(C,D,B) < 0
216 Int DBG_intersectChain(
vertexArray* chain, Int start, Int end, Real A[2], Real B[2])
219 for(i=start; i<=end-2; i++)
220 if(DBG_edgesIntersectGen(chain->getVertex(i), chain->getVertex(i+1), A, B))
235 for(temp2=temp1->getNext(); temp2 != temp1; temp2=temp2->getNext())
237 if(DBG_edgesIntersect(temp1, temp2))
244 for(temp1=poly->getNext(); temp1 != poly; temp1 = temp1->getNext())
245 for(temp2=temp1->getNext(); temp2 != temp1; temp2=temp2->getNext())
247 if(DBG_edgesIntersect(temp1, temp2))
260 if(DBG_edgesIntersect(edge, poly))
262 for(temp=poly->getNext(); temp != poly; temp=temp->getNext())
263 if(DBG_edgesIntersect(edge, temp))
273 if(DBG_edgeIntersectPoly(p1, p2))
275 for(temp=p1->getNext(); temp!= p1; temp = temp->getNext())
276 if(DBG_edgeIntersectPoly(temp, p2))
287 for(temp=pList; temp != NULL; temp = temp->getNextPolygon())
288 if(DBG_polygonSelfIntersect(temp))
291 for(temp=pList; temp!=NULL; temp=temp->getNextPolygon())
293 for(temp2=temp->getNextPolygon(); temp2 != NULL; temp2=temp2->getNextPolygon())
294 if(DBG_polygonsIntersect(temp, temp2))
304 return (poly->polyArea() > 0);
321 Int DBG_rayIntersectEdge(Real v0[2], Real dx, Real dy, Real v10[2], Real v1[2], Real v2[2])
330 Real denom = (v2[0]-v1[0])*(-dy) - (v2[1]-v1[1]) * (-dx);
331 Real nomRay = (v2[0]-v1[0]) * (v0[1] - v1[1]) - (v2[1]-v1[1])*(v0[0]-v1[0]);
332 Real nomEdge = (v0[0]-v1[0]) * (-dy) - (v0[1]-v1[1])*(-dx);
353 if(dx*(v1[0]-v0[0])>=0 && dy*(v1[1]-v0[1])>=0)
355 if(area(v0, v1, v10) * area(v0, v1, v2) >0)
365 if(nomEdge == denom) {
371 if(denom*nomRay>0 && denom*nomEdge>0 && nomEdge/denom <=1.0)
378 Int DBG_rayIntersectPoly(Real v0[2], Real dx, Real dy,
directedLine* poly)
382 if(DBG_rayIntersectEdge(v0, dx, dy, poly->getPrev()->head(), poly->head(), poly->tail()))
385 for(temp=poly->getNext(); temp != poly; temp = temp->getNext())
386 if(DBG_rayIntersectEdge(v0, dx, dy, temp->getPrev()->head(), temp->head(), temp->tail()))
400 assert( (DBG_rayIntersectPoly(v,1,0,poly) % 2 )
401 == (DBG_rayIntersectPoly(v,1,Real(0.1234), poly) % 2 )
403 if(DBG_rayIntersectPoly(v, 1, 0, poly) % 2 == 1)
425 for(temp = list; temp != NULL; temp = temp->getNextPolygon())
428 if(DBG_pointInsidePoly(poly->head(), temp))
437 if(poly->getDirection() == INCREASING)
438 poly->putDirection(DECREASING);
440 poly->putDirection(INCREASING);
443 poly->putNext(poly->getPrev());
444 poly->putPrev(oldNext);
447 for(temp=oldNext; temp!=poly; temp = oldNext)
449 if(temp->getDirection() == INCREASING)
450 temp->putDirection(DECREASING);
452 temp->putDirection(INCREASING);
454 oldNext = temp->getNext();
455 temp->putNext(temp->getPrev());
456 temp->putPrev(oldNext);
458 printf(
"reverse done\n");
463 if(polygon == NULL)
return 1;
465 if(polygon->head()[0] != polygon->getPrev()->tail()[0] ||
466 polygon->head()[1] != polygon->getPrev()->tail()[1])
468 for(temp=polygon->getNext(); temp != polygon; temp=temp->getNext())
470 if(temp->head()[0] != temp->getPrev()->tail()[0] ||
471 temp->head()[1] != temp->getPrev()->tail()[1])
485 if(polyList == NULL)
return 0;
489 if(DBG_polygonListIntersect(polyList))
491 fprintf(stderr,
"DBG_check: there are self intersections, don't know to modify the polygons\n");
496 for(temp = polyList; temp!= NULL; temp = temp ->getNextPolygon())
498 if(! DBG_checkConnectivity(temp))
500 fprintf(stderr,
"DBG_check, polygon not connected\n");
506 for(temp = polyList; temp!= NULL; temp = temp ->getNextPolygon())
512 if( DBG_enclosingPolygons(temp, polyList) % 2 == 0)
517 Int actualDir = DBG_isCounterclockwise(temp);
519 if(correctDir != actualDir)
521 fprintf(stderr,
"DBG_check: polygon with incorrect orientations. reversed\n");
536 for(temp=begin; temp != end; temp = temp->getNext())
538 if(DBG_edgesIntersect(e, temp))
541 if(DBG_edgesIntersect(e, end))
555 while( (next = end->getNext()) != begin)
558 if( (interc = DBG_edgeIntersectChainD(next, begin, end)))
561 if(DBG_edgesIntersect(next, interc->getNext()))
567 buf[0] = interc->tail()[0];
568 buf[1] = interc->tail()[1];
572 Real r = ((Real)i) / ((Real) n);
573 Real u = (1-r) * interc->head()[0] + r * interc->tail()[0];
574 Real v = (1-r) * interc->head()[1] + r * interc->tail()[1];
575 interc->tail()[0] = interc->getNext()->head()[0] = u;
576 interc->tail()[1] = interc->getNext()->head()[1] = v;
577 if( (! DBG_edgesIntersect(next, interc)) &&
578 (! DBG_edgesIntersect(next, interc->getNext())))
585 interc->tail()[0] = interc->getNext()->head()[0] = buf[0];
586 interc->tail()[1] = interc->getNext()->head()[1] = buf[1];
596 begin->deleteSingleLine(next);
600 if(DBG_polygonSelfIntersect(begin))
603 begin->deleteSingleLine(end);
610 end = end->getNext();
615 end = end->getNext();
637 if(crt->getPrev()->getPrev() == crt)
640 if(DBG_edgesIntersect(crt, crt->getNext()) ||
641 (crt->head()[0] == crt->getNext()->tail()[0] &&
642 crt->head()[1] == crt->getNext()->tail()[1])
646 crt=crt->deleteChain(crt, crt->getNext());
652 end = crt->getNext();
655 for(temp=end->getNext(); temp!=begin; temp= temp->getNext())
658 directedLine *intersect = DBG_edgeIntersectChainD(temp, begin, end);
659 if(intersect != NULL)
661 crt = crt->deleteChain(intersect, temp);
685 for(temp=list; temp != NULL; temp = tempNext)
688 tempNext = temp->getNextPolygon();
690 left = DBG_cutIntersectionPoly(temp, cutOccur);
692 ret=left->insertPolygon(ret);
705 if(polygonList == NULL)
708 DBG_collectSampledLinesPoly(polygonList, cHead, cTail);
712 for(temp = polygonList->getNextPolygon(); temp != NULL; temp = temp->getNextPolygon())
714 DBG_collectSampledLinesPoly(temp, tempHead, tempTail);
715 cTail->insert(tempHead);
729 retHead = retTail = polygon->getSampledLine();
730 for(temp = polygon->getNext(); temp != polygon; temp=temp->getNext())
732 retHead = temp->getSampledLine()->insert(retHead);