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.
37
** $Header: //depot/main/gfx/lib/glu/libnurbs/nurbtess/polyDBG.cc#3 $
47
static Real area(Real A[2], Real B[2], Real C[2])
57
Int DBG_isConvex(directedLine *poly)
60
if(area(poly->head(), poly->tail(), poly->getNext()->tail()) < 0.00000)
62
for(temp = poly->getNext(); temp != poly; temp = temp->getNext())
64
if(area(temp->head(), temp->tail(), temp->getNext()->tail()) < 0.00000)
70
Int DBG_is_U_monotone(directedLine* poly)
76
cur_sign = compV2InX(poly->tail(), poly->head());
78
n_changes = (compV2InX(poly->getPrev()->tail(), poly->getPrev()->head())
81
for(temp = poly->getNext(); temp != poly; temp = temp->getNext())
84
cur_sign = compV2InX(temp->tail(), temp->head());
86
if(cur_sign != prev_sign)
90
if(n_changes ==2) return 1;
94
/*if u-monotone, and there is a long horizontal edge*/
95
Int DBG_is_U_direction(directedLine* poly)
98
if(! DBG_is_U_monotone(poly))
104
if( fabs(poly->head()[0] - poly->tail()[0]) <= fabs(poly->head()[1]-poly->tail()[1]))
105
V_count += poly->get_npoints();
107
U_count += poly->get_npoints();
109
else if(poly->head()[1] == poly->tail()[1])
110
U_count += poly->get_npoints();
112
for(temp = poly->getNext(); temp != poly; temp = temp->getNext())
114
if( fabs(temp->head()[0] - temp->tail()[0]) <= fabs(temp->head()[1]-temp->tail()[1]))
115
V_count += temp->get_npoints();
117
U_count += temp->get_npoints();
119
if(temp->head()[0] == temp->tail()[0])
120
V_count += temp->get_npoints();
121
else if(temp->head()[1] == temp->tail()[1])
122
U_count += temp->get_npoints();
126
if(U_count > V_count) return 1;
130
/*given two line segments, determine whether
131
*they intersect each other or not.
132
*return 1 if they do,
135
Int DBG_edgesIntersect(directedLine* l1, directedLine* l2)
137
if(l1->getNext() == l2)
139
if(area(l1->head(), l1->tail(), l2->tail()) == 0) //colinear
141
if( (l1->tail()[0] - l1->head()[0])*(l2->tail()[0]-l2->head()[0]) +
142
(l1->tail()[1] - l1->head()[1])*(l2->tail()[1]-l2->head()[1]) >=0)
143
return 0; //not intersect
147
//else we use the normal code
149
else if(l1->getPrev() == l2)
151
if(area(l2->head(), l2->tail(), l1->tail()) == 0) //colinear
153
if( (l2->tail()[0] - l2->head()[0])*(l1->tail()[0]-l1->head()[0]) +
154
(l2->tail()[1] - l2->head()[1])*(l1->tail()[1]-l1->head()[1]) >=0)
155
return 0; //not intersect
159
//else we use the normal code
161
else //the two edges are not connected
163
if((l1->head()[0] == l2->head()[0] &&
164
l1->head()[1] == l2->head()[1]) ||
165
(l1->tail()[0] == l2->tail()[0] &&
166
l1->tail()[1] == l2->tail()[1]))
174
area(l1->head(), l1->tail(), l2->head())
176
area(l1->head(), l1->tail(), l2->tail())
181
area(l2->head(), l2->tail(), l1->head())
182
*area(l2->head(), l2->tail(), l1->tail())
191
/*whether AB and CD intersect
195
Int DBG_edgesIntersectGen(Real A[2], Real B[2], Real C[2], Real D[2])
199
area(A, B, C) * area(A,B,D) <0
203
area(C,D,A) * area(C,D,B) < 0
211
/*determien whether (A,B) interesect chain[start] to [end]
213
Int DBG_intersectChain(vertexArray* chain, Int start, Int end, Real A[2], Real B[2])
216
for(i=start; i<=end-2; i++)
217
if(DBG_edgesIntersectGen(chain->getVertex(i), chain->getVertex(i+1), A, B))
223
/*determine whether a polygon intersect itself or not
224
*return 1 is it does,
227
Int DBG_polygonSelfIntersect(directedLine* poly)
232
for(temp2=temp1->getNext(); temp2 != temp1; temp2=temp2->getNext())
234
if(DBG_edgesIntersect(temp1, temp2))
241
for(temp1=poly->getNext(); temp1 != poly; temp1 = temp1->getNext())
242
for(temp2=temp1->getNext(); temp2 != temp1; temp2=temp2->getNext())
244
if(DBG_edgesIntersect(temp1, temp2))
252
/*check whether a line segment intersects a polygon
254
Int DBG_edgeIntersectPoly(directedLine* edge, directedLine* poly)
257
if(DBG_edgesIntersect(edge, poly))
259
for(temp=poly->getNext(); temp != poly; temp=temp->getNext())
260
if(DBG_edgesIntersect(edge, temp))
265
/*check whether two polygons intersect
267
Int DBG_polygonsIntersect(directedLine* p1, directedLine* p2)
270
if(DBG_edgeIntersectPoly(p1, p2))
272
for(temp=p1->getNext(); temp!= p1; temp = temp->getNext())
273
if(DBG_edgeIntersectPoly(temp, p2))
278
/*check whether there are polygons intersecting each other in
281
Int DBG_polygonListIntersect(directedLine* pList)
284
for(temp=pList; temp != NULL; temp = temp->getNextPolygon())
285
if(DBG_polygonSelfIntersect(temp))
288
for(temp=pList; temp!=NULL; temp=temp->getNextPolygon())
290
for(temp2=temp->getNextPolygon(); temp2 != NULL; temp2=temp2->getNextPolygon())
291
if(DBG_polygonsIntersect(temp, temp2))
299
Int DBG_isCounterclockwise(directedLine* poly)
301
return (poly->polyArea() > 0);
304
/*ray: v0 with direction (dx,dy).
306
* the extra point v10[2] is given for the information at
307
*v1. Basically this edge is connectd to edge
308
* v10-v1. If v1 is on the ray,
309
* then we need v10 to determine whether this ray intersects
310
* the edge or not (that is, return 1 or return 0).
311
* If v1 is on the ray, then if v2 and v10 are on the same side of the ray,
312
* we return 0, otherwise return 1.
313
*For v2, if v2 is on the ray, we always return 0.
314
*Notice that v1 and v2 are not symmetric. So the edge is directed!!!
315
* The purpose for this convention is such that: a point is inside a polygon
316
* if and only if it intersets with odd number of edges.
318
Int DBG_rayIntersectEdge(Real v0[2], Real dx, Real dy, Real v10[2], Real v1[2], Real v2[2])
321
if( (v1[1] >= v0[1] && v2[1]<= v0[1] )
322
||(v2[1] >= v0[1] && v1[1]<= v0[1] )
324
printf("rayIntersectEdge, *********\n");
327
Real denom = (v2[0]-v1[0])*(-dy) - (v2[1]-v1[1]) * (-dx);
328
Real nomRay = (v2[0]-v1[0]) * (v0[1] - v1[1]) - (v2[1]-v1[1])*(v0[0]-v1[0]);
329
Real nomEdge = (v0[0]-v1[0]) * (-dy) - (v0[1]-v1[1])*(-dx);
332
/*if the ray is parallel to the edge, return 0: not intersect*/
336
/*if v0 is on the edge, return 0: not intersect*/
340
/*if v1 is on the positive ray, and the neighbor of v1 crosses the ray
344
{ /*v1 is on the positive or negative ray*/
347
printf("v1 is on the ray\n");
350
if(dx*(v1[0]-v0[0])>=0 && dy*(v1[1]-v0[1])>=0) /*v1 on positive ray*/
352
if(area(v0, v1, v10) * area(v0, v1, v2) >0)
357
else /*v1 on negative ray*/
361
/*if v2 is on the ray, always return 0: not intersect*/
362
if(nomEdge == denom) {
363
/* printf("v2 is on the ray\n");*/
368
if(denom*nomRay>0 && denom*nomEdge>0 && nomEdge/denom <=1.0)
374
/*return the number of intersections*/
375
Int DBG_rayIntersectPoly(Real v0[2], Real dx, Real dy, directedLine* poly)
379
if(DBG_rayIntersectEdge(v0, dx, dy, poly->getPrev()->head(), poly->head(), poly->tail()))
382
for(temp=poly->getNext(); temp != poly; temp = temp->getNext())
383
if(DBG_rayIntersectEdge(v0, dx, dy, temp->getPrev()->head(), temp->head(), temp->tail()))
385
/*printf("ray intersect poly: count=%i\n", count);*/
389
Int DBG_pointInsidePoly(Real v[2], directedLine* poly)
392
printf("enter pointInsidePoly , v=(%f,%f)\n", v[0], v[1]);
393
printf("the polygon is\n");
396
/*for debug purpose*/
397
assert( (DBG_rayIntersectPoly(v,1,0,poly) % 2 )
398
== (DBG_rayIntersectPoly(v,1,0.1234, poly) % 2 )
400
if(DBG_rayIntersectPoly(v, 1, 0, poly) % 2 == 1)
406
/*return the number of polygons which contain thie polygon
409
Int DBG_enclosingPolygons(directedLine* poly, directedLine* list)
414
printf("%i\n", DBG_pointInsidePoly(poly->head(),
415
list->getNextPolygon()
422
for(temp = list; temp != NULL; temp = temp->getNextPolygon())
425
if(DBG_pointInsidePoly(poly->head(), temp))
427
/* printf("count=%i\n", count);*/
432
void DBG_reverse(directedLine* poly)
434
if(poly->getDirection() == INCREASING)
435
poly->putDirection(DECREASING);
437
poly->putDirection(INCREASING);
439
directedLine* oldNext = poly->getNext();
440
poly->putNext(poly->getPrev());
441
poly->putPrev(oldNext);
444
for(temp=oldNext; temp!=poly; temp = oldNext)
446
if(temp->getDirection() == INCREASING)
447
temp->putDirection(DECREASING);
449
temp->putDirection(INCREASING);
451
oldNext = temp->getNext();
452
temp->putNext(temp->getPrev());
453
temp->putPrev(oldNext);
455
printf("reverse done\n");
458
Int DBG_checkConnectivity(directedLine *polygon)
460
if(polygon == NULL) return 1;
462
if(polygon->head()[0] != polygon->getPrev()->tail()[0] ||
463
polygon->head()[1] != polygon->getPrev()->tail()[1])
465
for(temp=polygon->getNext(); temp != polygon; temp=temp->getNext())
467
if(temp->head()[0] != temp->getPrev()->tail()[0] ||
468
temp->head()[1] != temp->getPrev()->tail()[1])
474
/*print out error message.
475
*If it cannot modify the polygon list to make it satify the
476
*requirements, return 1.
477
*otherwise modify the polygon list, and return 0
479
Int DBG_check(directedLine *polyList)
482
if(polyList == NULL) return 0;
484
/*if there are intersections, print out error message
486
if(DBG_polygonListIntersect(polyList))
488
fprintf(stderr, "DBG_check: there are self intersections, don't know to modify the polygons\n");
492
/*check the connectivity of each polygon*/
493
for(temp = polyList; temp!= NULL; temp = temp ->getNextPolygon())
495
if(! DBG_checkConnectivity(temp))
497
fprintf(stderr, "DBG_check, polygon not connected\n");
502
/*check the orientation of each polygon*/
503
for(temp = polyList; temp!= NULL; temp = temp ->getNextPolygon())
509
if( DBG_enclosingPolygons(temp, polyList) % 2 == 0)
510
correctDir = 1; /*counterclockwise*/
512
correctDir = 0; /*clockwise*/
514
Int actualDir = DBG_isCounterclockwise(temp);
516
if(correctDir != actualDir)
518
fprintf(stderr, "DBG_check: polygon with incorrect orientations. reversed\n");
527
/**************handle self intersections*****************/
528
//determine whether e interects [begin, end] or not
529
static directedLine* DBG_edgeIntersectChainD(directedLine *e,
530
directedLine *begin, directedLine *end)
533
for(temp=begin; temp != end; temp = temp->getNext())
535
if(DBG_edgesIntersect(e, temp))
538
if(DBG_edgesIntersect(e, end))
543
//given a polygon, cut the edges off and finally obtain a
544
//a polygon without intersections. The cut-off edges are
545
//dealloated. The new polygon is returned.
546
directedLine* DBG_cutIntersectionPoly(directedLine *polygon, int& cutOccur)
548
directedLine *begin, *end, *next;
552
while( (next = end->getNext()) != begin)
554
directedLine *interc = NULL;
555
if( (interc = DBG_edgeIntersectChainD(next, begin, end)))
558
if(DBG_edgesIntersect(next, interc->getNext()))
564
buf[0] = interc->tail()[0];
565
buf[1] = interc->tail()[1];
569
Real r = ((Real)i) / ((Real) n);
570
Real u = (1-r) * interc->head()[0] + r * interc->tail()[0];
571
Real v = (1-r) * interc->head()[1] + r * interc->tail()[1];
572
interc->tail()[0] = interc->getNext()->head()[0] = u;
573
interc->tail()[1] = interc->getNext()->head()[1] = v;
574
if( (! DBG_edgesIntersect(next, interc)) &&
575
(! DBG_edgesIntersect(next, interc->getNext())))
578
if(i==n) // we didn't fix it
582
interc->tail()[0] = interc->getNext()->head()[0] = buf[0];
583
interc->tail()[1] = interc->getNext()->head()[1] = buf[1];
593
begin->deleteSingleLine(next);
597
if(DBG_polygonSelfIntersect(begin))
599
directedLine* newEnd = end->getPrev();
600
begin->deleteSingleLine(end);
607
end = end->getNext();
612
end = end->getNext();
618
//given a polygon, cut the edges off and finally obtain a
619
//a polygon without intersections. The cut-off edges are
620
//dealloated. The new polygon is returned.
621
static directedLine* DBG_cutIntersectionPoly_notwork(directedLine *polygon)
623
directedLine *crt;//current polygon
632
//if there are less than 3 edges, we should stop
633
if(crt->getPrev()->getPrev() == crt)
636
if(DBG_edgesIntersect(crt, crt->getNext()) ||
637
(crt->head()[0] == crt->getNext()->tail()[0] &&
638
crt->head()[1] == crt->getNext()->tail()[1])
642
crt=crt->deleteChain(crt, crt->getNext());
646
//now we know crt and crt->getNext do not intersect
648
end = crt->getNext();
649
//printf("begin=(%f,%f)\n", begin->head()[0], begin->head()[1]);
650
//printf("end=(%f,%f)\n", end->head()[0], end->head()[1]);
651
for(temp=end->getNext(); temp!=begin; temp= temp->getNext())
653
//printf("temp=(%f,%f)\n", temp->head()[0], temp->head()[1]);
654
directedLine *intersect = DBG_edgeIntersectChainD(temp, begin, end);
655
if(intersect != NULL)
657
crt = crt->deleteChain(intersect, temp);
659
break; //the for loop
670
find = 0; //go to next loop
674
directedLine* DBG_cutIntersectionAllPoly(directedLine* list)
677
directedLine* tempNext=NULL;
678
directedLine* ret = NULL;
680
for(temp=list; temp != NULL; temp = tempNext)
683
tempNext = temp->getNextPolygon();
685
left = DBG_cutIntersectionPoly(temp, cutOccur);
687
ret=left->insertPolygon(ret);
692
sampledLine* DBG_collectSampledLinesAllPoly(directedLine *polygonList)
695
sampledLine* tempHead = NULL;
696
sampledLine* tempTail = NULL;
697
sampledLine* cHead = NULL;
698
sampledLine* cTail = NULL;
700
if(polygonList == NULL)
703
DBG_collectSampledLinesPoly(polygonList, cHead, cTail);
707
for(temp = polygonList->getNextPolygon(); temp != NULL; temp = temp->getNextPolygon())
709
DBG_collectSampledLinesPoly(temp, tempHead, tempTail);
710
cTail->insert(tempHead);
716
void DBG_collectSampledLinesPoly(directedLine *polygon, sampledLine*& retHead, sampledLine*& retTail)
719
sampledLine *ret = NULL;
725
retHead = retTail = polygon->getSampledLine();
726
for(temp = polygon->getNext(); temp != polygon; temp=temp->getNext())
728
retHead = temp->getSampledLine()->insert(retHead);