| 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 |
* mapdesc.c++ - $Revision: 1.2 $ |
| 39 |
* Derrick Burns - 1991 |
| 40 |
*/ |
| 41 |
|
| 42 |
#include "glimports.h" |
| 43 |
#include "mystdio.h" |
| 44 |
#include "myassert.h" |
| 45 |
#include "mystring.h" |
| 46 |
#include "mymath.h" |
| 47 |
#include "backend.h" |
| 48 |
#include "nurbsconsts.h" |
| 49 |
#include "mapdesc.h" |
| 50 |
|
| 51 |
Mapdesc::Mapdesc( long _type, int _israt, int _ncoords, Backend& b ) |
| 52 |
: backend( b ) |
| 53 |
{ |
| 54 |
type = _type; |
| 55 |
isrational = _israt; |
| 56 |
ncoords = _ncoords; |
| 57 |
hcoords = _ncoords + (_israt ? 0 : 1 ); |
| 58 |
inhcoords = _ncoords - (_israt ? 1 : 0 ); |
| 59 |
mask = ((1<<(inhcoords*2))-1); |
| 60 |
next = 0; |
| 61 |
|
| 62 |
assert( hcoords <= MAXCOORDS ); |
| 63 |
assert( inhcoords >= 1 ); |
| 64 |
|
| 65 |
pixel_tolerance = 1.0; |
| 66 |
error_tolerance = 1.0; |
| 67 |
bbox_subdividing = N_NOBBOXSUBDIVISION; |
| 68 |
culling_method = N_NOCULLING; |
| 69 |
sampling_method = N_NOSAMPLING; |
| 70 |
clampfactor = N_NOCLAMPING; |
| 71 |
minsavings = N_NOSAVINGSSUBDIVISION; |
| 72 |
s_steps = 0.0; |
| 73 |
t_steps = 0.0; |
| 74 |
maxrate = ( s_steps < 0.0 ) ? 0.0 : s_steps; |
| 75 |
maxsrate = ( s_steps < 0.0 ) ? 0.0 : s_steps; |
| 76 |
maxtrate = ( t_steps < 0.0 ) ? 0.0 : t_steps; |
| 77 |
identify( bmat ); |
| 78 |
identify( cmat ); |
| 79 |
identify( smat ); |
| 80 |
for( int i = 0; i != inhcoords; i++ ) |
| 81 |
bboxsize[i] = 1.0; |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
Mapdesc::setBboxsize( INREAL *mat ) |
| 86 |
{ |
| 87 |
for( int i = 0; i != inhcoords; i++ ) |
| 88 |
bboxsize[i] = (REAL) mat[i]; |
| 89 |
} |
| 90 |
|
| 91 |
void |
| 92 |
Mapdesc::identify( Maxmatrix& dest ) |
| 93 |
{ |
| 94 |
memset( dest, 0, sizeof( dest ) ); |
| 95 |
for( int i=0; i != hcoords; i++ ) |
| 96 |
dest[i][i] = 1.0; |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
Mapdesc::surfbbox( REAL bb[2][MAXCOORDS] ) |
| 101 |
{ |
| 102 |
backend.surfbbox( type, bb[0], bb[1] ); |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
Mapdesc::copy( REAL dest[MAXCOORDS][MAXCOORDS], long n, INREAL *src, |
| 107 |
long rstride, long cstride ) |
| 108 |
{ |
| 109 |
assert( n >= 0 ); |
| 110 |
for( int i=0; i != n; i++ ) |
| 111 |
for( int j=0; j != n; j++ ) |
| 112 |
dest[i][j] = src[i*rstride + j*cstride]; |
| 113 |
} |
| 114 |
|
| 115 |
/*-------------------------------------------------------------------------- |
| 116 |
* copyPt - copy a homogeneous point |
| 117 |
*-------------------------------------------------------------------------- |
| 118 |
*/ |
| 119 |
void |
| 120 |
Mapdesc::copyPt( REAL *d, REAL *s ) |
| 121 |
{ |
| 122 |
assert( hcoords > 0 ); |
| 123 |
switch( hcoords ) { |
| 124 |
case 4: |
| 125 |
d[3] = s[3]; |
| 126 |
d[2] = s[2]; |
| 127 |
d[1] = s[1]; |
| 128 |
d[0] = s[0]; |
| 129 |
break; |
| 130 |
case 3: |
| 131 |
d[2] = s[2]; |
| 132 |
d[1] = s[1]; |
| 133 |
d[0] = s[0]; |
| 134 |
break; |
| 135 |
case 2: |
| 136 |
d[1] = s[1]; |
| 137 |
d[0] = s[0]; |
| 138 |
break; |
| 139 |
case 1: |
| 140 |
d[0] = s[0]; |
| 141 |
break; |
| 142 |
case 5: |
| 143 |
d[4] = s[4]; |
| 144 |
d[3] = s[3]; |
| 145 |
d[2] = s[2]; |
| 146 |
d[1] = s[1]; |
| 147 |
d[0] = s[0]; |
| 148 |
break; |
| 149 |
default: |
| 150 |
memcpy( d, s, hcoords * sizeof( REAL ) ); |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/*-------------------------------------------------------------------------- |
| 156 |
* sumPt - compute affine combination of two homogeneous points |
| 157 |
*-------------------------------------------------------------------------- |
| 158 |
*/ |
| 159 |
void |
| 160 |
Mapdesc::sumPt( REAL *dst, REAL *src1, REAL *src2, register REAL alpha, register REAL beta ) |
| 161 |
{ |
| 162 |
assert( hcoords > 0 ); |
| 163 |
switch( hcoords ) { |
| 164 |
case 4: |
| 165 |
dst[3] = src1[3] * alpha + src2[3] * beta; |
| 166 |
dst[2] = src1[2] * alpha + src2[2] * beta; |
| 167 |
dst[1] = src1[1] * alpha + src2[1] * beta; |
| 168 |
dst[0] = src1[0] * alpha + src2[0] * beta; |
| 169 |
break; |
| 170 |
case 3: |
| 171 |
dst[2] = src1[2] * alpha + src2[2] * beta; |
| 172 |
dst[1] = src1[1] * alpha + src2[1] * beta; |
| 173 |
dst[0] = src1[0] * alpha + src2[0] * beta; |
| 174 |
break; |
| 175 |
case 2: |
| 176 |
dst[1] = src1[1] * alpha + src2[1] * beta; |
| 177 |
dst[0] = src1[0] * alpha + src2[0] * beta; |
| 178 |
break; |
| 179 |
case 1: |
| 180 |
dst[0] = src1[0] * alpha + src2[0] * beta; |
| 181 |
break; |
| 182 |
case 5: |
| 183 |
dst[4] = src1[4] * alpha + src2[4] * beta; |
| 184 |
dst[3] = src1[3] * alpha + src2[3] * beta; |
| 185 |
dst[2] = src1[2] * alpha + src2[2] * beta; |
| 186 |
dst[1] = src1[1] * alpha + src2[1] * beta; |
| 187 |
dst[0] = src1[0] * alpha + src2[0] * beta; |
| 188 |
break; |
| 189 |
default: { |
| 190 |
for( int i = 0; i != hcoords; i++ ) |
| 191 |
dst[i] = src1[i] * alpha + src2[i] * beta; |
| 192 |
} |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/*-------------------------------------------------------------------------- |
| 198 |
* clipbits - compute bit-vector indicating point/window position |
| 199 |
* of a (transformed) homogeneous point |
| 200 |
*-------------------------------------------------------------------------- |
| 201 |
*/ |
| 202 |
unsigned int |
| 203 |
Mapdesc::clipbits( REAL *p ) |
| 204 |
{ |
| 205 |
assert( inhcoords >= 0 ); |
| 206 |
assert( inhcoords <= 3 ); |
| 207 |
|
| 208 |
register int nc = inhcoords; |
| 209 |
register REAL pw = p[nc]; |
| 210 |
register REAL nw = -pw; |
| 211 |
register unsigned int bits = 0; |
| 212 |
|
| 213 |
if( pw == 0.0 ) return mask; |
| 214 |
|
| 215 |
if( pw > 0.0 ) { |
| 216 |
switch( nc ) { |
| 217 |
case 3: |
| 218 |
if( p[2] <= pw ) bits |= (1<<5); |
| 219 |
if( p[2] >= nw ) bits |= (1<<4); |
| 220 |
if( p[1] <= pw ) bits |= (1<<3); |
| 221 |
if( p[1] >= nw ) bits |= (1<<2); |
| 222 |
if( p[0] <= pw ) bits |= (1<<1); |
| 223 |
if( p[0] >= nw ) bits |= (1<<0); |
| 224 |
return bits; |
| 225 |
case 2: |
| 226 |
if( p[1] <= pw ) bits |= (1<<3); |
| 227 |
if( p[1] >= nw ) bits |= (1<<2); |
| 228 |
if( p[0] <= pw ) bits |= (1<<1); |
| 229 |
if( p[0] >= nw ) bits |= (1<<0); |
| 230 |
return bits; |
| 231 |
case 1: |
| 232 |
if( p[0] <= pw ) bits |= (1<<1); |
| 233 |
if( p[0] >= nw ) bits |= (1<<0); |
| 234 |
return bits; |
| 235 |
default: { |
| 236 |
int bit = 1; |
| 237 |
for( int i=0; i<nc; i++ ) { |
| 238 |
if( p[i] >= nw ) bits |= bit; |
| 239 |
bit <<= 1; |
| 240 |
if( p[i] <= pw ) bits |= bit; |
| 241 |
bit <<= 1; |
| 242 |
} |
| 243 |
abort(); |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
} else { |
| 248 |
switch( nc ) { |
| 249 |
case 3: |
| 250 |
if( p[2] <= nw ) bits |= (1<<5); |
| 251 |
if( p[2] >= pw ) bits |= (1<<4); |
| 252 |
if( p[1] <= nw ) bits |= (1<<3); |
| 253 |
if( p[1] >= pw ) bits |= (1<<2); |
| 254 |
if( p[0] <= nw ) bits |= (1<<1); |
| 255 |
if( p[0] >= pw ) bits |= (1<<0); |
| 256 |
return bits; |
| 257 |
case 2: |
| 258 |
if( p[1] <= nw ) bits |= (1<<3); |
| 259 |
if( p[1] >= pw ) bits |= (1<<2); |
| 260 |
if( p[0] <= nw ) bits |= (1<<1); |
| 261 |
if( p[0] >= pw ) bits |= (1<<0); |
| 262 |
return bits; |
| 263 |
case 1: |
| 264 |
if( p[0] <= nw ) bits |= (1<<1); |
| 265 |
if( p[0] >= pw ) bits |= (1<<0); |
| 266 |
return bits; |
| 267 |
default: { |
| 268 |
int bit = 1; |
| 269 |
for( int i=0; i<nc; i++ ) { |
| 270 |
if( p[i] >= pw ) bits |= bit; |
| 271 |
bit <<= 1; |
| 272 |
if( p[i] <= nw ) bits |= bit; |
| 273 |
bit <<= 1; |
| 274 |
} |
| 275 |
abort(); |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
return bits; |
| 281 |
} |
| 282 |
|
| 283 |
/*-------------------------------------------------------------------------- |
| 284 |
* xformRational - transform a homogeneous point |
| 285 |
*-------------------------------------------------------------------------- |
| 286 |
*/ |
| 287 |
void |
| 288 |
Mapdesc::xformRational( Maxmatrix mat, REAL *d, REAL *s ) |
| 289 |
{ |
| 290 |
assert( hcoords >= 0 ); |
| 291 |
|
| 292 |
if( hcoords == 3 ) { |
| 293 |
REAL x = s[0]; |
| 294 |
REAL y = s[1]; |
| 295 |
REAL z = s[2]; |
| 296 |
d[0] = x*mat[0][0]+y*mat[1][0]+z*mat[2][0]; |
| 297 |
d[1] = x*mat[0][1]+y*mat[1][1]+z*mat[2][1]; |
| 298 |
d[2] = x*mat[0][2]+y*mat[1][2]+z*mat[2][2]; |
| 299 |
} else if( hcoords == 4 ) { |
| 300 |
REAL x = s[0]; |
| 301 |
REAL y = s[1]; |
| 302 |
REAL z = s[2]; |
| 303 |
REAL w = s[3]; |
| 304 |
d[0] = x*mat[0][0]+y*mat[1][0]+z*mat[2][0]+w*mat[3][0]; |
| 305 |
d[1] = x*mat[0][1]+y*mat[1][1]+z*mat[2][1]+w*mat[3][1]; |
| 306 |
d[2] = x*mat[0][2]+y*mat[1][2]+z*mat[2][2]+w*mat[3][2]; |
| 307 |
d[3] = x*mat[0][3]+y*mat[1][3]+z*mat[2][3]+w*mat[3][3]; |
| 308 |
} else { |
| 309 |
for( int i=0; i != hcoords; i++ ) { |
| 310 |
d[i] = 0; |
| 311 |
for( int j = 0; j != hcoords; j++ ) |
| 312 |
d[i] += s[j] * mat[j][i]; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/*-------------------------------------------------------------------------- |
| 318 |
* xformNonrational - transform a inhomogeneous point to a homogeneous point |
| 319 |
*-------------------------------------------------------------------------- |
| 320 |
*/ |
| 321 |
void |
| 322 |
Mapdesc::xformNonrational( Maxmatrix mat, REAL *d, REAL *s ) |
| 323 |
{ |
| 324 |
if( inhcoords == 2 ) { |
| 325 |
REAL x = s[0]; |
| 326 |
REAL y = s[1]; |
| 327 |
d[0] = x*mat[0][0]+y*mat[1][0]+mat[2][0]; |
| 328 |
d[1] = x*mat[0][1]+y*mat[1][1]+mat[2][1]; |
| 329 |
d[2] = x*mat[0][2]+y*mat[1][2]+mat[2][2]; |
| 330 |
} else if( inhcoords == 3 ) { |
| 331 |
REAL x = s[0]; |
| 332 |
REAL y = s[1]; |
| 333 |
REAL z = s[2]; |
| 334 |
d[0] = x*mat[0][0]+y*mat[1][0]+z*mat[2][0]+mat[3][0]; |
| 335 |
d[1] = x*mat[0][1]+y*mat[1][1]+z*mat[2][1]+mat[3][1]; |
| 336 |
d[2] = x*mat[0][2]+y*mat[1][2]+z*mat[2][2]+mat[3][2]; |
| 337 |
d[3] = x*mat[0][3]+y*mat[1][3]+z*mat[2][3]+mat[3][3]; |
| 338 |
} else { |
| 339 |
assert( inhcoords >= 0 ); |
| 340 |
for( int i=0; i != hcoords; i++ ) { |
| 341 |
d[i] = mat[inhcoords][i]; |
| 342 |
for( int j = 0; j < inhcoords; j++ ) |
| 343 |
d[i] += s[j] * mat[j][i]; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/*-------------------------------------------------------------------------- |
| 349 |
* xformAndCullCheck - transform a set of points that may be EITHER |
| 350 |
* homogeneous or inhomogeneous depending on the map description and |
| 351 |
* check if they are either completely inside, completely outside, |
| 352 |
* or intersecting the viewing frustrum. |
| 353 |
*-------------------------------------------------------------------------- |
| 354 |
*/ |
| 355 |
int |
| 356 |
Mapdesc::xformAndCullCheck( |
| 357 |
REAL *pts, int uorder, int ustride, int vorder, int vstride ) |
| 358 |
{ |
| 359 |
assert( uorder > 0 ); |
| 360 |
assert( vorder > 0 ); |
| 361 |
|
| 362 |
unsigned int inbits = mask; |
| 363 |
unsigned int outbits = 0; |
| 364 |
|
| 365 |
REAL *p = pts; |
| 366 |
for( REAL *pend = p + uorder * ustride; p != pend; p += ustride ) { |
| 367 |
REAL *q = p; |
| 368 |
for( REAL *qend = q + vorder * vstride; q != qend; q += vstride ) { |
| 369 |
REAL cpts[MAXCOORDS]; |
| 370 |
xformCulling( cpts, q ); |
| 371 |
unsigned int bits = clipbits( cpts ); |
| 372 |
outbits |= bits; |
| 373 |
inbits &= bits; |
| 374 |
if( ( outbits == mask ) && ( inbits != mask ) ) return CULL_ACCEPT; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
if( outbits != mask ) { |
| 379 |
return CULL_TRIVIAL_REJECT; |
| 380 |
} else if( inbits == mask ) { |
| 381 |
return CULL_TRIVIAL_ACCEPT; |
| 382 |
} else { |
| 383 |
return CULL_ACCEPT; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/*-------------------------------------------------------------------------- |
| 388 |
* cullCheck - check if a set of homogeneous transformed points are |
| 389 |
* either completely inside, completely outside, |
| 390 |
* or intersecting the viewing frustrum. |
| 391 |
*-------------------------------------------------------------------------- |
| 392 |
*/ |
| 393 |
int |
| 394 |
Mapdesc::cullCheck( REAL *pts, int uorder, int ustride, int vorder, int vstride ) |
| 395 |
{ |
| 396 |
unsigned int inbits = mask; |
| 397 |
unsigned int outbits = 0; |
| 398 |
|
| 399 |
REAL *p = pts; |
| 400 |
for( REAL *pend = p + uorder * ustride; p != pend; p += ustride ) { |
| 401 |
REAL *q = p; |
| 402 |
for( REAL *qend = q + vorder * vstride; q != qend; q += vstride ) { |
| 403 |
unsigned int bits = clipbits( q ); |
| 404 |
outbits |= bits; |
| 405 |
inbits &= bits; |
| 406 |
if( ( outbits == mask ) && ( inbits != mask ) ) return CULL_ACCEPT; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if( outbits != mask ) { |
| 411 |
return CULL_TRIVIAL_REJECT; |
| 412 |
} else if( inbits == mask ) { |
| 413 |
return CULL_TRIVIAL_ACCEPT; |
| 414 |
} else { |
| 415 |
return CULL_ACCEPT; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
/*-------------------------------------------------------------------------- |
| 420 |
* cullCheck - check if a set of homogeneous transformed points are |
| 421 |
* either completely inside, completely outside, |
| 422 |
* or intersecting the viewing frustrum. |
| 423 |
*-------------------------------------------------------------------------- |
| 424 |
*/ |
| 425 |
int |
| 426 |
Mapdesc::cullCheck( REAL *pts, int order, int stride ) |
| 427 |
{ |
| 428 |
unsigned int inbits = mask; |
| 429 |
unsigned int outbits = 0; |
| 430 |
|
| 431 |
REAL *p = pts; |
| 432 |
for( REAL *pend = p + order * stride; p != pend; p += stride ) { |
| 433 |
unsigned int bits = clipbits( p ); |
| 434 |
outbits |= bits; |
| 435 |
inbits &= bits; |
| 436 |
if( ( outbits == mask ) && ( inbits != mask ) ) return CULL_ACCEPT; |
| 437 |
} |
| 438 |
|
| 439 |
if( outbits != mask ) { |
| 440 |
return CULL_TRIVIAL_REJECT; |
| 441 |
} else if( inbits == mask ) { |
| 442 |
return CULL_TRIVIAL_ACCEPT; |
| 443 |
} else { |
| 444 |
return CULL_ACCEPT; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
/*-------------------------------------------------------------------------- |
| 449 |
* xformSampling - transform a set of points that may be EITHER |
| 450 |
* homogeneous or inhomogeneous depending on the map description |
| 451 |
* into sampling space |
| 452 |
*-------------------------------------------------------------------------- |
| 453 |
*/ |
| 454 |
void |
| 455 |
Mapdesc::xformSampling( REAL *pts, int order, int stride, REAL *sp, int outstride ) |
| 456 |
{ |
| 457 |
xformMat( smat, pts, order, stride, sp, outstride ); |
| 458 |
} |
| 459 |
|
| 460 |
void |
| 461 |
Mapdesc::xformBounding( REAL *pts, int order, int stride, REAL *sp, int outstride ) |
| 462 |
{ |
| 463 |
xformMat( bmat, pts, order, stride, sp, outstride ); |
| 464 |
} |
| 465 |
|
| 466 |
/*-------------------------------------------------------------------------- |
| 467 |
* xformCulling - transform a set of points that may be EITHER |
| 468 |
* homogeneous or inhomogeneous depending on the map description |
| 469 |
* into culling space |
| 470 |
*-------------------------------------------------------------------------- |
| 471 |
*/ |
| 472 |
void |
| 473 |
Mapdesc::xformCulling( REAL *pts, int order, int stride, REAL *cp, int outstride ) |
| 474 |
{ |
| 475 |
xformMat( cmat, pts, order, stride, cp, outstride ); |
| 476 |
} |
| 477 |
|
| 478 |
/*-------------------------------------------------------------------------- |
| 479 |
* xformCulling - transform a set of points that may be EITHER |
| 480 |
* homogeneous or inhomogeneous depending on the map description |
| 481 |
* into culling space |
| 482 |
*-------------------------------------------------------------------------- |
| 483 |
*/ |
| 484 |
void |
| 485 |
Mapdesc::xformCulling( REAL *pts, |
| 486 |
int uorder, int ustride, |
| 487 |
int vorder, int vstride, |
| 488 |
REAL *cp, int outustride, int outvstride ) |
| 489 |
{ |
| 490 |
xformMat( cmat, pts, uorder, ustride, vorder, vstride, cp, outustride, outvstride ); |
| 491 |
} |
| 492 |
|
| 493 |
/*-------------------------------------------------------------------------- |
| 494 |
* xformSampling - transform a set of points that may be EITHER |
| 495 |
* homogeneous or inhomogeneous depending on the map description |
| 496 |
* into sampling space |
| 497 |
*-------------------------------------------------------------------------- |
| 498 |
*/ |
| 499 |
void |
| 500 |
Mapdesc::xformSampling( REAL *pts, |
| 501 |
int uorder, int ustride, |
| 502 |
int vorder, int vstride, |
| 503 |
REAL *sp, int outustride, int outvstride ) |
| 504 |
{ |
| 505 |
xformMat( smat, pts, uorder, ustride, vorder, vstride, sp, outustride, outvstride ); |
| 506 |
} |
| 507 |
|
| 508 |
void |
| 509 |
Mapdesc::xformBounding( REAL *pts, |
| 510 |
int uorder, int ustride, |
| 511 |
int vorder, int vstride, |
| 512 |
REAL *sp, int outustride, int outvstride ) |
| 513 |
{ |
| 514 |
xformMat( bmat, pts, uorder, ustride, vorder, vstride, sp, outustride, outvstride ); |
| 515 |
} |
| 516 |
|
| 517 |
void |
| 518 |
Mapdesc::xformMat( |
| 519 |
Maxmatrix mat, |
| 520 |
REAL * pts, |
| 521 |
int order, |
| 522 |
int stride, |
| 523 |
REAL * cp, |
| 524 |
int outstride ) |
| 525 |
{ |
| 526 |
if( isrational ) { |
| 527 |
REAL *pend = pts + order * stride; |
| 528 |
for( REAL *p = pts ; p != pend; p += stride ) { |
| 529 |
xformRational( mat, cp, p ); |
| 530 |
cp += outstride; |
| 531 |
} |
| 532 |
} else { |
| 533 |
REAL *pend = pts + order * stride; |
| 534 |
for( REAL *p = pts ; p != pend; p += stride ) { |
| 535 |
xformNonrational( mat, cp, p ); |
| 536 |
cp += outstride; |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
void |
| 542 |
Mapdesc::xformMat( Maxmatrix mat, REAL *pts, |
| 543 |
int uorder, int ustride, |
| 544 |
int vorder, int vstride, |
| 545 |
REAL *cp, int outustride, int outvstride ) |
| 546 |
{ |
| 547 |
if( isrational ) { |
| 548 |
REAL *pend = pts + uorder * ustride; |
| 549 |
for( REAL *p = pts ; p != pend; p += ustride ) { |
| 550 |
REAL *cpts2 = cp; |
| 551 |
REAL *qend = p + vorder * vstride; |
| 552 |
for( REAL *q = p; q != qend; q += vstride ) { |
| 553 |
xformRational( mat, cpts2, q ); |
| 554 |
cpts2 += outvstride; |
| 555 |
} |
| 556 |
cp += outustride; |
| 557 |
} |
| 558 |
} else { |
| 559 |
REAL *pend = pts + uorder * ustride; |
| 560 |
for( REAL *p = pts ; p != pend; p += ustride ) { |
| 561 |
REAL *cpts2 = cp; |
| 562 |
REAL *qend = p + vorder * vstride; |
| 563 |
for( REAL *q = p; q != qend; q += vstride ) { |
| 564 |
xformNonrational( mat, cpts2, q ); |
| 565 |
cpts2 += outvstride; |
| 566 |
} |
| 567 |
cp += outustride; |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
/*-------------------------------------------------------------------------- |
| 573 |
* subdivide - subdivide a curve along an isoparametric line |
| 574 |
*-------------------------------------------------------------------------- |
| 575 |
*/ |
| 576 |
|
| 577 |
void |
| 578 |
Mapdesc::subdivide( REAL *src, REAL *dst, REAL v, int stride, int order ) |
| 579 |
{ |
| 580 |
REAL mv = 1.0 - v; |
| 581 |
|
| 582 |
for( REAL *send=src+stride*order; src!=send; send-=stride, dst+=stride ) { |
| 583 |
copyPt( dst, src ); |
| 584 |
REAL *qpnt = src + stride; |
| 585 |
for( REAL *qp=src; qpnt!=send; qp=qpnt, qpnt+=stride ) |
| 586 |
sumPt( qp, qp, qpnt, mv, v ); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/*-------------------------------------------------------------------------- |
| 591 |
* subdivide - subdivide a patch along an isoparametric line |
| 592 |
*-------------------------------------------------------------------------- |
| 593 |
*/ |
| 594 |
|
| 595 |
void |
| 596 |
Mapdesc::subdivide( REAL *src, REAL *dst, REAL v, |
| 597 |
int so, int ss, int to, int ts ) |
| 598 |
{ |
| 599 |
REAL mv = 1.0 - v; |
| 600 |
|
| 601 |
for( REAL *slast = src+ss*so; src != slast; src += ss, dst += ss ) { |
| 602 |
REAL *sp = src; |
| 603 |
REAL *dp = dst; |
| 604 |
for( REAL *send = src+ts*to; sp != send; send -= ts, dp += ts ) { |
| 605 |
copyPt( dp, sp ); |
| 606 |
REAL *qp = sp; |
| 607 |
for( REAL *qpnt = sp+ts; qpnt != send; qp = qpnt, qpnt += ts ) |
| 608 |
sumPt( qp, qp, qpnt, mv, v ); |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
#define sign(x) ((x > 0) ? 1 : ((x < 0.0) ? -1 : 0)) |
| 615 |
|
| 616 |
/*-------------------------------------------------------------------------- |
| 617 |
* project - project a set of homogeneous coordinates into inhomogeneous ones |
| 618 |
*-------------------------------------------------------------------------- |
| 619 |
*/ |
| 620 |
int |
| 621 |
Mapdesc::project( REAL *src, int rstride, int cstride, |
| 622 |
REAL *dest, int trstride, int tcstride, |
| 623 |
int nrows, int ncols ) |
| 624 |
{ |
| 625 |
int s = sign( src[inhcoords] ); |
| 626 |
REAL *rlast = src + nrows * rstride; |
| 627 |
REAL *trptr = dest; |
| 628 |
for( REAL *rptr=src; rptr != rlast; rptr+=rstride, trptr+=trstride ) { |
| 629 |
REAL *clast = rptr + ncols * cstride; |
| 630 |
REAL *tcptr = trptr; |
| 631 |
for( REAL *cptr = rptr; cptr != clast; cptr+=cstride, tcptr+=tcstride ) { |
| 632 |
REAL *coordlast = cptr + inhcoords; |
| 633 |
if( sign( *coordlast ) != s ) return 0; |
| 634 |
REAL *tcoord = tcptr; |
| 635 |
for( REAL *coord = cptr; coord != coordlast; coord++, tcoord++ ) { |
| 636 |
*tcoord = *coord / *coordlast; |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
return 1; |
| 641 |
} |
| 642 |
|
| 643 |
/*-------------------------------------------------------------------------- |
| 644 |
* project - project a set of homogeneous coordinates into inhomogeneous ones |
| 645 |
*-------------------------------------------------------------------------- |
| 646 |
*/ |
| 647 |
int |
| 648 |
Mapdesc::project( REAL *src, int stride, REAL *dest, int tstride, int ncols ) |
| 649 |
{ |
| 650 |
int s = sign( src[inhcoords] ); |
| 651 |
|
| 652 |
REAL *clast = src + ncols * stride; |
| 653 |
for( REAL *cptr = src, *tcptr = dest; cptr != clast; cptr+=stride, tcptr+=tstride ) { |
| 654 |
REAL *coordlast = cptr + inhcoords; |
| 655 |
if( sign( *coordlast ) != s ) return 0; |
| 656 |
for( REAL *coord = cptr, *tcoord = tcptr; coord != coordlast; coord++, tcoord++ ) |
| 657 |
*tcoord = *coord / *coordlast; |
| 658 |
} |
| 659 |
|
| 660 |
return 1; |
| 661 |
} |
| 662 |
|
| 663 |
int |
| 664 |
Mapdesc::bboxTooBig( |
| 665 |
REAL *p, |
| 666 |
int rstride, |
| 667 |
int cstride, |
| 668 |
int nrows, |
| 669 |
int ncols, |
| 670 |
REAL bb[2][MAXCOORDS] ) |
| 671 |
{ |
| 672 |
REAL bbpts[MAXORDER][MAXORDER][MAXCOORDS]; |
| 673 |
const int trstride = sizeof(bbpts[0]) / sizeof(REAL); |
| 674 |
const int tcstride = sizeof(bbpts[0][0]) / sizeof(REAL); |
| 675 |
|
| 676 |
// points have been transformed, therefore they are homogeneous |
| 677 |
// project points |
| 678 |
int val = project( p, rstride, cstride, |
| 679 |
&bbpts[0][0][0], trstride, tcstride, nrows, ncols ); |
| 680 |
if( val == 0 ) return -1; |
| 681 |
|
| 682 |
// compute bounding box |
| 683 |
bbox( bb, &bbpts[0][0][0], trstride, tcstride, nrows, ncols ); |
| 684 |
|
| 685 |
// find out if bounding box can't fit in unit cube |
| 686 |
if( bbox_subdividing == N_BBOXROUND ) { |
| 687 |
for( int k=0; k != inhcoords; k++ ) |
| 688 |
if( ceilf(bb[1][k]) - floorf(bb[0][k]) > bboxsize[k] ) return 1; |
| 689 |
} else { |
| 690 |
for( int k=0; k != inhcoords; k++ ) |
| 691 |
if( bb[1][k] - bb[0][k] > bboxsize[k] ) return 1; |
| 692 |
} |
| 693 |
return 0; |
| 694 |
} |
| 695 |
|
| 696 |
void |
| 697 |
Mapdesc::bbox( |
| 698 |
REAL bb[2][MAXCOORDS], |
| 699 |
REAL *p, |
| 700 |
int rstride, |
| 701 |
int cstride, |
| 702 |
int nrows, |
| 703 |
int ncols ) |
| 704 |
{ |
| 705 |
int k; |
| 706 |
for( k=0; k != inhcoords; k++ ) |
| 707 |
bb[0][k] = bb[1][k] = p[k]; |
| 708 |
|
| 709 |
int i; |
| 710 |
for( i=0; i != nrows; i++ ) |
| 711 |
for( int j=0; j != ncols; j++ ) |
| 712 |
for( k=0; k != inhcoords; k++ ) { |
| 713 |
REAL x = p[i*rstride + j*cstride + k]; |
| 714 |
if( x < bb[0][k] ) bb[0][k] = x; |
| 715 |
else if( x > bb[1][k] ) bb[1][k] = x; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
/*-------------------------------------------------------------------------- |
| 720 |
* calcVelocityRational - calculate upper bound on first partial derivative |
| 721 |
* of a homogeneous set of points and bounds on each row of points. |
| 722 |
*-------------------------------------------------------------------------- |
| 723 |
*/ |
| 724 |
REAL |
| 725 |
Mapdesc::calcVelocityRational( REAL *p, int stride, int ncols ) |
| 726 |
{ |
| 727 |
REAL tmp[MAXORDER][MAXCOORDS]; |
| 728 |
|
| 729 |
assert( ncols <= MAXORDER ); |
| 730 |
|
| 731 |
const int tstride = sizeof(tmp[0]) / sizeof(REAL); |
| 732 |
|
| 733 |
if( project( p, stride, &tmp[0][0], tstride, ncols ) ) { |
| 734 |
return calcPartialVelocity( &tmp[0][0], tstride, ncols, 1, 1.0 ); |
| 735 |
} else { /* XXX */ |
| 736 |
return calcPartialVelocity( &tmp[0][0], tstride, ncols, 1, 1.0 ); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
/*-------------------------------------------------------------------------- |
| 741 |
* calcVelocityNonrational - calculate upper bound on first partial |
| 742 |
* derivative of a inhomogeneous set of points. |
| 743 |
*-------------------------------------------------------------------------- |
| 744 |
*/ |
| 745 |
REAL |
| 746 |
Mapdesc::calcVelocityNonrational( REAL *pts, int stride, int ncols ) |
| 747 |
{ |
| 748 |
return calcPartialVelocity( pts, stride, ncols, 1, 1.0 ); |
| 749 |
} |
| 750 |
|
| 751 |
int |
| 752 |
Mapdesc::isProperty( long property ) |
| 753 |
{ |
| 754 |
switch ( property ) { |
| 755 |
case N_PIXEL_TOLERANCE: |
| 756 |
case N_ERROR_TOLERANCE: |
| 757 |
case N_CULLING: |
| 758 |
case N_BBOX_SUBDIVIDING: |
| 759 |
case N_S_STEPS: |
| 760 |
case N_T_STEPS: |
| 761 |
case N_SAMPLINGMETHOD: |
| 762 |
case N_CLAMPFACTOR: |
| 763 |
case N_MINSAVINGS: |
| 764 |
return 1; |
| 765 |
default: |
| 766 |
return 0; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
REAL |
| 771 |
Mapdesc::getProperty( long property ) |
| 772 |
{ |
| 773 |
switch ( property ) { |
| 774 |
case N_PIXEL_TOLERANCE: |
| 775 |
return pixel_tolerance; |
| 776 |
case N_ERROR_TOLERANCE: |
| 777 |
return error_tolerance; |
| 778 |
case N_CULLING: |
| 779 |
return culling_method; |
| 780 |
case N_BBOX_SUBDIVIDING: |
| 781 |
return bbox_subdividing; |
| 782 |
case N_S_STEPS: |
| 783 |
return s_steps; |
| 784 |
case N_T_STEPS: |
| 785 |
return t_steps; |
| 786 |
case N_SAMPLINGMETHOD: |
| 787 |
return sampling_method; |
| 788 |
case N_CLAMPFACTOR: |
| 789 |
return clampfactor; |
| 790 |
case N_MINSAVINGS: |
| 791 |
return minsavings; |
| 792 |
default: |
| 793 |
abort(); |
| 794 |
return -1; //not necessary, needed to shut up compiler |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
void |
| 799 |
Mapdesc::setProperty( long property, REAL value ) |
| 800 |
{ |
| 801 |
|
| 802 |
switch ( property ) { |
| 803 |
case N_PIXEL_TOLERANCE: |
| 804 |
pixel_tolerance = value; |
| 805 |
break; |
| 806 |
case N_ERROR_TOLERANCE: |
| 807 |
error_tolerance = value; |
| 808 |
break; |
| 809 |
case N_CULLING: |
| 810 |
culling_method = value; |
| 811 |
break; |
| 812 |
case N_BBOX_SUBDIVIDING: |
| 813 |
if( value <= 0.0 ) value = N_NOBBOXSUBDIVISION; |
| 814 |
bbox_subdividing = value; |
| 815 |
break; |
| 816 |
case N_S_STEPS: |
| 817 |
if( value < 0.0 ) value = 0.0; |
| 818 |
s_steps = value; |
| 819 |
maxrate = ( value < 0.0 ) ? 0.0 : value; |
| 820 |
maxsrate = ( value < 0.0 ) ? 0.0 : value; |
| 821 |
break; |
| 822 |
case N_T_STEPS: |
| 823 |
if( value < 0.0 ) value = 0.0; |
| 824 |
t_steps = value; |
| 825 |
maxtrate = ( value < 0.0 ) ? 0.0 : value; |
| 826 |
break; |
| 827 |
case N_SAMPLINGMETHOD: |
| 828 |
sampling_method = value; |
| 829 |
break; |
| 830 |
case N_CLAMPFACTOR: |
| 831 |
if( value <= 0.0 ) value = N_NOCLAMPING; |
| 832 |
clampfactor = value; |
| 833 |
break; |
| 834 |
case N_MINSAVINGS: |
| 835 |
if( value <= 0.0 ) value = N_NOSAVINGSSUBDIVISION; |
| 836 |
minsavings = value; |
| 837 |
break; |
| 838 |
default: |
| 839 |
abort(); |
| 840 |
break; |
| 841 |
} |
| 842 |
} |
| 843 |
|