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*/
312 direction
= INCREASING
;
316 directedLine::~directedLine()
320 Real
* directedLine::head()
323 return (direction
==INCREASING
)? (sline
->get_points())[0] : (sline
->get_points())[sline
->get_npoints()-1];
326 /*inline*/ Real
* directedLine::getVertex(Int i
)
328 return (direction
==INCREASING
)? (sline
->get_points())[i
] : (sline
->get_points())[sline
->get_npoints() - 1 -i
];
331 Real
* directedLine::tail()
333 return (direction
==DECREASING
)? (sline
->get_points())[0] : (sline
->get_points())[sline
->get_npoints()-1];
336 /*insert a new line between prev and this*/
337 void directedLine::insert(directedLine
* nl
)
343 nl
->rootLink
= this; /*assuming that 'this' is the root!!!*/
346 Int
directedLine::numEdges()
350 if(next
== this) return 1;
353 for(temp
= next
; temp
!= this; temp
= temp
->next
)
358 Int
directedLine::numEdgesAllPolygons()
362 for(temp
=this; temp
!= NULL
; temp
=temp
->nextPolygon
)
364 ret
+= temp
->numEdges();
369 /*return 1 if the double linked list forms a polygon.
371 short directedLine::isPolygon()
375 /*a polygon contains at least 3 edges*/
376 if(numEdges() <=2) return 0;
379 if(! isConnected()) return 0;
381 /*check all other edges*/
382 for(temp
=next
; temp
!= this; temp
= temp
->next
){
383 if(!isConnected()) return 0;
388 /*check if the head of this edge is connected to
389 *the tail of the prev
391 short directedLine::isConnected()
393 if( (head()[0] == prev
->tail()[0]) && (head()[1] == prev
->tail()[1]))
399 Int
compV2InY(Real A
[2], Real B
[2])
401 if(A
[1] < B
[1]) return -1;
402 if(A
[1] == B
[1] && A
[0] < B
[0]) return -1;
403 if(A
[1] == B
[1] && A
[0] == B
[0]) return 0;
407 Int
compV2InX(Real A
[2], Real B
[2])
409 if(A
[0] < B
[0]) return -1;
410 if(A
[0] == B
[0] && A
[1] < B
[1]) return -1;
411 if(A
[0] == B
[0] && A
[1] == B
[1]) return 0;
415 /*compare two vertices NOT lines!
416 *A vertex is the head of a directed line.
417 *(x_1, y_1) <= (x_2, y_2) if
419 *or y_1 == y_2 && x_1 < x_2.
420 *return -1 if this->head() <= nl->head(),
423 Int
directedLine::compInY(directedLine
* nl
)
425 if(head()[1] < nl
->head()[1]) return -1;
426 if(head()[1] == nl
->head()[1] && head()[0] < nl
->head()[0]) return -1;
430 /*compare two vertices NOT lines!
431 *A vertex is the head of a directed line.
432 *(x_1, y_1) <= (x_2, y_2) if
434 *or x_1 == x_2 && y_1 < y_2.
435 *return -1 if this->head() <= nl->head(),
438 Int
directedLine::compInX(directedLine
* nl
)
440 if(head()[0] < nl
->head()[0]) return -1;
441 if(head()[0] == nl
->head()[0] && head()[1] < nl
->head()[1]) return -1;
445 /*used by sort precedures
447 static Int
compInY2(directedLine
* v1
, directedLine
* v2
)
449 return v1
->compInY(v2
);
452 static Int
compInX(directedLine
* v1
, directedLine
* v2
)
454 return v1
->compInX(v2
);
458 /*sort all the vertices NOT the lines!
459 *a vertex is the head of a directed line
461 directedLine
** directedLine::sortAllPolygons()
463 Int total_num_edges
= 0;
464 directedLine
** array
= toArrayAllPolygons(total_num_edges
);
465 quicksort( (void**)array
, 0, total_num_edges
-1, (Int (*)(void *, void *)) compInY2
);
470 void directedLine::printSingle()
472 if(direction
== INCREASING
)
473 printf("direction is INCREASING\n");
475 printf("direction is DECREASING\n");
476 printf("head=%f,%f)\n", head()[0], head()[1]);
480 /*print one polygon*/
481 void directedLine::printList()
485 for(temp
= next
; temp
!=this; temp
=temp
->next
)
489 /*print all the polygons*/
490 void directedLine::printAllPolygons()
493 for(temp
= this; temp
!=NULL
; temp
= temp
->nextPolygon
)
495 printf("polygon:\n");
500 /*insert this polygon into the head of the old polygon List*/
501 directedLine
* directedLine::insertPolygon(directedLine
* oldList
)
503 /*this polygon is a root*/
505 if(oldList
== NULL
) return this;
506 nextPolygon
= oldList
;
507 /* oldList->prevPolygon = this;*/
511 /*cutoff means delete. but we don't deallocate any space,
512 *so we use cutoff instead of delete
514 directedLine
* directedLine::cutoffPolygon(directedLine
*p
)
517 directedLine
* prev_polygon
= NULL
;
518 if(p
== NULL
) return this;
520 for(temp
=this; temp
!= p
; temp
= temp
->nextPolygon
)
524 fprintf(stderr
, "in cutoffPolygon, not found\n");
530 /* prev_polygon = p->prevPolygon;*/
533 if(prev_polygon
== NULL
) /*this is the one to cutoff*/
536 prev_polygon
->nextPolygon
= p
->nextPolygon
;
541 Int
directedLine::numPolygons()
543 if(nextPolygon
== NULL
) return 1;
544 else return 1+nextPolygon
->numPolygons();
548 /*let array[index ...] denote
549 *all the edges in this polygon
550 *return the next available index of array.
552 Int
directedLine::toArraySinglePolygon(directedLine
** array
, Int index
)
555 array
[index
++] = this;
556 for(temp
= next
; temp
!= this; temp
= temp
->next
)
558 array
[index
++] = temp
;
563 /*the space is allocated. The caller is responsible for
564 *deallocate the space.
565 *total_num_edges is set to be the total number of edges of all polygons
567 directedLine
** directedLine::toArrayAllPolygons(Int
& total_num_edges
)
569 total_num_edges
=numEdgesAllPolygons();
570 directedLine
** ret
= (directedLine
**) malloc(sizeof(directedLine
*) * total_num_edges
);
575 for(temp
=this; temp
!= NULL
; temp
=temp
->nextPolygon
) {
576 index
= temp
->toArraySinglePolygon(ret
, index
);
581 /*assume the polygon is a simple polygon, return
582 *the area enclosed by it.
583 *if thee order is counterclock wise, the area is positive.
585 Real
directedLine::polyArea()
590 x1
= this->head()[0];
591 y1
= this->head()[1];
592 x2
= this->next
->head()[0];
593 y2
= this->next
->head()[1];
594 ret
= -(x2
*y1
-x1
*y2
);
595 for(temp
=this->next
; temp
!=this; temp
= temp
->next
)
597 x1
= temp
->head()[0];
598 y1
= temp
->head()[1];
599 x2
= temp
->next
->head()[0];
600 y2
= temp
->next
->head()[1];
601 ret
+= -( x2
*y1
-x1
*y2
);
603 return Real(0.5)*ret
;
606 /*******************split or combine polygons begin********************/
607 /*conect a diagonal of a single simple polygon or two simple polygons.
608 *If the two vertices v1 (head) and v2 (head) are in the same simple polygon,
609 *then we actually split the simple polygon into two polygons.
610 *If instead two vertices velong to two difference polygons,
611 *then we combine the two polygons into one polygon.
612 *It is upto the caller to decide whether this is a split or a
616 *split a single simple polygon into two simple polygons by
617 *connecting a diagonal (two vertices).
618 *v1, v2: the two vertices are the head() of the two directedLines.
619 * this routine generates one new sampledLine which is returned in
621 *and it generates two directedLines returned in ret_p1 and ret_p2.
622 *ret_p1 and ret_p2 are used as the entry to the two new polygons.
623 *Notice the caller should not deallocate the space of v2 and v2 after
624 *calling this function, since all of the edges are connected to
628 *combine two simpolygons into one by connecting one diagonal.
629 *the returned polygon is returned in ret_p1.
632 void directedLine::connectDiagonal(directedLine
* v1
, directedLine
* v2
,
633 directedLine
** ret_p1
,
634 directedLine
** ret_p2
,
635 sampledLine
** generatedLine
,
636 directedLine
* polygonList
)
638 sampledLine
*nsline
= new sampledLine(2);
642 nsline
->setPoint(0, v1
->head());
643 nsline
->setPoint(1, v2
->head());
647 /*the increasing line is from v1 head to v2 head*/
648 directedLine
* newLineInc
= new directedLine(INCREASING
, nsline
);
652 directedLine
* newLineDec
= new directedLine(DECREASING
, nsline
);
655 directedLine
* v1Prev
= v1
->prev
;
656 directedLine
* v2Prev
= v2
->prev
;
658 v1
->prev
= newLineDec
;
659 v2Prev
->next
= newLineDec
;
660 newLineDec
->next
= v1
;
661 newLineDec
->prev
= v2Prev
;
663 v2
->prev
= newLineInc
;
664 v1Prev
->next
= newLineInc
;
665 newLineInc
->next
= v2
;
666 newLineInc
->prev
= v1Prev
;
668 *ret_p1
= newLineDec
;
669 *ret_p2
= newLineInc
;
670 *generatedLine
= nsline
;
673 //see the function connectDiangle
675 void directedLine::connectDiagonal_2slines(directedLine
* v1
, directedLine
* v2
,
676 directedLine
** ret_p1
,
677 directedLine
** ret_p2
,
678 directedLine
* polygonList
)
680 sampledLine
*nsline
= new sampledLine(2);
681 sampledLine
*nsline2
= new sampledLine(2);
683 nsline
->setPoint(0, v1
->head());
684 nsline
->setPoint(1, v2
->head());
685 nsline2
->setPoint(0, v1
->head());
686 nsline2
->setPoint(1, v2
->head());
688 /*the increasing line is from v1 head to v2 head*/
689 directedLine
* newLineInc
= new directedLine(INCREASING
, nsline
);
691 directedLine
* newLineDec
= new directedLine(DECREASING
, nsline2
);
693 directedLine
* v1Prev
= v1
->prev
;
694 directedLine
* v2Prev
= v2
->prev
;
696 v1
->prev
= newLineDec
;
697 v2Prev
->next
= newLineDec
;
698 newLineDec
->next
= v1
;
699 newLineDec
->prev
= v2Prev
;
701 v2
->prev
= newLineInc
;
702 v1Prev
->next
= newLineInc
;
703 newLineInc
->next
= v2
;
704 newLineInc
->prev
= v1Prev
;
706 *ret_p1
= newLineDec
;
707 *ret_p2
= newLineInc
;
711 Int
directedLine::samePolygon(directedLine
* v1
, directedLine
* v2
)
713 if(v1
== v2
) return 1;
715 for(temp
= v1
->next
; temp
!= v1
; temp
= temp
->next
)
717 if(temp
== v2
) return 1;
722 directedLine
* directedLine::findRoot()
724 if(rootBit
) return this;
726 for(temp
= next
; temp
!= this; temp
= temp
->next
)
727 if(temp
-> rootBit
) return temp
;
728 return NULL
; /*should not happen*/
731 directedLine
* directedLine::rootLinkFindRoot()
733 directedLine
* tempRoot
;
734 directedLine
* tempLink
;
737 while(tempLink
!= NULL
){
739 tempLink
= tempRoot
->rootLink
;
744 /*******************split or combine polygons end********************/
746 /*****************IO stuff begin*******************/
756 void directedLine::writeAllPolygons(char* filename
)
758 FILE* fp
= fopen(filename
, "w");
760 Int nPolygons
= numPolygons();
762 fprintf(fp
, "%i\n", nPolygons
);
763 for(root
= this; root
!= NULL
; root
= root
->nextPolygon
)
767 npoints
= root
->get_npoints()-1;
768 for(temp
= root
->next
; temp
!= root
; temp
=temp
->next
)
769 npoints
+= temp
->get_npoints()-1;
770 fprintf(fp
, "%i\n", npoints
/*root->numEdges()*/);
773 for(Int i
=0; i
<root
->get_npoints()-1; i
++){
774 fprintf(fp
, "%f ", root
->getVertex(i
)[0]);
775 fprintf(fp
, "%f ", root
->getVertex(i
)[1]);
778 for(temp
=root
->next
; temp
!= root
; temp
= temp
->next
)
780 for(Int i
=0; i
<temp
->get_npoints()-1; i
++){
782 fprintf(fp
, "%f ", temp
->getVertex(i
)[0]);
783 fprintf(fp
, "%f ", temp
->getVertex(i
)[1]);
792 directedLine
* readAllPolygons(char* filename
)
795 FILE* fp
= fopen(filename
, "r");
800 result
= fscanf(fp
, "%i", &nPolygons
);
801 assert(result
!= EOF
);
802 directedLine
*ret
= NULL
;
804 for(i
=0; i
<nPolygons
; i
++)
807 result
= fscanf(fp
, "%i", &nEdges
);
808 assert(result
!= EOF
);
809 Real vert
[2][2] = { { 0 } };
811 /*the first two vertices*/
812 result
= fscanf(fp
, "%f", &(vert
[0][0]));
813 assert(result
!= EOF
);
814 result
= fscanf(fp
, "%f", &(vert
[0][1]));
815 assert(result
!= EOF
);
816 result
= fscanf(fp
, "%f", &(vert
[1][0]));
817 assert(result
!= EOF
);
818 result
= fscanf(fp
, "%f", &(vert
[1][1]));
819 assert(result
!= EOF
);
820 VV
[1][0] = vert
[0][0];
821 VV
[1][1] = vert
[0][1];
822 sampledLine
*sLine
= new sampledLine(2, vert
);
823 directedLine
*thisPoly
= new directedLine(INCREASING
, sLine
);
824 thisPoly
->rootLinkSet(NULL
);
827 for(j
=2; j
<nEdges
; j
++)
829 vert
[0][0]=vert
[1][0];
830 vert
[0][1]=vert
[1][1];
831 result
= fscanf(fp
, "%f", &(vert
[1][0]));
832 assert(result
!= EOF
);
833 result
= fscanf(fp
, "%f", &(vert
[1][1]));
834 assert(result
!= EOF
);
835 sLine
= new sampledLine(2,vert
);
836 dLine
= new directedLine(INCREASING
, sLine
);
837 dLine
->rootLinkSet(thisPoly
);
838 thisPoly
->insert(dLine
);
843 sLine
= new sampledLine(2,VV
);
844 dLine
= new directedLine(INCREASING
, sLine
);
845 dLine
->rootLinkSet(thisPoly
);
846 thisPoly
->insert(dLine
);
848 ret
= thisPoly
->insertPolygon(ret
);