Debian Codegraph @

lib/database/src/so/nodes/nurbs/clients/softsurfeval.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * softsurfaceeval.c++ - surface evaluator
39 *
40 * $Revision: 1.1.1.1 $
41 */
42
43 #include "../SoAddPrefix.h"
44 #include "myassert.h"
45 #include <cstdio>
46 #include <algorithm>
47 #include <GL/gl.h>
48 #include "mystdio.h"
49 #include "mymath.h"
50 #include "softsurfmap.h"
51 #include "softsurfeval.h"
52 #include "nurbsconsts.h"
53
54 inline int equal( REAL x, REAL y ) { return (std::abs(x-y) < 1.0E-6) ? 1 : 0; }
55
56 _SoNurbsSurfaceEvaluator::~_SoNurbsSurfaceEvaluator()
57 {
58 for( _SoNurbsSurfaceMap *next; maps; maps = next ) {
59 next = maps->next;
60 delete maps;
61 }
62 }
63
64 inline _SoNurbsSurfaceMap *
65 _SoNurbsSurfaceEvaluator::firstMap( void )
66 {
67 return maps;
68 }
69
70 inline _SoNurbsSurfaceMap *
71 _SoNurbsSurfaceEvaluator::nextMap( _SoNurbsSurfaceMap *m )
72 {
73 return m->next;
74 }
75
76 inline int
77 _SoNurbsSurfaceEvaluator::isMap( _SoNurbsSurfaceMap *m )
78 {
79 return (m ? 1 : 0);
80 }
81
82 /*---------------------------------------------------------------------------
83 * disable - turn off a map
84 *---------------------------------------------------------------------------
85 */
86 void
87 _SoNurbsSurfaceEvaluator::disable( long type )
88 {
89 _SoNurbsSurfaceMap *map = typeToMap(type);
90 map->disable();
91 }
92
93 /*---------------------------------------------------------------------------
94 * enable - turn on a map
95 *---------------------------------------------------------------------------
96 */
97 void
98 _SoNurbsSurfaceEvaluator::enable( long type )
99 {
100 _SoNurbsSurfaceMap *map = typeToMap(type);
101 map->enable();
102 }
103
104 void
105 _SoNurbsSurfaceEvaluator::validreset()
106 {
107 valid[0][0] = valid[0][1] = 0;
108 valid[1][0] = valid[1][1] = 0;
109 valid[2][0] = valid[2][1] = 0;
110 }
111
112 /*-------------------------------------------------------------------------
113 * mapgrid2f - define a lattice of points with origin and offset
114 *-------------------------------------------------------------------------
115 */
116 void
117 _SoNurbsSurfaceEvaluator::mapgrid2f( long nu, REAL u0, REAL u1, long nv, REAL v0, REAL v1 )
118 {
119 validreset();
120 us = u0; dus = (u1-u0)/(REAL)nu;
121 vs = v0; dvs = (v1-v0)/(REAL)nv;
122 }
123
124 int
125 _SoNurbsSurfaceEvaluator::nextlru( void )
126 {
127 if( lru == 3 ) lru = 0;
128 return lru++;
129 }
130
131 void
132 _SoNurbsSurfaceEvaluator::bgnline( void )
133 {
134 glBegin(GL_LINE_STRIP);
135 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
136 map->startNewTriStrip();
137 }
138
139 void
140 _SoNurbsSurfaceEvaluator::endline( void )
141 {
142 glEnd();
143 }
144
145 void
146 _SoNurbsSurfaceEvaluator::range2f( long, REAL *, REAL * )
147 {
148 }
149
150 void
151 _SoNurbsSurfaceEvaluator::domain2f( REAL, REAL, REAL, REAL )
152 {
153 //printf( "domain2f [%g,%g] [%g,%g]\n", ulo, uhi, vlo, vhi );
154 }
155
156 void
157 _SoNurbsSurfaceEvaluator::bgnclosedline( void )
158 {
159 glBegin(GL_LINE_LOOP);
160 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
161 map->startNewTriStrip();
162 }
163
164 void
165 _SoNurbsSurfaceEvaluator::endclosedline( void )
166 {
167 glEnd();
168 }
169
170 void
171 _SoNurbsSurfaceEvaluator::bgntmesh( void )
172 {
173 glBegin(GL_TRIANGLES);
174 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
175 map->startNewTriStrip();
176 }
177
178 void
179 _SoNurbsSurfaceEvaluator::swaptmesh( void )
180 {
181 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
182 map->swapTriStripVertices();
183 }
184
185 void
186 _SoNurbsSurfaceEvaluator::endtmesh( void )
187 {
188 glEnd();
189 }
190
191 void
192 _SoNurbsSurfaceEvaluator::bgnqstrip( void )
193 {
194 glBegin(GL_TRIANGLES);
195 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
196 map->startNewTriStrip();
197 }
198
199 void
200 _SoNurbsSurfaceEvaluator::endqstrip( void )
201 {
202 glEnd();
203 }
204
205 /*-------------------------------------------------------------------------
206 * bgnmap2f - preamble to surface definition and evaluations
207 *-------------------------------------------------------------------------
208 */
209 void
210 _SoNurbsSurfaceEvaluator::bgnmap2f( long )
211 {
212 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
213 map->disable();
214 lru = 0;
215 vvals[0] = vvals[1] = vvals[2] =*(REAL *)(& NaN);
216 validreset();
217 }
218
219 /*-------------------------------------------------------------------------
220 * endmap2f - postamble to a map
221 *-------------------------------------------------------------------------
222 */
223 void
224 _SoNurbsSurfaceEvaluator::endmap2f( void )
225 {
226 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
227 map->disable();
228 }
229
230 void
231 _SoNurbsSurfaceEvaluator::addMap( _SoNurbsSurfaceMap *m )
232 {
233 m->next = maps;
234 maps = m;
235 }
236
237 /*-------------------------------------------------------------------------
238 * map2f - pass a desription of a surface map
239 *-------------------------------------------------------------------------
240 */
241 void
242 _SoNurbsSurfaceEvaluator::map2f(
243 long _type,
244 REAL _ulower, /* u lower domain coord */
245 REAL _uupper, /* u upper domain coord */
246 long _ustride, /* interpoint distance */
247 long _uorder, /* parametric order */
248 REAL _vlower, /* v lower domain coord */
249 REAL _vupper, /* v upper domain coord */
250 long _vstride, /* interpoint distance */
251 long _vorder, /* parametric order */
252 REAL *_ctlpts) /* control points */
253 {
254 _SoNurbsSurfaceMap *map = typeToMap(_type);
255 map->map2f( _ulower, _uupper, _ustride, _uorder,
256 _vlower, _vupper, _vstride, _vorder, _ctlpts );
257
258 vvals[0] = vvals[1] = vvals[2] =*(REAL *)(& NaN);
259 validreset();
260 }
261
262 /*---------------------------------------------------------------------------
263 * typeToMap - translate a type symbol to a map pointer
264 *---------------------------------------------------------------------------
265 */
266
267 _SoNurbsSurfaceMap *
268 _SoNurbsSurfaceEvaluator::typeToMap( long type )
269 {
270 _SoNurbsSurfaceMap *map;
271 for( map = firstMap(); map; map = nextMap(map) )
272 if( map->isType( type ) ) break;
273
274 return map;
275 }
276
277 /*-------------------------------------------------------------------------
278 * mapmesh2f - evaluate a mesh of points on lattice
279 *-------------------------------------------------------------------------
280 */
281 void
282 _SoNurbsSurfaceEvaluator::mapmesh2f( long style, long umin, long umax, long vmin, long vmax )
283 {
284 makeMesh( umin, umax, vmin, vmax );
285 }
286
287 /*-------------------------------------------------------------------------
288 * makeMesh - make the mesh of points on lattice
289 *-------------------------------------------------------------------------
290 */
291 void
292 _SoNurbsSurfaceEvaluator::makeMesh( long umin, long umax, long vmin, long vmax )
293 {
294 assert( umax - umin >= 0 );
295 assert( vmax - vmin >= 0 );
296
297 REAL domain[2];
298 REAL v0;
299 int vstart;
300
301 REAL v1 = vs + dvs * vmin;
302
303 int lastv = (int) (vmax + 1);
304 for( int gridv1 = (int) (vmin)+1; gridv1 != lastv; gridv1++ ) {
305
306 v0 = v1;
307 /* strength reduction == roundoff error */
308 v1 = vs + dvs * gridv1;
309
310 if( equal(v0, vvals[1]) ) {
311 if( ! equal( v1, vvals[0]) ) {
312 setv( v1, 0 );
313 }
314 vstart = 1;
315 } else if( equal(v0, vvals[0]) ) {
316 if( ! equal(v1, vvals[1]) ) {
317 setv( v1, 1 );
318 }
319 vstart = 0;
320 } else if( equal(v1, vvals[1]) ) {
321 setv( v0, 0 );
322 vstart = 0;
323 } else {
324 setv( v0, 1 );
325 setv( v1, 0 );
326 vstart = 1;
327 }
328
329 assert( equal(vvals[vstart],v0) );
330 assert( equal(vvals[1-vstart],v1) );
331
332 bgnqstrip();
333 if( vstart == 1 ) {
334 int lastu = (int) (umax + 1);
335 int gridi = (int) umin;
336 for( int gridu = gridi++; gridu != lastu; gridu = gridi++ ) {
337
338 /* strength reduction == roundoff error */
339 domain[0] = us + dus * gridu;
340
341 domain[1] = vvals[1];
342 compute( domain, 1, gridi );
343
344 domain[1] = vvals[0];
345 compute( domain, 0, gridi );
346 }
347 } else {
348 int lastu = (int) (umax + 1);
349 int gridi = (int) umin;
350 for( int gridu = gridi++; gridu != lastu; gridu = gridi++ ) {
351 /* strength reduction == roundoff error */
352 domain[0] = us + dus * gridu;
353
354 domain[1] = vvals[0];
355 compute( domain, 0, gridi );
356
357 domain[1] = vvals[1];
358 compute( domain, 1, gridi );
359 }
360 }
361 endqstrip();
362 }
363 }
364 /*-------------------------------------------------------------------------
365 * evalcoord2f - evaluate a point on a surface
366 *-------------------------------------------------------------------------
367 */
368 void
369 _SoNurbsSurfaceEvaluator::evalcoord2f( long, REAL u, REAL v )
370 {
371 REAL domain[2];
372 domain[0] = u;
373 domain[1] = v;
374 if( equal(v,vvals[0]) ) {
375 mapeval( domain, 0, 0 );
376 } else if( equal(v,vvals[1]) ) {
377 mapeval( domain, 1, 0 );
378 } else if( equal(v,vvals[2]) ) {
379 mapeval( domain, 2, 0 );
380 } else {
381 // approximately 20-30% of the time this branch is taken
382 int index = nextlru();
383 setv( v, index );
384 mapeval( domain, index, 0 );
385 }
386 }
387
388 /*-------------------------------------------------------------------------
389 * evalpoint2i - evaluate a grid point
390 *-------------------------------------------------------------------------
391 */
392 void
393 _SoNurbsSurfaceEvaluator::evalpoint2i( long u, long v )
394 {
395 int gridi = 1 + (int) u;
396 REAL domain[2];
397 domain[0] = u * dus + us;
398 domain[1] = v * dvs + vs;
399
400 if( equal(domain[1], vvals[1]) ) {
401 compute( domain, 1, gridi );
402 } else if( equal(domain[1], vvals[0]) ) {
403 compute( domain, 0, gridi );
404 } else if( equal(domain[1], vvals[2]) ) {
405 mapeval( domain, 2, 0 );
406 } else {
407 // this branch is executed less than 20% of the time
408 int index = nextlru();
409 setv( domain[1], index );
410 mapeval( domain, index, 0 );
411 }
412 }
413
414 /*-------------------------------------------------------------------------
415 * compute - output cached point or evaluate point and output
416 *-------------------------------------------------------------------------
417 */
418 void
419 _SoNurbsSurfaceEvaluator::compute( REAL *domain, int index, int place )
420 {
421 assert( vvals[index] == domain[1] );
422 if( ((place >= valid[index][0]) && (place < valid[index][1])) ) {
423 assert( place != 0 );
424 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
425 map->output( domain, index, place );
426 } else {
427 if( place >= MAXCACHED ) {
428 place = 0;
429 } else if( place == valid[index][1] ) {
430 valid[index][1]++;
431 } else if( valid[index][0] == valid[index][1] ) {
432 valid[index][0] = place;
433 valid[index][1] = place+1;
434 } else if( place == valid[index][0]-1 ) {
435 valid[index][0]--;
436 }
437 mapeval( domain, index, place );
438 }
439 }
440
441 /*---------------------------------------------------------------------------
442 * setv - set v coordinate for current isoparametric line
443 *---------------------------------------------------------------------------
444 */
445
446 void
447 _SoNurbsSurfaceEvaluator::setv( REAL v, int index )
448 {
449 valid[index][0] = valid[index][1] = 0;
450 vvals[index] = v;
451
452 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
453 map->setv( v, index );
454 }
455
456 /*---------------------------------------------------------------------------
457 * mapeval - evaluate all maps at given point of given isoline
458 *---------------------------------------------------------------------------
459 */
460
461 void
462 _SoNurbsSurfaceEvaluator::mapeval( REAL *domain, int index, int place )
463 {
464 for( _SoNurbsSurfaceMap *map = firstMap(); isMap( map ); map = nextMap(map) )
465 map->mapeval( domain, index, place );
466 }