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" )
64 lastedge
= 0; //needed to prevent purify UMR
67 Mesher::~Mesher( void )
69 if( vdata
) delete[] vdata
;
73 Mesher::init( unsigned int npts
)
76 if( stacksize
< npts
) {
78 if( vdata
) delete[] vdata
;
79 vdata
= new GridTrimVertex_p
[stacksize
];
84 Mesher::push( GridTrimVertex
*gt
)
86 assert( itop
+1 != (int)stacksize
);
98 backend
.bgntmesh( "addedge" );
121 Mesher::finishLower( GridTrimVertex
*gtlower
)
124 nextlower( gtlower
=new(p
) GridTrimVertex
);
131 Mesher::finishUpper( GridTrimVertex
*gtupper
)
134 nextupper( gtupper
=new(p
) GridTrimVertex
);
143 GridTrimVertex
*gtlower
, *gtupper
;
146 nextupper( gtupper
= new(p
) GridTrimVertex
);
147 nextlower( gtlower
= new(p
) GridTrimVertex
);
153 nextupper( gtupper
= new(p
) GridTrimVertex
);
154 nextlower( gtlower
);
156 assert( gtupper
->t
&& gtlower
->t
);
158 if( gtupper
->t
->param
[0] < gtlower
->t
->param
[0] ) {
161 if( nextupper( gtupper
=new(p
) GridTrimVertex
) == 0 ) {
162 finishLower(gtlower
);
165 } else if( gtupper
->t
->param
[0] > gtlower
->t
->param
[0] ) {
168 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
169 finishUpper(gtupper
);
173 if( lastedge
== 0 ) {
176 if( nextupper(gtupper
=new(p
) GridTrimVertex
) == 0 ) {
177 finishLower(gtlower
);
183 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
184 finishUpper(gtupper
);
191 if( gtupper
->t
->param
[0] < gtlower
->t
->param
[0] ) {
194 if( nextupper( gtupper
=new(p
) GridTrimVertex
) == 0 ) {
195 finishLower(gtlower
);
198 } else if( gtupper
->t
->param
[0] > gtlower
->t
->param
[0] ) {
201 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
202 finishUpper(gtupper
);
206 if( lastedge
== 0 ) {
209 if( nextupper( gtupper
=new(p
) GridTrimVertex
) == 0 ) {
210 finishLower(gtlower
);
216 if( nextlower( gtlower
=new(p
) GridTrimVertex
) == 0 ) {
217 finishUpper(gtupper
);
226 Mesher::isCcw( int ilast
)
228 REAL area
= det3( vdata
[ilast
]->t
, vdata
[itop
-1]->t
, vdata
[itop
-2]->t
);
229 return (area
< ZERO
) ? 0 : 1;
233 Mesher::isCw( int ilast
)
235 REAL area
= det3( vdata
[ilast
]->t
, vdata
[itop
-1]->t
, vdata
[itop
-2]->t
);
236 return (area
> -ZERO
) ? 0 : 1;
240 Mesher::equal( int x
, int y
)
242 return( last
[0] == vdata
[x
] && last
[1] == vdata
[y
] );
246 Mesher::copy( int x
, int y
)
248 last
[0] = vdata
[x
]; last
[1] = vdata
[y
];
252 Mesher::move( int x
, int y
)
258 Mesher::output( int x
)
260 backend
.tmeshvert( vdata
[x
] );
263 /*---------------------------------------------------------------------------
264 * addedge - addedge an edge to the triangulation
266 * This code has been re-written to generate large triangle meshes
267 * from a monotone polygon. Although smaller triangle meshes
268 * could be generated faster and with less code, larger meshes
269 * actually give better SYSTEM performance. This is because
270 * vertices are processed in the backend slower than they are
271 * generated by this code and any decrease in the number of vertices
272 * results in a decrease in the time spent in the backend.
273 *---------------------------------------------------------------------------
279 register int ilast
= itop
;
281 if( lastedge
== 0 ) {
282 if( equal( 0, 1 ) ) {
285 for( register int i
= 2; i
< ilast
; i
++ ) {
289 copy( ilast
, ilast
-1 );
290 } else if( equal( ilast
-2, ilast
-1) ) {
293 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
299 closeMesh(); openMesh();
302 for( register int i
= 1; i
< ilast
; i
++ ) {
306 copy( ilast
, ilast
-1 );
312 for( register int i
= 2; i
< ilast
; i
++ ) {
316 copy( ilast
-1, ilast
);
317 } else if( equal( ilast
-1, ilast
-2) ) {
320 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
326 closeMesh(); openMesh();
329 for( register int i
= 1; i
< ilast
; i
++ ) {
333 copy( ilast
-1, ilast
);
337 //for( register long k=0; k<=ilast; k++ ) pop( k );
343 register int ilast
= itop
;
345 if( lastedge
== 0 ) {
346 if( equal( 0, 1 ) ) {
349 for( register int i
= 2; i
< ilast
; i
++ ) {
353 copy( ilast
, ilast
-1 );
354 } else if( equal( ilast
-2, ilast
-1) ) {
357 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
363 closeMesh(); openMesh();
366 for( register int i
= 1; i
< ilast
; i
++ ) {
370 copy( ilast
, ilast
-1 );
373 //for( register long k=0; k<ilast-1; k++ ) pop( k );
378 if( ! isCcw( ilast
) ) return;
381 } while( (itop
> 1) && isCcw( ilast
) );
383 if( equal( ilast
-1, ilast
-2 ) ) {
386 for( register int i
=ilast
-3; i
>=itop
-1; i
-- ) {
390 copy( ilast
, itop
-1 );
391 } else if( equal( itop
, itop
-1 ) ) {
394 for( register int i
= itop
+1; i
< ilast
; i
++ ) {
398 copy( ilast
-1, ilast
);
400 closeMesh(); openMesh();
403 for( register int i
=ilast
-2; i
>=itop
-1; i
-- ) {
407 copy( ilast
, itop
-1 );
409 //for( register int k=itop; k<ilast; k++ ) pop( k );
417 register int ilast
= itop
;
419 if( lastedge
== 1 ) {
423 for( register int i
= 2; i
< ilast
; i
++ ) {
427 copy( ilast
-1, ilast
);
428 } else if( equal( ilast
-1, ilast
-2) ) {
431 for( register int i
= ilast
-3; i
>= 0; i
-- ) {
437 closeMesh(); openMesh();
440 for( register int i
= 1; i
< ilast
; i
++ ) {
444 copy( ilast
-1, ilast
);
448 //for( register long k=0; k<ilast-1; k++ ) pop( k );
453 if( ! isCw( ilast
) ) return;
456 } while( (itop
> 1) && isCw( ilast
) );
458 if( equal( ilast
-2, ilast
-1) ) {
461 for( register int i
=ilast
-3; i
>=itop
-1; i
--) {
465 copy( itop
-1, ilast
);
466 } else if( equal( itop
-1, itop
) ) {
469 for( register int i
=itop
+1; i
<ilast
; i
++ ) {
473 copy( ilast
, ilast
-1 );
475 closeMesh(); openMesh();
478 for( register int i
=ilast
-2; i
>=itop
-1; i
-- ) {
482 copy( itop
-1, ilast
);
484 //for( register int k=itop; k<ilast; k++ ) pop( k );