1f220fa62Smrg/*
2f220fa62Smrg** License Applicability. Except to the extent portions of this file are
3f220fa62Smrg** made subject to an alternative license as permitted in the SGI Free
4f220fa62Smrg** Software License B, Version 1.1 (the "License"), the contents of this
5f220fa62Smrg** file are subject only to the provisions of the License. You may not use
6f220fa62Smrg** this file except in compliance with the License. You may obtain a copy
7f220fa62Smrg** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8f220fa62Smrg** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9f220fa62Smrg**
10f220fa62Smrg** http://oss.sgi.com/projects/FreeB
11f220fa62Smrg**
12f220fa62Smrg** Note that, as provided in the License, the Software is distributed on an
13f220fa62Smrg** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14f220fa62Smrg** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15f220fa62Smrg** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16f220fa62Smrg** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17f220fa62Smrg**
18f220fa62Smrg** Original Code. The Original Code is: OpenGL Sample Implementation,
19f220fa62Smrg** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20f220fa62Smrg** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21f220fa62Smrg** Copyright in any portions created by third parties is as indicated
22f220fa62Smrg** elsewhere herein. All Rights Reserved.
23f220fa62Smrg**
24f220fa62Smrg** Additional Notice Provisions: The application programming interfaces
25f220fa62Smrg** established by SGI in conjunction with the Original Code are The
26f220fa62Smrg** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27f220fa62Smrg** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28f220fa62Smrg** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29f220fa62Smrg** Window System(R) (Version 1.3), released October 19, 1998. This software
30f220fa62Smrg** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31f220fa62Smrg** published by SGI, but has not been independently verified as being
32f220fa62Smrg** compliant with the OpenGL(R) version 1.2.1 Specification.
33f220fa62Smrg*/
34f220fa62Smrg
35f220fa62Smrg/*
36f220fa62Smrg * splitarcs.c++
37f220fa62Smrg *
38f220fa62Smrg */
39f220fa62Smrg
40f220fa62Smrg#include "glimports.h"
41f220fa62Smrg#include "myassert.h"
42f220fa62Smrg#include "mysetjmp.h"
43f220fa62Smrg#include "mystdio.h"
44f220fa62Smrg#include "subdivider.h"
45f220fa62Smrg#include "arcsorter.h"
46f220fa62Smrg#include "arc.h"
47f220fa62Smrg#include "bin.h"
48f220fa62Smrg
49f220fa62Smrg/* local preprocessor definitions */
50f220fa62Smrg#define MAXARCS	10
51f220fa62Smrg
52f220fa62Smrg/*----------------------------------------------------------------------------
53f220fa62Smrg * Subdivider::split - split trim regions in source bin by line (param == value).
54f220fa62Smrg *----------------------------------------------------------------------------
55f220fa62Smrg */
56f220fa62Smrg
57f220fa62Smrgvoid
58f220fa62SmrgSubdivider::split( Bin& bin, Bin& left, Bin& right, int param, REAL value )
59f220fa62Smrg{
60f220fa62Smrg    Bin	intersections, unknown;
61f220fa62Smrg
62f220fa62Smrg    partition( bin, left, intersections, right, unknown, param, value );
63f220fa62Smrg
64f220fa62Smrg    int	count = intersections.numarcs();
65f220fa62Smrg    if( count % 2 ) {
66f220fa62Smrg#ifndef NDEBUG
67f220fa62Smrg	left.show( "left" );
68f220fa62Smrg	intersections.show( "intersections" );
69f220fa62Smrg	right.show( "right" );
70f220fa62Smrg#endif
71f220fa62Smrg	::mylongjmp( jumpbuffer, 29 );
72f220fa62Smrg    }
73f220fa62Smrg
74f220fa62Smrg    Arc_ptr arclist[MAXARCS], *list;
75f220fa62Smrg    if( count >= MAXARCS ) {
76f220fa62Smrg	list = new Arc_ptr[count];
77f220fa62Smrg    } else {
78f220fa62Smrg	list = arclist;
79f220fa62Smrg    }
80f220fa62Smrg
81f220fa62Smrg    Arc_ptr jarc, *last, *lptr;
82f220fa62Smrg    for( last = list; (jarc=intersections.removearc()) != NULL; last++ )
83f220fa62Smrg	*last = jarc;
84f220fa62Smrg
85f220fa62Smrg    if( param == 0 ) { /* sort into increasing t order */
86f220fa62Smrg	ArcSdirSorter sorter(*this);
87f220fa62Smrg	sorter.qsort( list, count );
88f220fa62Smrg
89f220fa62Smrg        //::qsort ((void *)list, count, sizeof(Arc_ptr), (cmpfunc)compare_s);
90f220fa62Smrg	for( lptr=list; lptr<last; lptr+=2 )
91f220fa62Smrg	    check_s ( lptr[0], lptr[1] );
92f220fa62Smrg	for( lptr=list; lptr<last; lptr+=2 )
93f220fa62Smrg	    join_s ( left, right, lptr[0], lptr[1] );
94f220fa62Smrg	for( lptr=list; lptr != last; lptr++ ) {
95f220fa62Smrg	    if( ((*lptr)->head()[0] <= value) && ((*lptr)->tail()[0] <= value) )
96f220fa62Smrg		left.addarc( *lptr  );
97f220fa62Smrg	    else
98f220fa62Smrg		right.addarc( *lptr  );
99f220fa62Smrg	}
100f220fa62Smrg    } else { /* sort into decreasing s order */
101f220fa62Smrg	ArcTdirSorter sorter(*this);
102f220fa62Smrg	sorter.qsort( list, count );
103f220fa62Smrg        //::qsort ((void *)list, count, sizeof(Arc_ptr), (cmpfunc)compare_t);
104f220fa62Smrg	for( lptr=list; lptr<last; lptr+=2 )
105f220fa62Smrg	    check_t ( lptr[0], lptr[1] );
106f220fa62Smrg	for( lptr=list; lptr<last; lptr+=2 )
107f220fa62Smrg	    join_t ( left, right, lptr[0], lptr[1] );
108f220fa62Smrg	for( lptr=list; lptr != last; lptr++ ) {
109f220fa62Smrg	    if( ((*lptr)->head()[1] <= value) && ((*lptr)->tail()[1] <= value) )
110f220fa62Smrg		left.addarc( *lptr  );
111f220fa62Smrg	    else
112f220fa62Smrg		right.addarc( *lptr  );
113f220fa62Smrg	}
114f220fa62Smrg    }
115f220fa62Smrg
116f220fa62Smrg    if( list != arclist ) delete[] list;
117f220fa62Smrg    unknown.adopt();
118f220fa62Smrg}
119f220fa62Smrg
120f220fa62Smrg
121f220fa62Smrgvoid
122f220fa62SmrgSubdivider::check_s( Arc_ptr jarc1, Arc_ptr jarc2 )
123f220fa62Smrg{
124f220fa62Smrg    assert( jarc1->check( ) != 0 );
125f220fa62Smrg    assert( jarc2->check( ) != 0 );
126f220fa62Smrg    assert( jarc1->next->check( ) != 0 );
127f220fa62Smrg    assert( jarc2->next->check( ) != 0 );
128f220fa62Smrg    assert( jarc1 != jarc2 );
129f220fa62Smrg
130f220fa62Smrg    /* XXX - if these assertions fail, it is due to user error or
131f220fa62Smrg	     undersampling */
132f220fa62Smrg    if( ! ( jarc1->tail()[0] < (jarc1)->head()[0] ) ) {
133f220fa62Smrg#ifndef NDEBUG
134f220fa62Smrg	_glu_dprintf( "s difference %f\n",  (jarc1)->tail()[0] - (jarc1)->head()[0] );
135f220fa62Smrg#endif
136f220fa62Smrg	::mylongjmp( jumpbuffer, 28 );
137f220fa62Smrg    }
138f220fa62Smrg
139f220fa62Smrg    if( ! ( jarc2->tail()[0] > (jarc2)->head()[0] ) ) {
140f220fa62Smrg#ifndef NDEBUG
141f220fa62Smrg	_glu_dprintf( "s difference %f\n",  (jarc2)->tail()[0] - (jarc2)->head()[0] );
142f220fa62Smrg#endif
143f220fa62Smrg	::mylongjmp( jumpbuffer, 28 );
144f220fa62Smrg    }
145f220fa62Smrg}
146f220fa62Smrg
147f220fa62Smrginline void
148f220fa62SmrgSubdivider::link( Arc_ptr jarc1, Arc_ptr jarc2, Arc_ptr up, Arc_ptr down )
149f220fa62Smrg{
150f220fa62Smrg    up->nuid = down->nuid = 0;		// XXX
151f220fa62Smrg
152f220fa62Smrg    up->next = jarc2;
153f220fa62Smrg    down->next = jarc1;
154f220fa62Smrg    up->prev = jarc1->prev;
155f220fa62Smrg    down->prev = jarc2->prev;
156f220fa62Smrg
157f220fa62Smrg    down->next->prev = down;
158f220fa62Smrg    up->next->prev = up;
159f220fa62Smrg    down->prev->next = down;
160f220fa62Smrg    up->prev->next = up;
161f220fa62Smrg}
162f220fa62Smrg
163f220fa62Smrginline void
164f220fa62SmrgSubdivider::simple_link( Arc_ptr jarc1, Arc_ptr jarc2 )
165f220fa62Smrg{
166f220fa62Smrg    Arc_ptr tmp = jarc2->prev;
167f220fa62Smrg    jarc2->prev = jarc1->prev;
168f220fa62Smrg    jarc1->prev = tmp;
169f220fa62Smrg    jarc2->prev->next = jarc2;
170f220fa62Smrg    jarc1->prev->next = jarc1;
171f220fa62Smrg}
172f220fa62Smrg
173f220fa62Smrg
174f220fa62Smrg/*----------------------------------------------------------------------------
175f220fa62Smrg * join - add a pair of oppositely directed jordan arcs between two arcs
176f220fa62Smrg *----------------------------------------------------------------------------
177f220fa62Smrg */
178f220fa62Smrg
179f220fa62Smrgvoid
180f220fa62SmrgSubdivider::join_s( Bin& left, Bin& right, Arc_ptr jarc1, Arc_ptr jarc2 )
181f220fa62Smrg{
182f220fa62Smrg    assert( jarc1->check( ) != 0);
183f220fa62Smrg    assert( jarc2->check( ) != 0);
184f220fa62Smrg    assert( jarc1 != jarc2 );
185f220fa62Smrg
186f220fa62Smrg    if( ! jarc1->getitail() )
187f220fa62Smrg	jarc1 = jarc1->next;
188f220fa62Smrg
189f220fa62Smrg    if( ! jarc2->getitail() )
190f220fa62Smrg	jarc2 = jarc2->next;
191f220fa62Smrg
192f220fa62Smrg    REAL s = jarc1->tail()[0];
193f220fa62Smrg    REAL t1 = jarc1->tail()[1];
194f220fa62Smrg    REAL t2 = jarc2->tail()[1];
195f220fa62Smrg
196f220fa62Smrg    if( t1 == t2 ) {
197f220fa62Smrg	simple_link( jarc1, jarc2 );
198f220fa62Smrg    } else {
199f220fa62Smrg	Arc_ptr newright = new(arcpool) Arc( arc_right, 0 );
200f220fa62Smrg	Arc_ptr newleft = new(arcpool) Arc( arc_left, 0 );
201f220fa62Smrg	assert( t1 < t2 );
202f220fa62Smrg	if( isBezierArcType() ) {
203f220fa62Smrg	    arctessellator.bezier( newright, s, s, t1, t2 );
204f220fa62Smrg	    arctessellator.bezier( newleft, s, s, t2, t1 );
205f220fa62Smrg	} else {
206f220fa62Smrg	    arctessellator.pwl_right( newright, s, t1, t2, stepsizes[0] );
207f220fa62Smrg	    arctessellator.pwl_left( newleft, s, t2, t1, stepsizes[2] );
208f220fa62Smrg	}
209f220fa62Smrg	link( jarc1, jarc2, newright, newleft );
210f220fa62Smrg	left.addarc( newright  );
211f220fa62Smrg	right.addarc( newleft  );
212f220fa62Smrg    }
213f220fa62Smrg
214f220fa62Smrg    assert( jarc1->check( ) != 0 );
215f220fa62Smrg    assert( jarc2->check( ) != 0 );
216f220fa62Smrg    assert( jarc1->next->check( ) != 0);
217f220fa62Smrg    assert( jarc2->next->check( ) != 0);
218f220fa62Smrg}
219f220fa62Smrg
220f220fa62Smrgvoid
221f220fa62SmrgSubdivider::check_t( Arc_ptr jarc1, Arc_ptr jarc2 )
222f220fa62Smrg{
223f220fa62Smrg    assert( jarc1->check( ) != 0 );
224f220fa62Smrg    assert( jarc2->check( ) != 0 );
225f220fa62Smrg    assert( jarc1->next->check( ) != 0 );
226f220fa62Smrg    assert( jarc2->next->check( ) != 0 );
227f220fa62Smrg    assert( jarc1 != jarc2 );
228f220fa62Smrg
229f220fa62Smrg    /* XXX - if these assertions fail, it is due to user error or
230f220fa62Smrg	     undersampling */
231f220fa62Smrg    if( ! ( jarc1->tail()[1] < (jarc1)->head()[1] ) ) {
232f220fa62Smrg#ifndef NDEBUG
233f220fa62Smrg	_glu_dprintf( "t difference %f\n",  jarc1->tail()[1] - (jarc1)->head()[1] );
234f220fa62Smrg#endif
235f220fa62Smrg	::mylongjmp( jumpbuffer, 28 );
236f220fa62Smrg    }
237f220fa62Smrg
238f220fa62Smrg    if( ! ( jarc2->tail()[1] > (jarc2)->head()[1] ) ) {
239f220fa62Smrg#ifndef NDEBUG
240f220fa62Smrg	_glu_dprintf( "t difference %f\n",  jarc2->tail()[1] - (jarc2)->head()[1] );
241f220fa62Smrg#endif
242f220fa62Smrg	::mylongjmp( jumpbuffer, 28 );
243f220fa62Smrg    }
244f220fa62Smrg}
245f220fa62Smrg
246f220fa62Smrg/*----------------------------------------------------------------------------
247f220fa62Smrg * join_t - add a pair of oppositely directed jordan arcs between two arcs
248f220fa62Smrg *----------------------------------------------------------------------------
249f220fa62Smrg */
250f220fa62Smrg
251f220fa62Smrgvoid
252f220fa62SmrgSubdivider::join_t( Bin& bottom, Bin& top, Arc_ptr jarc1, Arc_ptr jarc2 )
253f220fa62Smrg{
254f220fa62Smrg    assert( jarc1->check( ) != 0 );
255f220fa62Smrg    assert( jarc2->check( ) != 0 );
256f220fa62Smrg    assert( jarc1->next->check( ) != 0 );
257f220fa62Smrg    assert( jarc2->next->check( ) != 0 );
258f220fa62Smrg    assert( jarc1 != jarc2 );
259f220fa62Smrg
260f220fa62Smrg    if( ! jarc1->getitail() )
261f220fa62Smrg	jarc1 = jarc1->next;
262f220fa62Smrg
263f220fa62Smrg    if( ! jarc2->getitail() )
264f220fa62Smrg	jarc2 = jarc2->next;
265f220fa62Smrg
266f220fa62Smrg    REAL s1 = jarc1->tail()[0];
267f220fa62Smrg    REAL s2 = jarc2->tail()[0];
268f220fa62Smrg    REAL t  = jarc1->tail()[1];
269f220fa62Smrg
270f220fa62Smrg    if( s1 == s2 ) {
271f220fa62Smrg	simple_link( jarc1, jarc2 );
272f220fa62Smrg    } else {
273f220fa62Smrg	Arc_ptr newtop = new(arcpool) Arc( arc_top, 0 );
274f220fa62Smrg	Arc_ptr newbot = new(arcpool) Arc( arc_bottom, 0 );
275f220fa62Smrg	assert( s1 > s2 );
276f220fa62Smrg	if( isBezierArcType() ) {
277f220fa62Smrg	    arctessellator.bezier( newtop, s1, s2, t, t );
278f220fa62Smrg	    arctessellator.bezier( newbot, s2, s1, t, t );
279f220fa62Smrg	} else {
280f220fa62Smrg	    arctessellator.pwl_top( newtop, t, s1, s2, stepsizes[1] );
281f220fa62Smrg	    arctessellator.pwl_bottom( newbot, t, s2, s1, stepsizes[3] );
282f220fa62Smrg	}
283f220fa62Smrg	link( jarc1, jarc2, newtop, newbot );
284f220fa62Smrg	bottom.addarc( newtop  );
285f220fa62Smrg	top.addarc( newbot  );
286f220fa62Smrg    }
287f220fa62Smrg
288f220fa62Smrg    assert( jarc1->check( ) != 0 );
289f220fa62Smrg    assert( jarc2->check( ) != 0 );
290f220fa62Smrg    assert( jarc1->next->check( ) != 0 );
291f220fa62Smrg    assert( jarc2->next->check( ) != 0 );
292f220fa62Smrg}
293f220fa62Smrg
294