| 1 |
greg |
2.1 |
#ifndef lint
|
| 2 |
greg |
2.3 |
static const char RCSid[] = "$Id: objutil.c,v 2.2 2020/04/02 20:44:15 greg Exp $";
|
| 3 |
greg |
2.1 |
#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 |
greg |
2.2 |
#include <ctype.h>
|
| 12 |
greg |
2.1 |
#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 |
|
|
register 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 = findName(gname, (const char **)sc->grpname, sc->ngrps);
|
| 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 = findName(mname, (const char **)sc->matname, sc->nmats);
|
| 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 |
|
|
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 |
|
|
if (f->grp == grp)
|
| 174 |
|
|
return(0);
|
| 175 |
|
|
f->grp = grp;
|
| 176 |
|
|
return(1);
|
| 177 |
|
|
}
|
| 178 |
|
|
|
| 179 |
|
|
/* Change group for the indicated faces */
|
| 180 |
|
|
int
|
| 181 |
|
|
changeGroup(Scene *sc, const char *gname, int flreq, int flexc)
|
| 182 |
|
|
{
|
| 183 |
|
|
int grp = findName(gname, (const char **)sc->grpname, sc->ngrps);
|
| 184 |
|
|
if (grp < 0) {
|
| 185 |
|
|
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
|
| 186 |
|
|
sc->grpname[grp=sc->ngrps++] = savqstr((char *)gname);
|
| 187 |
|
|
}
|
| 188 |
|
|
return(foreachFace(sc, chngFaceGroup, flreq, flexc, (void *)&grp));
|
| 189 |
|
|
}
|
| 190 |
|
|
|
| 191 |
|
|
/* Callback for changeMaterial() */
|
| 192 |
|
|
static int
|
| 193 |
|
|
chngFaceMaterial(Scene *sc, Face *f, void *ptr)
|
| 194 |
|
|
{
|
| 195 |
|
|
int mat = *(int *)ptr;
|
| 196 |
|
|
if (f->mat == mat)
|
| 197 |
|
|
return(0);
|
| 198 |
|
|
f->mat = mat;
|
| 199 |
|
|
return(1);
|
| 200 |
|
|
}
|
| 201 |
|
|
|
| 202 |
|
|
/* Change material for the indicated faces */
|
| 203 |
|
|
int
|
| 204 |
|
|
changeMaterial(Scene *sc, const char *mname, int flreq, int flexc)
|
| 205 |
|
|
{
|
| 206 |
|
|
int mat = findName(mname, (const char **)sc->matname, sc->nmats);
|
| 207 |
|
|
if (mat < 0) {
|
| 208 |
|
|
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
|
| 209 |
|
|
sc->matname[mat=sc->nmats++] = savqstr((char *)mname);
|
| 210 |
|
|
}
|
| 211 |
|
|
return(foreachFace(sc, chngFaceMaterial, flreq, flexc, (void *)&mat));
|
| 212 |
|
|
}
|
| 213 |
|
|
|
| 214 |
|
|
/* Compare floating point values for (near) equality */
|
| 215 |
|
|
static int
|
| 216 |
|
|
fdiffer(double f1, double f2, double eps)
|
| 217 |
|
|
{
|
| 218 |
|
|
if (f2 != .0)
|
| 219 |
|
|
f1 = f1/f2 - 1.;
|
| 220 |
|
|
return((f1 > eps) | (f1 < -eps));
|
| 221 |
|
|
}
|
| 222 |
|
|
|
| 223 |
|
|
/* Compare two texture coordinates for (near) equality */
|
| 224 |
|
|
static int
|
| 225 |
|
|
tex_diff(const TexCoord *t1, const TexCoord *t2, double eps)
|
| 226 |
|
|
{
|
| 227 |
|
|
if (fdiffer(t1->u, t2->u, eps))
|
| 228 |
|
|
return(1);
|
| 229 |
|
|
if (fdiffer(t1->v, t2->v, eps))
|
| 230 |
|
|
return(1);
|
| 231 |
|
|
return(0);
|
| 232 |
|
|
}
|
| 233 |
|
|
|
| 234 |
|
|
/* Compare two surface normals for (near) equality */
|
| 235 |
|
|
static int
|
| 236 |
|
|
norm_diff(const Normal n1, const Normal n2, double eps)
|
| 237 |
|
|
{
|
| 238 |
|
|
if (fabs(n1[0]-n2[0]) > eps)
|
| 239 |
|
|
return(1);
|
| 240 |
|
|
if (fabs(n1[1]-n2[1]) > eps)
|
| 241 |
|
|
return(1);
|
| 242 |
|
|
if (fabs(n1[2]-n2[2]) > eps)
|
| 243 |
|
|
return(1);
|
| 244 |
|
|
return(0);
|
| 245 |
|
|
}
|
| 246 |
|
|
|
| 247 |
|
|
/* Replace the given vertex with an equivalent one */
|
| 248 |
|
|
static int
|
| 249 |
|
|
replace_vertex(Scene *sc, int prev, int repl, double eps)
|
| 250 |
|
|
{
|
| 251 |
|
|
int repl_tex[10];
|
| 252 |
|
|
int repl_norm[10];
|
| 253 |
|
|
Face *f, *flast=NULL;
|
| 254 |
|
|
int i, j=0;
|
| 255 |
|
|
/* check to see if it's even used */
|
| 256 |
|
|
if (sc->vert[prev].vflist == NULL)
|
| 257 |
|
|
return(0);
|
| 258 |
|
|
/* get replacement textures */
|
| 259 |
|
|
repl_tex[0] = -1;
|
| 260 |
|
|
for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) {
|
| 261 |
|
|
/* make sure prev isn't in there */
|
| 262 |
|
|
for (j = 0; j < f->nv; j++)
|
| 263 |
|
|
if (f->v[j].vid == prev)
|
| 264 |
|
|
return(0);
|
| 265 |
|
|
for (j = 0; j < f->nv; j++)
|
| 266 |
|
|
if (f->v[j].vid == repl)
|
| 267 |
|
|
break;
|
| 268 |
|
|
if (j >= f->nv)
|
| 269 |
|
|
goto linkerr;
|
| 270 |
|
|
if (f->v[j].tid < 0)
|
| 271 |
|
|
continue;
|
| 272 |
|
|
/* see if it's new */
|
| 273 |
|
|
for (i = 0; repl_tex[i] >= 0; i++) {
|
| 274 |
|
|
if (repl_tex[i] == f->v[j].tid)
|
| 275 |
|
|
break;
|
| 276 |
|
|
if (!tex_diff(&sc->tex[repl_tex[i]],
|
| 277 |
|
|
&sc->tex[f->v[j].tid], eps)) {
|
| 278 |
|
|
f->v[j].tid = repl_tex[i];
|
| 279 |
|
|
break; /* consolidate */
|
| 280 |
|
|
}
|
| 281 |
|
|
}
|
| 282 |
|
|
if (repl_tex[i] >= 0)
|
| 283 |
|
|
continue; /* have this one already */
|
| 284 |
|
|
/* else add it */
|
| 285 |
|
|
repl_tex[i++] = f->v[j].tid;
|
| 286 |
|
|
repl_tex[i] = -1;
|
| 287 |
|
|
if (i >= 9)
|
| 288 |
|
|
break; /* that's all we have room for */
|
| 289 |
|
|
}
|
| 290 |
|
|
/* get replacement normals */
|
| 291 |
|
|
repl_norm[0] = -1;
|
| 292 |
|
|
for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) {
|
| 293 |
|
|
for (j = 0; j < f->nv; j++)
|
| 294 |
|
|
if (f->v[j].vid == repl)
|
| 295 |
|
|
break;
|
| 296 |
|
|
if (f->v[j].nid < 0)
|
| 297 |
|
|
continue;
|
| 298 |
|
|
/* see if it's new */
|
| 299 |
|
|
for (i = 0; repl_norm[i] >= 0; i++) {
|
| 300 |
|
|
if (repl_norm[i] == f->v[j].nid)
|
| 301 |
|
|
break;
|
| 302 |
|
|
if (!norm_diff(sc->norm[repl_norm[i]],
|
| 303 |
|
|
sc->norm[f->v[j].nid], eps)) {
|
| 304 |
|
|
f->v[j].nid = repl_norm[i];
|
| 305 |
|
|
break; /* consolidate */
|
| 306 |
|
|
}
|
| 307 |
|
|
}
|
| 308 |
|
|
if (repl_norm[i] >= 0)
|
| 309 |
|
|
continue; /* have this one already */
|
| 310 |
|
|
/* else add it */
|
| 311 |
|
|
repl_norm[i++] = f->v[j].nid;
|
| 312 |
|
|
repl_norm[i] = -1;
|
| 313 |
|
|
if (i >= 9)
|
| 314 |
|
|
break; /* that's all we have room for */
|
| 315 |
|
|
}
|
| 316 |
|
|
/* replace occurrences of vertex */
|
| 317 |
|
|
for (f = sc->vert[prev].vflist; f != NULL; f = f->v[j].fnext) {
|
| 318 |
|
|
for (j = 0; j < f->nv; j++)
|
| 319 |
|
|
if (f->v[j].vid == prev)
|
| 320 |
|
|
break;
|
| 321 |
|
|
if (j >= f->nv)
|
| 322 |
|
|
goto linkerr;
|
| 323 |
|
|
/* XXX doesn't allow for multiple references to prev in face */
|
| 324 |
|
|
f->v[j].vid = repl; /* replace vertex itself */
|
| 325 |
|
|
if (faceArea(sc, f, NULL) <= FTINY)
|
| 326 |
|
|
f->flags |= FACE_DEGENERATE;
|
| 327 |
|
|
if (f->v[j].tid >= 0) /* replace texture if appropriate */
|
| 328 |
|
|
for (i = 0; repl_tex[i] >= 0; i++) {
|
| 329 |
|
|
if (repl_tex[i] == f->v[j].tid)
|
| 330 |
|
|
break;
|
| 331 |
|
|
if (!tex_diff(&sc->tex[repl_tex[i]],
|
| 332 |
|
|
&sc->tex[f->v[j].tid], eps)) {
|
| 333 |
|
|
f->v[j].tid = repl_tex[i];
|
| 334 |
|
|
break;
|
| 335 |
|
|
}
|
| 336 |
|
|
}
|
| 337 |
|
|
if (f->v[j].nid >= 0) /* replace normal if appropriate */
|
| 338 |
|
|
for (i = 0; repl_norm[i] >= 0; i++) {
|
| 339 |
|
|
if (repl_norm[i] == f->v[j].nid)
|
| 340 |
|
|
break;
|
| 341 |
|
|
if (!norm_diff(sc->norm[repl_norm[i]],
|
| 342 |
|
|
sc->norm[f->v[j].nid], eps)) {
|
| 343 |
|
|
f->v[j].nid = repl_norm[i];
|
| 344 |
|
|
break;
|
| 345 |
|
|
}
|
| 346 |
|
|
}
|
| 347 |
|
|
flast = f;
|
| 348 |
|
|
}
|
| 349 |
|
|
/* transfer face list */
|
| 350 |
|
|
flast->v[j].fnext = sc->vert[repl].vflist;
|
| 351 |
|
|
sc->vert[repl].vflist = sc->vert[prev].vflist;
|
| 352 |
|
|
sc->vert[prev].vflist = NULL;
|
| 353 |
|
|
return(1);
|
| 354 |
|
|
linkerr:
|
| 355 |
|
|
error(CONSISTENCY, "Link error in replace_vertex()");
|
| 356 |
|
|
return(0); /* shouldn't return */
|
| 357 |
|
|
}
|
| 358 |
|
|
|
| 359 |
|
|
/* Eliminate duplicate vertices, return # joined */
|
| 360 |
|
|
int
|
| 361 |
|
|
coalesceVertices(Scene *sc, double eps)
|
| 362 |
|
|
{
|
| 363 |
|
|
int nelim = 0;
|
| 364 |
|
|
LUTAB myLookup;
|
| 365 |
|
|
LUENT *le;
|
| 366 |
|
|
char vertfmt[32], vertbuf[64];
|
| 367 |
|
|
double d;
|
| 368 |
|
|
int i;
|
| 369 |
|
|
|
| 370 |
|
|
if (eps >= 1.)
|
| 371 |
|
|
return(0);
|
| 372 |
|
|
if (sc->nverts <= 3)
|
| 373 |
|
|
return(0);
|
| 374 |
|
|
/* create hash table */
|
| 375 |
|
|
myLookup.hashf = lu_shash;
|
| 376 |
|
|
myLookup.keycmp = (lut_keycmpf_t *)strcmp;
|
| 377 |
|
|
myLookup.freek = (lut_free_t *)freeqstr;
|
| 378 |
|
|
myLookup.freed = NULL;
|
| 379 |
|
|
if (!lu_init(&myLookup, sc->nverts))
|
| 380 |
|
|
return(0);
|
| 381 |
|
|
if (eps <= 5e-15)
|
| 382 |
|
|
strcpy(vertfmt, "%.15e %.15e %.15e");
|
| 383 |
|
|
else {
|
| 384 |
|
|
int nsigdig = 0;
|
| 385 |
|
|
for (d = eps; d < 0.5; d *= 10.)
|
| 386 |
|
|
++nsigdig;
|
| 387 |
|
|
sprintf(vertfmt, "%%.%de %%.%de %%.%de",
|
| 388 |
|
|
nsigdig, nsigdig, nsigdig);
|
| 389 |
|
|
}
|
| 390 |
|
|
/* find coicident vertices */
|
| 391 |
|
|
for (i = 0; i < sc->nverts; i++) {
|
| 392 |
|
|
if (verbose && !((i+1) & 0x3fff))
|
| 393 |
|
|
fprintf(stderr, "%3d%% complete\r", 100*i/sc->nverts);
|
| 394 |
|
|
/* check for match */
|
| 395 |
|
|
sprintf(vertbuf, vertfmt, sc->vert[i].p[0],
|
| 396 |
|
|
sc->vert[i].p[1], sc->vert[i].p[2]);
|
| 397 |
|
|
le = lu_find(&myLookup, vertbuf);
|
| 398 |
|
|
if (le->data != NULL) { /* coincident vertex */
|
| 399 |
|
|
nelim += replace_vertex(sc, i,
|
| 400 |
|
|
(Vertex *)le->data - sc->vert, eps);
|
| 401 |
|
|
continue;
|
| 402 |
|
|
}
|
| 403 |
|
|
if (le->key == NULL) /* else create new entry */
|
| 404 |
|
|
le->key = savqstr(vertbuf);
|
| 405 |
|
|
le->data = (char *)&sc->vert[i];
|
| 406 |
|
|
}
|
| 407 |
|
|
lu_done(&myLookup); /* clean up */
|
| 408 |
|
|
return(nelim);
|
| 409 |
|
|
}
|
| 410 |
|
|
|
| 411 |
|
|
/* Identify duplicate faces */
|
| 412 |
|
|
int
|
| 413 |
|
|
findDuplicateFaces(Scene *sc)
|
| 414 |
|
|
{
|
| 415 |
|
|
int nchecked = 0;
|
| 416 |
|
|
int nfound = 0;
|
| 417 |
|
|
Face *f, *f1;
|
| 418 |
|
|
int vid;
|
| 419 |
|
|
int i, j;
|
| 420 |
|
|
/* start fresh */
|
| 421 |
|
|
for (f = sc->flist; f != NULL; f = f->next)
|
| 422 |
|
|
f->flags &= ~FACE_DUPLICATE;
|
| 423 |
|
|
/* check each face */
|
| 424 |
|
|
for (f = sc->flist; f != NULL; f = f->next) {
|
| 425 |
|
|
nchecked++;
|
| 426 |
|
|
if (verbose && !(nchecked & 0x3fff))
|
| 427 |
|
|
fprintf(stderr, "%3d%% complete\r",
|
| 428 |
|
|
100*nchecked/sc->nfaces);
|
| 429 |
|
|
if (f->flags & FACE_DUPLICATE)
|
| 430 |
|
|
continue; /* already identified */
|
| 431 |
|
|
vid = f->v[0].vid;
|
| 432 |
|
|
/* look for duplicates */
|
| 433 |
|
|
for (f1 = sc->vert[vid].vflist; f1 != NULL;
|
| 434 |
|
|
f1 = f1->v[j].fnext) {
|
| 435 |
|
|
for (j = 0; j < f1->nv; j++)
|
| 436 |
|
|
if (f1->v[j].vid == vid)
|
| 437 |
|
|
break;
|
| 438 |
|
|
if (j >= f1->nv)
|
| 439 |
|
|
break; /* missing link! */
|
| 440 |
|
|
if (f1 == f)
|
| 441 |
|
|
continue; /* shouldn't happen */
|
| 442 |
|
|
if (f1->flags & FACE_DUPLICATE)
|
| 443 |
|
|
continue; /* already marked */
|
| 444 |
|
|
if (f1->nv != f->nv)
|
| 445 |
|
|
continue; /* couldn't be dup. */
|
| 446 |
|
|
for (i = f->nv; --i > 0; )
|
| 447 |
|
|
if (f->v[i].vid != f1->v[(j+i)%f1->nv].vid)
|
| 448 |
|
|
break; /* vertex mismatch */
|
| 449 |
|
|
if (i) {
|
| 450 |
|
|
#if DUP_CHECK_REVERSE /* check reverse direction */
|
| 451 |
|
|
for (i = f->nv; --i > 0; )
|
| 452 |
|
|
if (f1->v[(j+f1->nv-i)%f1->nv].vid
|
| 453 |
|
|
!= f->v[i].vid)
|
| 454 |
|
|
break;
|
| 455 |
|
|
if (i) /* no match */
|
| 456 |
|
|
#endif
|
| 457 |
|
|
continue;
|
| 458 |
|
|
}
|
| 459 |
|
|
f1->flags |= FACE_DUPLICATE;
|
| 460 |
|
|
++nfound;
|
| 461 |
|
|
}
|
| 462 |
|
|
}
|
| 463 |
|
|
return(nfound);
|
| 464 |
|
|
}
|
| 465 |
|
|
|
| 466 |
|
|
/* Delete indicated faces */
|
| 467 |
|
|
int
|
| 468 |
|
|
deleteFaces(Scene *sc, int flreq, int flexc)
|
| 469 |
|
|
{
|
| 470 |
|
|
int fltest = flreq | flexc;
|
| 471 |
|
|
int orig_nfaces = sc->nfaces;
|
| 472 |
|
|
Face fhead;
|
| 473 |
|
|
Face *f, *ftst;
|
| 474 |
|
|
|
| 475 |
|
|
fhead.next = sc->flist;
|
| 476 |
|
|
f = &fhead;
|
| 477 |
|
|
while ((ftst = f->next) != NULL)
|
| 478 |
|
|
if ((ftst->flags & fltest) == flreq) {
|
| 479 |
|
|
Face *vf; /* remove from vertex lists */
|
| 480 |
|
|
int vid, i, j;
|
| 481 |
|
|
for (i = 0; i < ftst->nv; i++) {
|
| 482 |
|
|
vid = ftst->v[i].vid;
|
| 483 |
|
|
vf = sc->vert[vid].vflist;
|
| 484 |
|
|
if (vf == ftst) {
|
| 485 |
|
|
sc->vert[vid].vflist = ftst->v[i].fnext;
|
| 486 |
|
|
continue;
|
| 487 |
|
|
}
|
| 488 |
|
|
while (vf != NULL) {
|
| 489 |
|
|
for (j = 0; j < vf->nv; j++)
|
| 490 |
|
|
if (vf->v[j].vid == vid)
|
| 491 |
|
|
break;
|
| 492 |
|
|
if (j >= vf->nv)
|
| 493 |
|
|
break; /* error */
|
| 494 |
|
|
if (vf->v[j].fnext == ftst) {
|
| 495 |
|
|
vf->v[j].fnext =
|
| 496 |
|
|
ftst->v[i].fnext;
|
| 497 |
|
|
break;
|
| 498 |
|
|
}
|
| 499 |
|
|
vf = vf->v[j].fnext;
|
| 500 |
|
|
}
|
| 501 |
|
|
}
|
| 502 |
|
|
f->next = ftst->next; /* remove from scene list */
|
| 503 |
|
|
efree((char *)ftst);
|
| 504 |
|
|
sc->nfaces--;
|
| 505 |
|
|
} else
|
| 506 |
|
|
f = f->next;
|
| 507 |
|
|
sc->flist = fhead.next;
|
| 508 |
|
|
return(orig_nfaces - sc->nfaces);
|
| 509 |
|
|
}
|
| 510 |
|
|
|
| 511 |
|
|
/* Compute face area (and normal) */
|
| 512 |
|
|
double
|
| 513 |
|
|
faceArea(const Scene *sc, const Face *f, Normal nrm)
|
| 514 |
|
|
{
|
| 515 |
|
|
FVECT fnrm;
|
| 516 |
|
|
double area;
|
| 517 |
|
|
FVECT v1, v2, v3;
|
| 518 |
|
|
double *p0;
|
| 519 |
|
|
int i;
|
| 520 |
|
|
|
| 521 |
|
|
if (f->flags & FACE_DEGENERATE)
|
| 522 |
|
|
return(.0); /* should we check this? */
|
| 523 |
|
|
fnrm[0] = fnrm[1] = fnrm[2] = .0;
|
| 524 |
|
|
p0 = sc->vert[f->v[0].vid].p;
|
| 525 |
|
|
VSUB(v1, sc->vert[f->v[1].vid].p, p0);
|
| 526 |
|
|
for (i = 2; i < f->nv; i++) {
|
| 527 |
|
|
VSUB(v2, sc->vert[f->v[i].vid].p, p0);
|
| 528 |
|
|
fcross(v3, v1, v2);
|
| 529 |
|
|
VADD(fnrm, fnrm, v3);
|
| 530 |
|
|
VCOPY(v1, v2);
|
| 531 |
|
|
}
|
| 532 |
|
|
area = 0.5*normalize(fnrm);
|
| 533 |
|
|
if (nrm != NULL) {
|
| 534 |
|
|
if (area != 0.)
|
| 535 |
|
|
VCOPY(nrm, fnrm);
|
| 536 |
|
|
else
|
| 537 |
|
|
nrm[0] = nrm[1] = nrm[2] = 0.;
|
| 538 |
|
|
}
|
| 539 |
|
|
return(area);
|
| 540 |
|
|
}
|
| 541 |
|
|
|
| 542 |
|
|
/* Add a descriptive comment */
|
| 543 |
|
|
void
|
| 544 |
|
|
addComment(Scene *sc, const char *comment)
|
| 545 |
|
|
{
|
| 546 |
|
|
sc->descr = chunk_alloc(char *, sc->descr, sc->ndescr);
|
| 547 |
|
|
sc->descr[sc->ndescr++] = savqstr((char *)comment);
|
| 548 |
|
|
}
|
| 549 |
|
|
|
| 550 |
|
|
/* Clear comments */
|
| 551 |
|
|
void
|
| 552 |
|
|
clearComments(Scene *sc)
|
| 553 |
|
|
{
|
| 554 |
|
|
while (sc->ndescr > 0)
|
| 555 |
|
|
freeqstr(sc->descr[--sc->ndescr]);
|
| 556 |
|
|
efree((char *)sc->descr);
|
| 557 |
|
|
sc->ndescr = 0;
|
| 558 |
|
|
}
|
| 559 |
|
|
|
| 560 |
|
|
/* Add a face to a vertex face list */
|
| 561 |
|
|
static void
|
| 562 |
|
|
add2facelist(Scene *sc, Face *f, int i)
|
| 563 |
|
|
{
|
| 564 |
|
|
int vid = f->v[i].vid;
|
| 565 |
|
|
Face *fp = sc->vert[vid].vflist;
|
| 566 |
|
|
int j;
|
| 567 |
|
|
|
| 568 |
|
|
f->v[i].fnext = NULL; /* will put at end */
|
| 569 |
|
|
if (fp == NULL) { /* new list */
|
| 570 |
|
|
sc->vert[vid].vflist = f;
|
| 571 |
|
|
return;
|
| 572 |
|
|
}
|
| 573 |
|
|
for ( ; ; ) { /* else find position */
|
| 574 |
|
|
if (fp == f)
|
| 575 |
|
|
return; /* already in list */
|
| 576 |
|
|
for (j = 0; j < fp->nv; j++)
|
| 577 |
|
|
if (fp->v[j].vid == vid)
|
| 578 |
|
|
break;
|
| 579 |
|
|
if (j >= fp->nv)
|
| 580 |
|
|
error(CONSISTENCY, "Link error in add2facelist()");
|
| 581 |
|
|
if (fp->v[j].fnext == NULL)
|
| 582 |
|
|
break; /* reached the end */
|
| 583 |
|
|
fp = fp->v[j].fnext;
|
| 584 |
|
|
}
|
| 585 |
|
|
fp->v[j].fnext = f; /* append new face */
|
| 586 |
|
|
}
|
| 587 |
|
|
|
| 588 |
|
|
/* Allocate an empty scene */
|
| 589 |
|
|
Scene *
|
| 590 |
|
|
newScene(void)
|
| 591 |
|
|
{
|
| 592 |
|
|
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene));
|
| 593 |
|
|
/* default group & material */
|
| 594 |
|
|
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps);
|
| 595 |
|
|
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP");
|
| 596 |
|
|
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats);
|
| 597 |
|
|
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL");
|
| 598 |
|
|
|
| 599 |
|
|
return(sc);
|
| 600 |
|
|
}
|
| 601 |
|
|
|
| 602 |
|
|
/* Duplicate a scene */
|
| 603 |
|
|
Scene *
|
| 604 |
|
|
dupScene(const Scene *osc)
|
| 605 |
|
|
{
|
| 606 |
|
|
Scene *sc;
|
| 607 |
|
|
const Face *fo;
|
| 608 |
|
|
Face *f;
|
| 609 |
|
|
int i;
|
| 610 |
|
|
|
| 611 |
|
|
if (osc == NULL)
|
| 612 |
|
|
return(NULL);
|
| 613 |
|
|
sc = newScene();
|
| 614 |
|
|
for (i = 0; i < osc->ndescr; i++)
|
| 615 |
|
|
addComment(sc, osc->descr[i]);
|
| 616 |
|
|
if (osc->ngrps) {
|
| 617 |
|
|
sc->grpname = (char **)emalloc(sizeof(char *) *
|
| 618 |
|
|
(osc->ngrps+(CHUNKSIZ-1)));
|
| 619 |
|
|
for (i = 0; i < osc->ngrps; i++)
|
| 620 |
|
|
sc->grpname[i] = savqstr(osc->grpname[i]);
|
| 621 |
|
|
sc->ngrps = osc->ngrps;
|
| 622 |
|
|
}
|
| 623 |
|
|
if (osc->nmats) {
|
| 624 |
|
|
sc->matname = (char **)emalloc(sizeof(char *) *
|
| 625 |
|
|
(osc->nmats+(CHUNKSIZ-1)));
|
| 626 |
|
|
for (i = 0; i < osc->nmats; i++)
|
| 627 |
|
|
sc->matname[i] = savqstr(osc->matname[i]);
|
| 628 |
|
|
sc->nmats = osc->nmats;
|
| 629 |
|
|
}
|
| 630 |
|
|
if (osc->nverts) {
|
| 631 |
|
|
sc->vert = (Vertex *)emalloc(sizeof(Vertex) *
|
| 632 |
|
|
(osc->nverts+(CHUNKSIZ-1)));
|
| 633 |
|
|
memcpy((void *)sc->vert, (const void *)osc->vert,
|
| 634 |
|
|
sizeof(Vertex)*osc->nverts);
|
| 635 |
|
|
sc->nverts = osc->nverts;
|
| 636 |
|
|
for (i = 0; i < sc->nverts; i++)
|
| 637 |
|
|
sc->vert[i].vflist = NULL;
|
| 638 |
|
|
}
|
| 639 |
|
|
if (osc->ntex) {
|
| 640 |
|
|
sc->tex = (TexCoord *)emalloc(sizeof(TexCoord) *
|
| 641 |
|
|
(osc->ntex+(CHUNKSIZ-1)));
|
| 642 |
|
|
memcpy((void *)sc->tex, (const void *)osc->tex,
|
| 643 |
|
|
sizeof(TexCoord)*osc->ntex);
|
| 644 |
|
|
sc->ntex = osc->ntex;
|
| 645 |
|
|
}
|
| 646 |
|
|
if (osc->nnorms) {
|
| 647 |
|
|
sc->norm = (Normal *)emalloc(sizeof(Normal) *
|
| 648 |
|
|
(osc->nnorms+CHUNKSIZ));
|
| 649 |
|
|
memcpy((void *)sc->norm, (const void *)osc->norm,
|
| 650 |
|
|
sizeof(Normal)*osc->nnorms);
|
| 651 |
|
|
sc->nnorms = osc->nnorms;
|
| 652 |
|
|
}
|
| 653 |
|
|
for (fo = osc->flist; fo != NULL; fo = fo->next) {
|
| 654 |
|
|
f = (Face *)emalloc(sizeof(Face) +
|
| 655 |
|
|
sizeof(VertEnt)*(fo->nv-3));
|
| 656 |
|
|
memcpy((void *)f, (const void *)fo, sizeof(Face) +
|
| 657 |
|
|
sizeof(VertEnt)*(fo->nv-3));
|
| 658 |
|
|
for (i = 0; i < f->nv; i++)
|
| 659 |
|
|
add2facelist(sc, f, i);
|
| 660 |
|
|
f->next = sc->flist;
|
| 661 |
|
|
sc->flist = f;
|
| 662 |
|
|
sc->nfaces++;
|
| 663 |
|
|
}
|
| 664 |
|
|
return(sc);
|
| 665 |
|
|
}
|
| 666 |
|
|
|
| 667 |
greg |
2.2 |
/* Transform entire scene */
|
| 668 |
greg |
2.3 |
int
|
| 669 |
|
|
xfScene(Scene *sc, int xac, char *xav[])
|
| 670 |
|
|
{
|
| 671 |
|
|
XF myxf;
|
| 672 |
|
|
FVECT vec;
|
| 673 |
|
|
int i;
|
| 674 |
|
|
|
| 675 |
|
|
if ((sc == NULL) | (xac <= 0) | (xav == NULL))
|
| 676 |
|
|
return(0);
|
| 677 |
|
|
/* compute matrix */
|
| 678 |
|
|
if (xf(&myxf, xac, xav) < xac)
|
| 679 |
|
|
return(0);
|
| 680 |
|
|
/* transform vertices */
|
| 681 |
|
|
for (i = 0; i < sc->nverts; i++) {
|
| 682 |
|
|
VCOPY(vec, sc->vert[i].p);
|
| 683 |
|
|
multp3(vec, vec, myxf.xfm);
|
| 684 |
|
|
VCOPY(sc->vert[i].p, vec);
|
| 685 |
|
|
}
|
| 686 |
|
|
/* transform normals */
|
| 687 |
|
|
for (i = 0; i < sc->nnorms; i++) {
|
| 688 |
|
|
VCOPY(vec, sc->norm[i]);
|
| 689 |
|
|
multv3(vec, vec, myxf.xfm);
|
| 690 |
|
|
vec[0] /= myxf.sca; vec[1] /= myxf.sca; vec[2] /= myxf.sca;
|
| 691 |
|
|
VCOPY(sc->norm[i], vec);
|
| 692 |
|
|
}
|
| 693 |
|
|
return(xac); /* all done */
|
| 694 |
|
|
}
|
| 695 |
|
|
|
| 696 |
|
|
/* Ditto, using transform string rather than pre-parsed words */
|
| 697 |
greg |
2.2 |
#define MAXAC 100
|
| 698 |
|
|
int
|
| 699 |
|
|
xfmScene(Scene *sc, const char *xfm)
|
| 700 |
|
|
{
|
| 701 |
|
|
char *xav[MAXAC+1];
|
| 702 |
|
|
int xac, i;
|
| 703 |
|
|
|
| 704 |
|
|
if ((sc == NULL) | (xfm == NULL))
|
| 705 |
|
|
return(0);
|
| 706 |
greg |
2.3 |
/* skip spaces at beginning */
|
| 707 |
|
|
while (isspace(*xfm))
|
| 708 |
greg |
2.2 |
xfm++;
|
| 709 |
|
|
if (!*xfm)
|
| 710 |
|
|
return(0);
|
| 711 |
greg |
2.3 |
/* parse string into words */
|
| 712 |
greg |
2.2 |
xav[0] = strcpy((char *)malloc(strlen(xfm)+1), xfm);
|
| 713 |
|
|
xac = 1; i = 0;
|
| 714 |
|
|
for ( ; ; ) {
|
| 715 |
|
|
while (!isspace(xfm[++i]))
|
| 716 |
|
|
if (!xfm[i])
|
| 717 |
|
|
break;
|
| 718 |
|
|
while (isspace(xfm[i]))
|
| 719 |
|
|
xav[0][i++] = '\0';
|
| 720 |
|
|
if (!xfm[i])
|
| 721 |
|
|
break;
|
| 722 |
greg |
2.3 |
if (xac >= MAXAC-1) {
|
| 723 |
|
|
free(xav[0]);
|
| 724 |
|
|
return(0);
|
| 725 |
|
|
}
|
| 726 |
greg |
2.2 |
xav[xac++] = xav[0] + i;
|
| 727 |
|
|
}
|
| 728 |
|
|
xav[xac] = NULL;
|
| 729 |
greg |
2.3 |
i = xfScene(sc, xac, xav);
|
| 730 |
greg |
2.2 |
free(xav[0]);
|
| 731 |
greg |
2.3 |
return(i);
|
| 732 |
greg |
2.2 |
}
|
| 733 |
|
|
#undef MAXAC
|
| 734 |
|
|
|
| 735 |
greg |
2.1 |
/* Free a scene */
|
| 736 |
|
|
void
|
| 737 |
|
|
freeScene(Scene *sc)
|
| 738 |
|
|
{
|
| 739 |
|
|
int i;
|
| 740 |
|
|
Face *f;
|
| 741 |
|
|
|
| 742 |
|
|
if (sc == NULL)
|
| 743 |
|
|
return;
|
| 744 |
|
|
clearComments(sc);
|
| 745 |
|
|
for (i = sc->ngrps; i-- > 0; )
|
| 746 |
|
|
freeqstr(sc->grpname[i]);
|
| 747 |
|
|
efree((char *)sc->grpname);
|
| 748 |
|
|
for (i = sc->nmats; i-- > 0; )
|
| 749 |
|
|
freeqstr(sc->matname[i]);
|
| 750 |
|
|
efree((char *)sc->matname);
|
| 751 |
|
|
efree((char *)sc->vert);
|
| 752 |
|
|
efree((char *)sc->tex);
|
| 753 |
|
|
efree((char *)sc->norm);
|
| 754 |
|
|
while ((f = sc->flist) != NULL) {
|
| 755 |
|
|
sc->flist = f->next;
|
| 756 |
|
|
efree((char *)f);
|
| 757 |
|
|
}
|
| 758 |
|
|
efree((char *)sc);
|
| 759 |
|
|
}
|