| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: objutil.c,v 2.19 2021/04/23 18:31:45 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Basic .OBJ scene handling routines.
|
| 6 |
*
|
| 7 |
* Created by Greg Ward on Wed Feb 11 2004.
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include <stdlib.h>
|
| 11 |
#include <ctype.h>
|
| 12 |
#include "rtio.h"
|
| 13 |
#include "rtmath.h"
|
| 14 |
#include "rterror.h"
|
| 15 |
#include "lookup.h"
|
| 16 |
#include "objutil.h"
|
| 17 |
|
| 18 |
/* Find an existing name in a list of names */
|
| 19 |
int
|
| 20 |
findName(const char *nm, const char **nmlist, int n)
|
| 21 |
{
|
| 22 |
int i;
|
| 23 |
|
| 24 |
for (i = n; i-- > 0; )
|
| 25 |
if (!strcmp(nmlist[i], nm))
|
| 26 |
break;
|
| 27 |
return(i);
|
| 28 |
}
|
| 29 |
|
| 30 |
/* Clear face selection */
|
| 31 |
void
|
| 32 |
clearSelection(Scene *sc, int set)
|
| 33 |
{
|
| 34 |
Face *f;
|
| 35 |
|
| 36 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 37 |
if (set)
|
| 38 |
f->flags |= FACE_SELECTED;
|
| 39 |
else
|
| 40 |
f->flags &= ~FACE_SELECTED;
|
| 41 |
}
|
| 42 |
|
| 43 |
/* Select faces by object name (modifies current) */
|
| 44 |
void
|
| 45 |
selectGroup(Scene *sc, const char *gname, int invert)
|
| 46 |
{
|
| 47 |
int gid = getGroupID(sc, gname);
|
| 48 |
Face *f;
|
| 49 |
|
| 50 |
if (gid < 0)
|
| 51 |
return;
|
| 52 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 53 |
if (f->grp == gid) {
|
| 54 |
if (invert)
|
| 55 |
f->flags &= ~FACE_SELECTED;
|
| 56 |
else
|
| 57 |
f->flags |= FACE_SELECTED;
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
/* Select faces by material name (modifies current) */
|
| 62 |
void
|
| 63 |
selectMaterial(Scene *sc, const char *mname, int invert)
|
| 64 |
{
|
| 65 |
int mid = getMaterialID(sc, mname);
|
| 66 |
Face *f;
|
| 67 |
|
| 68 |
if (mid < 0)
|
| 69 |
return;
|
| 70 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 71 |
if (f->mat == mid) {
|
| 72 |
if (invert)
|
| 73 |
f->flags &= ~FACE_SELECTED;
|
| 74 |
else
|
| 75 |
f->flags |= FACE_SELECTED;
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
/* Invert face selection */
|
| 80 |
void
|
| 81 |
invertSelection(Scene *sc)
|
| 82 |
{
|
| 83 |
Face *f;
|
| 84 |
|
| 85 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 86 |
f->flags ^= FACE_SELECTED;
|
| 87 |
}
|
| 88 |
|
| 89 |
/* Count selected faces */
|
| 90 |
int
|
| 91 |
numberSelected(Scene *sc)
|
| 92 |
{
|
| 93 |
int nsel = 0;
|
| 94 |
Face *f;
|
| 95 |
|
| 96 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 97 |
nsel += ((f->flags & FACE_SELECTED) != 0);
|
| 98 |
return(nsel);
|
| 99 |
}
|
| 100 |
|
| 101 |
/* Execute callback on indicated faces */
|
| 102 |
int
|
| 103 |
foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *),
|
| 104 |
int flreq, int flexc, void *c_data)
|
| 105 |
{
|
| 106 |
const int fltest = flreq | flexc;
|
| 107 |
int sum = 0;
|
| 108 |
Face *f;
|
| 109 |
|
| 110 |
if ((sc == NULL) | (cb == NULL))
|
| 111 |
return(0);
|
| 112 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 113 |
if ((f->flags & fltest) == flreq) {
|
| 114 |
int res = (*cb)(sc, f, c_data);
|
| 115 |
if (res < 0)
|
| 116 |
return(res);
|
| 117 |
sum += res;
|
| 118 |
}
|
| 119 |
return(sum);
|
| 120 |
}
|
| 121 |
|
| 122 |
/* Callback for removeTexture() */
|
| 123 |
static int
|
| 124 |
remFaceTexture(Scene *sc, Face *f, void *dummy)
|
| 125 |
{
|
| 126 |
int hadTexture = 0;
|
| 127 |
int i;
|
| 128 |
|
| 129 |
for (i = f->nv; i-- > 0; ) {
|
| 130 |
if (f->v[i].tid < 0)
|
| 131 |
continue;
|
| 132 |
f->v[i].tid = -1;
|
| 133 |
hadTexture = 1;
|
| 134 |
}
|
| 135 |
return(hadTexture);
|
| 136 |
}
|
| 137 |
|
| 138 |
/* Remove texture coordinates from the indicated faces */
|
| 139 |
int
|
| 140 |
removeTexture(Scene *sc, int flreq, int flexc)
|
| 141 |
{
|
| 142 |
return(foreachFace(sc, remFaceTexture, flreq, flexc, NULL));
|
| 143 |
}
|
| 144 |
|
| 145 |
/* Callback for removeNormals() */
|
| 146 |
static int
|
| 147 |
remFaceNormal(Scene *sc, Face *f, void *dummy)
|
| 148 |
{
|
| 149 |
int hadNormal = 0;
|
| 150 |
int i;
|
| 151 |
|
| 152 |
for (i = f->nv; i-- > 0; ) {
|
| 153 |
if (f->v[i].nid < 0)
|
| 154 |
continue;
|
| 155 |
f->v[i].nid = -1;
|
| 156 |
hadNormal = 1;
|
| 157 |
}
|
| 158 |
return(hadNormal);
|
| 159 |
}
|
| 160 |
|
| 161 |
/* Remove surface normals from the indicated faces */
|
| 162 |
int
|
| 163 |
removeNormals(Scene *sc, int flreq, int flexc)
|
| 164 |
{
|
| 165 |
return(foreachFace(sc, remFaceNormal, flreq, flexc, NULL));
|
| 166 |
}
|
| 167 |
|
| 168 |
/* Callback for changeGroup() */
|
| 169 |
static int
|
| 170 |
chngFaceGroup(Scene *sc, Face *f, void *ptr)
|
| 171 |
{
|
| 172 |
int grp = *(int *)ptr;
|
| 173 |
|
| 174 |
if (f->grp == grp)
|
| 175 |
return(0);
|
| 176 |
f->grp = grp;
|
| 177 |
return(1);
|
| 178 |
}
|
| 179 |
|
| 180 |
/* Change group for the indicated faces */
|
| 181 |
int
|
| 182 |
changeGroup(Scene *sc, const char *gname, int flreq, int flexc)
|
| 183 |
{
|
| 184 |
int grp = getGroupID(sc, gname);
|
| 185 |
|
| 186 |
if (grp < 0) {
|
| 187 |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
|
| 188 |
sc->grpname[grp=sc->ngrps++] = savqstr((char *)gname);
|
| 189 |
}
|
| 190 |
return(foreachFace(sc, chngFaceGroup, flreq, flexc, (void *)&grp));
|
| 191 |
}
|
| 192 |
|
| 193 |
/* Callback for changeMaterial() */
|
| 194 |
static int
|
| 195 |
chngFaceMaterial(Scene *sc, Face *f, void *ptr)
|
| 196 |
{
|
| 197 |
int mat = *(int *)ptr;
|
| 198 |
|
| 199 |
if (f->mat == mat)
|
| 200 |
return(0);
|
| 201 |
f->mat = mat;
|
| 202 |
return(1);
|
| 203 |
}
|
| 204 |
|
| 205 |
/* Change material for the indicated faces */
|
| 206 |
int
|
| 207 |
changeMaterial(Scene *sc, const char *mname, int flreq, int flexc)
|
| 208 |
{
|
| 209 |
int mat = getMaterialID(sc, mname);
|
| 210 |
|
| 211 |
if (mat < 0) {
|
| 212 |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
|
| 213 |
sc->matname[mat=sc->nmats++] = savqstr((char *)mname);
|
| 214 |
}
|
| 215 |
return(foreachFace(sc, chngFaceMaterial, flreq, flexc, (void *)&mat));
|
| 216 |
}
|
| 217 |
|
| 218 |
/* Compare floating point values for (near) equality */
|
| 219 |
static int
|
| 220 |
fdiffer(double f1, double f2, double eps)
|
| 221 |
{
|
| 222 |
if (f2 != .0)
|
| 223 |
f1 = f1/f2 - 1.;
|
| 224 |
return((f1 > eps) | (f1 < -eps));
|
| 225 |
}
|
| 226 |
|
| 227 |
/* Compare two texture coordinates for (near) equality */
|
| 228 |
static int
|
| 229 |
tex_diff(const TexCoord *t1, const TexCoord *t2, double eps)
|
| 230 |
{
|
| 231 |
if (fdiffer(t1->u, t2->u, eps))
|
| 232 |
return(1);
|
| 233 |
if (fdiffer(t1->v, t2->v, eps))
|
| 234 |
return(1);
|
| 235 |
return(0);
|
| 236 |
}
|
| 237 |
|
| 238 |
/* Compare two surface normals for (near) equality */
|
| 239 |
static int
|
| 240 |
norm_diff(const Normal n1, const Normal n2, double eps)
|
| 241 |
{
|
| 242 |
if (fabs(n1[0]-n2[0]) > eps)
|
| 243 |
return(1);
|
| 244 |
if (fabs(n1[1]-n2[1]) > eps)
|
| 245 |
return(1);
|
| 246 |
if (fabs(n1[2]-n2[2]) > eps)
|
| 247 |
return(1);
|
| 248 |
return(0);
|
| 249 |
}
|
| 250 |
|
| 251 |
/* Replace the given vertex with an equivalent one */
|
| 252 |
static int
|
| 253 |
replace_vertex(Scene *sc, int prev, int repl, double eps)
|
| 254 |
{
|
| 255 |
int repl_tex[10];
|
| 256 |
int repl_norm[10];
|
| 257 |
Face *f, *flast=NULL;
|
| 258 |
int i, j=0;
|
| 259 |
/* check to see if it's even used */
|
| 260 |
if (sc->vert[prev].vflist == NULL)
|
| 261 |
return(0);
|
| 262 |
/* get replacement textures */
|
| 263 |
repl_tex[0] = -1;
|
| 264 |
for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) {
|
| 265 |
/* make sure prev isn't in there */
|
| 266 |
for (j = f->nv; j-- > 0; )
|
| 267 |
if (f->v[j].vid == prev)
|
| 268 |
return(0);
|
| 269 |
for (j = f->nv; j-- > 0; )
|
| 270 |
if (f->v[j].vid == repl)
|
| 271 |
break;
|
| 272 |
if (j < 0)
|
| 273 |
goto linkerr;
|
| 274 |
if (f->v[j].tid < 0)
|
| 275 |
continue;
|
| 276 |
/* see if it's new */
|
| 277 |
for (i = 0; repl_tex[i] >= 0; i++) {
|
| 278 |
if (repl_tex[i] == f->v[j].tid)
|
| 279 |
break;
|
| 280 |
if (!tex_diff(&sc->tex[repl_tex[i]],
|
| 281 |
&sc->tex[f->v[j].tid], eps)) {
|
| 282 |
f->v[j].tid = repl_tex[i];
|
| 283 |
break; /* consolidate */
|
| 284 |
}
|
| 285 |
}
|
| 286 |
if (repl_tex[i] >= 0)
|
| 287 |
continue; /* have this one already */
|
| 288 |
/* else add it */
|
| 289 |
repl_tex[i++] = f->v[j].tid;
|
| 290 |
repl_tex[i] = -1;
|
| 291 |
if (i >= 9)
|
| 292 |
break; /* that's all we have room for */
|
| 293 |
}
|
| 294 |
/* get replacement normals */
|
| 295 |
repl_norm[0] = -1;
|
| 296 |
for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) {
|
| 297 |
for (j = f->nv; j-- > 0; )
|
| 298 |
if (f->v[j].vid == repl)
|
| 299 |
break;
|
| 300 |
if (f->v[j].nid < 0)
|
| 301 |
continue;
|
| 302 |
/* see if it's new */
|
| 303 |
for (i = 0; repl_norm[i] >= 0; i++) {
|
| 304 |
if (repl_norm[i] == f->v[j].nid)
|
| 305 |
break;
|
| 306 |
if (!norm_diff(sc->norm[repl_norm[i]],
|
| 307 |
sc->norm[f->v[j].nid], eps)) {
|
| 308 |
f->v[j].nid = repl_norm[i];
|
| 309 |
break; /* consolidate */
|
| 310 |
}
|
| 311 |
}
|
| 312 |
if (repl_norm[i] >= 0)
|
| 313 |
continue; /* have this one already */
|
| 314 |
/* else add it */
|
| 315 |
repl_norm[i++] = f->v[j].nid;
|
| 316 |
repl_norm[i] = -1;
|
| 317 |
if (i >= 9)
|
| 318 |
break; /* that's all we have room for */
|
| 319 |
}
|
| 320 |
/* replace occurrences of vertex */
|
| 321 |
for (f = sc->vert[prev].vflist; f != NULL; f = f->v[j].fnext) {
|
| 322 |
for (j = f->nv; j-- > 0; )
|
| 323 |
if (f->v[j].vid == prev)
|
| 324 |
break;
|
| 325 |
if (j < 0)
|
| 326 |
goto linkerr;
|
| 327 |
/* XXX doesn't allow for multiple references to prev in face */
|
| 328 |
f->v[j].vid = repl; /* replace vertex itself */
|
| 329 |
if (faceArea(sc, f, NULL) <= FTINY*FTINY)
|
| 330 |
f->flags |= FACE_DEGENERATE;
|
| 331 |
if (f->v[j].tid >= 0) /* replace texture if appropriate */
|
| 332 |
for (i = 0; repl_tex[i] >= 0; i++) {
|
| 333 |
if (repl_tex[i] == f->v[j].tid)
|
| 334 |
break;
|
| 335 |
if (!tex_diff(&sc->tex[repl_tex[i]],
|
| 336 |
&sc->tex[f->v[j].tid], eps)) {
|
| 337 |
f->v[j].tid = repl_tex[i];
|
| 338 |
break;
|
| 339 |
}
|
| 340 |
}
|
| 341 |
if (f->v[j].nid >= 0) /* replace normal if appropriate */
|
| 342 |
for (i = 0; repl_norm[i] >= 0; i++) {
|
| 343 |
if (repl_norm[i] == f->v[j].nid)
|
| 344 |
break;
|
| 345 |
if (!norm_diff(sc->norm[repl_norm[i]],
|
| 346 |
sc->norm[f->v[j].nid], eps)) {
|
| 347 |
f->v[j].nid = repl_norm[i];
|
| 348 |
break;
|
| 349 |
}
|
| 350 |
}
|
| 351 |
flast = f;
|
| 352 |
}
|
| 353 |
/* transfer face list */
|
| 354 |
flast->v[j].fnext = sc->vert[repl].vflist;
|
| 355 |
sc->vert[repl].vflist = sc->vert[prev].vflist;
|
| 356 |
sc->vert[prev].vflist = NULL;
|
| 357 |
return(1);
|
| 358 |
linkerr:
|
| 359 |
error(CONSISTENCY, "Link error in replace_vertex()");
|
| 360 |
return(0); /* shouldn't return */
|
| 361 |
}
|
| 362 |
|
| 363 |
/* Eliminate duplicate vertices, return # joined */
|
| 364 |
int
|
| 365 |
coalesceVertices(Scene *sc, double eps)
|
| 366 |
{
|
| 367 |
int nelim = 0;
|
| 368 |
LUTAB myLookup;
|
| 369 |
LUENT *le;
|
| 370 |
char vertfmt[32], vertbuf[64];
|
| 371 |
double d;
|
| 372 |
int i;
|
| 373 |
|
| 374 |
if (eps >= 1.)
|
| 375 |
return(0);
|
| 376 |
if (sc->nverts <= 3)
|
| 377 |
return(0);
|
| 378 |
/* create hash table */
|
| 379 |
myLookup.hashf = lu_shash;
|
| 380 |
myLookup.keycmp = (lut_keycmpf_t *)strcmp;
|
| 381 |
myLookup.freek = (lut_free_t *)freeqstr;
|
| 382 |
myLookup.freed = NULL;
|
| 383 |
if (!lu_init(&myLookup, sc->nverts))
|
| 384 |
return(0);
|
| 385 |
if (eps <= 5e-15)
|
| 386 |
strcpy(vertfmt, "%.15e %.15e %.15e");
|
| 387 |
else {
|
| 388 |
int nsigdig = 0;
|
| 389 |
for (d = eps; d < 0.5; d *= 10.)
|
| 390 |
++nsigdig;
|
| 391 |
sprintf(vertfmt, "%%.%de %%.%de %%.%de",
|
| 392 |
nsigdig, nsigdig, nsigdig);
|
| 393 |
}
|
| 394 |
/* find coicident vertices */
|
| 395 |
for (i = 0; i < sc->nverts; i++) {
|
| 396 |
if (verbose && !((i+1) & 0x3fff))
|
| 397 |
fprintf(stderr, "%3d%% complete\r", 100*i/sc->nverts);
|
| 398 |
/* check for match */
|
| 399 |
sprintf(vertbuf, vertfmt, sc->vert[i].p[0],
|
| 400 |
sc->vert[i].p[1], sc->vert[i].p[2]);
|
| 401 |
le = lu_find(&myLookup, vertbuf);
|
| 402 |
if (le->data != NULL) { /* coincident vertex */
|
| 403 |
nelim += replace_vertex(sc, i,
|
| 404 |
(Vertex *)le->data - sc->vert, eps);
|
| 405 |
continue;
|
| 406 |
}
|
| 407 |
if (le->key == NULL) /* else create new entry */
|
| 408 |
le->key = savqstr(vertbuf);
|
| 409 |
le->data = (char *)&sc->vert[i];
|
| 410 |
}
|
| 411 |
lu_done(&myLookup); /* clean up */
|
| 412 |
return(nelim);
|
| 413 |
}
|
| 414 |
|
| 415 |
/* Identify duplicate faces */
|
| 416 |
int
|
| 417 |
findDuplicateFaces(Scene *sc)
|
| 418 |
{
|
| 419 |
int nchecked = 0;
|
| 420 |
int nfound = 0;
|
| 421 |
Face *f, *f1;
|
| 422 |
int vid;
|
| 423 |
int i, j;
|
| 424 |
/* start fresh */
|
| 425 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 426 |
f->flags &= ~FACE_DUPLICATE;
|
| 427 |
/* check each face */
|
| 428 |
for (f = sc->flist; f != NULL; f = f->next) {
|
| 429 |
nchecked++;
|
| 430 |
if (verbose && !(nchecked & 0x3fff))
|
| 431 |
fprintf(stderr, "%3d%% complete\r",
|
| 432 |
100*nchecked/sc->nfaces);
|
| 433 |
if (f->flags & FACE_DUPLICATE)
|
| 434 |
continue; /* already identified */
|
| 435 |
vid = f->v[0].vid;
|
| 436 |
/* look for duplicates */
|
| 437 |
for (f1 = sc->vert[vid].vflist; f1 != NULL;
|
| 438 |
f1 = f1->v[j].fnext) {
|
| 439 |
for (j = f1->nv; j-- > 0; )
|
| 440 |
if (f1->v[j].vid == vid)
|
| 441 |
break;
|
| 442 |
if (j < 0)
|
| 443 |
break; /* missing link! */
|
| 444 |
if (f1 == f)
|
| 445 |
continue; /* shouldn't happen */
|
| 446 |
if (f1->flags & FACE_DUPLICATE)
|
| 447 |
continue; /* already marked */
|
| 448 |
if (f1->nv != f->nv)
|
| 449 |
continue; /* couldn't be dup. */
|
| 450 |
for (i = f->nv; --i > 0; )
|
| 451 |
if (f->v[i].vid != f1->v[(j+i)%f1->nv].vid)
|
| 452 |
break; /* vertex mismatch */
|
| 453 |
if (i) {
|
| 454 |
#if DUP_CHECK_REVERSE /* check reverse direction */
|
| 455 |
for (i = f->nv; --i > 0; )
|
| 456 |
if (f1->v[(j+f1->nv-i)%f1->nv].vid
|
| 457 |
!= f->v[i].vid)
|
| 458 |
break;
|
| 459 |
if (i) /* no match */
|
| 460 |
#endif
|
| 461 |
continue;
|
| 462 |
}
|
| 463 |
f1->flags |= FACE_DUPLICATE;
|
| 464 |
++nfound;
|
| 465 |
}
|
| 466 |
}
|
| 467 |
if (verbose)
|
| 468 |
fprintf(stderr, "Found %d duplicate faces\n", nfound);
|
| 469 |
return(nfound);
|
| 470 |
}
|
| 471 |
|
| 472 |
/* Delete indicated faces */
|
| 473 |
int
|
| 474 |
deleteFaces(Scene *sc, int flreq, int flexc)
|
| 475 |
{
|
| 476 |
const int fltest = flreq | flexc;
|
| 477 |
int orig_nfaces = sc->nfaces;
|
| 478 |
Face fhead;
|
| 479 |
Face *f, *ftst;
|
| 480 |
|
| 481 |
fhead.next = sc->flist;
|
| 482 |
f = &fhead;
|
| 483 |
while ((ftst = f->next) != NULL)
|
| 484 |
if ((ftst->flags & fltest) == flreq) {
|
| 485 |
Face *vf; /* remove from vertex lists */
|
| 486 |
int vid, i, j;
|
| 487 |
for (i = 0; i < ftst->nv; i++) {
|
| 488 |
vid = ftst->v[i].vid;
|
| 489 |
vf = sc->vert[vid].vflist;
|
| 490 |
if (vf == ftst) {
|
| 491 |
sc->vert[vid].vflist = ftst->v[i].fnext;
|
| 492 |
continue;
|
| 493 |
}
|
| 494 |
while (vf != NULL) {
|
| 495 |
for (j = vf->nv; j-- > 0; )
|
| 496 |
if (vf->v[j].vid == vid)
|
| 497 |
break;
|
| 498 |
if (j < 0)
|
| 499 |
break; /* error */
|
| 500 |
if (vf->v[j].fnext == ftst) {
|
| 501 |
vf->v[j].fnext =
|
| 502 |
ftst->v[i].fnext;
|
| 503 |
break;
|
| 504 |
}
|
| 505 |
vf = vf->v[j].fnext;
|
| 506 |
}
|
| 507 |
}
|
| 508 |
f->next = ftst->next; /* remove from scene list */
|
| 509 |
efree((char *)ftst);
|
| 510 |
sc->nfaces--;
|
| 511 |
} else
|
| 512 |
f = f->next;
|
| 513 |
sc->flist = fhead.next;
|
| 514 |
return(orig_nfaces - sc->nfaces);
|
| 515 |
}
|
| 516 |
|
| 517 |
/* Compute face area (and normal) */
|
| 518 |
double
|
| 519 |
faceArea(const Scene *sc, const Face *f, Normal nrm)
|
| 520 |
{
|
| 521 |
FVECT fnrm;
|
| 522 |
double area;
|
| 523 |
FVECT v1, v2, v3;
|
| 524 |
double *p0;
|
| 525 |
int i;
|
| 526 |
|
| 527 |
if (f->flags & FACE_DEGENERATE)
|
| 528 |
return(.0); /* should we check this? */
|
| 529 |
fnrm[0] = fnrm[1] = fnrm[2] = .0;
|
| 530 |
p0 = sc->vert[f->v[0].vid].p;
|
| 531 |
VSUB(v1, sc->vert[f->v[1].vid].p, p0);
|
| 532 |
for (i = 2; i < f->nv; i++) {
|
| 533 |
VSUB(v2, sc->vert[f->v[i].vid].p, p0);
|
| 534 |
fcross(v3, v1, v2);
|
| 535 |
VADD(fnrm, fnrm, v3);
|
| 536 |
VCOPY(v1, v2);
|
| 537 |
}
|
| 538 |
area = 0.5*normalize(fnrm);
|
| 539 |
if (nrm != NULL) {
|
| 540 |
if (area != 0.)
|
| 541 |
VCOPY(nrm, fnrm);
|
| 542 |
else
|
| 543 |
nrm[0] = nrm[1] = nrm[2] = 0.;
|
| 544 |
}
|
| 545 |
return(area);
|
| 546 |
}
|
| 547 |
|
| 548 |
/* Add a descriptive comment */
|
| 549 |
void
|
| 550 |
addComment(Scene *sc, const char *comment)
|
| 551 |
{
|
| 552 |
sc->descr = chunk_alloc(char *, sc->descr, sc->ndescr);
|
| 553 |
sc->descr[sc->ndescr++] = savqstr((char *)comment);
|
| 554 |
}
|
| 555 |
|
| 556 |
/* Find index for comment containing the given string (starting from n) */
|
| 557 |
int
|
| 558 |
findComment(Scene *sc, const char *match, int n)
|
| 559 |
{
|
| 560 |
if (n >= sc->ndescr)
|
| 561 |
return(-1);
|
| 562 |
n *= (n > 0);
|
| 563 |
while (n < sc->ndescr)
|
| 564 |
if (strstr(sc->descr[n], match) != NULL)
|
| 565 |
return(n);
|
| 566 |
return(-1);
|
| 567 |
}
|
| 568 |
|
| 569 |
/* Clear comments */
|
| 570 |
void
|
| 571 |
clearComments(Scene *sc)
|
| 572 |
{
|
| 573 |
while (sc->ndescr > 0)
|
| 574 |
freeqstr(sc->descr[--sc->ndescr]);
|
| 575 |
efree((char *)sc->descr);
|
| 576 |
sc->ndescr = 0;
|
| 577 |
}
|
| 578 |
|
| 579 |
/* Add a face to a vertex face list */
|
| 580 |
static void
|
| 581 |
add2facelist(Scene *sc, Face *f, int i)
|
| 582 |
{
|
| 583 |
int vid = f->v[i].vid;
|
| 584 |
Face *fp = sc->vert[vid].vflist;
|
| 585 |
int j;
|
| 586 |
|
| 587 |
f->v[i].fnext = NULL; /* will put at end */
|
| 588 |
if (fp == NULL) { /* new list */
|
| 589 |
sc->vert[vid].vflist = f;
|
| 590 |
return;
|
| 591 |
}
|
| 592 |
for ( ; ; ) { /* else find position */
|
| 593 |
if (fp == f)
|
| 594 |
return; /* already in list */
|
| 595 |
for (j = fp->nv; j-- > 0; )
|
| 596 |
if (fp->v[j].vid == vid)
|
| 597 |
break;
|
| 598 |
if (j < 0)
|
| 599 |
error(CONSISTENCY, "Link error in add2facelist()");
|
| 600 |
if (fp->v[j].fnext == NULL)
|
| 601 |
break; /* reached the end */
|
| 602 |
fp = fp->v[j].fnext;
|
| 603 |
}
|
| 604 |
fp->v[j].fnext = f; /* append new face */
|
| 605 |
}
|
| 606 |
|
| 607 |
/* Add a vertex to a scene */
|
| 608 |
int
|
| 609 |
addVertex(Scene *sc, double x, double y, double z)
|
| 610 |
{
|
| 611 |
sc->vert = chunk_alloc(Vertex, sc->vert, sc->nverts);
|
| 612 |
sc->vert[sc->nverts].p[0] = x;
|
| 613 |
sc->vert[sc->nverts].p[1] = y;
|
| 614 |
sc->vert[sc->nverts].p[2] = z;
|
| 615 |
sc->vert[sc->nverts].vflist = NULL;
|
| 616 |
return(sc->nverts++);
|
| 617 |
}
|
| 618 |
|
| 619 |
/* Add a texture coordinate to a scene */
|
| 620 |
int
|
| 621 |
addTexture(Scene *sc, double u, double v)
|
| 622 |
{
|
| 623 |
sc->tex = chunk_alloc(TexCoord, sc->tex, sc->ntex);
|
| 624 |
sc->tex[sc->ntex].u = u;
|
| 625 |
sc->tex[sc->ntex].v = v;
|
| 626 |
return(sc->ntex++);
|
| 627 |
}
|
| 628 |
|
| 629 |
/* Add a surface normal to a scene */
|
| 630 |
int
|
| 631 |
addNormal(Scene *sc, double xn, double yn, double zn)
|
| 632 |
{
|
| 633 |
FVECT nrm;
|
| 634 |
|
| 635 |
nrm[0] = xn; nrm[1] = yn; nrm[2] = zn;
|
| 636 |
if (normalize(nrm) == .0)
|
| 637 |
return(-1);
|
| 638 |
sc->norm = chunk_alloc(Normal, sc->norm, sc->nnorms);
|
| 639 |
VCOPY(sc->norm[sc->nnorms], nrm);
|
| 640 |
return(sc->nnorms++);
|
| 641 |
}
|
| 642 |
|
| 643 |
/* Set current (last) group */
|
| 644 |
void
|
| 645 |
setGroup(Scene *sc, const char *nm)
|
| 646 |
{
|
| 647 |
sc->lastgrp = getGroupID(sc, nm);
|
| 648 |
if (sc->lastgrp >= 0)
|
| 649 |
return;
|
| 650 |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
|
| 651 |
sc->grpname[sc->lastgrp=sc->ngrps++] = savqstr((char *)nm);
|
| 652 |
}
|
| 653 |
|
| 654 |
/* Set current (last) material */
|
| 655 |
void
|
| 656 |
setMaterial(Scene *sc, const char *nm)
|
| 657 |
{
|
| 658 |
sc->lastmat = getMaterialID(sc, nm);
|
| 659 |
if (sc->lastmat >= 0)
|
| 660 |
return;
|
| 661 |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
|
| 662 |
sc->matname[sc->lastmat=sc->nmats++] = savqstr((char *)nm);
|
| 663 |
}
|
| 664 |
|
| 665 |
/* Add new face to a scene */
|
| 666 |
Face *
|
| 667 |
addFace(Scene *sc, VNDX vid[], int nv)
|
| 668 |
{
|
| 669 |
Face *f;
|
| 670 |
int i;
|
| 671 |
|
| 672 |
if (nv < 3)
|
| 673 |
return(NULL);
|
| 674 |
f = (Face *)emalloc(sizeof(Face)+sizeof(VertEnt)*(nv-3));
|
| 675 |
f->flags = 0;
|
| 676 |
f->nv = nv;
|
| 677 |
f->grp = sc->lastgrp;
|
| 678 |
f->mat = sc->lastmat;
|
| 679 |
for (i = 0; i < nv; i++) { /* add each vertex */
|
| 680 |
int j;
|
| 681 |
f->v[i].vid = vid[i][0];
|
| 682 |
f->v[i].tid = vid[i][1];
|
| 683 |
f->v[i].nid = vid[i][2];
|
| 684 |
f->v[i].fnext = NULL;
|
| 685 |
for (j = i; j-- > 0; )
|
| 686 |
if (f->v[j].vid == vid[i][0])
|
| 687 |
break;
|
| 688 |
if (j < 0) { /* first occurrence? */
|
| 689 |
f->v[i].fnext = sc->vert[vid[i][0]].vflist;
|
| 690 |
sc->vert[vid[i][0]].vflist = f;
|
| 691 |
} else if (nv == 3) /* degenerate triangle? */
|
| 692 |
f->flags |= FACE_DEGENERATE;
|
| 693 |
}
|
| 694 |
f->next = sc->flist; /* push onto face list */
|
| 695 |
sc->flist = f;
|
| 696 |
sc->nfaces++;
|
| 697 |
/* check face area */
|
| 698 |
if (!(f->flags & FACE_DEGENERATE) && faceArea(sc, f, NULL) <= FTINY*FTINY)
|
| 699 |
f->flags |= FACE_DEGENERATE;
|
| 700 |
return(f);
|
| 701 |
}
|
| 702 |
|
| 703 |
/* Get neighbor vertices: malloc array with valence in index[0] */
|
| 704 |
int *
|
| 705 |
getVertexNeighbors(Scene *sc, int vid)
|
| 706 |
{
|
| 707 |
int *varr;
|
| 708 |
Face *f;
|
| 709 |
int j=0;
|
| 710 |
|
| 711 |
if (sc == NULL || (vid < 0) | (vid >= sc->nverts) ||
|
| 712 |
(f = sc->vert[vid].vflist) == NULL)
|
| 713 |
return(NULL);
|
| 714 |
|
| 715 |
varr = (int *)emalloc(sizeof(int)*CHUNKSIZ);
|
| 716 |
varr[0] = 0; /* add unique neighbors */
|
| 717 |
for ( ; f != NULL; f = f->v[j].fnext) {
|
| 718 |
int i, cvi;
|
| 719 |
for (j = f->nv; j--; ) /* find ourself in poly */
|
| 720 |
if (f->v[j].vid == vid)
|
| 721 |
break;
|
| 722 |
if (j < 0) /* this is an error */
|
| 723 |
break;
|
| 724 |
if (f->nv < 3) /* also never happens */
|
| 725 |
continue;
|
| 726 |
/* check previous neighbor */
|
| 727 |
cvi = f->v[ (j > 0) ? j-1 : f->nv-1 ].vid;
|
| 728 |
for (i = varr[0]+1; --i; ) /* making sure not in list */
|
| 729 |
if (varr[i] == cvi)
|
| 730 |
break;
|
| 731 |
if (!i) { /* new neighbor? */
|
| 732 |
varr = chunk_alloc(int, varr, varr[0]+1);
|
| 733 |
varr[++varr[0]] = cvi;
|
| 734 |
}
|
| 735 |
/* check next neighbor */
|
| 736 |
cvi = f->v[ (j < f->nv-1) ? j+1 : 0 ].vid;
|
| 737 |
for (i = varr[0]+1; --i; )
|
| 738 |
if (varr[i] == cvi)
|
| 739 |
break;
|
| 740 |
if (!i) {
|
| 741 |
varr = chunk_alloc(int, varr, varr[0]+1);
|
| 742 |
varr[++varr[0]] = cvi;
|
| 743 |
}
|
| 744 |
}
|
| 745 |
if (!varr[0]) {
|
| 746 |
efree((char *)varr); /* something went awry */
|
| 747 |
return(NULL);
|
| 748 |
}
|
| 749 |
/* tighten array & return */
|
| 750 |
return((int *)erealloc((char *)varr, sizeof(int)*(varr[0]+1)));
|
| 751 |
}
|
| 752 |
|
| 753 |
/* Callback for growBoundingBox() */
|
| 754 |
static int
|
| 755 |
addBBox(Scene *sc, Face *f, void *p)
|
| 756 |
{
|
| 757 |
double (*bbox)[3] = (double (*)[3])p;
|
| 758 |
int i, j;
|
| 759 |
|
| 760 |
for (i = f->nv; i-- > 0; ) {
|
| 761 |
double *p3 = sc->vert[f->v[i].vid].p;
|
| 762 |
for (j = 3; j--; ) {
|
| 763 |
if (p3[j] < bbox[0][j])
|
| 764 |
bbox[0][j] = p3[j];
|
| 765 |
if (p3[j] > bbox[1][j])
|
| 766 |
bbox[1][j] = p3[j];
|
| 767 |
}
|
| 768 |
}
|
| 769 |
return(1);
|
| 770 |
}
|
| 771 |
|
| 772 |
/* Expand bounding box min & max (initialize bbox to all zeroes) */
|
| 773 |
int
|
| 774 |
growBoundingBox(Scene *sc, double bbox[2][3], int flreq, int flexc)
|
| 775 |
{
|
| 776 |
if (sc == NULL || sc->nfaces <= 0 || bbox == NULL)
|
| 777 |
return(0);
|
| 778 |
|
| 779 |
if (VABSEQ(bbox[0], bbox[1])) { /* first run */
|
| 780 |
bbox[0][0] = bbox[0][1] = bbox[0][2] = FHUGE;
|
| 781 |
bbox[1][0] = bbox[1][1] = bbox[1][2] = -FHUGE;
|
| 782 |
}
|
| 783 |
return(foreachFace(sc, addBBox, flreq, flexc, bbox));
|
| 784 |
}
|
| 785 |
|
| 786 |
/* Allocate an empty scene */
|
| 787 |
Scene *
|
| 788 |
newScene(void)
|
| 789 |
{
|
| 790 |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene));
|
| 791 |
/* default group & material */
|
| 792 |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
|
| 793 |
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP");
|
| 794 |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
|
| 795 |
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL");
|
| 796 |
|
| 797 |
return(sc);
|
| 798 |
}
|
| 799 |
|
| 800 |
/* Duplicate a scene, optionally selecting faces */
|
| 801 |
Scene *
|
| 802 |
dupScene(const Scene *osc, int flreq, int flexc)
|
| 803 |
{
|
| 804 |
int fltest = flreq | flexc;
|
| 805 |
Scene *sc;
|
| 806 |
const Face *fo;
|
| 807 |
Face *f;
|
| 808 |
int i;
|
| 809 |
|
| 810 |
if (osc == NULL)
|
| 811 |
return(NULL);
|
| 812 |
sc = newScene();
|
| 813 |
for (i = 0; i < osc->ndescr; i++)
|
| 814 |
addComment(sc, osc->descr[i]);
|
| 815 |
if (osc->ngrps > 1) {
|
| 816 |
sc->grpname = (char **)erealloc((char *)sc->grpname,
|
| 817 |
sizeof(char *) * (osc->ngrps+(CHUNKSIZ-1)));
|
| 818 |
for (i = 1; i < osc->ngrps; i++)
|
| 819 |
sc->grpname[i] = savqstr(osc->grpname[i]);
|
| 820 |
sc->ngrps = osc->ngrps;
|
| 821 |
}
|
| 822 |
if (osc->nmats > 1) {
|
| 823 |
sc->matname = (char **)erealloc((char *)sc->matname,
|
| 824 |
sizeof(char *) * (osc->nmats+(CHUNKSIZ-1)));
|
| 825 |
for (i = 1; i < osc->nmats; i++)
|
| 826 |
sc->matname[i] = savqstr(osc->matname[i]);
|
| 827 |
sc->nmats = osc->nmats;
|
| 828 |
}
|
| 829 |
if (osc->nverts) {
|
| 830 |
sc->vert = (Vertex *)emalloc(sizeof(Vertex) *
|
| 831 |
(osc->nverts+(CHUNKSIZ-1)));
|
| 832 |
memcpy(sc->vert, osc->vert, sizeof(Vertex)*osc->nverts);
|
| 833 |
sc->nverts = osc->nverts;
|
| 834 |
for (i = 0; i < sc->nverts; i++)
|
| 835 |
sc->vert[i].vflist = NULL;
|
| 836 |
}
|
| 837 |
if (osc->ntex) {
|
| 838 |
sc->tex = (TexCoord *)emalloc(sizeof(TexCoord) *
|
| 839 |
(osc->ntex+(CHUNKSIZ-1)));
|
| 840 |
memcpy(sc->tex, osc->tex, sizeof(TexCoord)*osc->ntex);
|
| 841 |
sc->ntex = osc->ntex;
|
| 842 |
}
|
| 843 |
if (osc->nnorms) {
|
| 844 |
sc->norm = (Normal *)emalloc(sizeof(Normal) *
|
| 845 |
(osc->nnorms+CHUNKSIZ));
|
| 846 |
memcpy(sc->norm, osc->norm, sizeof(Normal)*osc->nnorms);
|
| 847 |
sc->nnorms = osc->nnorms;
|
| 848 |
}
|
| 849 |
for (fo = osc->flist; fo != NULL; fo = fo->next) {
|
| 850 |
if ((fo->flags & fltest) != flreq)
|
| 851 |
continue;
|
| 852 |
f = (Face *)emalloc(sizeof(Face) + sizeof(VertEnt)*(fo->nv-3));
|
| 853 |
memcpy(f, fo, sizeof(Face) + sizeof(VertEnt)*(fo->nv-3));
|
| 854 |
for (i = 0; i < f->nv; i++)
|
| 855 |
add2facelist(sc, f, i);
|
| 856 |
f->next = sc->flist;
|
| 857 |
sc->flist = f;
|
| 858 |
sc->nfaces++;
|
| 859 |
}
|
| 860 |
deleteUnreferenced(sc); /* jetsam */
|
| 861 |
return(sc);
|
| 862 |
}
|
| 863 |
|
| 864 |
/* Add one scene to another, not checking for redundancies */
|
| 865 |
int
|
| 866 |
addScene(Scene *scdst, const Scene *scsrc)
|
| 867 |
{
|
| 868 |
VNDX my_vlist[4];
|
| 869 |
int *vert_map = NULL;
|
| 870 |
int tex_off = 0;
|
| 871 |
int norm_off = 0;
|
| 872 |
VNDX *vlist = my_vlist;
|
| 873 |
int vllen = sizeof(my_vlist)/sizeof(VNDX);
|
| 874 |
int cur_mat = 0;
|
| 875 |
int cur_grp = 0;
|
| 876 |
int fcnt = 0;
|
| 877 |
const Face *f;
|
| 878 |
int i;
|
| 879 |
|
| 880 |
if ((scdst == NULL) | (scsrc == NULL))
|
| 881 |
return(-1);
|
| 882 |
if (scsrc->nfaces <= 0)
|
| 883 |
return(0);
|
| 884 |
/* map vertices */
|
| 885 |
vert_map = (int *)emalloc(sizeof(int)*scsrc->nverts);
|
| 886 |
for (i = 0; i < scsrc->nverts; i++) {
|
| 887 |
const Vertex *v = scsrc->vert + i;
|
| 888 |
if (v->vflist == NULL) {
|
| 889 |
vert_map[i] = -1;
|
| 890 |
continue;
|
| 891 |
}
|
| 892 |
vert_map[i] = addVertex(scdst, v->p[0], v->p[1], v->p[2]);
|
| 893 |
}
|
| 894 |
tex_off = scdst->ntex; /* append texture coords */
|
| 895 |
if (scsrc->ntex > 0) {
|
| 896 |
scdst->tex = (TexCoord *)erealloc((char *)scdst->tex,
|
| 897 |
sizeof(TexCoord)*(tex_off+scsrc->ntex+(CHUNKSIZ-1)));
|
| 898 |
memcpy(scdst->tex+tex_off, scsrc->tex,
|
| 899 |
sizeof(TexCoord)*scsrc->ntex);
|
| 900 |
}
|
| 901 |
norm_off = scdst->nnorms; /* append normals */
|
| 902 |
if (scsrc->nnorms > 0) {
|
| 903 |
scdst->norm = (Normal *)erealloc((char *)scdst->norm,
|
| 904 |
sizeof(Normal)*(norm_off+scsrc->nnorms+(CHUNKSIZ-1)));
|
| 905 |
memcpy(scdst->norm+norm_off, scsrc->norm,
|
| 906 |
sizeof(Normal)*scsrc->nnorms);
|
| 907 |
}
|
| 908 |
/* add faces */
|
| 909 |
scdst->lastgrp = scdst->lastmat = 0;
|
| 910 |
for (f = scsrc->flist; f != NULL; f = f->next) {
|
| 911 |
if (f->grp != cur_grp)
|
| 912 |
setGroup(scdst, scsrc->grpname[cur_grp = f->grp]);
|
| 913 |
if (f->mat != cur_mat)
|
| 914 |
setMaterial(scdst, scsrc->matname[cur_mat = f->mat]);
|
| 915 |
if (f->nv > vllen) {
|
| 916 |
vlist = (VNDX *)( vlist == my_vlist ?
|
| 917 |
emalloc(sizeof(VNDX)*f->nv) :
|
| 918 |
erealloc((char *)vlist, sizeof(VNDX)*f->nv) );
|
| 919 |
vllen = f->nv;
|
| 920 |
}
|
| 921 |
memset(vlist, 0xff, sizeof(VNDX)*f->nv);
|
| 922 |
for (i = f->nv; i-- > 0; ) {
|
| 923 |
if (f->v[i].vid >= 0)
|
| 924 |
vlist[i][0] = vert_map[f->v[i].vid];
|
| 925 |
if (f->v[i].tid >= 0)
|
| 926 |
vlist[i][1] = f->v[i].tid + tex_off;
|
| 927 |
if (f->v[i].nid >= 0)
|
| 928 |
vlist[i][2] = f->v[i].nid + norm_off;
|
| 929 |
}
|
| 930 |
fcnt += (addFace(scdst, vlist, f->nv) != NULL);
|
| 931 |
}
|
| 932 |
/* clean up */
|
| 933 |
if (vlist != my_vlist) efree((char *)vlist);
|
| 934 |
efree((char *)vert_map);
|
| 935 |
return(fcnt);
|
| 936 |
}
|
| 937 |
|
| 938 |
#define MAXAC 100
|
| 939 |
|
| 940 |
/* Transform entire scene */
|
| 941 |
int
|
| 942 |
xfScene(Scene *sc, int xac, char *xav[])
|
| 943 |
{
|
| 944 |
char comm[24+MAXAC*8];
|
| 945 |
char *cp;
|
| 946 |
XF myxf;
|
| 947 |
FVECT vec;
|
| 948 |
int i;
|
| 949 |
|
| 950 |
if ((sc == NULL) | (xac <= 0) | (xav == NULL))
|
| 951 |
return(0);
|
| 952 |
/* compute matrix */
|
| 953 |
if (xf(&myxf, xac, xav) < xac)
|
| 954 |
return(0);
|
| 955 |
/* transform vertices */
|
| 956 |
for (i = 0; i < sc->nverts; i++) {
|
| 957 |
VCOPY(vec, sc->vert[i].p);
|
| 958 |
multp3(vec, vec, myxf.xfm);
|
| 959 |
VCOPY(sc->vert[i].p, vec);
|
| 960 |
}
|
| 961 |
/* transform normals */
|
| 962 |
for (i = 0; i < sc->nnorms; i++) {
|
| 963 |
VCOPY(vec, sc->norm[i]);
|
| 964 |
multv3(vec, vec, myxf.xfm);
|
| 965 |
vec[0] /= myxf.sca; vec[1] /= myxf.sca; vec[2] /= myxf.sca;
|
| 966 |
VCOPY(sc->norm[i], vec);
|
| 967 |
}
|
| 968 |
/* add comment */
|
| 969 |
cp = strcpy(comm, "Transformed by:");
|
| 970 |
for (i = 0; i < xac; i++) {
|
| 971 |
while (*cp) cp++;
|
| 972 |
*cp++ = ' ';
|
| 973 |
strcpy(cp, xav[i]);
|
| 974 |
}
|
| 975 |
addComment(sc, comm);
|
| 976 |
return(xac); /* all done */
|
| 977 |
}
|
| 978 |
|
| 979 |
/* Ditto, using transform string rather than pre-parsed words */
|
| 980 |
int
|
| 981 |
xfmScene(Scene *sc, const char *xfm)
|
| 982 |
{
|
| 983 |
char *xav[MAXAC+1];
|
| 984 |
int xac, i;
|
| 985 |
|
| 986 |
if ((sc == NULL) | (xfm == NULL))
|
| 987 |
return(0);
|
| 988 |
/* skip spaces at beginning */
|
| 989 |
while (isspace(*xfm))
|
| 990 |
xfm++;
|
| 991 |
if (!*xfm)
|
| 992 |
return(0);
|
| 993 |
/* parse string into words */
|
| 994 |
xav[0] = savqstr((char *)xfm);
|
| 995 |
xac = 1; i = 0;
|
| 996 |
for ( ; ; ) {
|
| 997 |
while (!isspace(xfm[++i]))
|
| 998 |
if (!xfm[i])
|
| 999 |
break;
|
| 1000 |
while (isspace(xfm[i]))
|
| 1001 |
xav[0][i++] = '\0';
|
| 1002 |
if (!xfm[i])
|
| 1003 |
break;
|
| 1004 |
if (xac >= MAXAC-1) {
|
| 1005 |
freeqstr(xav[0]);
|
| 1006 |
return(0);
|
| 1007 |
}
|
| 1008 |
xav[xac++] = xav[0] + i;
|
| 1009 |
}
|
| 1010 |
xav[xac] = NULL;
|
| 1011 |
i = xfScene(sc, xac, xav);
|
| 1012 |
freeqstr(xav[0]);
|
| 1013 |
return(i);
|
| 1014 |
}
|
| 1015 |
#undef MAXAC
|
| 1016 |
|
| 1017 |
/* Delete unreferenced vertices, normals, texture coords */
|
| 1018 |
void
|
| 1019 |
deleteUnreferenced(Scene *sc)
|
| 1020 |
{
|
| 1021 |
int *vmap;
|
| 1022 |
Face *f;
|
| 1023 |
int nused, i;
|
| 1024 |
/* allocate index map */
|
| 1025 |
if (!sc->nverts)
|
| 1026 |
return;
|
| 1027 |
i = sc->nverts;
|
| 1028 |
if (sc->ntex > i)
|
| 1029 |
i = sc->ntex;
|
| 1030 |
if (sc->nnorms > i)
|
| 1031 |
i = sc->nnorms;
|
| 1032 |
vmap = (int *)emalloc(sizeof(int)*i);
|
| 1033 |
/* remap positions */
|
| 1034 |
for (i = nused = 0; i < sc->nverts; i++) {
|
| 1035 |
if (sc->vert[i].vflist == NULL) {
|
| 1036 |
vmap[i] = -1;
|
| 1037 |
continue;
|
| 1038 |
}
|
| 1039 |
if (nused != i)
|
| 1040 |
sc->vert[nused] = sc->vert[i];
|
| 1041 |
vmap[i] = nused++;
|
| 1042 |
}
|
| 1043 |
if (nused == sc->nverts)
|
| 1044 |
goto skip_pos;
|
| 1045 |
sc->vert = (Vertex *)erealloc((char *)sc->vert,
|
| 1046 |
sizeof(Vertex)*(nused+(CHUNKSIZ-1)));
|
| 1047 |
sc->nverts = nused;
|
| 1048 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 1049 |
for (i = f->nv; i--; )
|
| 1050 |
if ((f->v[i].vid = vmap[f->v[i].vid]) < 0)
|
| 1051 |
error(CONSISTENCY,
|
| 1052 |
"Link error in del_unref_verts()");
|
| 1053 |
skip_pos:
|
| 1054 |
/* remap texture coord's */
|
| 1055 |
if (!sc->ntex)
|
| 1056 |
goto skip_tex;
|
| 1057 |
memset((void *)vmap, 0, sizeof(int)*sc->ntex);
|
| 1058 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 1059 |
for (i = f->nv; i--; )
|
| 1060 |
if (f->v[i].tid >= 0)
|
| 1061 |
vmap[f->v[i].tid] = 1;
|
| 1062 |
for (i = nused = 0; i < sc->ntex; i++) {
|
| 1063 |
if (!vmap[i])
|
| 1064 |
continue;
|
| 1065 |
if (nused != i)
|
| 1066 |
sc->tex[nused] = sc->tex[i];
|
| 1067 |
vmap[i] = nused++;
|
| 1068 |
}
|
| 1069 |
if (nused == sc->ntex)
|
| 1070 |
goto skip_tex;
|
| 1071 |
sc->tex = (TexCoord *)erealloc((char *)sc->tex,
|
| 1072 |
sizeof(TexCoord)*(nused+(CHUNKSIZ-1)));
|
| 1073 |
sc->ntex = nused;
|
| 1074 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 1075 |
for (i = f->nv; i--; )
|
| 1076 |
if (f->v[i].tid >= 0)
|
| 1077 |
f->v[i].tid = vmap[f->v[i].tid];
|
| 1078 |
skip_tex:
|
| 1079 |
/* remap normals */
|
| 1080 |
if (!sc->nnorms)
|
| 1081 |
goto skip_norms;
|
| 1082 |
memset((void *)vmap, 0, sizeof(int)*sc->nnorms);
|
| 1083 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 1084 |
for (i = f->nv; i--; )
|
| 1085 |
if (f->v[i].nid >= 0)
|
| 1086 |
vmap[f->v[i].nid] = 1;
|
| 1087 |
for (i = nused = 0; i < sc->nnorms; i++) {
|
| 1088 |
if (!vmap[i])
|
| 1089 |
continue;
|
| 1090 |
if (nused != i)
|
| 1091 |
memcpy(sc->norm[nused], sc->norm[i], sizeof(Normal));
|
| 1092 |
vmap[i] = nused++;
|
| 1093 |
}
|
| 1094 |
if (nused == sc->nnorms)
|
| 1095 |
goto skip_norms;
|
| 1096 |
sc->norm = (Normal *)erealloc((char *)sc->norm,
|
| 1097 |
sizeof(Normal)*(nused+(CHUNKSIZ-1)));
|
| 1098 |
sc->nnorms = nused;
|
| 1099 |
for (f = sc->flist; f != NULL; f = f->next)
|
| 1100 |
for (i = f->nv; i--; )
|
| 1101 |
if (f->v[i].nid >= 0)
|
| 1102 |
f->v[i].nid = vmap[f->v[i].nid];
|
| 1103 |
skip_norms:
|
| 1104 |
/* clean up */
|
| 1105 |
efree((char *)vmap);
|
| 1106 |
}
|
| 1107 |
|
| 1108 |
/* Free a scene */
|
| 1109 |
void
|
| 1110 |
freeScene(Scene *sc)
|
| 1111 |
{
|
| 1112 |
int i;
|
| 1113 |
Face *f;
|
| 1114 |
|
| 1115 |
if (sc == NULL)
|
| 1116 |
return;
|
| 1117 |
clearComments(sc);
|
| 1118 |
for (i = sc->ngrps; i-- > 0; )
|
| 1119 |
freeqstr(sc->grpname[i]);
|
| 1120 |
efree((char *)sc->grpname);
|
| 1121 |
for (i = sc->nmats; i-- > 0; )
|
| 1122 |
freeqstr(sc->matname[i]);
|
| 1123 |
efree((char *)sc->matname);
|
| 1124 |
efree((char *)sc->vert);
|
| 1125 |
efree((char *)sc->tex);
|
| 1126 |
efree((char *)sc->norm);
|
| 1127 |
while ((f = sc->flist) != NULL) {
|
| 1128 |
sc->flist = f->next;
|
| 1129 |
efree((char *)f);
|
| 1130 |
}
|
| 1131 |
efree((char *)sc);
|
| 1132 |
}
|