1 /*
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:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
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.
17 **
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.
23 **
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.
33 */
34 
35 /*
36  * quilt.c++
37  *
38  */
39 
40 //#include "glimports.h"
41 //#include "mystdio.h"
42 //#include "myassert.h"
43 #include "quilt.h"
44 #include "backend.h"
45 #include "mapdesc.h"
46 #include "flist.h"
47 #include "patchlist.h"
48 #include "simplemath.h" //min()
49 
50 /* local preprocessor definitions */
51 #define DEF_PATCH_STEPSIZE	.4
52 #define fsizeof(x)		(sizeof(x)/sizeof(REAL))
53 
54 
55 Quilt::Quilt( Mapdesc *_mapdesc )
56 {
57     mapdesc = _mapdesc;
58 }
59 
60 void
61 Quilt::deleteMe( Pool& p )
62 {
63     for( Quiltspec *q=qspec; q != eqspec; q++ ) {
64 #if 1
65 	if( q->breakpoints) delete[] q->breakpoints;
66     q->breakpoints = 0;
67 #else
68 	if( q->breakpoints) {
69 	   delete[] q->breakpoints;
70 	   q->breakpoints = 0;
71 printf("in here\n");
72         }
73 #endif
74     }
75     if( cpts ) delete[] cpts;
76     cpts = 0;
77     PooledObj::deleteMe( p );
78 }
79 
80 void
81 Quilt::show( void )
82 {
83 #ifndef NDEBUG
84     int nc = mapdesc->getNcoords();
85     REAL *ps  = cpts;
86     ps += qspec[0].offset;
87     ps += qspec[1].offset;
88     for( int i=0; i!= qspec[0].order * qspec[0].width; i++ ) {
89 	for( int j = 0; j!= qspec[1].order * qspec[1].width; j++ ) {
90 	    for( int k=0; k < nc; k++ )
91 		_glu_dprintf(  "%g ", ps[i*qspec[0].stride + j*qspec[1].stride + k] );
92 	    _glu_dprintf(  "\n" );
93 	}
94 	_glu_dprintf(  "\n" );
95     }
96     _glu_dprintf( "\n" );
97 #endif
98 }
99 
100 /*--------------------------------------------------------------------------
101  * Quilt::select - find which map in each quilt contains the points
102  *			pta and ptb with pta[i] < ptb[i]
103  *--------------------------------------------------------------------------
104  */
105 
106 void
107 Quilt::select( REAL *pta, REAL *ptb )
108 {
109     int dim = eqspec - qspec;
110     int i, j;
111     for( i=0; i<dim; i++) {
112 	for( j=qspec[i].width-1; j>=0; j-- )
113 	    if( (qspec[i].breakpoints[j] <= pta[i]   ) &&
114 	    	(ptb[i] <= qspec[i].breakpoints[j+1] ) )
115 		 break;
116 	assert( j != -1 );
117 	qspec[i].index = j;
118     }
119 }
120 
121 void
122 Quilt::download( Backend &backend )
123 {
124     if( getDimension() == 2 ) {
125 	REAL *ps  = cpts;
126 	ps += qspec[0].offset;
127 	ps += qspec[1].offset;
128 	ps += qspec[0].index * qspec[0].order * qspec[0].stride;
129 	ps += qspec[1].index * qspec[1].order * qspec[1].stride;
130 	backend.surfpts( mapdesc->getType(), ps,
131 		  qspec[0].stride,
132 		  qspec[1].stride,
133 		  qspec[0].order,
134 		  qspec[1].order,
135 		  qspec[0].breakpoints[qspec[0].index],
136 		  qspec[0].breakpoints[qspec[0].index+1],
137 		  qspec[1].breakpoints[qspec[1].index],
138 		  qspec[1].breakpoints[qspec[1].index+1] );
139     } else {
140 	REAL *ps  = cpts;
141 	ps += qspec[0].offset;
142 	ps += qspec[0].index * qspec[0].order * qspec[0].stride;
143 	backend.curvpts( mapdesc->getType(), ps,
144 		      qspec[0].stride,
145 		      qspec[0].order,
146 		      qspec[0].breakpoints[qspec[0].index],
147 		      qspec[0].breakpoints[qspec[0].index+1] );
148     }
149 }
150 
151 /*--------------------------------------------------------------------------
152  * Quilt::downloadAll - download each map that contains the current patch
153  *--------------------------------------------------------------------------
154  */
155 
156 void
157 Quilt::downloadAll( REAL *pta, REAL *ptb, Backend &backend )
158 {
159     for( Quilt *m = this; m; m=m->next ) {
160 	m->select( pta, ptb );
161 	m->download( backend );
162     }
163 }
164 
165 /*--------------------------------------------------------------------------
166   * Quilt::isCulled - determine if an entire quilt is trivially rejected.
167  *--------------------------------------------------------------------------
168  */
169 
170 int
171 Quilt::isCulled( void )
172 {
173     if( mapdesc->isCulling() )
174 	return mapdesc->xformAndCullCheck( cpts + qspec[0].offset + qspec[1].offset,
175 			qspec[0].order * qspec[0].width, qspec[0].stride,
176 			qspec[1].order * qspec[1].width, qspec[1].stride );
177     else
178 	return CULL_ACCEPT;
179 }
180 
181 /*---------------------------------------------------------------------------
182  * Quilt::getRange - retrieve the valid paramater range of a set of quilts
183  *---------------------------------------------------------------------------
184  */
185 void
186 Quilt::getRange( REAL *from, REAL *to, Flist& slist, Flist &tlist )
187 {
188     getRange( from, to, 0, slist );
189     getRange( from, to, 1, tlist );
190 }
191 
192 /*---------------------------------------------------------------------------
193  * Quilt::getRange - retrieve the valid paramater range of a set of quilts
194  *---------------------------------------------------------------------------
195  */
196 void
197 Quilt::getRange( REAL *from, REAL *to, int i, Flist &list )
198 {
199     Quilt *maps = this;
200     from[i] = maps->qspec[i].breakpoints[0];
201     to[i]   = maps->qspec[i].breakpoints[maps->qspec[i].width];
202     int maxpts = 0;
203     Quilt_ptr m;
204     for( m=maps; m; m=m->next ) {
205 	if( m->qspec[i].breakpoints[0] > from[i] )
206 	    from[i] = m->qspec[i].breakpoints[0];
207 	if( m->qspec[i].breakpoints[m->qspec[i].width] < to[i] )
208 	    to[i] = m->qspec[i].breakpoints[m->qspec[i].width];
209 	maxpts += m->qspec[i].width + 1;
210     }
211 
212     list.grow( maxpts );
213 
214     for( m=maps; m; m=m->next )
215 	for( int j=0; j<=m->qspec[i].width; j++ ) {
216 	    list.add( m->qspec[i].breakpoints[j] );
217 	}
218 
219     list.filter( );
220     list.taper( from[i], to[i] );
221 }
222 
223 void
224 Quilt::getRange( REAL *from, REAL *to, Flist& slist )
225 {
226     getRange( from, to, 0, slist );
227 }
228 
229 void
230 Quilt::findRates( Flist& slist, Flist& tlist, REAL rate[2] )
231 {
232     findSampleRates( slist, tlist );
233     rate[0] = qspec[0].step_size;
234     rate[1] = qspec[1].step_size;
235 
236     for( Quilt *q = next; q; q = q->next ) {
237 	q->findSampleRates( slist, tlist );
238 	if( q->qspec[0].step_size < rate[0] )
239 	    rate[0] = q->qspec[0].step_size;
240 	if( q->qspec[1].step_size < rate[1] )
241 	    rate[1] = q->qspec[1].step_size;
242     }
243 }
244 
245 void
246 Quilt::findSampleRates( Flist& slist, Flist& tlist )
247 {
248     qspec[0].step_size = DEF_PATCH_STEPSIZE *
249 	    (qspec[0].breakpoints[qspec[0].width] - qspec[0].breakpoints[0]);
250     qspec[1].step_size = DEF_PATCH_STEPSIZE *
251 	    (qspec[1].breakpoints[qspec[1].width] - qspec[1].breakpoints[0]);
252 
253     for( int i = slist.start; i < slist.end-1; i++ ) {
254 	for( int j = tlist.start; j < tlist.end-1; j++ ) {
255 
256 	    REAL pta[2], ptb[2];
257 	    pta[0] = slist.pts[i];
258 	    ptb[0] = slist.pts[i+1];
259 	    pta[1] = tlist.pts[j];
260 	    ptb[1] = tlist.pts[j+1];
261 	    Patchlist patchlist( this, pta, ptb );
262 	    patchlist.getstepsize();
263 
264 	    {
265 	    float edge_len_s = min(glu_abs(ptb[0]-pta[0]),1.0);
266 	    float edge_len_t = min(glu_abs(ptb[1]-pta[1]),1.0);
267 
268 	    if( patchlist.getStepsize(0)/edge_len_s < qspec[0].step_size )
269 	       qspec[0].step_size = patchlist.getStepsize(0)/edge_len_s;
270 	    if( patchlist.getStepsize(1)/edge_len_t < qspec[1].step_size )
271 	      qspec[1].step_size = patchlist.getStepsize(1)/edge_len_t;
272 	    }
273 	}
274     }
275 }
276