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.
40 #include "glimports.h"
43 #include "gridvertex.h"
44 #include "gridtrimvertex.h"
53 const float Mesher::ZERO
= 0.0;
55 Mesher::Mesher( Backend
& b
)
57 p( sizeof( GridTrimVertex
), 100, "GridTrimVertexPool" )
61 lastedge
= 0; //needed to prevent purify UMR
64 Mesher::~Mesher( void )
66 if( vdata
) delete[] vdata
;
70 Mesher::init( unsigned int npts
)
73 if( stacksize
< npts
) {
75 if( vdata
) delete[] vdata
;
76 vdata
= new GridTrimVertex_p
[stacksize
];
81 Mesher::push( GridTrimVertex
*gt
)
83 assert( itop
+1 != (int)stacksize
);
95 backend
.bgntmesh( "addedge" );
118 Mesher::finishLower( GridTrimVertex
*gtlower
)
121 nextlower( gtlower
=new(p
) GridTrimVertex
);
128 Mesher::finishUpper( GridTrimVertex
*gtupper
)
131 nextupper( gtupper
=new(p
) GridTrimVertex
);
140 GridTrimVertex
*gtlower
, *gtupper
;
143 nextupper( gtupper
= new(p
) GridTrimVertex
);
144 nextlower( gtlower
= new(p
) GridTrimVertex
);
150 nextupper( gtupper
= new(p
) GridTrimVertex
);
151 nextlower( gtlower
);
153 assert( gtupper
->t
&& gtlower
->t
);
155 if( gtupper
->t
->param
[0] < gtlower
->t
->param
[0] ) {
158 if( nextupper( gtupper
=new(p
) GridTrimVertex
) == 0 ) {
159 finishLower(gtlower
);
162 } else if( gtupper
->t
->param
[0] > gtlower
->t
->param
[0] ) {
165 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
166 finishUpper(gtupper
);
170 if( lastedge
== 0 ) {
173 if( nextupper(gtupper
=new(p
) GridTrimVertex
) == 0 ) {
174 finishLower(gtlower
);
180 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
181 finishUpper(gtupper
);
188 if( gtupper
->t
->param
[0] < gtlower
->t
->param
[0] ) {
191 if( nextupper( gtupper
=new(p
) GridTrimVertex
) == 0 ) {
192 finishLower(gtlower
);
195 } else if( gtupper
->t
->param
[0] > gtlower
->t
->param
[0] ) {
198 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
199 finishUpper(gtupper
);
203 if( lastedge
== 0 ) {
206 if( nextupper( gtupper
=new(p
) GridTrimVertex
) == 0 ) {
207 finishLower(gtlower
);
213 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
214 finishUpper(gtupper
);
223 Mesher::isCcw( int ilast
)
225 REAL area
= det3( vdata
[ilast
]->t
, vdata
[itop
-1]->t
, vdata
[itop
-2]->t
);
226 return (area
< ZERO
) ? 0 : 1;
230 Mesher::isCw( int ilast
)
232 REAL area
= det3( vdata
[ilast
]->t
, vdata
[itop
-1]->t
, vdata
[itop
-2]->t
);
233 return (area
> -ZERO
) ? 0 : 1;
237 Mesher::equal( int x
, int y
)
239 return( last
[0] == vdata
[x
] && last
[1] == vdata
[y
] );
243 Mesher::copy( int x
, int y
)
245 last
[0] = vdata
[x
]; last
[1] = vdata
[y
];
249 Mesher::move( int x
, int y
)
255 Mesher::output( int x
)
257 backend
.tmeshvert( vdata
[x
] );
260 /*---------------------------------------------------------------------------
261 * addedge - addedge an edge to the triangulation
263 * This code has been re-written to generate large triangle meshes
264 * from a monotone polygon. Although smaller triangle meshes
265 * could be generated faster and with less code, larger meshes
266 * actually give better SYSTEM performance. This is because
267 * vertices are processed in the backend slower than they are
268 * generated by this code and any decrease in the number of vertices
269 * results in a decrease in the time spent in the backend.
270 *---------------------------------------------------------------------------
276 register int ilast
= itop
;
278 if( lastedge
== 0 ) {
279 if( equal( 0, 1 ) ) {
282 for( register int i
= 2; i
< ilast
; i
++ ) {
286 copy( ilast
, ilast
-1 );
287 } else if( equal( ilast
-2, ilast
-1) ) {
290 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
296 closeMesh(); openMesh();
299 for( register int i
= 1; i
< ilast
; i
++ ) {
303 copy( ilast
, ilast
-1 );
309 for( register int i
= 2; i
< ilast
; i
++ ) {
313 copy( ilast
-1, ilast
);
314 } else if( equal( ilast
-1, ilast
-2) ) {
317 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
323 closeMesh(); openMesh();
326 for( register int i
= 1; i
< ilast
; i
++ ) {
330 copy( ilast
-1, ilast
);
334 //for( register long k=0; k<=ilast; k++ ) pop( k );
340 register int ilast
= itop
;
342 if( lastedge
== 0 ) {
343 if( equal( 0, 1 ) ) {
346 for( register int i
= 2; i
< ilast
; i
++ ) {
350 copy( ilast
, ilast
-1 );
351 } else if( equal( ilast
-2, ilast
-1) ) {
354 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
360 closeMesh(); openMesh();
363 for( register int i
= 1; i
< ilast
; i
++ ) {
367 copy( ilast
, ilast
-1 );
370 //for( register long k=0; k<ilast-1; k++ ) pop( k );
375 if( ! isCcw( ilast
) ) return;
378 } while( (itop
> 1) && isCcw( ilast
) );
380 if( equal( ilast
-1, ilast
-2 ) ) {
383 for( register int i
=ilast
-3; i
>=itop
-1; i
-- ) {
387 copy( ilast
, itop
-1 );
388 } else if( equal( itop
, itop
-1 ) ) {
391 for( register int i
= itop
+1; i
< ilast
; i
++ ) {
395 copy( ilast
-1, ilast
);
397 closeMesh(); openMesh();
400 for( register int i
=ilast
-2; i
>=itop
-1; i
-- ) {
404 copy( ilast
, itop
-1 );
406 //for( register int k=itop; k<ilast; k++ ) pop( k );
414 register int ilast
= itop
;
416 if( lastedge
== 1 ) {
420 for( register int i
= 2; i
< ilast
; i
++ ) {
424 copy( ilast
-1, ilast
);
425 } else if( equal( ilast
-1, ilast
-2) ) {
428 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
434 closeMesh(); openMesh();
437 for( register int i
= 1; i
< ilast
; i
++ ) {
441 copy( ilast
-1, ilast
);
445 //for( register long k=0; k<ilast-1; k++ ) pop( k );
450 if( ! isCw( ilast
) ) return;
453 } while( (itop
> 1) && isCw( ilast
) );
455 if( equal( ilast
-2, ilast
-1) ) {
458 for( register int i
=ilast
-3; i
>=itop
-1; i
--) {
462 copy( itop
-1, ilast
);
463 } else if( equal( itop
-1, itop
) ) {
466 for( register int i
=itop
+1; i
<ilast
; i
++ ) {
470 copy( ilast
, ilast
-1 );
472 closeMesh(); openMesh();
475 for( register int i
=ilast
-2; i
>=itop
-1; i
-- ) {
479 copy( itop
-1, ilast
);
481 //for( register int k=itop; k<ilast; k++ ) pop( k );