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.
34 ** $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
37 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/nurbtess/directedLine.cc,v 1.1 2001/03/17 00:25:41 brianp Exp $
43 #include "glimports.h"
46 #include "quicksort.h"
47 #include "directedLine.h"
50 //we must return the newLine
51 directedLine
* directedLine::deleteChain(directedLine
* begin
, directedLine
* end
)
53 if(begin
->head()[0] == end
->tail()[0] &&
54 begin
->head()[1] == end
->tail()[1]
57 directedLine
*ret
= begin
->prev
;
58 begin
->prev
->next
= end
->next
;
59 end
->next
->prev
= begin
->prev
;
68 directedLine
* newLine
;
69 sampledLine
* sline
= new sampledLine(begin
->head(), end
->tail());
70 newLine
= new directedLine(INCREASING
, sline
);
71 directedLine
*p
= begin
->prev
;
72 directedLine
*n
= end
->next
;
86 void directedLine::deleteSingleLine(directedLine
* dline
)
88 //make sure that dline->prev->tail is the same as
89 //dline->next->head. This is for numerical erros.
90 //for example, if we delete a line which is almost degeneate
91 //within (epsilon), then we want to make that the polygon after deletion
92 //is still a valid polygon
94 dline
->next
->head()[0] = dline
->prev
->tail()[0];
95 dline
->next
->head()[1] = dline
->prev
->tail()[1];
97 dline
->prev
->next
= dline
->next
;
98 dline
->next
->prev
= dline
->prev
;
104 static Int
myequal(Real a
[2], Real b
[2])
107 if(a[0]==b[0] && a[1] == b[1])
114 if(fabs(a
[0]-b
[0]) < 0.00001 &&
115 fabs(a
[1]-b
[1]) < 0.00001)
122 directedLine
* directedLine::deleteDegenerateLines()
124 //if there is only one edge or two edges, don't do anything
125 if(this->next
== this)
127 if(this->next
== this->prev
)
130 //find a nondegenerate line
132 directedLine
* first
= NULL
;
133 if(! myequal(head(), tail()))
135 if(head()[0] != tail()[0] ||
136 head()[1] != tail()[1])
141 for(temp
= this->next
; temp
!= this; temp
= temp
->next
)
144 if(temp->head()[0] != temp->tail()[0] ||
145 temp->head()[1] != temp->tail()[1])
147 if(! myequal(temp
->head(), temp
->tail()))
156 //if there are no non-degenerate lines, then we simply return NULL.
159 deleteSinglePolygonWithSline();
163 directedLine
* tempNext
= NULL
;
164 for(temp
=first
->next
; temp
!= first
; temp
= tempNext
)
166 tempNext
= temp
->getNext();
168 if(temp->head()[0] == temp->tail()[0] &&
169 temp->head()[1] == temp->tail()[1])
172 if(myequal(temp
->head(), temp
->tail()))
173 deleteSingleLine(temp
);
178 directedLine
* directedLine::deleteDegenerateLinesAllPolygons()
181 directedLine
*tempNext
= NULL
;
182 directedLine
* ret
= NULL
;
183 directedLine
* retEnd
= NULL
;
184 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
186 tempNext
= temp
->nextPolygon
;
187 temp
->nextPolygon
= NULL
;
190 ret
= retEnd
= temp
->deleteDegenerateLines();
195 directedLine
*newPolygon
= temp
->deleteDegenerateLines();
196 if(newPolygon
!= NULL
)
198 retEnd
->nextPolygon
= temp
->deleteDegenerateLines();
199 retEnd
= retEnd
->nextPolygon
;
206 directedLine
* directedLine::cutIntersectionAllPoly(int &cutOccur
)
209 directedLine
*tempNext
= NULL
;
210 directedLine
* ret
= NULL
;
211 directedLine
* retEnd
= NULL
;
213 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
216 tempNext
= temp
->nextPolygon
;
217 temp
->nextPolygon
= NULL
;
221 ret
= retEnd
= DBG_cutIntersectionPoly(temp
, eachCutOccur
);
228 retEnd
->nextPolygon
= DBG_cutIntersectionPoly(temp
, eachCutOccur
);
229 retEnd
= retEnd
->nextPolygon
;
238 void directedLine::deleteSinglePolygonWithSline()
240 directedLine
*temp
, *tempNext
;
242 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
244 tempNext
= temp
->next
;
250 void directedLine::deletePolygonListWithSline()
252 directedLine
*temp
, *tempNext
;
253 for(temp
=this; temp
!= NULL
; temp
=tempNext
)
255 tempNext
= temp
->nextPolygon
;
256 temp
->deleteSinglePolygonWithSline();
260 void directedLine::deleteSinglePolygon()
262 directedLine
*temp
, *tempNext
;
264 for(temp
=this; temp
!= NULL
; temp
= tempNext
)
266 tempNext
= temp
->next
;
271 void directedLine::deletePolygonList()
273 directedLine
*temp
, *tempNext
;
274 for(temp
=this; temp
!= NULL
; temp
=tempNext
)
276 tempNext
= temp
->nextPolygon
;
277 temp
->deleteSinglePolygon();
283 directedLine::directedLine(short dir
, sampledLine
* sl
)
290 // prevPolygon = NULL;
291 rootBit
= 0;/*important to initilzae to 0 meaning not root yet*/
297 void directedLine::init(short dir
, sampledLine
* sl
)
303 directedLine::directedLine()
308 rootBit
= 0;/*important to initilzae to 0 meaning not root yet*/
312 directedLine::~directedLine()
316 Real
* directedLine::head()
319 return (direction
==INCREASING
)? (sline
->get_points())[0] : (sline
->get_points())[sline
->get_npoints()-1];
322 /*inline*/ Real
* directedLine::getVertex(Int i
)
324 return (direction
==INCREASING
)? (sline
->get_points())[i
] : (sline
->get_points())[sline
->get_npoints() - 1 -i
];
327 Real
* directedLine::tail()
329 return (direction
==DECREASING
)? (sline
->get_points())[0] : (sline
->get_points())[sline
->get_npoints()-1];
332 /*insert a new line between prev and this*/
333 void directedLine::insert(directedLine
* nl
)
339 nl
->rootLink
= this; /*assuming that 'this' is the root!!!*/
342 Int
directedLine::numEdges()
346 if(next
== this) return 1;
349 for(temp
= next
; temp
!= this; temp
= temp
->next
)
354 Int
directedLine::numEdgesAllPolygons()
358 for(temp
=this; temp
!= NULL
; temp
=temp
->nextPolygon
)
360 ret
+= temp
->numEdges();
365 /*return 1 if the double linked list forms a polygon.
367 short directedLine::isPolygon()
371 /*a polygon contains at least 3 edges*/
372 if(numEdges() <=2) return 0;
375 if(! isConnected()) return 0;
377 /*check all other edges*/
378 for(temp
=next
; temp
!= this; temp
= temp
->next
){
379 if(!isConnected()) return 0;
384 /*check if the head of this edge is connected to
385 *the tail of the prev
387 short directedLine::isConnected()
389 if( (head()[0] == prev
->tail()[0]) && (head()[1] == prev
->tail()[1]))
395 Int
compV2InY(Real A
[2], Real B
[2])
397 if(A
[1] < B
[1]) return -1;
398 if(A
[1] == B
[1] && A
[0] < B
[0]) return -1;
399 if(A
[1] == B
[1] && A
[0] == B
[0]) return 0;
403 Int
compV2InX(Real A
[2], Real B
[2])
405 if(A
[0] < B
[0]) return -1;
406 if(A
[0] == B
[0] && A
[1] < B
[1]) return -1;
407 if(A
[0] == B
[0] && A
[1] == B
[1]) return 0;
411 /*compare two vertices NOT lines!
412 *A vertex is the head of a directed line.
413 *(x_1, y_1) <= (x_2, y_2) if
415 *or y_1 == y_2 && x_1 < x_2.
416 *return -1 if this->head() <= nl->head(),
419 Int
directedLine::compInY(directedLine
* nl
)
421 if(head()[1] < nl
->head()[1]) return -1;
422 if(head()[1] == nl
->head()[1] && head()[0] < nl
->head()[0]) return -1;
426 /*compare two vertices NOT lines!
427 *A vertex is the head of a directed line.
428 *(x_1, y_1) <= (x_2, y_2) if
430 *or x_1 == x_2 && y_1 < y_2.
431 *return -1 if this->head() <= nl->head(),
434 Int
directedLine::compInX(directedLine
* nl
)
436 if(head()[0] < nl
->head()[0]) return -1;
437 if(head()[0] == nl
->head()[0] && head()[1] < nl
->head()[1]) return -1;
441 /*used by sort precedures
443 static Int
compInY2(directedLine
* v1
, directedLine
* v2
)
445 return v1
->compInY(v2
);
448 static Int
compInX(directedLine
* v1
, directedLine
* v2
)
450 return v1
->compInX(v2
);
454 /*sort all the vertices NOT the lines!
455 *a vertex is the head of a directed line
457 directedLine
** directedLine::sortAllPolygons()
459 Int total_num_edges
= 0;
460 directedLine
** array
= toArrayAllPolygons(total_num_edges
);
461 quicksort( (void**)array
, 0, total_num_edges
-1, (Int (*)(void *, void *)) compInY2
);
466 void directedLine::printSingle()
468 if(direction
== INCREASING
)
469 printf("direction is INCREASING\n");
471 printf("direction is DECREASING\n");
472 printf("head=%f,%f)\n", head()[0], head()[1]);
476 /*print one polygon*/
477 void directedLine::printList()
481 for(temp
= next
; temp
!=this; temp
=temp
->next
)
485 /*print all the polygons*/
486 void directedLine::printAllPolygons()
489 for(temp
= this; temp
!=NULL
; temp
= temp
->nextPolygon
)
491 printf("polygon:\n");
496 /*insert this polygon into the head of the old polygon List*/
497 directedLine
* directedLine::insertPolygon(directedLine
* oldList
)
499 /*this polygon is a root*/
501 if(oldList
== NULL
) return this;
502 nextPolygon
= oldList
;
503 /* oldList->prevPolygon = this;*/
507 /*cutoff means delete. but we don't deallocate any space,
508 *so we use cutoff instead of delete
510 directedLine
* directedLine::cutoffPolygon(directedLine
*p
)
513 directedLine
* prev_polygon
= NULL
;
514 if(p
== NULL
) return this;
516 for(temp
=this; temp
!= p
; temp
= temp
->nextPolygon
)
520 fprintf(stderr
, "in cutoffPolygon, not found\n");
526 /* prev_polygon = p->prevPolygon;*/
529 if(prev_polygon
== NULL
) /*this is the one to cutoff*/
532 prev_polygon
->nextPolygon
= p
->nextPolygon
;
537 Int
directedLine::numPolygons()
539 if(nextPolygon
== NULL
) return 1;
540 else return 1+nextPolygon
->numPolygons();
544 /*let array[index ...] denote
545 *all the edges in this polygon
546 *return the next available index of array.
548 Int
directedLine::toArraySinglePolygon(directedLine
** array
, Int index
)
551 array
[index
++] = this;
552 for(temp
= next
; temp
!= this; temp
= temp
->next
)
554 array
[index
++] = temp
;
559 /*the space is allocated. The caller is responsible for
560 *deallocate the space.
561 *total_num_edges is set to be the total number of edges of all polygons
563 directedLine
** directedLine::toArrayAllPolygons(Int
& total_num_edges
)
565 total_num_edges
=numEdgesAllPolygons();
566 directedLine
** ret
= (directedLine
**) malloc(sizeof(directedLine
*) * total_num_edges
);
571 for(temp
=this; temp
!= NULL
; temp
=temp
->nextPolygon
) {
572 index
= temp
->toArraySinglePolygon(ret
, index
);
577 /*assume the polygon is a simple polygon, return
578 *the area enclosed by it.
579 *if thee order is counterclock wise, the area is positive.
581 Real
directedLine::polyArea()
586 x1
= this->head()[0];
587 y1
= this->head()[1];
588 x2
= this->next
->head()[0];
589 y2
= this->next
->head()[1];
590 ret
= -(x2
*y1
-x1
*y2
);
591 for(temp
=this->next
; temp
!=this; temp
= temp
->next
)
593 x1
= temp
->head()[0];
594 y1
= temp
->head()[1];
595 x2
= temp
->next
->head()[0];
596 y2
= temp
->next
->head()[1];
597 ret
+= -( x2
*y1
-x1
*y2
);
602 /*******************split or combine polygons begin********************/
603 /*conect a diagonal of a single simple polygon or two simple polygons.
604 *If the two vertices v1 (head) and v2 (head) are in the same simple polygon,
605 *then we actually split the simple polygon into two polygons.
606 *If instead two vertices velong to two difference polygons,
607 *then we combine the two polygons into one polygon.
608 *It is upto the caller to decide whether this is a split or a
612 *split a single simple polygon into two simple polygons by
613 *connecting a diagonal (two vertices).
614 *v1, v2: the two vertices are the head() of the two directedLines.
615 * this routine generates one new sampledLine which is returned in
617 *and it generates two directedLines returned in ret_p1 and ret_p2.
618 *ret_p1 and ret_p2 are used as the entry to the two new polygons.
619 *Notice the caller should not deallocate the space of v2 and v2 after
620 *calling this function, since all of the edges are connected to
624 *combine two simpolygons into one by connecting one diagonal.
625 *the returned polygon is returned in ret_p1.
628 void directedLine::connectDiagonal(directedLine
* v1
, directedLine
* v2
,
629 directedLine
** ret_p1
,
630 directedLine
** ret_p2
,
631 sampledLine
** generatedLine
,
632 directedLine
* polygonList
)
634 sampledLine
*nsline
= new sampledLine(2);
638 nsline
->setPoint(0, v1
->head());
639 nsline
->setPoint(1, v2
->head());
643 /*the increasing line is from v1 head to v2 head*/
644 directedLine
* newLineInc
= new directedLine(INCREASING
, nsline
);
648 directedLine
* newLineDec
= new directedLine(DECREASING
, nsline
);
651 directedLine
* v1Prev
= v1
->prev
;
652 directedLine
* v2Prev
= v2
->prev
;
654 v1
->prev
= newLineDec
;
655 v2Prev
->next
= newLineDec
;
656 newLineDec
->next
= v1
;
657 newLineDec
->prev
= v2Prev
;
659 v2
->prev
= newLineInc
;
660 v1Prev
->next
= newLineInc
;
661 newLineInc
->next
= v2
;
662 newLineInc
->prev
= v1Prev
;
664 *ret_p1
= newLineDec
;
665 *ret_p2
= newLineInc
;
666 *generatedLine
= nsline
;
669 //see the function connectDiangle
671 void directedLine::connectDiagonal_2slines(directedLine
* v1
, directedLine
* v2
,
672 directedLine
** ret_p1
,
673 directedLine
** ret_p2
,
674 directedLine
* polygonList
)
676 sampledLine
*nsline
= new sampledLine(2);
677 sampledLine
*nsline2
= new sampledLine(2);
679 nsline
->setPoint(0, v1
->head());
680 nsline
->setPoint(1, v2
->head());
681 nsline2
->setPoint(0, v1
->head());
682 nsline2
->setPoint(1, v2
->head());
684 /*the increasing line is from v1 head to v2 head*/
685 directedLine
* newLineInc
= new directedLine(INCREASING
, nsline
);
687 directedLine
* newLineDec
= new directedLine(DECREASING
, nsline2
);
689 directedLine
* v1Prev
= v1
->prev
;
690 directedLine
* v2Prev
= v2
->prev
;
692 v1
->prev
= newLineDec
;
693 v2Prev
->next
= newLineDec
;
694 newLineDec
->next
= v1
;
695 newLineDec
->prev
= v2Prev
;
697 v2
->prev
= newLineInc
;
698 v1Prev
->next
= newLineInc
;
699 newLineInc
->next
= v2
;
700 newLineInc
->prev
= v1Prev
;
702 *ret_p1
= newLineDec
;
703 *ret_p2
= newLineInc
;
707 Int
directedLine::samePolygon(directedLine
* v1
, directedLine
* v2
)
709 if(v1
== v2
) return 1;
711 for(temp
= v1
->next
; temp
!= v1
; temp
= temp
->next
)
713 if(temp
== v2
) return 1;
718 directedLine
* directedLine::findRoot()
720 if(rootBit
) return this;
722 for(temp
= next
; temp
!= this; temp
= temp
->next
)
723 if(temp
-> rootBit
) return temp
;
724 return NULL
; /*should not happen*/
727 directedLine
* directedLine::rootLinkFindRoot()
729 directedLine
* tempRoot
;
730 directedLine
* tempLink
;
733 while(tempLink
!= NULL
){
735 tempLink
= tempRoot
->rootLink
;
740 /*******************split or combine polygons end********************/
742 /*****************IO stuff begin*******************/
752 void directedLine::writeAllPolygons(char* filename
)
754 FILE* fp
= fopen(filename
, "w");
756 Int nPolygons
= numPolygons();
758 fprintf(fp
, "%i\n", nPolygons
);
759 for(root
= this; root
!= NULL
; root
= root
->nextPolygon
)
763 npoints
= root
->get_npoints()-1;
764 for(temp
= root
->next
; temp
!= root
; temp
=temp
->next
)
765 npoints
+= temp
->get_npoints()-1;
766 fprintf(fp
, "%i\n", npoints
/*root->numEdges()*/);
769 for(Int i
=0; i
<root
->get_npoints()-1; i
++){
770 fprintf(fp
, "%f ", root
->getVertex(i
)[0]);
771 fprintf(fp
, "%f ", root
->getVertex(i
)[1]);
774 for(temp
=root
->next
; temp
!= root
; temp
= temp
->next
)
776 for(Int i
=0; i
<temp
->get_npoints()-1; i
++){
778 fprintf(fp
, "%f ", temp
->getVertex(i
)[0]);
779 fprintf(fp
, "%f ", temp
->getVertex(i
)[1]);
788 directedLine
* readAllPolygons(char* filename
)
791 FILE* fp
= fopen(filename
, "r");
794 fscanf(fp
, "%i", &nPolygons
);
795 directedLine
*ret
= NULL
;
797 for(i
=0; i
<nPolygons
; i
++)
800 fscanf(fp
, "%i", &nEdges
);
803 /*the first two vertices*/
804 fscanf(fp
, "%f", &(vert
[0][0]));
805 fscanf(fp
, "%f", &(vert
[0][1]));
806 fscanf(fp
, "%f", &(vert
[1][0]));
807 fscanf(fp
, "%f", &(vert
[1][1]));
808 VV
[1][0] = vert
[0][0];
809 VV
[1][1] = vert
[0][1];
810 sampledLine
*sLine
= new sampledLine(2, vert
);
811 directedLine
*thisPoly
= new directedLine(INCREASING
, sLine
);
812 thisPoly
->rootLinkSet(NULL
);
815 for(j
=2; j
<nEdges
; j
++)
817 vert
[0][0]=vert
[1][0];
818 vert
[0][1]=vert
[1][1];
819 fscanf(fp
, "%f", &(vert
[1][0]));
820 fscanf(fp
, "%f", &(vert
[1][1]));
821 sLine
= new sampledLine(2,vert
);
822 dLine
= new directedLine(INCREASING
, sLine
);
823 dLine
->rootLinkSet(thisPoly
);
824 thisPoly
->insert(dLine
);
829 sLine
= new sampledLine(2,VV
);
830 dLine
= new directedLine(INCREASING
, sLine
);
831 dLine
->rootLinkSet(thisPoly
);
832 thisPoly
->insert(dLine
);
834 ret
= thisPoly
->insertPolygon(ret
);