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/22 11:41:40 $ $Revision: 1.2 $
37 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/nurbtess/monoTriangulation.cc,v 1.2 2001/03/22 11:41:40 joukj Exp $
43 #include "glimports.h"
46 #include "monoTriangulation.h"
47 #include "polyUtil.h" /*for area*/
48 #include "partitionX.h"
49 #include "monoPolyPart.h"
53 extern directedLine
* polygonConvert(directedLine
* polygon
);
57 void monoTriangulationOpt(directedLine
* poly
, primStream
* pStream
)
60 Int n_edges
= poly
->numEdges();
61 directedLine
** cusps
= (directedLine
**) malloc(sizeof(directedLine
*)*n_edges
);
63 findInteriorCuspsX(poly
, n_cusps
, cusps
);
64 if(n_cusps
==0) //u monotine
66 monoTriangulationFun(poly
, compV2InX
, pStream
);
68 else if(n_cusps
== 1) // one interior cusp
70 directedLine
* new_polygon
= polygonConvert(cusps
[0]);
71 directedLine
* other
= findDiagonal_singleCuspX(new_polygon
);
72 //<other> should NOT be null unless there are self-intersecting
73 //trim curves. In that case, we don't want to core dump, instead,
74 //we triangulate anyway, and print out error message.
77 monoTriangulationFun(poly
, compV2InX
, pStream
);
84 new_polygon
->connectDiagonal_2slines(new_polygon
, other
,
89 monoTriangulationFun(ret_p1
, compV2InX
, pStream
);
90 monoTriangulationFun(ret_p2
, compV2InX
, pStream
);
92 ret_p1
->deleteSinglePolygonWithSline();
93 ret_p2
->deleteSinglePolygonWithSline();
98 //we need a general partitionX funtion (supposed to be in partitionX.C,
99 //not implemented yet. XXX
100 monoTriangulationFun(poly
, compV2InY
, pStream
);
106 void monoTriangulationRecOpt(Real
* topVertex
, Real
* botVertex
,
107 vertexArray
* left_chain
, Int left_current
,
108 vertexArray
* right_chain
, Int right_current
,
112 Int n_left
= left_chain
->getNumElements();
113 Int n_right
= right_chain
->getNumElements();
114 if(left_current
>= n_left
-1 ||
115 right_current
>= n_right
-1)
117 monoTriangulationRec(topVertex
, botVertex
, left_chain
, left_current
,
118 right_chain
, right_current
, pStream
);
121 //now both left and right have at least two vertices each.
122 Real left_v
= left_chain
->getVertex(left_current
)[1];
123 Real right_v
= right_chain
->getVertex(right_current
)[1];
125 if(left_v
<= right_v
) //first left vertex is below right
127 //find the last vertex of right which is above or equal to left
128 for(j
=right_current
; j
<=n_right
-1; j
++)
130 if(right_chain
->getVertex(j
)[1] < left_v
)
133 monoTriangulationRecGen(topVertex
, left_chain
->getVertex(left_current
),
134 left_chain
, left_current
, left_current
,
135 right_chain
, right_current
, j
-1,
137 monoTriangulationRecOpt(right_chain
->getVertex(j
-1),
139 left_chain
, left_current
,
143 else //first right vertex is strictly below left
145 //find the last vertex of left which is strictly above right
146 for(i
=left_current
; i
<=n_left
-1; i
++)
148 if(left_chain
->getVertex(i
)[1] <= right_v
)
151 monoTriangulationRecGen(topVertex
, right_chain
->getVertex(right_current
),
152 left_chain
, left_current
, i
-1,
153 right_chain
, right_current
, right_current
,
155 monoTriangulationRecOpt(left_chain
->getVertex(i
-1),
158 right_chain
, right_current
,
164 void monoTriangulationRecGenTBOpt(Real
* topVertex
, Real
* botVertex
,
165 vertexArray
* inc_chain
, Int inc_current
, Int inc_end
,
166 vertexArray
* dec_chain
, Int dec_current
, Int dec_end
,
169 pStream
->triangle(topVertex
, inc_chain
->getVertex(inc_current
), dec_chain
->getVertex(dec_current
));
171 /*printf("**(%f,%f)\n", inc_chain->getArray()[0][0],inc_chain->getArray()[0][1]);*/
172 triangulateXYMonoTB(inc_end
-inc_current
+1, inc_chain
->getArray()+inc_current
, dec_end
-dec_current
+1, dec_chain
->getArray()+dec_current
, pStream
);
174 pStream
->triangle(botVertex
, dec_chain
->getVertex(dec_end
), inc_chain
->getVertex(inc_end
));
180 *the strip is going top to bottom. compared to the funtion
181 * triangulateXYmono()
183 void triangulateXYMonoTB(Int n_left
, Real
** leftVerts
,
184 Int n_right
, Real
** rightVerts
,
192 assert(n_left
>=1 && n_right
>=1);
193 if(leftVerts
[0][1] >= rightVerts
[0][1])
197 topMostV
= leftVerts
[0];
203 topMostV
= rightVerts
[0];
208 if(i
>= n_left
) /*case1: no more in left*/
211 if(j
<n_right
-1) /*at least two vertices in right*/
214 pStream
->insert(topMostV
);
215 for(k
=n_right
-1; k
>=j
; k
--)
216 pStream
->insert(rightVerts
[j
]);
218 pStream
->end(PRIMITIVE_STREAM_FAN
);
224 else if(j
>= n_right
) /*case2: no more in right*/
227 if(i
<n_left
-1) /*at least two vertices in left*/
230 pStream
->insert(topMostV
);
232 for(k
=i
; k
<n_left
; k
++)
233 pStream
->insert(leftVerts
[k
]);
235 pStream
->end(PRIMITIVE_STREAM_FAN
);
240 else /* case3: neither is empty, plus the topMostV, there is at least one triangle to output*/
243 if(leftVerts
[i
][1] >= rightVerts
[j
][1])
246 pStream
->insert(rightVerts
[j
]); /*the origin of this fan*/
248 pStream
->insert(topMostV
);
250 /*find the last k>=i such that
251 *leftverts[k][1] >= rightverts[j][1]
256 if(leftVerts
[k
][1] < rightVerts
[j
][1])
263 pStream
->insert(leftVerts
[l
]);
266 pStream
->end(PRIMITIVE_STREAM_FAN
);
267 //update i for next loop
269 topMostV
= leftVerts
[k
];
272 else /*leftVerts[i][1] < rightVerts[j][1]*/
275 pStream
->insert(leftVerts
[i
]);/*the origion of this fan*/
277 /*find the last k>=j such that
278 *rightverts[k][1] > leftverts[i][1]*/
282 if(rightVerts
[k
][1] <= leftVerts
[i
][1])
289 pStream
->insert(rightVerts
[l
]);
291 pStream
->insert(topMostV
);
292 pStream
->end(PRIMITIVE_STREAM_FAN
);
294 topMostV
= rightVerts
[j
-1];
300 static int chainConvex(vertexArray
* inc_chain
, Int inc_current
, Int inc_end
)
303 //if there are no more than 2 vertices, return 1
304 if(inc_current
>= inc_end
-1) return 1;
305 for(i
=inc_current
; i
<= inc_end
-2; i
++)
307 if(area(inc_chain
->getVertex(i
), inc_chain
->getVertex(i
+1), inc_chain
->getVertex(i
+2)) <0)
313 static int chainConcave(vertexArray
* dec_chain
, Int dec_current
, Int dec_end
)
316 //if there are no more than 2 vertices, return 1
317 if(dec_current
>= dec_end
-1) return 1;
318 for(i
=dec_current
; i
<=dec_end
-2; i
++)
320 if(area(dec_chain
->getVertex(i
), dec_chain
->getVertex(i
+1), dec_chain
->getVertex(i
+2)) >0)
326 void monoTriangulationRecGenInU(Real
* topVertex
, Real
* botVertex
,
327 vertexArray
* inc_chain
, Int inc_current
, Int inc_end
,
328 vertexArray
* dec_chain
, Int dec_current
, Int dec_end
,
334 void monoTriangulationRecGenOpt(Real
* topVertex
, Real
* botVertex
,
335 vertexArray
* inc_chain
, Int inc_current
, Int inc_end
,
336 vertexArray
* dec_chain
, Int dec_current
, Int dec_end
,
340 //copy this to a polygon: directedLine Lioop
345 if(inc_current
<= inc_end
) //at least one vertex in inc_chain
347 sline
= new sampledLine(topVertex
, inc_chain
->getVertex(inc_current
));
348 poly
= new directedLine(INCREASING
, sline
);
349 for(i
=inc_current
; i
<=inc_end
-1; i
++)
351 sline
= new sampledLine(inc_chain
->getVertex(i
), inc_chain
->getVertex(i
+1));
352 dline
= new directedLine(INCREASING
, sline
);
355 sline
= new sampledLine(inc_chain
->getVertex(inc_end
), botVertex
);
356 dline
= new directedLine(INCREASING
, sline
);
359 else //inc_chian is empty
361 sline
= new sampledLine(topVertex
, botVertex
);
362 dline
= new directedLine(INCREASING
, sline
);
366 assert(poly
!= NULL
);
368 if(dec_current
<= dec_end
) //at least on vertex in dec_Chain
370 sline
= new sampledLine(botVertex
, dec_chain
->getVertex(dec_end
));
371 dline
= new directedLine(INCREASING
, sline
);
373 for(i
=dec_end
; i
>dec_current
; i
--)
375 sline
= new sampledLine(dec_chain
->getVertex(i
), dec_chain
->getVertex(i
-1));
376 dline
= new directedLine(INCREASING
, sline
);
379 sline
= new sampledLine(dec_chain
->getVertex(dec_current
), topVertex
);
380 dline
= new directedLine(INCREASING
, sline
);
383 else //dec_chain is empty
385 sline
= new sampledLine(botVertex
, topVertex
);
386 dline
= new directedLine(INCREASING
, sline
);
392 Int n_edges
= poly
->numEdges();
393 directedLine
** cusps
= (directedLine
**) malloc(sizeof(directedLine
*)*n_edges
);
395 findInteriorCuspsX(poly
, n_cusps
, cusps
);
397 if(n_cusps
==0) //u monotine
399 monoTriangulationFun(poly
, compV2InX
, pStream
);
401 else if(n_cusps
== 1) // one interior cusp
403 directedLine
* new_polygon
= polygonConvert(cusps
[0]);
404 directedLine
* other
= findDiagonal_singleCuspX(new_polygon
);
405 //<other> should NOT be null unless there are self-intersecting
406 //trim curves. In that case, we don't want to core dump, instead,
407 //we triangulate anyway, and print out error message.
410 monoTriangulationFun(poly
, compV2InX
, pStream
);
414 directedLine
* ret_p1
;
415 directedLine
* ret_p2
;
417 new_polygon
->connectDiagonal_2slines(new_polygon
, other
,
422 monoTriangulationFun(ret_p1
, compV2InX
, pStream
);
423 monoTriangulationFun(ret_p2
, compV2InX
, pStream
);
425 ret_p1
->deleteSinglePolygonWithSline();
426 ret_p2
->deleteSinglePolygonWithSline();
431 //we need a general partitionX funtion (supposed to be in partitionX.C,
432 //not implemented yet. XXX
433 //monoTriangulationFun(poly, compV2InY, pStream);
435 directedLine
* new_polygon
= polygonConvert(poly
);
436 directedLine
* list
= monoPolyPart(new_polygon
);
437 for(directedLine
* temp
= list
; temp
!= NULL
; temp
= temp
->getNextPolygon())
439 monoTriangulationFun(temp
, compV2InX
, pStream
);
442 list
->deletePolygonListWithSline();
448 if(numInteriorCuspsX(poly) == 0) //is u monotone
449 monoTriangulationFun(poly, compV2InX, pStream);
450 else //it is not u motone
451 monoTriangulationFun(poly, compV2InY, pStream);
454 poly
->deleteSinglePolygonWithSline();
458 //apparently the following code is not reachable,
459 //it is for test purpose
460 if(inc_current
> inc_end
|| dec_current
>dec_end
)
462 monoTriangulationRecGen(topVertex
, botVertex
, inc_chain
, inc_current
, inc_end
,
463 dec_chain
, dec_current
, dec_end
,
470 area(dec_chain
->getVertex(dec_current
),
472 inc_chain
->getVertex(inc_current
)) >=0
473 && chainConvex(inc_chain
, inc_current
, inc_end
)
474 && chainConcave(dec_chain
, dec_current
, dec_end
)
475 && area(inc_chain
->getVertex(inc_end
), botVertex
, dec_chain
->getVertex(dec_end
)) >=0
478 monoTriangulationRecFunGen(topVertex
, botVertex
,
479 inc_chain
, inc_current
, inc_end
,
480 dec_chain
, dec_current
, dec_end
,
485 monoTriangulationRecGen(topVertex
, botVertex
, inc_chain
, inc_current
, inc_end
,
486 dec_chain
, dec_current
, dec_end
,
491 /*if inc_current>inc_end, then inc_chain has no points to be considered
494 void monoTriangulationRecGen(Real
* topVertex
, Real
* botVertex
,
495 vertexArray
* inc_chain
, Int inc_current
, Int inc_end
,
496 vertexArray
* dec_chain
, Int dec_current
, Int dec_end
,
503 if(inc_current
> inc_end
&& dec_current
>dec_end
)
505 else if(inc_current
>inc_end
) /*no more vertices on inc_chain*/
507 dec_array
= dec_chain
->getArray();
508 reflexChain
rChain(100,0);
509 /*put the top vertex into the reflex chain*/
510 rChain
.processNewVertex(topVertex
, pStream
);
511 /*process all the vertices on the dec_chain*/
512 for(i
=dec_current
; i
<=dec_end
; i
++){
513 rChain
.processNewVertex(dec_array
[i
], pStream
);
515 /*process the bottom vertex*/
516 rChain
.processNewVertex(botVertex
, pStream
);
518 else if(dec_current
> dec_end
) /*no more vertices on dec_chain*/
520 inc_array
= inc_chain
->getArray();
522 reflexChain
rChain(100,1);
523 /*put the top vertex into the reflex chain*/
524 rChain
.processNewVertex(topVertex
, pStream
);
525 /*process all the vertices on the inc_chain*/
526 for(i
=inc_current
; i
<=inc_end
; i
++){
527 rChain
.processNewVertex(inc_array
[i
], pStream
);
529 /*process the bottom vertex*/
530 rChain
.processNewVertex(botVertex
, pStream
);
532 else /*neither chain is empty*/
534 inc_array
= inc_chain
-> getArray();
535 dec_array
= dec_chain
-> getArray();
537 /*if top of inc_chain is 'lower' than top of dec_chain, process all the
538 *vertices on the dec_chain which are higher than top of inc_chain
540 if(compV2InY(inc_array
[inc_current
], dec_array
[dec_current
]) <= 0)
543 reflexChain
rChain(100, 0);
544 rChain
.processNewVertex(topVertex
, pStream
);
545 for(i
=dec_current
; i
<=dec_end
; i
++)
547 if(compV2InY(inc_array
[inc_current
], dec_array
[i
]) <= 0)
548 rChain
.processNewVertex(dec_array
[i
], pStream
);
552 rChain
.outputFan(inc_array
[inc_current
], pStream
);
553 monoTriangulationRecGen(dec_array
[i
-1], botVertex
,
554 inc_chain
, inc_current
, inc_end
,
555 dec_chain
, i
, dec_end
,
558 else /*compV2InY(inc_array[inc_current], dec_array[dec_current]) > 0*/
561 reflexChain
rChain(100, 1);
562 rChain
.processNewVertex(topVertex
, pStream
);
563 for(i
=inc_current
; i
<=inc_end
; i
++)
565 if(compV2InY(inc_array
[i
], dec_array
[dec_current
]) >0)
566 rChain
.processNewVertex(inc_array
[i
], pStream
);
570 rChain
.outputFan(dec_array
[dec_current
], pStream
);
571 monoTriangulationRecGen(inc_array
[i
-1], botVertex
,
572 inc_chain
, i
, inc_end
,
573 dec_chain
, dec_current
,dec_end
,
576 }/*end case neither is empty*/
579 void monoTriangulationFun(directedLine
* monoPolygon
, Int (*compFun
)(Real
*, Real
*), primStream
* pStream
)
582 /*find the top vertex, bottom vertex, inccreasing chain, and decreasing chain,
583 *then call monoTriangulationRec
588 topV
= botV
= monoPolygon
;
589 for(tempV
= monoPolygon
->getNext(); tempV
!= monoPolygon
; tempV
= tempV
->getNext())
591 if(compFun(topV
->head(), tempV
->head())<0) {
594 if(compFun(botV
->head(), tempV
->head())>0) {
599 /*creat increase and decrease chains*/
600 vertexArray
inc_chain(20); /*this is a dynamic array*/
601 for(i
=1; i
<=topV
->get_npoints()-2; i
++) { /*the first vertex is the top vertex which doesn't belong to inc_chain*/
602 inc_chain
.appendVertex(topV
->getVertex(i
));
604 for(tempV
= topV
->getNext(); tempV
!= botV
; tempV
= tempV
->getNext())
606 for(i
=0; i
<=tempV
->get_npoints()-2; i
++){
607 inc_chain
.appendVertex(tempV
->getVertex(i
));
611 vertexArray
dec_chain(20);
612 for(tempV
= topV
->getPrev(); tempV
!= botV
; tempV
= tempV
->getPrev())
614 for(i
=tempV
->get_npoints()-2; i
>=0; i
--){
615 dec_chain
.appendVertex(tempV
->getVertex(i
));
618 for(i
=botV
->get_npoints()-2; i
>=1; i
--){
619 dec_chain
.appendVertex(tempV
->getVertex(i
));
622 monoTriangulationRecFun(topV
->head(), botV
->head(), &inc_chain
, 0, &dec_chain
, 0, compFun
, pStream
);
626 void monoTriangulation(directedLine
* monoPolygon
, primStream
* pStream
)
629 /*find the top vertex, bottom vertex, inccreasing chain, and decreasing chain,
630 *then call monoTriangulationRec
635 topV
= botV
= monoPolygon
;
636 for(tempV
= monoPolygon
->getNext(); tempV
!= monoPolygon
; tempV
= tempV
->getNext())
638 if(compV2InY(topV
->head(), tempV
->head())<0) {
641 if(compV2InY(botV
->head(), tempV
->head())>0) {
645 /*creat increase and decrease chains*/
646 vertexArray
inc_chain(20); /*this is a dynamic array*/
647 for(i
=1; i
<=topV
->get_npoints()-2; i
++) { /*the first vertex is the top vertex which doesn't belong to inc_chain*/
648 inc_chain
.appendVertex(topV
->getVertex(i
));
650 for(tempV
= topV
->getNext(); tempV
!= botV
; tempV
= tempV
->getNext())
652 for(i
=0; i
<=tempV
->get_npoints()-2; i
++){
653 inc_chain
.appendVertex(tempV
->getVertex(i
));
657 vertexArray
dec_chain(20);
658 for(tempV
= topV
->getPrev(); tempV
!= botV
; tempV
= tempV
->getPrev())
660 for(i
=tempV
->get_npoints()-2; i
>=0; i
--){
661 dec_chain
.appendVertex(tempV
->getVertex(i
));
664 for(i
=botV
->get_npoints()-2; i
>=1; i
--){
665 dec_chain
.appendVertex(tempV
->getVertex(i
));
668 monoTriangulationRec(topV
->head(), botV
->head(), &inc_chain
, 0, &dec_chain
, 0, pStream
);
672 /*the chain could be increasing or decreasing, although we use the
674 *the argument is_increase_chain indicates whether this chain
675 *is increasing (left chain in V-monotone case) or decreaing (right chain
676 *in V-monotone case).
678 void monoTriangulation2(Real
* topVertex
, Real
* botVertex
,
679 vertexArray
* inc_chain
, Int inc_smallIndex
,
681 Int is_increase_chain
,
684 assert( inc_chain
!= NULL
);
687 if(inc_smallIndex
> inc_largeIndex
)
688 return; //no triangles
689 if(inc_smallIndex
== inc_largeIndex
)
691 if(is_increase_chain
)
692 pStream
->triangle(inc_chain
->getVertex(inc_smallIndex
), botVertex
, topVertex
);
694 pStream
->triangle(inc_chain
->getVertex(inc_smallIndex
), topVertex
, botVertex
);
699 if(is_increase_chain
&& botVertex
[1] == inc_chain
->getVertex(inc_largeIndex
)[1])
701 pStream
->triangle(botVertex
, inc_chain
->getVertex(inc_largeIndex
-1),
702 inc_chain
->getVertex(inc_largeIndex
));
703 monoTriangulation2(topVertex
, botVertex
, inc_chain
, inc_smallIndex
,
709 else if( (!is_increase_chain
) && topVertex
[1] == inc_chain
->getVertex(inc_smallIndex
)[1])
711 pStream
->triangle(topVertex
, inc_chain
->getVertex(inc_smallIndex
+1),
712 inc_chain
->getVertex(inc_smallIndex
));
713 monoTriangulation2(topVertex
, botVertex
, inc_chain
, inc_smallIndex
+1,
714 inc_largeIndex
, is_increase_chain
, pStream
);
718 inc_array
= inc_chain
->getArray();
720 reflexChain
rChain(20,is_increase_chain
); /*1 means the chain is increasing*/
722 rChain
.processNewVertex(topVertex
, pStream
);
724 for(i
=inc_smallIndex
; i
<=inc_largeIndex
; i
++){
725 rChain
.processNewVertex(inc_array
[i
], pStream
);
727 rChain
.processNewVertex(botVertex
, pStream
);
731 /*if compFun == compV2InY, top to bottom: V-monotone
732 *if compFun == compV2InX, right to left: U-monotone
734 void monoTriangulationRecFunGen(Real
* topVertex
, Real
* botVertex
,
735 vertexArray
* inc_chain
, Int inc_current
, Int inc_end
,
736 vertexArray
* dec_chain
, Int dec_current
, Int dec_end
,
737 Int (*compFun
)(Real
*, Real
*),
740 assert( inc_chain
!= NULL
&& dec_chain
!= NULL
);
741 assert( ! (inc_current
> inc_end
&&
742 dec_current
> dec_end
));
748 assert( ! ( (inc_chain
==NULL
) && (dec_chain
==NULL
)));
750 if(inc_current
> inc_end
) /*no more vertices on inc_chain*/
753 dec_array
= dec_chain
->getArray();
754 reflexChain
rChain(20,0);
755 /*put the top vertex into the reflex chain*/
756 rChain
.processNewVertex(topVertex
, pStream
);
757 /*process all the vertices on the dec_chain*/
758 for(i
=dec_current
; i
<=dec_end
; i
++){
759 rChain
.processNewVertex(dec_array
[i
], pStream
);
761 /*process the bottom vertex*/
762 rChain
.processNewVertex(botVertex
, pStream
);
765 else if(dec_current
> dec_end
) /*no more vertices on dec_chain*/
767 inc_array
= inc_chain
->getArray();
768 reflexChain
rChain(20,1);
769 /*put the top vertex into the reflex chain*/
770 rChain
.processNewVertex(topVertex
, pStream
);
771 /*process all the vertices on the inc_chain*/
772 for(i
=inc_current
; i
<=inc_end
; i
++){
773 rChain
.processNewVertex(inc_array
[i
], pStream
);
775 /*process the bottom vertex*/
776 rChain
.processNewVertex(botVertex
, pStream
);
778 else /*neither chain is empty*/
780 inc_array
= inc_chain
-> getArray();
781 dec_array
= dec_chain
-> getArray();
783 /*if top of inc_chain is 'lower' than top of dec_chain, process all the
784 *vertices on the dec_chain which are higher than top of inc_chain
786 if(compFun(inc_array
[inc_current
], dec_array
[dec_current
]) <= 0)
789 reflexChain
rChain(20, 0);
790 rChain
.processNewVertex(topVertex
, pStream
);
791 for(i
=dec_current
; i
<=dec_end
; i
++)
793 if(compFun(inc_array
[inc_current
], dec_array
[i
]) <= 0)
794 rChain
.processNewVertex(dec_array
[i
], pStream
);
798 rChain
.outputFan(inc_array
[inc_current
], pStream
);
799 monoTriangulationRecFunGen(dec_array
[i
-1], botVertex
,
800 inc_chain
, inc_current
, inc_end
,
801 dec_chain
, i
, dec_end
,
805 else /*compFun(inc_array[inc_current], dec_array[dec_current]) > 0*/
808 reflexChain
rChain(20, 1);
809 rChain
.processNewVertex(topVertex
, pStream
);
810 for(i
=inc_current
; i
<=inc_end
; i
++)
812 if(compFun(inc_array
[i
], dec_array
[dec_current
]) >0)
813 rChain
.processNewVertex(inc_array
[i
], pStream
);
817 rChain
.outputFan(dec_array
[dec_current
], pStream
);
818 monoTriangulationRecFunGen(inc_array
[i
-1], botVertex
,
819 inc_chain
, i
,inc_end
,
820 dec_chain
, dec_current
,dec_end
,
824 }/*end case neither is empty*/
827 /*if compFun == compV2InY, top to bottom: V-monotone
828 *if compFun == compV2InX, right to left: U-monotone
830 void monoTriangulationRecFun(Real
* topVertex
, Real
* botVertex
,
831 vertexArray
* inc_chain
, Int inc_current
,
832 vertexArray
* dec_chain
, Int dec_current
,
833 Int (*compFun
)(Real
*, Real
*),
836 assert( inc_chain
!= NULL
&& dec_chain
!= NULL
);
837 assert( ! (inc_current
>=inc_chain
->getNumElements() &&
838 dec_current
>=dec_chain
->getNumElements()));
844 assert( ! ( (inc_chain
==NULL
) && (dec_chain
==NULL
)));
846 if(inc_current
>=inc_chain
->getNumElements()) /*no more vertices on inc_chain*/
849 dec_array
= dec_chain
->getArray();
850 dec_nVertices
= dec_chain
->getNumElements();
851 reflexChain
rChain(20,0);
852 /*put the top vertex into the reflex chain*/
853 rChain
.processNewVertex(topVertex
, pStream
);
854 /*process all the vertices on the dec_chain*/
855 for(i
=dec_current
; i
<dec_nVertices
; i
++){
856 rChain
.processNewVertex(dec_array
[i
], pStream
);
858 /*process the bottom vertex*/
859 rChain
.processNewVertex(botVertex
, pStream
);
862 else if(dec_current
>= dec_chain
->getNumElements()) /*no more vertices on dec_chain*/
864 inc_array
= inc_chain
->getArray();
865 inc_nVertices
= inc_chain
->getNumElements();
866 reflexChain
rChain(20,1);
867 /*put the top vertex into the reflex chain*/
868 rChain
.processNewVertex(topVertex
, pStream
);
869 /*process all the vertices on the inc_chain*/
870 for(i
=inc_current
; i
<inc_nVertices
; i
++){
871 rChain
.processNewVertex(inc_array
[i
], pStream
);
873 /*process the bottom vertex*/
874 rChain
.processNewVertex(botVertex
, pStream
);
876 else /*neither chain is empty*/
878 inc_array
= inc_chain
-> getArray();
879 dec_array
= dec_chain
-> getArray();
880 inc_nVertices
= inc_chain
->getNumElements();
881 dec_nVertices
= dec_chain
->getNumElements();
882 /*if top of inc_chain is 'lower' than top of dec_chain, process all the
883 *vertices on the dec_chain which are higher than top of inc_chain
885 if(compFun(inc_array
[inc_current
], dec_array
[dec_current
]) <= 0)
888 reflexChain
rChain(20, 0);
889 rChain
.processNewVertex(topVertex
, pStream
);
890 for(i
=dec_current
; i
<dec_nVertices
; i
++)
892 if(compFun(inc_array
[inc_current
], dec_array
[i
]) <= 0)
893 rChain
.processNewVertex(dec_array
[i
], pStream
);
897 rChain
.outputFan(inc_array
[inc_current
], pStream
);
898 monoTriangulationRecFun(dec_array
[i
-1], botVertex
,
899 inc_chain
, inc_current
,
904 else /*compFun(inc_array[inc_current], dec_array[dec_current]) > 0*/
907 reflexChain
rChain(20, 1);
908 rChain
.processNewVertex(topVertex
, pStream
);
909 for(i
=inc_current
; i
<inc_nVertices
; i
++)
911 if(compFun(inc_array
[i
], dec_array
[dec_current
]) >0)
912 rChain
.processNewVertex(inc_array
[i
], pStream
);
916 rChain
.outputFan(dec_array
[dec_current
], pStream
);
917 monoTriangulationRecFun(inc_array
[i
-1], botVertex
,
919 dec_chain
, dec_current
,
923 }/*end case neither is empty*/
927 void monoTriangulationRec(Real
* topVertex
, Real
* botVertex
,
928 vertexArray
* inc_chain
, Int inc_current
,
929 vertexArray
* dec_chain
, Int dec_current
,
932 assert( inc_chain
!= NULL
&& dec_chain
!= NULL
);
933 assert( ! (inc_current
>=inc_chain
->getNumElements() &&
934 dec_current
>=dec_chain
->getNumElements()));
940 assert( ! ( (inc_chain
==NULL
) && (dec_chain
==NULL
)));
942 if(inc_current
>=inc_chain
->getNumElements()) /*no more vertices on inc_chain*/
945 dec_array
= dec_chain
->getArray();
946 dec_nVertices
= dec_chain
->getNumElements();
947 reflexChain
rChain(20,0);
948 /*put the top vertex into the reflex chain*/
949 rChain
.processNewVertex(topVertex
, pStream
);
950 /*process all the vertices on the dec_chain*/
951 for(i
=dec_current
; i
<dec_nVertices
; i
++){
952 rChain
.processNewVertex(dec_array
[i
], pStream
);
954 /*process the bottom vertex*/
955 rChain
.processNewVertex(botVertex
, pStream
);
958 else if(dec_current
>= dec_chain
->getNumElements()) /*no more vertices on dec_chain*/
960 inc_array
= inc_chain
->getArray();
961 inc_nVertices
= inc_chain
->getNumElements();
962 reflexChain
rChain(20,1);
963 /*put the top vertex into the reflex chain*/
964 rChain
.processNewVertex(topVertex
, pStream
);
965 /*process all the vertices on the inc_chain*/
966 for(i
=inc_current
; i
<inc_nVertices
; i
++){
967 rChain
.processNewVertex(inc_array
[i
], pStream
);
969 /*process the bottom vertex*/
970 rChain
.processNewVertex(botVertex
, pStream
);
972 else /*neither chain is empty*/
974 inc_array
= inc_chain
-> getArray();
975 dec_array
= dec_chain
-> getArray();
976 inc_nVertices
= inc_chain
->getNumElements();
977 dec_nVertices
= dec_chain
->getNumElements();
978 /*if top of inc_chain is 'lower' than top of dec_chain, process all the
979 *vertices on the dec_chain which are higher than top of inc_chain
981 if(compV2InY(inc_array
[inc_current
], dec_array
[dec_current
]) <= 0)
984 reflexChain
rChain(20, 0);
985 rChain
.processNewVertex(topVertex
, pStream
);
986 for(i
=dec_current
; i
<dec_nVertices
; i
++)
988 if(compV2InY(inc_array
[inc_current
], dec_array
[i
]) <= 0)
989 rChain
.processNewVertex(dec_array
[i
], pStream
);
993 rChain
.outputFan(inc_array
[inc_current
], pStream
);
994 monoTriangulationRec(dec_array
[i
-1], botVertex
,
995 inc_chain
, inc_current
,
999 else /*compV2InY(inc_array[inc_current], dec_array[dec_current]) > 0*/
1002 reflexChain
rChain(20, 1);
1003 rChain
.processNewVertex(topVertex
, pStream
);
1004 for(i
=inc_current
; i
<inc_nVertices
; i
++)
1006 if(compV2InY(inc_array
[i
], dec_array
[dec_current
]) >0)
1007 rChain
.processNewVertex(inc_array
[i
], pStream
);
1011 rChain
.outputFan(dec_array
[dec_current
], pStream
);
1012 monoTriangulationRec(inc_array
[i
-1], botVertex
,
1014 dec_chain
, dec_current
,
1017 }/*end case neither is empty*/
1022 /* the name here assumes that the polygon is Y-monotone, but
1023 *this function also works for X-monotone polygons.
1024 * a monotne polygon consists of two extrem verteices: topVertex and botVertex, and
1025 *two monotone chains: inc_chain, and dec_chain. The edges of the increasing chain (inc_chain)
1026 *is ordered by following pointer: next, while the edges of the decreasing chain (dec_chain)
1027 *is ordered by following pointer: prev
1028 * inc_index index the vertex which is the toppest of the inc_chain which we are handling currently.
1029 * dec_index index the vertex which is the toppest of the dec_chain which we are handling currently.
1031 void monoTriangulationRec(directedLine
* inc_chain
, Int inc_index
,
1032 directedLine
* dec_chain
, Int dec_index
,
1033 directedLine
* topVertex
, Int top_index
,
1034 directedLine
* botVertex
,
1035 primStream
* pStream
)
1038 directedLine
*temp
, *oldtemp
;
1039 Int tempIndex
, oldtempIndex
;
1041 assert(inc_chain
!= NULL
&& dec_chain
!= NULL
);
1043 if(inc_chain
== botVertex
) {
1044 reflexChain
rChain(20, 0);
1045 rChain
.processNewVertex(topVertex
->getVertex(top_index
), pStream
);
1046 for(i
=dec_index
; i
< dec_chain
->get_npoints(); i
++){
1047 rChain
.processNewVertex(dec_chain
->getVertex(i
), pStream
);
1049 for(temp
= dec_chain
->getPrev(); temp
!= botVertex
; temp
= temp
->getPrev())
1051 for(i
=0; i
<temp
->get_npoints(); i
++){
1052 rChain
.processNewVertex(temp
->getVertex(i
), pStream
);
1056 else if(dec_chain
==botVertex
) {
1057 reflexChain
rChain(20, 1);
1058 rChain
.processNewVertex(topVertex
->getVertex(top_index
), pStream
);
1059 for(i
=inc_index
; i
< inc_chain
->get_npoints(); i
++){
1060 rChain
.processNewVertex(inc_chain
->getVertex(i
), pStream
);
1062 for(temp
= inc_chain
->getPrev(); temp
!= botVertex
; temp
= temp
->getNext())
1064 for(i
=0; i
<temp
->get_npoints(); i
++){
1065 rChain
.processNewVertex(temp
->getVertex(i
), pStream
);
1069 else /*neither reached the bottom*/{
1070 if(compV2InY(inc_chain
->getVertex(inc_index
), dec_chain
->getVertex(dec_index
)) <=0) {
1071 reflexChain
rChain(20, 0);
1072 rChain
.processNewVertex(topVertex
-> getVertex(top_index
), pStream
);
1074 tempIndex
= dec_index
;
1075 while( compV2InY(inc_chain
->getVertex(inc_index
), temp
->getVertex(tempIndex
))<=0) {
1077 oldtempIndex
= tempIndex
;
1078 rChain
.processNewVertex(temp
->getVertex(tempIndex
), pStream
);
1080 if(tempIndex
== temp
->get_npoints()-1){
1082 temp
= temp
->getPrev();
1088 rChain
.outputFan(inc_chain
->getVertex(inc_index
), pStream
);
1089 monoTriangulationRec(inc_chain
, inc_index
, temp
, tempIndex
, oldtemp
, oldtempIndex
, botVertex
, pStream
);
1092 reflexChain
rChain(20, 1);
1093 rChain
.processNewVertex(topVertex
-> getVertex(top_index
), pStream
);
1095 tempIndex
= inc_index
;
1096 while( compV2InY(temp
->getVertex(tempIndex
), dec_chain
->getVertex(dec_index
))>0){
1098 oldtempIndex
= tempIndex
;
1099 rChain
.processNewVertex(temp
->getVertex(tempIndex
), pStream
);
1101 if(tempIndex
== temp
->get_npoints()-1){
1103 temp
= temp
->getNext();
1109 rChain
.outputFan(dec_chain
->getVertex(dec_index
), pStream
);
1110 monoTriangulationRec(temp
, tempIndex
, dec_chain
, dec_index
, oldtemp
, oldtempIndex
, botVertex
, pStream
);
1112 } /*end case neither reached the bottom*/
1115 /***************************vertexArray begin here**********************************/
1116 vertexArray::vertexArray(Real2
* vertices
, Int nVertices
)
1119 size
= index
= nVertices
;
1120 array
= (Real
**) malloc(sizeof(Real
*) * nVertices
);
1122 for(i
=0; i
<nVertices
; i
++)
1124 array
[i
] = vertices
[i
];
1125 array
[i
] = vertices
[i
];
1129 vertexArray::vertexArray(Int s
)
1132 array
= (Real
**) malloc(sizeof(Real
*) * s
);
1137 vertexArray::~vertexArray()
1142 void vertexArray::appendVertex(Real
* ptr
)
1146 Real
** temp
= (Real
**) malloc(sizeof(Real
*) * (2*size
+1));
1148 for(i
=0; i
<index
; i
++)
1154 array
[index
++] = ptr
;
1157 void vertexArray::print()
1159 printf("vertex Array:index=%i, size=%i\n", index
, size
);
1160 for(Int i
=0; i
<index
; i
++)
1162 printf("(%f,%f) ", array
[i
][0], array
[i
][1]);
1167 /*find the first i such that array[i][1] >= v
1168 * and array[i+1][1] <v
1169 * if index == 0 (the array is empty, return -1.
1170 * if v is above all, return -1.
1171 * if v is below all, return index-1.
1173 Int
vertexArray::findIndexAbove(Real v
)
1178 else if(array
[0][1] < v
)
1182 for(i
=1; i
<index
; i
++)
1191 /*find the first i<=endIndex such that array[i][1] <= v
1192 * and array[i-1][1] > v
1193 *if sartIndex>endIndex, then return endIndex+1.
1194 *otherwise, startIndex<=endIndex, it is assumed that
1195 * 0<=startIndex<=endIndex<index.
1196 * if v is below all, return endIndex+1
1197 * if v is above all, return startIndex.
1199 Int
vertexArray::findIndexBelowGen(Real v
, Int startIndex
, Int endIndex
)
1202 if(startIndex
> endIndex
)
1204 else if(array
[endIndex
][1] > v
)
1206 else //now array[endIndex][1] <= v
1208 for(i
=endIndex
-1; i
>=startIndex
; i
--)
1217 /*find the first i<=endIndex such that array[i-1][1] >= v
1218 * and array[i][1] < v
1219 *if sartIndex>endIndex, then return endIndex+1.
1220 *otherwise, startIndex<=endIndex, it is assumed that
1221 * 0<=startIndex<=endIndex<index.
1222 * if v is below or equal to all, return endIndex+1
1223 * if v is strictly above all, return startIndex.
1225 Int
vertexArray::findIndexStrictBelowGen(Real v
, Int startIndex
, Int endIndex
)
1228 if(startIndex
> endIndex
)
1230 else if(array
[endIndex
][1] >= v
)
1232 else //now array[endIndex][1] < v
1234 for(i
=endIndex
-1; i
>=startIndex
; i
--)
1236 if(array
[i
][1] >= v
)
1243 /*find the first i>startIndex such that array[i-1][1] > v
1244 * and array[i][1] >=v
1245 *if sartIndex>endIndex, then return startIndex-1.
1246 *otherwise, startIndex<=endIndex, it is assumed that
1247 * 0<=startIndex<=endIndex<index.
1248 * if v is strictly above all, return startIndex-1
1249 * if v is strictly below all, return endIndex.
1251 Int
vertexArray::findIndexFirstAboveEqualGen(Real v
, Int startIndex
, Int endIndex
)
1255 if(startIndex
> endIndex
)
1256 return startIndex
-1;
1257 else if(array
[startIndex
][1] < v
)
1258 return startIndex
-1;
1259 else //now array[startIndex][1] >= v
1262 for(i
=startIndex
; i
<=endIndex
; i
++)
1264 if(array
[i
][1] <= v
)
1267 if(i
>endIndex
) // v is strictly below all
1269 else if(array
[i
][1] == v
)
1278 /*find the first i>=startIndex such that array[i][1] >= v
1279 * and array[i+1][1] <v
1280 *if sartIndex>endIndex, then return startIndex-1.
1281 *otherwise, startIndex<=endIndex, it is assumed that
1282 * 0<=startIndex<=endIndex<index.
1283 * if v is above all, return startIndex-1
1284 * if v is below all, return endIndex.
1286 Int
vertexArray::findIndexAboveGen(Real v
, Int startIndex
, Int endIndex
)
1289 if(startIndex
> endIndex
)
1290 return startIndex
-1;
1291 else if(array
[startIndex
][1] < v
)
1292 return startIndex
-1;
1293 else //now array[startIndex][1] >= v
1295 for(i
=startIndex
+1; i
<=endIndex
; i
++)
1304 Int
vertexArray::findDecreaseChainFromEnd(Int begin
, Int end
)
1307 Real prevU
= array
[i
][0];
1309 for(i
=end
-1; i
>=begin
; i
--){
1310 thisU
= array
[i
][0];
1319 //if(V(start) == v, return start, other wise return the
1320 //last i so that V(i)==v
1321 Int
vertexArray::skipEqualityFromStart(Real v
, Int start
, Int end
)
1324 if(array
[start
][1] != v
)
1326 //now array[start][1] == v
1327 for(i
=start
+1; i
<= end
; i
++)
1328 if(array
[i
][1] != v
)
1334 /***************************vertexArray end****************************************/
1338 /***************************relfex chain stuff begin here*****************************/
1340 reflexChain::reflexChain(Int size
, Int is_increasing
)
1342 queue
= (Real2
*) malloc(sizeof(Real2
) * size
);
1346 isIncreasing
= is_increasing
;
1349 reflexChain::~reflexChain()
1354 /*put (u,v) at the end of the queue
1355 *pay attention to space
1357 void reflexChain::insert(Real u
, Real v
)
1360 if(index_queue
>= size_queue
) {
1361 Real2
*temp
= (Real2
*) malloc(sizeof(Real2
) * (2*size_queue
+1));
1365 for(i
=0; i
<index_queue
; i
++){
1366 temp
[i
][0] = queue
[i
][0];
1367 temp
[i
][1] = queue
[i
][1];
1372 size_queue
= 2*size_queue
+ 1;
1375 queue
[index_queue
][0] = u
;
1376 queue
[index_queue
][1] = v
;
1380 void reflexChain::insert(Real v
[2])
1386 static Real area(Real A[2], Real B[2], Real C[2])
1388 Real Bx, By, Cx, Cy;
1393 return Bx*Cy - Cx*By;
1397 /*the chain is reflex, and the vertex v is
1398 *on the other side of the chain, so that
1399 *we can outout the fan with v as the
1402 void reflexChain::outputFan(Real v
[2], primStream
* pStream
)
1408 for(i
=0; i
<index_queue
; i
++)
1409 pStream
->insert(queue
[i
]);
1412 for(i
=index_queue
-1; i
>=0; i
--)
1413 pStream
->insert(queue
[i
]);
1415 pStream
->end(PRIMITIVE_STREAM_FAN
);
1418 void reflexChain::processNewVertex(Real v
[2], primStream
* pStream
)
1422 /*if there are at most one vertex in the queue, then simply insert
1424 if(index_queue
<=1){
1429 /*there are at least two vertices in the queue*/
1432 for(i
=j
; i
>=1; i
--) {
1434 isReflex
= (area(queue
[i
-1], queue
[i
], v
) <= 0.0);
1436 else /*decreasing*/{
1437 isReflex
= (area(v
, queue
[i
], queue
[i
-1]) <= 0.0);
1445 *if i<j then vertices: i+1--j are convex
1446 * output triangle fan:
1447 * v, and queue[i], i+1, ..., j
1455 pStream
->insert(queue
[k
]);
1459 pStream
->insert(queue
[k
]);
1462 pStream
->end(PRIMITIVE_STREAM_FAN
);
1465 /*delete vertices i+1--j from the queue*/
1467 /*finally insert v at the end of the queue*/
1472 void reflexChain::print()
1475 printf("reflex chain: isIncreasing=%i\n", isIncreasing
);
1476 for(i
=0; i
<index_queue
; i
++) {
1477 printf("(%f,%f) ", queue
[i
][0], queue
[i
][1]);