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.
41 #include "glimports.h"
44 #include "quicksort.h"
45 #include "directedLine.h"
49 #pragma warning 726 10
52 //we must return the newLine
53 directedLine
* directedLine::deleteChain(directedLine
* begin
, directedLine
* end
)
55 if(begin
->head()[0] == end
->tail()[0] &&
56 begin
->head()[1] == end
->tail()[1]
59 directedLine
*ret
= begin
->prev
;
60 begin
->prev
->next
= end
->next
;
61 end
->next
->prev
= begin
->prev
;
70 directedLine
* newLine
;
71 sampledLine
* sline
= new sampledLine(begin
->head(), end
->tail());
72 newLine
= new directedLine(INCREASING
, sline
);
73 directedLine
*p
= begin
->prev
;
74 directedLine
*n
= end
->next
;
88 void directedLine::deleteSingleLine(directedLine
* dline
)
90 //make sure that dline->prev->tail is the same as
91 //dline->next->head. This is for numerical erros.
92 //for example, if we delete a line which is almost degeneate
93 //within (epsilon), then we want to make that the polygon after deletion
94 //is still a valid polygon
96 dline
->next
->head()[0] = dline
->prev
->tail()[0];
97 dline
->next
->head()[1] = dline
->prev
->tail()[1];
99 dline
->prev
->next
= dline
->next
;
100 dline
->next
->prev
= dline
->prev
;
106 static Int
myequal(Real a
[2], Real b
[2])
109 if(a[0]==b[0] && a[1] == b[1])
116 if(fabs(a
[0]-b
[0]) < 0.00001 &&
117 fabs(a
[1]-b
[1]) < 0.00001)
124 directedLine
* directedLine::deleteDegenerateLines()
126 //if there is only one edge or two edges, don't do anything
127 if(this->next
== this)
129 if(this->next
== this->prev
)
132 //find a nondegenerate line
134 directedLine
* first
= NULL
;
135 if(! myequal(head(), tail()))
137 if(head()[0] != tail()[0] ||
138 head()[1] != tail()[1])
143 for(temp
= this->next
; temp
!= this; temp
= temp
->next
)
146 if(temp->head()[0] != temp->tail()[0] ||
147 temp->head()[1] != temp->tail()[1])
149 if(! myequal(temp
->head(), temp
->tail()))
158 //if there are no non-degenerate lines, then we simply return NULL.
161 deleteSinglePolygonWithSline();
165 directedLine
* tempNext
= NULL
;
166 for(temp
=first
->next
; temp
!= first
; temp
= tempNext
)
168 tempNext
= temp
->getNext();
170 if(temp->head()[0] == temp->tail()[0] &&
171 temp->head()[1] == temp->tail()[1])
174 if(myequal(temp
->head(), temp
->tail()))
175 deleteSingleLine(temp
);
180 directedLine
* directedLine::deleteDegenerateLinesAllPolygons()
183 directedLine
*tempNext
= NULL
;
184 directedLine
* ret
= NULL
;
185 directedLine
* retEnd
= NULL
;
186 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
188 tempNext
= temp
->nextPolygon
;
189 temp
->nextPolygon
= NULL
;
192 ret
= retEnd
= temp
->deleteDegenerateLines();
197 directedLine
*newPolygon
= temp
->deleteDegenerateLines();
198 if(newPolygon
!= NULL
)
200 retEnd
->nextPolygon
= temp
->deleteDegenerateLines();
201 retEnd
= retEnd
->nextPolygon
;
208 directedLine
* directedLine::cutIntersectionAllPoly(int &cutOccur
)
211 directedLine
*tempNext
= NULL
;
212 directedLine
* ret
= NULL
;
213 directedLine
* retEnd
= NULL
;
215 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
218 tempNext
= temp
->nextPolygon
;
219 temp
->nextPolygon
= NULL
;
223 ret
= retEnd
= DBG_cutIntersectionPoly(temp
, eachCutOccur
);
230 retEnd
->nextPolygon
= DBG_cutIntersectionPoly(temp
, eachCutOccur
);
231 retEnd
= retEnd
->nextPolygon
;
240 void directedLine::deleteSinglePolygonWithSline()
242 directedLine
*temp
, *tempNext
;
244 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
246 tempNext
= temp
->next
;
252 void directedLine::deletePolygonListWithSline()
254 directedLine
*temp
, *tempNext
;
255 for(temp
=this; temp
!= NULL
; temp
=tempNext
)
257 tempNext
= temp
->nextPolygon
;
258 temp
->deleteSinglePolygonWithSline();
262 void directedLine::deleteSinglePolygon()
264 directedLine
*temp
, *tempNext
;
266 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
268 tempNext
= temp
->next
;
273 void directedLine::deletePolygonList()
275 directedLine
*temp
, *tempNext
;
276 for(temp
=this; temp
!= NULL
; temp
=tempNext
)
278 tempNext
= temp
->nextPolygon
;
279 temp
->deleteSinglePolygon();
285 directedLine::directedLine(short dir
, sampledLine
* sl
)
292 // prevPolygon = NULL;
293 rootBit
= 0;/*important to initilzae to 0 meaning not root yet*/
299 void directedLine::init(short dir
, sampledLine
* sl
)
305 directedLine::directedLine()
310 rootBit
= 0;/*important to initilzae to 0 meaning not root yet*/
314 directedLine::~directedLine()
318 Real
* directedLine::head()
321 return (direction
==INCREASING
)? (sline
->get_points())[0] : (sline
->get_points())[sline
->get_npoints()-1];
324 /*inline*/ Real
* directedLine::getVertex(Int i
)
326 return (direction
==INCREASING
)? (sline
->get_points())[i
] : (sline
->get_points())[sline
->get_npoints() - 1 -i
];
329 Real
* directedLine::tail()
331 return (direction
==DECREASING
)? (sline
->get_points())[0] : (sline
->get_points())[sline
->get_npoints()-1];
334 /*insert a new line between prev and this*/
335 void directedLine::insert(directedLine
* nl
)
341 nl
->rootLink
= this; /*assuming that 'this' is the root!!!*/
344 Int
directedLine::numEdges()
348 if(next
== this) return 1;
351 for(temp
= next
; temp
!= this; temp
= temp
->next
)
356 Int
directedLine::numEdgesAllPolygons()
360 for(temp
=this; temp
!= NULL
; temp
=temp
->nextPolygon
)
362 ret
+= temp
->numEdges();
367 /*return 1 if the double linked list forms a polygon.
369 short directedLine::isPolygon()
373 /*a polygon contains at least 3 edges*/
374 if(numEdges() <=2) return 0;
377 if(! isConnected()) return 0;
379 /*check all other edges*/
380 for(temp
=next
; temp
!= this; temp
= temp
->next
){
381 if(!isConnected()) return 0;
386 /*check if the head of this edge is connected to
387 *the tail of the prev
389 short directedLine::isConnected()
391 if( (head()[0] == prev
->tail()[0]) && (head()[1] == prev
->tail()[1]))
397 Int
compV2InY(Real A
[2], Real B
[2])
399 if(A
[1] < B
[1]) return -1;
400 if(A
[1] == B
[1] && A
[0] < B
[0]) return -1;
401 if(A
[1] == B
[1] && A
[0] == B
[0]) return 0;
405 Int
compV2InX(Real A
[2], Real B
[2])
407 if(A
[0] < B
[0]) return -1;
408 if(A
[0] == B
[0] && A
[1] < B
[1]) return -1;
409 if(A
[0] == B
[0] && A
[1] == B
[1]) return 0;
413 /*compare two vertices NOT lines!
414 *A vertex is the head of a directed line.
415 *(x_1, y_1) <= (x_2, y_2) if
417 *or y_1 == y_2 && x_1 < x_2.
418 *return -1 if this->head() <= nl->head(),
421 Int
directedLine::compInY(directedLine
* nl
)
423 if(head()[1] < nl
->head()[1]) return -1;
424 if(head()[1] == nl
->head()[1] && head()[0] < nl
->head()[0]) return -1;
428 /*compare two vertices NOT lines!
429 *A vertex is the head of a directed line.
430 *(x_1, y_1) <= (x_2, y_2) if
432 *or x_1 == x_2 && y_1 < y_2.
433 *return -1 if this->head() <= nl->head(),
436 Int
directedLine::compInX(directedLine
* nl
)
438 if(head()[0] < nl
->head()[0]) return -1;
439 if(head()[0] == nl
->head()[0] && head()[1] < nl
->head()[1]) return -1;
443 /*used by sort precedures
445 static Int
compInY2(directedLine
* v1
, directedLine
* v2
)
447 return v1
->compInY(v2
);
450 static Int
compInX(directedLine
* v1
, directedLine
* v2
)
452 return v1
->compInX(v2
);
456 /*sort all the vertices NOT the lines!
457 *a vertex is the head of a directed line
459 directedLine
** directedLine::sortAllPolygons()
461 Int total_num_edges
= 0;
462 directedLine
** array
= toArrayAllPolygons(total_num_edges
);
463 quicksort( (void**)array
, 0, total_num_edges
-1, (Int (*)(void *, void *)) compInY2
);
468 void directedLine::printSingle()
470 if(direction
== INCREASING
)
471 printf("direction is INCREASING\n");
473 printf("direction is DECREASING\n");
474 printf("head=%f,%f)\n", head()[0], head()[1]);
478 /*print one polygon*/
479 void directedLine::printList()
483 for(temp
= next
; temp
!=this; temp
=temp
->next
)
487 /*print all the polygons*/
488 void directedLine::printAllPolygons()
491 for(temp
= this; temp
!=NULL
; temp
= temp
->nextPolygon
)
493 printf("polygon:\n");
498 /*insert this polygon into the head of the old polygon List*/
499 directedLine
* directedLine::insertPolygon(directedLine
* oldList
)
501 /*this polygon is a root*/
503 if(oldList
== NULL
) return this;
504 nextPolygon
= oldList
;
505 /* oldList->prevPolygon = this;*/
509 /*cutoff means delete. but we don't deallocate any space,
510 *so we use cutoff instead of delete
512 directedLine
* directedLine::cutoffPolygon(directedLine
*p
)
515 directedLine
* prev_polygon
= NULL
;
516 if(p
== NULL
) return this;
518 for(temp
=this; temp
!= p
; temp
= temp
->nextPolygon
)
522 fprintf(stderr
, "in cutoffPolygon, not found\n");
528 /* prev_polygon = p->prevPolygon;*/
531 if(prev_polygon
== NULL
) /*this is the one to cutoff*/
534 prev_polygon
->nextPolygon
= p
->nextPolygon
;
539 Int
directedLine::numPolygons()
541 if(nextPolygon
== NULL
) return 1;
542 else return 1+nextPolygon
->numPolygons();
546 /*let array[index ...] denote
547 *all the edges in this polygon
548 *return the next available index of array.
550 Int
directedLine::toArraySinglePolygon(directedLine
** array
, Int index
)
553 array
[index
++] = this;
554 for(temp
= next
; temp
!= this; temp
= temp
->next
)
556 array
[index
++] = temp
;
561 /*the space is allocated. The caller is responsible for
562 *deallocate the space.
563 *total_num_edges is set to be the total number of edges of all polygons
565 directedLine
** directedLine::toArrayAllPolygons(Int
& total_num_edges
)
567 total_num_edges
=numEdgesAllPolygons();
568 directedLine
** ret
= (directedLine
**) malloc(sizeof(directedLine
*) * total_num_edges
);
573 for(temp
=this; temp
!= NULL
; temp
=temp
->nextPolygon
) {
574 index
= temp
->toArraySinglePolygon(ret
, index
);
579 /*assume the polygon is a simple polygon, return
580 *the area enclosed by it.
581 *if thee order is counterclock wise, the area is positive.
583 Real
directedLine::polyArea()
588 x1
= this->head()[0];
589 y1
= this->head()[1];
590 x2
= this->next
->head()[0];
591 y2
= this->next
->head()[1];
592 ret
= -(x2
*y1
-x1
*y2
);
593 for(temp
=this->next
; temp
!=this; temp
= temp
->next
)
595 x1
= temp
->head()[0];
596 y1
= temp
->head()[1];
597 x2
= temp
->next
->head()[0];
598 y2
= temp
->next
->head()[1];
599 ret
+= -( x2
*y1
-x1
*y2
);
601 return Real(0.5)*ret
;
604 /*******************split or combine polygons begin********************/
605 /*conect a diagonal of a single simple polygon or two simple polygons.
606 *If the two vertices v1 (head) and v2 (head) are in the same simple polygon,
607 *then we actually split the simple polygon into two polygons.
608 *If instead two vertices velong to two difference polygons,
609 *then we combine the two polygons into one polygon.
610 *It is upto the caller to decide whether this is a split or a
614 *split a single simple polygon into two simple polygons by
615 *connecting a diagonal (two vertices).
616 *v1, v2: the two vertices are the head() of the two directedLines.
617 * this routine generates one new sampledLine which is returned in
619 *and it generates two directedLines returned in ret_p1 and ret_p2.
620 *ret_p1 and ret_p2 are used as the entry to the two new polygons.
621 *Notice the caller should not deallocate the space of v2 and v2 after
622 *calling this function, since all of the edges are connected to
626 *combine two simpolygons into one by connecting one diagonal.
627 *the returned polygon is returned in ret_p1.
630 void directedLine::connectDiagonal(directedLine
* v1
, directedLine
* v2
,
631 directedLine
** ret_p1
,
632 directedLine
** ret_p2
,
633 sampledLine
** generatedLine
,
634 directedLine
* polygonList
)
636 sampledLine
*nsline
= new sampledLine(2);
640 nsline
->setPoint(0, v1
->head());
641 nsline
->setPoint(1, v2
->head());
645 /*the increasing line is from v1 head to v2 head*/
646 directedLine
* newLineInc
= new directedLine(INCREASING
, nsline
);
650 directedLine
* newLineDec
= new directedLine(DECREASING
, nsline
);
653 directedLine
* v1Prev
= v1
->prev
;
654 directedLine
* v2Prev
= v2
->prev
;
656 v1
->prev
= newLineDec
;
657 v2Prev
->next
= newLineDec
;
658 newLineDec
->next
= v1
;
659 newLineDec
->prev
= v2Prev
;
661 v2
->prev
= newLineInc
;
662 v1Prev
->next
= newLineInc
;
663 newLineInc
->next
= v2
;
664 newLineInc
->prev
= v1Prev
;
666 *ret_p1
= newLineDec
;
667 *ret_p2
= newLineInc
;
668 *generatedLine
= nsline
;
671 //see the function connectDiangle
673 void directedLine::connectDiagonal_2slines(directedLine
* v1
, directedLine
* v2
,
674 directedLine
** ret_p1
,
675 directedLine
** ret_p2
,
676 directedLine
* polygonList
)
678 sampledLine
*nsline
= new sampledLine(2);
679 sampledLine
*nsline2
= new sampledLine(2);
681 nsline
->setPoint(0, v1
->head());
682 nsline
->setPoint(1, v2
->head());
683 nsline2
->setPoint(0, v1
->head());
684 nsline2
->setPoint(1, v2
->head());
686 /*the increasing line is from v1 head to v2 head*/
687 directedLine
* newLineInc
= new directedLine(INCREASING
, nsline
);
689 directedLine
* newLineDec
= new directedLine(DECREASING
, nsline2
);
691 directedLine
* v1Prev
= v1
->prev
;
692 directedLine
* v2Prev
= v2
->prev
;
694 v1
->prev
= newLineDec
;
695 v2Prev
->next
= newLineDec
;
696 newLineDec
->next
= v1
;
697 newLineDec
->prev
= v2Prev
;
699 v2
->prev
= newLineInc
;
700 v1Prev
->next
= newLineInc
;
701 newLineInc
->next
= v2
;
702 newLineInc
->prev
= v1Prev
;
704 *ret_p1
= newLineDec
;
705 *ret_p2
= newLineInc
;
709 Int
directedLine::samePolygon(directedLine
* v1
, directedLine
* v2
)
711 if(v1
== v2
) return 1;
713 for(temp
= v1
->next
; temp
!= v1
; temp
= temp
->next
)
715 if(temp
== v2
) return 1;
720 directedLine
* directedLine::findRoot()
722 if(rootBit
) return this;
724 for(temp
= next
; temp
!= this; temp
= temp
->next
)
725 if(temp
-> rootBit
) return temp
;
726 return NULL
; /*should not happen*/
729 directedLine
* directedLine::rootLinkFindRoot()
731 directedLine
* tempRoot
;
732 directedLine
* tempLink
;
735 while(tempLink
!= NULL
){
737 tempLink
= tempRoot
->rootLink
;
742 /*******************split or combine polygons end********************/
744 /*****************IO stuff begin*******************/
754 void directedLine::writeAllPolygons(char* filename
)
756 FILE* fp
= fopen(filename
, "w");
758 Int nPolygons
= numPolygons();
760 fprintf(fp
, "%i\n", nPolygons
);
761 for(root
= this; root
!= NULL
; root
= root
->nextPolygon
)
765 npoints
= root
->get_npoints()-1;
766 for(temp
= root
->next
; temp
!= root
; temp
=temp
->next
)
767 npoints
+= temp
->get_npoints()-1;
768 fprintf(fp
, "%i\n", npoints
/*root->numEdges()*/);
771 for(Int i
=0; i
<root
->get_npoints()-1; i
++){
772 fprintf(fp
, "%f ", root
->getVertex(i
)[0]);
773 fprintf(fp
, "%f ", root
->getVertex(i
)[1]);
776 for(temp
=root
->next
; temp
!= root
; temp
= temp
->next
)
778 for(Int i
=0; i
<temp
->get_npoints()-1; i
++){
780 fprintf(fp
, "%f ", temp
->getVertex(i
)[0]);
781 fprintf(fp
, "%f ", temp
->getVertex(i
)[1]);
790 directedLine
* readAllPolygons(char* filename
)
793 FILE* fp
= fopen(filename
, "r");
796 fscanf(fp
, "%i", &nPolygons
);
797 directedLine
*ret
= NULL
;
799 for(i
=0; i
<nPolygons
; i
++)
802 fscanf(fp
, "%i", &nEdges
);
805 /*the first two vertices*/
806 fscanf(fp
, "%f", &(vert
[0][0]));
807 fscanf(fp
, "%f", &(vert
[0][1]));
808 fscanf(fp
, "%f", &(vert
[1][0]));
809 fscanf(fp
, "%f", &(vert
[1][1]));
810 VV
[1][0] = vert
[0][0];
811 VV
[1][1] = vert
[0][1];
812 sampledLine
*sLine
= new sampledLine(2, vert
);
813 directedLine
*thisPoly
= new directedLine(INCREASING
, sLine
);
814 thisPoly
->rootLinkSet(NULL
);
817 for(j
=2; j
<nEdges
; j
++)
819 vert
[0][0]=vert
[1][0];
820 vert
[0][1]=vert
[1][1];
821 fscanf(fp
, "%f", &(vert
[1][0]));
822 fscanf(fp
, "%f", &(vert
[1][1]));
823 sLine
= new sampledLine(2,vert
);
824 dLine
= new directedLine(INCREASING
, sLine
);
825 dLine
->rootLinkSet(thisPoly
);
826 thisPoly
->insert(dLine
);
831 sLine
= new sampledLine(2,VV
);
832 dLine
= new directedLine(INCREASING
, sLine
);
833 dLine
->rootLinkSet(thisPoly
);
834 thisPoly
->insert(dLine
);
836 ret
= thisPoly
->insertPolygon(ret
);