| 1 |
/* $Id: nurbsutl.c,v 1.2 2000/11/02 00:28:54 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.0 |
| 6 |
* Copyright (C) 1995-1996 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* NURBS implementation written by Bogdan Sikorski ([email protected]) |
| 25 |
* See README2 for more info. |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
#include <math.h> |
| 30 |
#include "nurbs.h" |
| 31 |
|
| 32 |
GLenum |
| 33 |
test_knot(GLint nknots, GLfloat *knot, GLint order) |
| 34 |
{ |
| 35 |
GLsizei i; |
| 36 |
GLint knot_mult; |
| 37 |
GLfloat tmp_knot; |
| 38 |
|
| 39 |
tmp_knot=knot[0]; |
| 40 |
knot_mult=1; |
| 41 |
for(i=1;i<nknots;i++) |
| 42 |
{ |
| 43 |
if(knot[i] < tmp_knot) |
| 44 |
return GLU_NURBS_ERROR4; |
| 45 |
if(fabs(tmp_knot-knot[i]) > EPSILON) |
| 46 |
{ |
| 47 |
if(knot_mult>order) |
| 48 |
return GLU_NURBS_ERROR5; |
| 49 |
knot_mult=1; |
| 50 |
tmp_knot=knot[i]; |
| 51 |
} |
| 52 |
else |
| 53 |
++knot_mult; |
| 54 |
} |
| 55 |
return GLU_NO_ERROR; |
| 56 |
} |
| 57 |
|
| 58 |
/* qsort function */ |
| 59 |
static int |
| 60 |
knot_sort(const void *a, const void *b) |
| 61 |
{ |
| 62 |
GLfloat x,y; |
| 63 |
|
| 64 |
x=*((GLfloat *)a); |
| 65 |
y=*((GLfloat *)b); |
| 66 |
if(fabs(x-y) < EPSILON) |
| 67 |
return 0; |
| 68 |
if(x > y) |
| 69 |
return 1; |
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
/* insert into dest knot all values within the valid range from src knot */ |
| 74 |
/* that do not appear in dest */ |
| 75 |
void |
| 76 |
collect_unified_knot(knot_str_type *dest, knot_str_type *src, |
| 77 |
GLfloat maximal_min_knot, GLfloat minimal_max_knot) |
| 78 |
{ |
| 79 |
GLfloat *src_knot,*dest_knot; |
| 80 |
GLint src_t_min,src_t_max,dest_t_min,dest_t_max; |
| 81 |
GLint src_nknots,dest_nknots; |
| 82 |
GLint i,j,k,new_cnt; |
| 83 |
GLboolean not_found_flag; |
| 84 |
|
| 85 |
src_knot=src->unified_knot; |
| 86 |
dest_knot=dest->unified_knot; |
| 87 |
src_t_min=src->t_min; |
| 88 |
src_t_max=src->t_max; |
| 89 |
dest_t_min=dest->t_min; |
| 90 |
dest_t_max=dest->t_max; |
| 91 |
src_nknots=src->unified_nknots; |
| 92 |
dest_nknots=dest->unified_nknots; |
| 93 |
|
| 94 |
k=new_cnt=dest_nknots; |
| 95 |
for(i=src_t_min;i<=src_t_max;i++) |
| 96 |
if(src_knot[i] - maximal_min_knot > -EPSILON && |
| 97 |
src_knot[i] - minimal_max_knot < EPSILON) |
| 98 |
{ |
| 99 |
not_found_flag=GL_TRUE; |
| 100 |
for(j=dest_t_min;j<=dest_t_max;j++) |
| 101 |
if(fabs(dest_knot[j]-src_knot[i]) < EPSILON) |
| 102 |
{ |
| 103 |
not_found_flag=GL_FALSE; |
| 104 |
break; |
| 105 |
} |
| 106 |
if(not_found_flag) |
| 107 |
{ |
| 108 |
/* knot from src is not in dest - add this knot to dest */ |
| 109 |
dest_knot[k++]=src_knot[i]; |
| 110 |
++new_cnt; |
| 111 |
++(dest->t_max); /* the valid range widens */ |
| 112 |
++(dest->delta_nknots); /* increment the extra knot value counter */ |
| 113 |
} |
| 114 |
} |
| 115 |
dest->unified_nknots=new_cnt; |
| 116 |
qsort((void *)dest_knot,(size_t)new_cnt,(size_t)sizeof(GLfloat), |
| 117 |
&knot_sort); |
| 118 |
} |
| 119 |
|
| 120 |
/* basing on the new common knot range for all attributes set */ |
| 121 |
/* t_min and t_max values for each knot - they will be used later on */ |
| 122 |
/* by explode_knot() and calc_new_ctrl_pts */ |
| 123 |
static void |
| 124 |
set_new_t_min_t_max(knot_str_type *geom_knot, knot_str_type *color_knot, |
| 125 |
knot_str_type *normal_knot, knot_str_type *texture_knot, |
| 126 |
GLfloat maximal_min_knot, GLfloat minimal_max_knot) |
| 127 |
{ |
| 128 |
GLuint t_min,t_max,cnt; |
| 129 |
|
| 130 |
if(minimal_max_knot-maximal_min_knot < EPSILON) |
| 131 |
{ |
| 132 |
/* knot common range empty */ |
| 133 |
geom_knot->t_min=geom_knot->t_max=0; |
| 134 |
color_knot->t_min=color_knot->t_max=0; |
| 135 |
normal_knot->t_min=normal_knot->t_max=0; |
| 136 |
texture_knot->t_min=texture_knot->t_max=0; |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
if(geom_knot->unified_knot!=NULL) |
| 141 |
{ |
| 142 |
cnt=geom_knot->unified_nknots; |
| 143 |
for(t_min=0;t_min<cnt;t_min++) |
| 144 |
if(fabs((geom_knot->unified_knot)[t_min] - maximal_min_knot) < |
| 145 |
EPSILON) |
| 146 |
break; |
| 147 |
for(t_max=cnt-1;t_max;t_max--) |
| 148 |
if(fabs((geom_knot->unified_knot)[t_max] - minimal_max_knot) < |
| 149 |
EPSILON) |
| 150 |
break; |
| 151 |
} |
| 152 |
else |
| 153 |
if(geom_knot->nknots) |
| 154 |
{ |
| 155 |
cnt=geom_knot->nknots; |
| 156 |
for(t_min=0;t_min<cnt;t_min++) |
| 157 |
if(fabs((geom_knot->knot)[t_min] - maximal_min_knot) < EPSILON) |
| 158 |
break; |
| 159 |
for(t_max=cnt-1;t_max;t_max--) |
| 160 |
if(fabs((geom_knot->knot)[t_max] - minimal_max_knot) < EPSILON) |
| 161 |
break; |
| 162 |
} |
| 163 |
geom_knot->t_min=t_min; |
| 164 |
geom_knot->t_max=t_max; |
| 165 |
if(color_knot->unified_knot!=NULL) |
| 166 |
{ |
| 167 |
cnt=color_knot->unified_nknots; |
| 168 |
for(t_min=0;t_min<cnt;t_min++) |
| 169 |
if(fabs((color_knot->unified_knot)[t_min] - maximal_min_knot) < |
| 170 |
EPSILON) |
| 171 |
break; |
| 172 |
for(t_max=cnt-1;t_max;t_max--) |
| 173 |
if(fabs((color_knot->unified_knot)[t_max] - minimal_max_knot) < |
| 174 |
EPSILON) |
| 175 |
break; |
| 176 |
color_knot->t_min=t_min; |
| 177 |
color_knot->t_max=t_max; |
| 178 |
} |
| 179 |
if(normal_knot->unified_knot!=NULL) |
| 180 |
{ |
| 181 |
cnt=normal_knot->unified_nknots; |
| 182 |
for(t_min=0;t_min<cnt;t_min++) |
| 183 |
if(fabs((normal_knot->unified_knot)[t_min] - maximal_min_knot) < |
| 184 |
EPSILON) |
| 185 |
break; |
| 186 |
for(t_max=cnt-1;t_max;t_max--) |
| 187 |
if(fabs((normal_knot->unified_knot)[t_max] - minimal_max_knot) < |
| 188 |
EPSILON) |
| 189 |
break; |
| 190 |
normal_knot->t_min=t_min; |
| 191 |
normal_knot->t_max=t_max; |
| 192 |
} |
| 193 |
if(texture_knot->unified_knot!=NULL) |
| 194 |
{ |
| 195 |
cnt=texture_knot->unified_nknots; |
| 196 |
for(t_min=0;t_min<cnt;t_min++) |
| 197 |
if(fabs((texture_knot->unified_knot)[t_min] - maximal_min_knot) |
| 198 |
< EPSILON) |
| 199 |
break; |
| 200 |
for(t_max=cnt-1;t_max;t_max--) |
| 201 |
if(fabs((texture_knot->unified_knot)[t_max] - minimal_max_knot) |
| 202 |
< EPSILON) |
| 203 |
break; |
| 204 |
texture_knot->t_min=t_min; |
| 205 |
texture_knot->t_max=t_max; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/* modify all knot valid ranges in such a way that all have the same */ |
| 211 |
/* range, common to all knots */ |
| 212 |
/* do this by knot insertion */ |
| 213 |
GLenum |
| 214 |
select_knot_working_range(GLUnurbsObj *nobj,knot_str_type *geom_knot, |
| 215 |
knot_str_type *color_knot, knot_str_type *normal_knot, |
| 216 |
knot_str_type *texture_knot) |
| 217 |
{ |
| 218 |
GLint max_nknots; |
| 219 |
GLfloat maximal_min_knot,minimal_max_knot; |
| 220 |
GLint i; |
| 221 |
|
| 222 |
/* find the maximum modified knot length */ |
| 223 |
max_nknots=geom_knot->nknots; |
| 224 |
if(color_knot->unified_knot) |
| 225 |
max_nknots+=color_knot->nknots; |
| 226 |
if(normal_knot->unified_knot) |
| 227 |
max_nknots+=normal_knot->nknots; |
| 228 |
if(texture_knot->unified_knot) |
| 229 |
max_nknots+=texture_knot->nknots; |
| 230 |
maximal_min_knot=(geom_knot->knot)[geom_knot->t_min]; |
| 231 |
minimal_max_knot=(geom_knot->knot)[geom_knot->t_max]; |
| 232 |
/* any attirb data ? */ |
| 233 |
if(max_nknots!=geom_knot->nknots) |
| 234 |
{ |
| 235 |
/* allocate space for the unified knots */ |
| 236 |
if((geom_knot->unified_knot= |
| 237 |
(GLfloat *)malloc(sizeof(GLfloat)*max_nknots))==NULL) |
| 238 |
{ |
| 239 |
call_user_error(nobj,GLU_OUT_OF_MEMORY); |
| 240 |
return GLU_ERROR; |
| 241 |
} |
| 242 |
/* copy the original knot to the unified one */ |
| 243 |
geom_knot->unified_nknots=geom_knot->nknots; |
| 244 |
for(i=0;i<geom_knot->nknots;i++) |
| 245 |
(geom_knot->unified_knot)[i]=(geom_knot->knot)[i]; |
| 246 |
if(color_knot->unified_knot) |
| 247 |
{ |
| 248 |
if((color_knot->knot)[color_knot->t_min] - maximal_min_knot > |
| 249 |
EPSILON) |
| 250 |
maximal_min_knot=(color_knot->knot)[color_knot->t_min]; |
| 251 |
if(minimal_max_knot - (color_knot->knot)[color_knot->t_max] > |
| 252 |
EPSILON) |
| 253 |
minimal_max_knot=(color_knot->knot)[color_knot->t_max]; |
| 254 |
if((color_knot->unified_knot= |
| 255 |
(GLfloat *)malloc(sizeof(GLfloat)*max_nknots))==NULL) |
| 256 |
{ |
| 257 |
free(geom_knot->unified_knot); |
| 258 |
call_user_error(nobj,GLU_OUT_OF_MEMORY); |
| 259 |
return GLU_ERROR; |
| 260 |
} |
| 261 |
/* copy the original knot to the unified one */ |
| 262 |
color_knot->unified_nknots=color_knot->nknots; |
| 263 |
for(i=0;i<color_knot->nknots;i++) |
| 264 |
(color_knot->unified_knot)[i]=(color_knot->knot)[i]; |
| 265 |
} |
| 266 |
if(normal_knot->unified_knot) |
| 267 |
{ |
| 268 |
if((normal_knot->knot)[normal_knot->t_min] - maximal_min_knot > |
| 269 |
EPSILON) |
| 270 |
maximal_min_knot=(normal_knot->knot)[normal_knot->t_min]; |
| 271 |
if(minimal_max_knot - (normal_knot->knot)[normal_knot->t_max] > |
| 272 |
EPSILON) |
| 273 |
minimal_max_knot=(normal_knot->knot)[normal_knot->t_max]; |
| 274 |
if((normal_knot->unified_knot= |
| 275 |
(GLfloat *)malloc(sizeof(GLfloat)*max_nknots))==NULL) |
| 276 |
{ |
| 277 |
free(geom_knot->unified_knot); |
| 278 |
free(color_knot->unified_knot); |
| 279 |
call_user_error(nobj,GLU_OUT_OF_MEMORY); |
| 280 |
return GLU_ERROR; |
| 281 |
} |
| 282 |
/* copy the original knot to the unified one */ |
| 283 |
normal_knot->unified_nknots=normal_knot->nknots; |
| 284 |
for(i=0;i<normal_knot->nknots;i++) |
| 285 |
(normal_knot->unified_knot)[i]=(normal_knot->knot)[i]; |
| 286 |
} |
| 287 |
if(texture_knot->unified_knot) |
| 288 |
{ |
| 289 |
if((texture_knot->knot)[texture_knot->t_min] - maximal_min_knot > |
| 290 |
EPSILON) |
| 291 |
maximal_min_knot=(texture_knot->knot)[texture_knot->t_min]; |
| 292 |
if(minimal_max_knot - (texture_knot->knot)[texture_knot->t_max] > |
| 293 |
EPSILON) |
| 294 |
minimal_max_knot=(texture_knot->knot)[texture_knot->t_max]; |
| 295 |
if((texture_knot->unified_knot= |
| 296 |
(GLfloat *)malloc(sizeof(GLfloat)*max_nknots))==NULL) |
| 297 |
{ |
| 298 |
free(geom_knot->unified_knot); |
| 299 |
free(color_knot->unified_knot); |
| 300 |
free(normal_knot->unified_knot); |
| 301 |
call_user_error(nobj,GLU_OUT_OF_MEMORY); |
| 302 |
return GLU_ERROR; |
| 303 |
} |
| 304 |
/* copy the original knot to the unified one */ |
| 305 |
texture_knot->unified_nknots=texture_knot->nknots; |
| 306 |
for(i=0;i<texture_knot->nknots;i++) |
| 307 |
(texture_knot->unified_knot)[i]=(texture_knot->knot)[i]; |
| 308 |
} |
| 309 |
/* work on the geometry knot with all additional knot values */ |
| 310 |
/* appearing in attirbutive knots */ |
| 311 |
if(minimal_max_knot-maximal_min_knot < EPSILON) |
| 312 |
{ |
| 313 |
/* empty working range */ |
| 314 |
geom_knot->unified_nknots=0; |
| 315 |
color_knot->unified_nknots=0; |
| 316 |
normal_knot->unified_nknots=0; |
| 317 |
texture_knot->unified_nknots=0; |
| 318 |
} |
| 319 |
else |
| 320 |
{ |
| 321 |
if(color_knot->unified_knot) |
| 322 |
collect_unified_knot(geom_knot,color_knot,maximal_min_knot, |
| 323 |
minimal_max_knot); |
| 324 |
if(normal_knot->unified_knot) |
| 325 |
collect_unified_knot(geom_knot,normal_knot,maximal_min_knot, |
| 326 |
minimal_max_knot); |
| 327 |
if(texture_knot->unified_knot) |
| 328 |
collect_unified_knot(geom_knot,texture_knot,maximal_min_knot, |
| 329 |
minimal_max_knot); |
| 330 |
/* since we have now built the "unified" geometry knot */ |
| 331 |
/* add same knot values to all attributive knots */ |
| 332 |
if(color_knot->unified_knot) |
| 333 |
collect_unified_knot(color_knot,geom_knot,maximal_min_knot, |
| 334 |
minimal_max_knot); |
| 335 |
if(normal_knot->unified_knot) |
| 336 |
collect_unified_knot(normal_knot,geom_knot,maximal_min_knot, |
| 337 |
minimal_max_knot); |
| 338 |
if(texture_knot->unified_knot) |
| 339 |
collect_unified_knot(texture_knot,geom_knot,maximal_min_knot, |
| 340 |
minimal_max_knot); |
| 341 |
} |
| 342 |
} |
| 343 |
set_new_t_min_t_max(geom_knot,color_knot,normal_knot,texture_knot, |
| 344 |
maximal_min_knot,minimal_max_knot); |
| 345 |
return GLU_NO_ERROR; |
| 346 |
} |
| 347 |
|
| 348 |
void |
| 349 |
free_unified_knots(knot_str_type *geom_knot, knot_str_type *color_knot, |
| 350 |
knot_str_type *normal_knot, knot_str_type *texture_knot) |
| 351 |
{ |
| 352 |
if(geom_knot->unified_knot) |
| 353 |
free(geom_knot->unified_knot); |
| 354 |
if(color_knot->unified_knot) |
| 355 |
free(color_knot->unified_knot); |
| 356 |
if(normal_knot->unified_knot) |
| 357 |
free(normal_knot->unified_knot); |
| 358 |
if(texture_knot->unified_knot) |
| 359 |
free(texture_knot->unified_knot); |
| 360 |
} |
| 361 |
|
| 362 |
GLenum |
| 363 |
explode_knot(knot_str_type *the_knot) |
| 364 |
{ |
| 365 |
GLfloat *knot,*new_knot; |
| 366 |
GLint nknots,n_new_knots=0; |
| 367 |
GLint t_min,t_max; |
| 368 |
GLint ord; |
| 369 |
GLsizei i,j,k; |
| 370 |
GLfloat tmp_float; |
| 371 |
|
| 372 |
if(the_knot->unified_knot) |
| 373 |
{ |
| 374 |
knot=the_knot->unified_knot; |
| 375 |
nknots=the_knot->unified_nknots; |
| 376 |
} |
| 377 |
else |
| 378 |
{ |
| 379 |
knot=the_knot->knot; |
| 380 |
nknots=the_knot->nknots; |
| 381 |
} |
| 382 |
ord=the_knot->order; |
| 383 |
t_min=the_knot->t_min; |
| 384 |
t_max=the_knot->t_max; |
| 385 |
|
| 386 |
for(i=t_min;i<=t_max;) |
| 387 |
{ |
| 388 |
tmp_float=knot[i]; |
| 389 |
for(j=0;j<ord && (i+j)<=t_max;j++) |
| 390 |
if(fabs(tmp_float-knot[i+j])>EPSILON) |
| 391 |
break; |
| 392 |
n_new_knots+=ord-j; |
| 393 |
i+=j; |
| 394 |
} |
| 395 |
/* alloc space for new_knot */ |
| 396 |
if((new_knot=(GLfloat *)malloc(sizeof(GLfloat)*(nknots+n_new_knots)))==NULL) |
| 397 |
{ |
| 398 |
return GLU_OUT_OF_MEMORY; |
| 399 |
} |
| 400 |
/* fill in new knot */ |
| 401 |
for(j=0;j<t_min;j++) |
| 402 |
new_knot[j]=knot[j]; |
| 403 |
for(i=j;i<=t_max;i++) |
| 404 |
{ |
| 405 |
tmp_float=knot[i]; |
| 406 |
for(k=0;k<ord;k++) |
| 407 |
{ |
| 408 |
new_knot[j++]=knot[i]; |
| 409 |
if(tmp_float==knot[i+1]) |
| 410 |
i++; |
| 411 |
} |
| 412 |
} |
| 413 |
for(i=t_max+1;i<(int)nknots;i++) |
| 414 |
new_knot[j++]=knot[i]; |
| 415 |
/* fill in the knot structure */ |
| 416 |
the_knot->new_knot=new_knot; |
| 417 |
the_knot->delta_nknots+=n_new_knots; |
| 418 |
the_knot->t_max+=n_new_knots; |
| 419 |
return GLU_NO_ERROR; |
| 420 |
} |
| 421 |
|
| 422 |
GLenum |
| 423 |
calc_alphas(knot_str_type *the_knot) |
| 424 |
{ |
| 425 |
GLfloat tmp_float; |
| 426 |
int i,j,k,m,n; |
| 427 |
int order; |
| 428 |
GLfloat *alpha,*alpha_new,*tmp_alpha; |
| 429 |
GLfloat denom; |
| 430 |
GLfloat *knot,*new_knot; |
| 431 |
|
| 432 |
|
| 433 |
knot=the_knot->knot; |
| 434 |
order=the_knot->order; |
| 435 |
new_knot=the_knot->new_knot; |
| 436 |
n=the_knot->nknots-the_knot->order; |
| 437 |
m=n+the_knot->delta_nknots; |
| 438 |
if((alpha=(GLfloat *)malloc(sizeof(GLfloat)*n*m))==NULL) |
| 439 |
{ |
| 440 |
return GLU_OUT_OF_MEMORY; |
| 441 |
} |
| 442 |
if((alpha_new=(GLfloat *)malloc(sizeof(GLfloat)*n*m))==NULL) |
| 443 |
{ |
| 444 |
free(alpha); |
| 445 |
return GLU_OUT_OF_MEMORY; |
| 446 |
} |
| 447 |
for(j=0;j<m;j++) |
| 448 |
{ |
| 449 |
for(i=0;i<n;i++) |
| 450 |
{ |
| 451 |
if((knot[i] <= new_knot[j]) && (new_knot[j] < knot[i+1])) |
| 452 |
tmp_float=1.0; |
| 453 |
else |
| 454 |
tmp_float=0.0; |
| 455 |
alpha[i+j*n]=tmp_float; |
| 456 |
} |
| 457 |
} |
| 458 |
for(k=1;k<order;k++) |
| 459 |
{ |
| 460 |
for(j=0;j<m;j++) |
| 461 |
for(i=0;i<n;i++) |
| 462 |
{ |
| 463 |
denom=knot[i+k]-knot[i]; |
| 464 |
if(fabs(denom)<EPSILON) |
| 465 |
tmp_float=0.0; |
| 466 |
else |
| 467 |
tmp_float=(new_knot[j+k]-knot[i])/denom* |
| 468 |
alpha[i+j*n]; |
| 469 |
denom=knot[i+k+1]-knot[i+1]; |
| 470 |
if(fabs(denom)>EPSILON) |
| 471 |
tmp_float+=(knot[i+k+1]-new_knot[j+k])/denom* |
| 472 |
alpha[(i+1)+j*n]; |
| 473 |
alpha_new[i+j*n]=tmp_float; |
| 474 |
} |
| 475 |
tmp_alpha=alpha_new; |
| 476 |
alpha_new=alpha; |
| 477 |
alpha=tmp_alpha; |
| 478 |
} |
| 479 |
the_knot->alpha=alpha; |
| 480 |
free(alpha_new); |
| 481 |
return GLU_NO_ERROR; |
| 482 |
} |
| 483 |
|
| 484 |
GLenum |
| 485 |
calc_new_ctrl_pts(GLfloat *ctrl,GLint stride,knot_str_type *the_knot, |
| 486 |
GLint dim,GLfloat **new_ctrl,GLint *ncontrol) |
| 487 |
{ |
| 488 |
GLsizei i,j,k,l,m,n; |
| 489 |
GLsizei index1,index2; |
| 490 |
GLfloat *alpha; |
| 491 |
GLfloat *new_knot; |
| 492 |
|
| 493 |
new_knot=the_knot->new_knot; |
| 494 |
n=the_knot->nknots-the_knot->order; |
| 495 |
alpha=the_knot->alpha; |
| 496 |
|
| 497 |
m=the_knot->t_max+1-the_knot->t_min-the_knot->order; |
| 498 |
k=the_knot->t_min; |
| 499 |
/* allocate space for new control points */ |
| 500 |
if((*new_ctrl=(GLfloat *)malloc(sizeof(GLfloat)*dim*m))==NULL) |
| 501 |
{ |
| 502 |
return GLU_OUT_OF_MEMORY; |
| 503 |
} |
| 504 |
for(j=0;j<m;j++) |
| 505 |
{ |
| 506 |
for(l=0;l<dim;l++) |
| 507 |
(*new_ctrl)[j*dim+l]=0.0; |
| 508 |
for(i=0;i<n;i++) |
| 509 |
{ |
| 510 |
index1=i+(j+k)*n; |
| 511 |
index2=i*stride; |
| 512 |
for(l=0;l<dim;l++) |
| 513 |
(*new_ctrl)[j*dim+l]+=alpha[index1]*ctrl[index2+l]; |
| 514 |
} |
| 515 |
} |
| 516 |
*ncontrol=(GLint)m; |
| 517 |
return GLU_NO_ERROR; |
| 518 |
} |
| 519 |
|
| 520 |
static GLint |
| 521 |
calc_factor(GLfloat *pts,GLint order,GLint indx,GLint stride,GLfloat tolerance, |
| 522 |
GLint dim) |
| 523 |
{ |
| 524 |
GLdouble model[16],proj[16]; |
| 525 |
GLint viewport[4]; |
| 526 |
GLdouble x,y,z,w,winx1,winy1,winz,winx2,winy2; |
| 527 |
GLint i; |
| 528 |
GLdouble len,dx,dy; |
| 529 |
|
| 530 |
glGetDoublev(GL_MODELVIEW_MATRIX,model); |
| 531 |
glGetDoublev(GL_PROJECTION_MATRIX,proj); |
| 532 |
glGetIntegerv(GL_VIEWPORT,viewport); |
| 533 |
if(dim==4) |
| 534 |
{ |
| 535 |
w=(GLdouble)pts[indx+3]; |
| 536 |
x=(GLdouble)pts[indx]/w; |
| 537 |
y=(GLdouble)pts[indx+1]/w; |
| 538 |
z=(GLdouble)pts[indx+2]/w; |
| 539 |
gluProject(x,y,z,model,proj,viewport,&winx1,&winy1,&winz); |
| 540 |
len=0.0; |
| 541 |
for(i=1;i<order;i++) |
| 542 |
{ |
| 543 |
w=(GLdouble)pts[indx+i*stride+3]; |
| 544 |
x=(GLdouble)pts[indx+i*stride]/w; |
| 545 |
y=(GLdouble)pts[indx+i*stride+1]/w; |
| 546 |
z=(GLdouble)pts[indx+i*stride+2]/w; |
| 547 |
if(gluProject(x,y,z,model,proj,viewport,&winx2,&winy2,&winz)) |
| 548 |
{ |
| 549 |
dx=winx2-winx1; |
| 550 |
dy=winy2-winy1; |
| 551 |
len+=sqrt(dx*dx+dy*dy); |
| 552 |
} |
| 553 |
winx1=winx2; winy1=winy2; |
| 554 |
} |
| 555 |
} |
| 556 |
else |
| 557 |
{ |
| 558 |
x=(GLdouble)pts[indx]; |
| 559 |
y=(GLdouble)pts[indx+1]; |
| 560 |
if(dim==2) |
| 561 |
z=0.0; |
| 562 |
else |
| 563 |
z=(GLdouble)pts[indx+2]; |
| 564 |
gluProject(x,y,z,model,proj,viewport,&winx1,&winy1,&winz); |
| 565 |
len=0.0; |
| 566 |
for(i=1;i<order;i++) |
| 567 |
{ |
| 568 |
x=(GLdouble)pts[indx+i*stride]; |
| 569 |
y=(GLdouble)pts[indx+i*stride+1]; |
| 570 |
if(dim==2) |
| 571 |
z=0.0; |
| 572 |
else |
| 573 |
z=(GLdouble)pts[indx+i*stride+2]; |
| 574 |
if(gluProject(x,y,z,model,proj,viewport,&winx2,&winy2,&winz)) |
| 575 |
{ |
| 576 |
dx=winx2-winx1; |
| 577 |
dy=winy2-winy1; |
| 578 |
len+=sqrt(dx*dx+dy*dy); |
| 579 |
} |
| 580 |
winx1=winx2; winy1=winy2; |
| 581 |
} |
| 582 |
} |
| 583 |
len /= tolerance; |
| 584 |
return ((GLint)len+1); |
| 585 |
} |
| 586 |
|
| 587 |
static GLenum |
| 588 |
calc_sampling_3D(new_ctrl_type *new_ctrl, GLfloat tolerance, GLint dim, |
| 589 |
GLint uorder, GLint vorder, GLint **ufactors, GLint **vfactors) |
| 590 |
{ |
| 591 |
GLfloat *ctrl; |
| 592 |
GLint tmp_factor1,tmp_factor2; |
| 593 |
GLint ufactor_cnt,vfactor_cnt; |
| 594 |
GLint offset1,offset2,offset3; |
| 595 |
GLint i,j; |
| 596 |
|
| 597 |
ufactor_cnt=new_ctrl->s_bezier_cnt; |
| 598 |
vfactor_cnt=new_ctrl->t_bezier_cnt; |
| 599 |
if((*ufactors=(GLint *)malloc(sizeof(GLint)*ufactor_cnt*3)) |
| 600 |
==NULL) |
| 601 |
{ |
| 602 |
return GLU_OUT_OF_MEMORY; |
| 603 |
} |
| 604 |
if((*vfactors=(GLint *)malloc(sizeof(GLint)*vfactor_cnt*3)) |
| 605 |
==NULL) |
| 606 |
{ |
| 607 |
free(ufactors); |
| 608 |
return GLU_OUT_OF_MEMORY; |
| 609 |
} |
| 610 |
ctrl=new_ctrl->geom_ctrl; |
| 611 |
offset1=new_ctrl->geom_t_stride*vorder; |
| 612 |
offset2=new_ctrl->geom_s_stride*uorder; |
| 613 |
for(j=0;j<vfactor_cnt;j++) |
| 614 |
{ |
| 615 |
*(*vfactors+j*3+1)=tmp_factor1=calc_factor(ctrl,vorder, |
| 616 |
j*offset1,dim,tolerance,dim); |
| 617 |
/* loop ufactor_cnt-1 times */ |
| 618 |
for(i=1;i<ufactor_cnt;i++) |
| 619 |
{ |
| 620 |
tmp_factor2=calc_factor(ctrl,vorder, |
| 621 |
j*offset1+i*offset2,dim,tolerance,dim); |
| 622 |
if(tmp_factor2>tmp_factor1) |
| 623 |
tmp_factor1=tmp_factor2; |
| 624 |
} |
| 625 |
/* last time for the opposite edge */ |
| 626 |
*(*vfactors+j*3+2)=tmp_factor2=calc_factor(ctrl,vorder, |
| 627 |
j*offset1+i*offset2-new_ctrl->geom_s_stride, |
| 628 |
dim,tolerance,dim); |
| 629 |
if(tmp_factor2>tmp_factor1) |
| 630 |
*(*vfactors+j*3)=tmp_factor2; |
| 631 |
else |
| 632 |
*(*vfactors+j*3)=tmp_factor1; |
| 633 |
} |
| 634 |
offset3=new_ctrl->geom_s_stride; |
| 635 |
offset2=new_ctrl->geom_s_stride*uorder; |
| 636 |
for(j=0;j<ufactor_cnt;j++) |
| 637 |
{ |
| 638 |
*(*ufactors+j*3+1)=tmp_factor1=calc_factor(ctrl,uorder, |
| 639 |
j*offset2,offset3,tolerance,dim); |
| 640 |
/* loop vfactor_cnt-1 times */ |
| 641 |
for(i=1;i<vfactor_cnt;i++) |
| 642 |
{ |
| 643 |
tmp_factor2=calc_factor(ctrl,uorder, |
| 644 |
j*offset2+i*offset1,offset3,tolerance,dim); |
| 645 |
if(tmp_factor2>tmp_factor1) |
| 646 |
tmp_factor1=tmp_factor2; |
| 647 |
} |
| 648 |
/* last time for the opposite edge */ |
| 649 |
*(*ufactors+j*3+2)=tmp_factor2=calc_factor(ctrl,uorder, |
| 650 |
j*offset2+i*offset1-new_ctrl->geom_t_stride, |
| 651 |
offset3,tolerance,dim); |
| 652 |
if(tmp_factor2>tmp_factor1) |
| 653 |
*(*ufactors+j*3)=tmp_factor2; |
| 654 |
else |
| 655 |
*(*ufactors+j*3)=tmp_factor1; |
| 656 |
} |
| 657 |
return GL_NO_ERROR; |
| 658 |
} |
| 659 |
|
| 660 |
static GLenum |
| 661 |
calc_sampling_2D(GLfloat *ctrl, GLint cnt, GLint order, |
| 662 |
GLfloat tolerance, GLint dim, GLint **factors) |
| 663 |
{ |
| 664 |
GLint factor_cnt; |
| 665 |
GLint tmp_factor; |
| 666 |
GLint offset; |
| 667 |
GLint i; |
| 668 |
|
| 669 |
factor_cnt=cnt/order; |
| 670 |
if((*factors=(GLint *)malloc(sizeof(GLint)*factor_cnt))==NULL) |
| 671 |
{ |
| 672 |
return GLU_OUT_OF_MEMORY; |
| 673 |
} |
| 674 |
offset=order*dim; |
| 675 |
for(i=0;i<factor_cnt;i++) |
| 676 |
{ |
| 677 |
tmp_factor=calc_factor(ctrl,order,i*offset,dim,tolerance,dim); |
| 678 |
if(tmp_factor == 0) |
| 679 |
(*factors)[i]=1; |
| 680 |
else |
| 681 |
(*factors)[i]=tmp_factor; |
| 682 |
} |
| 683 |
return GL_NO_ERROR; |
| 684 |
} |
| 685 |
|
| 686 |
static void |
| 687 |
set_sampling_and_culling( GLUnurbsObj *nobj ) |
| 688 |
{ |
| 689 |
if(nobj->auto_load_matrix==GL_FALSE) |
| 690 |
{ |
| 691 |
GLint i; |
| 692 |
GLfloat m[4]; |
| 693 |
|
| 694 |
glPushAttrib( (GLbitfield) (GL_VIEWPORT_BIT | GL_TRANSFORM_BIT)); |
| 695 |
for(i=0;i<4;i++) |
| 696 |
m[i]=nobj->sampling_matrices.viewport[i]; |
| 697 |
glViewport(m[0],m[1],m[2],m[3]); |
| 698 |
glMatrixMode(GL_PROJECTION); |
| 699 |
glPushMatrix(); |
| 700 |
glLoadMatrixf(nobj->sampling_matrices.proj); |
| 701 |
glMatrixMode(GL_MODELVIEW); |
| 702 |
glPushMatrix(); |
| 703 |
glLoadMatrixf(nobj->sampling_matrices.model); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
static void |
| 708 |
revert_sampling_and_culling( GLUnurbsObj *nobj ) |
| 709 |
{ |
| 710 |
if(nobj->auto_load_matrix==GL_FALSE) |
| 711 |
{ |
| 712 |
glMatrixMode(GL_MODELVIEW); |
| 713 |
glPopMatrix(); |
| 714 |
glMatrixMode(GL_PROJECTION); |
| 715 |
glPopMatrix(); |
| 716 |
glPopAttrib(); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
GLenum |
| 721 |
glu_do_sampling_3D( GLUnurbsObj *nobj, new_ctrl_type *new_ctrl, |
| 722 |
GLint **sfactors, GLint **tfactors) |
| 723 |
{ |
| 724 |
GLint dim; |
| 725 |
GLenum err; |
| 726 |
|
| 727 |
*sfactors=NULL; |
| 728 |
*tfactors=NULL; |
| 729 |
dim=nobj->surface.geom.dim; |
| 730 |
set_sampling_and_culling(nobj); |
| 731 |
if((err=calc_sampling_3D(new_ctrl,nobj->sampling_tolerance,dim, |
| 732 |
nobj->surface.geom.sorder,nobj->surface.geom.torder, |
| 733 |
sfactors,tfactors))==GLU_ERROR) |
| 734 |
{ |
| 735 |
revert_sampling_and_culling(nobj); |
| 736 |
call_user_error(nobj,err); |
| 737 |
return GLU_ERROR; |
| 738 |
} |
| 739 |
revert_sampling_and_culling(nobj); |
| 740 |
return GLU_NO_ERROR; |
| 741 |
} |
| 742 |
|
| 743 |
GLenum |
| 744 |
glu_do_sampling_2D( GLUnurbsObj *nobj, GLfloat *ctrl, GLint cnt, GLint order, |
| 745 |
GLint dim, GLint **factors) |
| 746 |
{ |
| 747 |
GLenum err; |
| 748 |
|
| 749 |
*factors=NULL; |
| 750 |
set_sampling_and_culling(nobj); |
| 751 |
if((err=calc_sampling_2D(ctrl,cnt,order,nobj->sampling_tolerance,dim, |
| 752 |
factors))==GLU_ERROR) |
| 753 |
{ |
| 754 |
revert_sampling_and_culling(nobj); |
| 755 |
call_user_error(nobj,err); |
| 756 |
return GLU_ERROR; |
| 757 |
} |
| 758 |
revert_sampling_and_culling(nobj); |
| 759 |
return GLU_NO_ERROR; |
| 760 |
} |
| 761 |
|
| 762 |
/* TODO - i don't like this culling - this one just tests if at least one */ |
| 763 |
/* ctrl point lies within the viewport . Also the point_in_viewport() */ |
| 764 |
/* should be included in the fnctions for efficiency reasons */ |
| 765 |
|
| 766 |
static GLboolean |
| 767 |
point_in_viewport(GLfloat *pt, GLint dim) |
| 768 |
{ |
| 769 |
GLdouble model[16],proj[16]; |
| 770 |
GLint viewport[4]; |
| 771 |
GLdouble x,y,z,w,winx,winy,winz; |
| 772 |
|
| 773 |
glGetDoublev(GL_MODELVIEW_MATRIX,model); |
| 774 |
glGetDoublev(GL_PROJECTION_MATRIX,proj); |
| 775 |
glGetIntegerv(GL_VIEWPORT,viewport); |
| 776 |
if(dim==3) |
| 777 |
{ |
| 778 |
x=(GLdouble)pt[0]; |
| 779 |
y=(GLdouble)pt[1]; |
| 780 |
z=(GLdouble)pt[2]; |
| 781 |
gluProject(x,y,z,model,proj,viewport,&winx,&winy,&winz); |
| 782 |
} |
| 783 |
else |
| 784 |
{ |
| 785 |
w=(GLdouble)pt[3]; |
| 786 |
x=(GLdouble)pt[0]/w; |
| 787 |
y=(GLdouble)pt[1]/w; |
| 788 |
z=(GLdouble)pt[2]/w; |
| 789 |
gluProject(x,y,z,model,proj,viewport,&winx,&winy,&winz); |
| 790 |
} |
| 791 |
if((GLint)winx >= viewport[0] && (GLint)winx < viewport[2] && |
| 792 |
(GLint)winy >= viewport[1] && (GLint)winy < viewport[3]) |
| 793 |
return GL_TRUE; |
| 794 |
return GL_FALSE; |
| 795 |
} |
| 796 |
|
| 797 |
GLboolean |
| 798 |
fine_culling_test_3D(GLUnurbsObj *nobj,GLfloat *pts,GLint s_cnt,GLint t_cnt, |
| 799 |
GLint s_stride,GLint t_stride, GLint dim) |
| 800 |
{ |
| 801 |
GLint i,j; |
| 802 |
|
| 803 |
if(nobj->culling==GL_FALSE) |
| 804 |
return GL_FALSE; |
| 805 |
set_sampling_and_culling(nobj); |
| 806 |
|
| 807 |
if(dim==3) |
| 808 |
{ |
| 809 |
for(i=0;i<s_cnt;i++) |
| 810 |
for(j=0;j<t_cnt;j++) |
| 811 |
if(point_in_viewport(pts+i*s_stride+j*t_stride,dim)) |
| 812 |
{ |
| 813 |
revert_sampling_and_culling(nobj); |
| 814 |
return GL_FALSE; |
| 815 |
} |
| 816 |
} |
| 817 |
else |
| 818 |
{ |
| 819 |
for(i=0;i<s_cnt;i++) |
| 820 |
for(j=0;j<t_cnt;j++) |
| 821 |
if(point_in_viewport(pts+i*s_stride+j*t_stride,dim)) |
| 822 |
{ |
| 823 |
revert_sampling_and_culling(nobj); |
| 824 |
return GL_FALSE; |
| 825 |
} |
| 826 |
} |
| 827 |
revert_sampling_and_culling(nobj); |
| 828 |
return GL_TRUE; |
| 829 |
} |
| 830 |
|
| 831 |
/*GLboolean |
| 832 |
fine_culling_test_3D(GLUnurbsObj *nobj,GLfloat *pts,GLint s_cnt,GLint t_cnt, |
| 833 |
GLint s_stride,GLint t_stride, GLint dim) |
| 834 |
{ |
| 835 |
GLint visible_cnt; |
| 836 |
GLfloat feedback_buffer[5]; |
| 837 |
GLsizei buffer_size; |
| 838 |
GLint i,j; |
| 839 |
|
| 840 |
if(nobj->culling==GL_FALSE) |
| 841 |
return GL_FALSE; |
| 842 |
buffer_size=5; |
| 843 |
set_sampling_and_culling(nobj); |
| 844 |
|
| 845 |
glFeedbackBuffer(buffer_size,GL_2D,feedback_buffer); |
| 846 |
glRenderMode(GL_FEEDBACK); |
| 847 |
if(dim==3) |
| 848 |
{ |
| 849 |
for(i=0;i<s_cnt;i++) |
| 850 |
{ |
| 851 |
glBegin(GL_LINE_LOOP); |
| 852 |
for(j=0;j<t_cnt;j++) |
| 853 |
glVertex3fv(pts+i*s_stride+j*t_stride); |
| 854 |
glEnd(); |
| 855 |
} |
| 856 |
for(j=0;j<t_cnt;j++) |
| 857 |
{ |
| 858 |
glBegin(GL_LINE_LOOP); |
| 859 |
for(i=0;i<s_cnt;i++) |
| 860 |
glVertex3fv(pts+i*s_stride+j*t_stride); |
| 861 |
glEnd(); |
| 862 |
} |
| 863 |
} |
| 864 |
else |
| 865 |
{ |
| 866 |
for(i=0;i<s_cnt;i++) |
| 867 |
{ |
| 868 |
glBegin(GL_LINE_LOOP); |
| 869 |
for(j=0;j<t_cnt;j++) |
| 870 |
glVertex4fv(pts+i*s_stride+j*t_stride); |
| 871 |
glEnd(); |
| 872 |
} |
| 873 |
for(j=0;j<t_cnt;j++) |
| 874 |
{ |
| 875 |
glBegin(GL_LINE_LOOP); |
| 876 |
for(i=0;i<s_cnt;i++) |
| 877 |
glVertex4fv(pts+i*s_stride+j*t_stride); |
| 878 |
glEnd(); |
| 879 |
} |
| 880 |
} |
| 881 |
visible_cnt=glRenderMode(GL_RENDER); |
| 882 |
|
| 883 |
revert_sampling_and_culling(nobj); |
| 884 |
return (GLboolean)(visible_cnt==0); |
| 885 |
}*/ |
| 886 |
|
| 887 |
GLboolean |
| 888 |
fine_culling_test_2D(GLUnurbsObj *nobj,GLfloat *pts,GLint cnt, |
| 889 |
GLint stride, GLint dim) |
| 890 |
{ |
| 891 |
GLint i; |
| 892 |
|
| 893 |
if(nobj->culling==GL_FALSE) |
| 894 |
return GL_FALSE; |
| 895 |
set_sampling_and_culling(nobj); |
| 896 |
|
| 897 |
if(dim==3) |
| 898 |
{ |
| 899 |
for(i=0;i<cnt;i++) |
| 900 |
if(point_in_viewport(pts+i*stride,dim)) |
| 901 |
{ |
| 902 |
revert_sampling_and_culling(nobj); |
| 903 |
return GL_FALSE; |
| 904 |
} |
| 905 |
} |
| 906 |
else |
| 907 |
{ |
| 908 |
for(i=0;i<cnt;i++) |
| 909 |
if(point_in_viewport(pts+i*stride,dim)) |
| 910 |
{ |
| 911 |
revert_sampling_and_culling(nobj); |
| 912 |
return GL_FALSE; |
| 913 |
} |
| 914 |
} |
| 915 |
revert_sampling_and_culling(nobj); |
| 916 |
return GL_TRUE; |
| 917 |
} |
| 918 |
|
| 919 |
/*GLboolean |
| 920 |
fine_culling_test_2D(GLUnurbsObj *nobj,GLfloat *pts,GLint cnt, |
| 921 |
GLint stride, GLint dim) |
| 922 |
{ |
| 923 |
GLint visible_cnt; |
| 924 |
GLfloat feedback_buffer[5]; |
| 925 |
GLsizei buffer_size; |
| 926 |
GLint i; |
| 927 |
|
| 928 |
if(nobj->culling==GL_FALSE) |
| 929 |
return GL_FALSE; |
| 930 |
buffer_size=5; |
| 931 |
set_sampling_and_culling(nobj); |
| 932 |
|
| 933 |
glFeedbackBuffer(buffer_size,GL_2D,feedback_buffer); |
| 934 |
glRenderMode(GL_FEEDBACK); |
| 935 |
glBegin(GL_LINE_LOOP); |
| 936 |
if(dim==3) |
| 937 |
{ |
| 938 |
for(i=0;i<cnt;i++) |
| 939 |
glVertex3fv(pts+i*stride); |
| 940 |
} |
| 941 |
else |
| 942 |
{ |
| 943 |
for(i=0;i<cnt;i++) |
| 944 |
glVertex4fv(pts+i*stride); |
| 945 |
} |
| 946 |
glEnd(); |
| 947 |
visible_cnt=glRenderMode(GL_RENDER); |
| 948 |
|
| 949 |
revert_sampling_and_culling(nobj); |
| 950 |
return (GLboolean)(visible_cnt==0); |
| 951 |
}*/ |
| 952 |
|