| 1 | greg | 1.1 | #ifndef lint | 
| 2 | greg | 2.13 | static const char RCSid[] = "$Id: replmarks.c,v 2.12 2004/09/28 17:54:18 greg Exp $"; | 
| 3 | greg | 1.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | * Replace markers in Radiance scene description with objects or instances. | 
| 6 |  |  | * | 
| 7 |  |  | *      Created:        17 Feb 1991     Greg Ward | 
| 8 |  |  | */ | 
| 9 |  |  |  | 
| 10 | greg | 2.5 | #include <stdlib.h> | 
| 11 | greg | 1.1 | #include <ctype.h> | 
| 12 |  |  | #include <math.h> | 
| 13 | schorsch | 2.7 | #include <stdio.h> | 
| 14 | greg | 1.1 |  | 
| 15 | schorsch | 2.7 | #include "platform.h" | 
| 16 | schorsch | 2.9 | #include "rtio.h" | 
| 17 | schorsch | 2.8 | #include "rtprocess.h" | 
| 18 | greg | 1.1 | #include "fvect.h" | 
| 19 |  |  |  | 
| 20 |  |  | #ifdef  M_PI | 
| 21 | gregl | 2.4 | #define  PI             ((double)M_PI) | 
| 22 | greg | 1.1 | #else | 
| 23 |  |  | #define  PI             3.14159265358979323846 | 
| 24 |  |  | #endif | 
| 25 |  |  |  | 
| 26 | greg | 1.2 | #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7) | 
| 27 |  |  |  | 
| 28 | greg | 1.1 | #define  MAXVERT        6       /* maximum number of vertices for markers */ | 
| 29 | greg | 2.13 | #define  MAXMARK        128     /* maximum number of markers */ | 
| 30 | greg | 1.1 |  | 
| 31 | greg | 2.11 | #define  USE_XFORM      1       /* use !xform inline command */ | 
| 32 |  |  | #define  USE_INSTANCE   2       /* use instance primitive */ | 
| 33 |  |  | #define  USE_MESH       3       /* use mesh primitive */ | 
| 34 |  |  |  | 
| 35 | greg | 1.1 | typedef struct { | 
| 36 |  |  | short   beg, end;               /* beginning and ending vertex */ | 
| 37 |  |  | float   len2;                   /* length squared */ | 
| 38 |  |  | }  EDGE;                        /* a marker edge */ | 
| 39 |  |  |  | 
| 40 | greg | 2.2 | struct mrkr { | 
| 41 |  |  | char    *modout;                /* output modifier */ | 
| 42 |  |  | double  mscale;                 /* scale by this to get unit */ | 
| 43 |  |  | char    *modin;                 /* input modifier indicating marker */ | 
| 44 |  |  | char    *objname;               /* output object file or octree */ | 
| 45 | greg | 2.11 | int     usetype;                /* one of USE_* above */ | 
| 46 | greg | 2.13 | }  marker[MAXMARK+1];           /* array of markers */ | 
| 47 | greg | 2.2 | int     nmarkers = 0;           /* number of markers */ | 
| 48 | greg | 1.1 |  | 
| 49 | greg | 2.2 | int     expand;                 /* expand commands? */ | 
| 50 | greg | 1.1 |  | 
| 51 |  |  | char    *progname; | 
| 52 |  |  |  | 
| 53 | schorsch | 2.9 | static void convert(char *name, FILE *fin); | 
| 54 |  |  | static void cvcomm(char *fname, FILE *fin); | 
| 55 |  |  | static void cvobject(char *fname, FILE *fin); | 
| 56 |  |  | static void replace(char *fname, struct mrkr *m, char *mark, FILE *fin); | 
| 57 |  |  | static int edgecmp(const void *e1, const void *e2); | 
| 58 |  |  | static int buildxf(char *xf, double markscale, FILE *fin); | 
| 59 |  |  | static int addrot(char *xf, FVECT xp, FVECT yp, FVECT zp); | 
| 60 | greg | 1.1 |  | 
| 61 | schorsch | 2.9 |  | 
| 62 |  |  | int | 
| 63 |  |  | main( | 
| 64 |  |  | int     argc, | 
| 65 |  |  | char    *argv[] | 
| 66 |  |  | ) | 
| 67 | greg | 1.1 | { | 
| 68 |  |  | FILE    *fp; | 
| 69 |  |  | int     i, j; | 
| 70 |  |  |  | 
| 71 |  |  | progname = argv[0]; | 
| 72 | greg | 2.2 | i = 1; | 
| 73 |  |  | while (i < argc && argv[i][0] == '-') { | 
| 74 |  |  | do { | 
| 75 |  |  | switch (argv[i][1]) { | 
| 76 |  |  | case 'i': | 
| 77 | greg | 2.11 | marker[nmarkers].usetype = USE_INSTANCE; | 
| 78 |  |  | marker[nmarkers].objname = argv[++i]; | 
| 79 |  |  | break; | 
| 80 |  |  | case 'I': | 
| 81 |  |  | marker[nmarkers].usetype = USE_MESH; | 
| 82 | greg | 2.2 | marker[nmarkers].objname = argv[++i]; | 
| 83 |  |  | break; | 
| 84 |  |  | case 'x': | 
| 85 | greg | 2.11 | marker[nmarkers].usetype = USE_XFORM; | 
| 86 | greg | 2.2 | marker[nmarkers].objname = argv[++i]; | 
| 87 |  |  | break; | 
| 88 |  |  | case 'e': | 
| 89 |  |  | expand = 1; | 
| 90 |  |  | break; | 
| 91 |  |  | case 'm': | 
| 92 |  |  | marker[nmarkers].modout = argv[++i]; | 
| 93 |  |  | break; | 
| 94 |  |  | case 's': | 
| 95 |  |  | marker[nmarkers].mscale = atof(argv[++i]); | 
| 96 |  |  | break; | 
| 97 |  |  | default: | 
| 98 |  |  | goto userr; | 
| 99 |  |  | } | 
| 100 |  |  | if (++i >= argc) | 
| 101 |  |  | goto userr; | 
| 102 |  |  | } while (argv[i][0] == '-'); | 
| 103 |  |  | if (marker[nmarkers].objname == NULL) | 
| 104 |  |  | goto userr; | 
| 105 | greg | 2.13 | if (nmarkers >= MAXMARK) { | 
| 106 |  |  | fprintf(stderr, "%s: too many markers\n", progname); | 
| 107 |  |  | return 1; | 
| 108 |  |  | } | 
| 109 | greg | 2.2 | marker[nmarkers++].modin = argv[i++]; | 
| 110 |  |  | marker[nmarkers].mscale = marker[nmarkers-1].mscale; | 
| 111 |  |  | } | 
| 112 |  |  | if (nmarkers == 0) | 
| 113 | greg | 1.1 | goto userr; | 
| 114 |  |  | /* simple header */ | 
| 115 |  |  | putchar('#'); | 
| 116 |  |  | for (j = 0; j < i; j++) { | 
| 117 |  |  | putchar(' '); | 
| 118 |  |  | fputs(argv[j], stdout); | 
| 119 |  |  | } | 
| 120 |  |  | putchar('\n'); | 
| 121 |  |  | if (i == argc) | 
| 122 |  |  | convert("<stdin>", stdin); | 
| 123 |  |  | else | 
| 124 |  |  | for ( ; i < argc; i++) { | 
| 125 |  |  | if ((fp = fopen(argv[i], "r")) == NULL) { | 
| 126 |  |  | perror(argv[i]); | 
| 127 |  |  | exit(1); | 
| 128 |  |  | } | 
| 129 |  |  | convert(argv[i], fp); | 
| 130 |  |  | fclose(fp); | 
| 131 |  |  | } | 
| 132 | schorsch | 2.9 | return 0; | 
| 133 | greg | 1.1 | userr: | 
| 134 |  |  | fprintf(stderr, | 
| 135 | greg | 2.11 | "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n", | 
| 136 | greg | 1.1 | progname); | 
| 137 | schorsch | 2.9 | return 1; | 
| 138 | greg | 1.1 | } | 
| 139 |  |  |  | 
| 140 |  |  |  | 
| 141 | schorsch | 2.9 | void | 
| 142 |  |  | convert(                /* replace marks in a stream */ | 
| 143 |  |  | char    *name, | 
| 144 |  |  | register FILE   *fin | 
| 145 |  |  | ) | 
| 146 | greg | 1.1 | { | 
| 147 |  |  | register int    c; | 
| 148 |  |  |  | 
| 149 |  |  | while ((c = getc(fin)) != EOF) { | 
| 150 |  |  | if (isspace(c))                         /* blank */ | 
| 151 |  |  | continue; | 
| 152 |  |  | if (c == '#') {                         /* comment */ | 
| 153 |  |  | putchar(c); | 
| 154 |  |  | do { | 
| 155 |  |  | if ((c = getc(fin)) == EOF) | 
| 156 |  |  | return; | 
| 157 |  |  | putchar(c); | 
| 158 |  |  | } while (c != '\n'); | 
| 159 |  |  | } else if (c == '!') {                  /* command */ | 
| 160 |  |  | ungetc(c, fin); | 
| 161 |  |  | cvcomm(name, fin); | 
| 162 |  |  | } else {                                /* object */ | 
| 163 |  |  | ungetc(c, fin); | 
| 164 |  |  | cvobject(name, fin); | 
| 165 |  |  | } | 
| 166 |  |  | } | 
| 167 |  |  | } | 
| 168 |  |  |  | 
| 169 |  |  |  | 
| 170 | schorsch | 2.9 | void | 
| 171 |  |  | cvcomm(         /* convert a command */ | 
| 172 |  |  | char    *fname, | 
| 173 |  |  | FILE    *fin | 
| 174 |  |  | ) | 
| 175 | greg | 1.1 | { | 
| 176 | schorsch | 2.8 | FILE    *pin; | 
| 177 | greg | 1.1 | char    buf[512], *fgetline(); | 
| 178 |  |  |  | 
| 179 |  |  | fgetline(buf, sizeof(buf), fin); | 
| 180 |  |  | if (expand) { | 
| 181 |  |  | if ((pin = popen(buf+1, "r")) == NULL) { | 
| 182 |  |  | fprintf(stderr, | 
| 183 |  |  | "%s: (%s): cannot execute \"%s\"\n", | 
| 184 |  |  | progname, fname, buf); | 
| 185 |  |  | exit(1); | 
| 186 |  |  | } | 
| 187 |  |  | convert(buf, pin); | 
| 188 |  |  | pclose(pin); | 
| 189 |  |  | } else | 
| 190 |  |  | printf("\n%s\n", buf); | 
| 191 |  |  | } | 
| 192 |  |  |  | 
| 193 |  |  |  | 
| 194 | schorsch | 2.9 | void | 
| 195 |  |  | cvobject(               /* convert an object */ | 
| 196 |  |  | char    *fname, | 
| 197 |  |  | FILE    *fin | 
| 198 |  |  | ) | 
| 199 | greg | 1.1 | { | 
| 200 | greg | 2.6 | extern char     *fgetword(); | 
| 201 | greg | 1.1 | char    buf[128], typ[16], nam[128]; | 
| 202 | greg | 2.2 | int     i, n; | 
| 203 |  |  | register int    j; | 
| 204 | greg | 1.1 |  | 
| 205 |  |  | if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3) | 
| 206 |  |  | goto readerr; | 
| 207 | greg | 2.2 | if (!strcmp(typ, "polygon")) | 
| 208 |  |  | for (j = 0; j < nmarkers; j++) | 
| 209 |  |  | if (!strcmp(buf, marker[j].modin)) { | 
| 210 |  |  | replace(fname, &marker[j], nam, fin); | 
| 211 |  |  | return; | 
| 212 |  |  | } | 
| 213 | greg | 1.1 | printf("\n%s %s %s\n", buf, typ, nam); | 
| 214 |  |  | if (!strcmp(typ, "alias")) {            /* alias special case */ | 
| 215 |  |  | if (fscanf(fin, "%s", buf) != 1) | 
| 216 |  |  | goto readerr; | 
| 217 |  |  | printf("\t%s\n", buf); | 
| 218 |  |  | return; | 
| 219 |  |  | } | 
| 220 |  |  | for (i = 0; i < 3; i++) {               /* pass along arguments */ | 
| 221 |  |  | if (fscanf(fin, "%d", &n) != 1) | 
| 222 |  |  | goto readerr; | 
| 223 |  |  | printf("%d", n); | 
| 224 |  |  | for (j = 0; j < n; j++) { | 
| 225 | greg | 2.5 | if (fgetword(buf, sizeof(buf), fin) == NULL) | 
| 226 | greg | 1.1 | goto readerr; | 
| 227 |  |  | if (j%3 == 0) | 
| 228 |  |  | putchar('\n'); | 
| 229 | greg | 2.5 | putchar('\t'); | 
| 230 |  |  | fputword(buf, stdout); | 
| 231 | greg | 1.1 | } | 
| 232 |  |  | putchar('\n'); | 
| 233 |  |  | } | 
| 234 |  |  | return; | 
| 235 |  |  | readerr: | 
| 236 |  |  | fprintf(stderr, "%s: (%s): read error for %s \"%s\"\n", | 
| 237 |  |  | progname, fname, typ, nam); | 
| 238 |  |  | } | 
| 239 |  |  |  | 
| 240 |  |  |  | 
| 241 | schorsch | 2.9 | void | 
| 242 |  |  | replace(                /* replace marker */ | 
| 243 |  |  | char    *fname, | 
| 244 |  |  | register struct mrkr    *m, | 
| 245 |  |  | char    *mark, | 
| 246 |  |  | FILE    *fin | 
| 247 |  |  | ) | 
| 248 | greg | 1.1 | { | 
| 249 |  |  | int     n; | 
| 250 |  |  | char    buf[256]; | 
| 251 |  |  |  | 
| 252 | greg | 2.5 | buf[0] = '\0';                  /* bug fix thanks to schorsch */ | 
| 253 | greg | 2.11 | if (m->usetype == USE_XFORM) { | 
| 254 | greg | 2.10 | sprintf(buf, "xform -n %s", mark); | 
| 255 | greg | 2.2 | if (m->modout != NULL) | 
| 256 |  |  | sprintf(buf+strlen(buf), " -m %s", m->modout); | 
| 257 |  |  | if (buildxf(buf+strlen(buf), m->mscale, fin) < 0) | 
| 258 | greg | 1.1 | goto badxf; | 
| 259 | greg | 2.2 | sprintf(buf+strlen(buf), " %s", m->objname); | 
| 260 | greg | 1.1 | if (expand) { | 
| 261 |  |  | fflush(stdout); | 
| 262 |  |  | system(buf); | 
| 263 |  |  | } else | 
| 264 |  |  | printf("\n!%s\n", buf); | 
| 265 |  |  | } else { | 
| 266 | greg | 2.2 | if ((n = buildxf(buf, m->mscale, fin)) < 0) | 
| 267 | greg | 1.1 | goto badxf; | 
| 268 | greg | 2.11 | printf("\n%s %s %s\n", | 
| 269 |  |  | m->modout==NULL?"void":m->modout, | 
| 270 |  |  | m->usetype==USE_INSTANCE?"instance":"mesh", | 
| 271 |  |  | mark); | 
| 272 | greg | 2.2 | printf("%d %s%s\n0\n0\n", n+1, m->objname, buf); | 
| 273 | greg | 1.1 | } | 
| 274 |  |  | return; | 
| 275 |  |  | badxf: | 
| 276 |  |  | fprintf(stderr, "%s: (%s): bad arguments for marker \"%s\"\n", | 
| 277 |  |  | progname, fname, mark); | 
| 278 |  |  | exit(1); | 
| 279 |  |  | } | 
| 280 |  |  |  | 
| 281 |  |  |  | 
| 282 | schorsch | 2.9 | int | 
| 283 |  |  | edgecmp(                        /* compare two edges, descending order */ | 
| 284 |  |  | const void *e1, | 
| 285 |  |  | const void *e2 | 
| 286 |  |  | ) | 
| 287 | greg | 1.1 | { | 
| 288 | schorsch | 2.9 | if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2) | 
| 289 | greg | 1.1 | return(-1); | 
| 290 | schorsch | 2.9 | if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2) | 
| 291 | greg | 1.1 | return(1); | 
| 292 |  |  | return(0); | 
| 293 |  |  | } | 
| 294 |  |  |  | 
| 295 |  |  |  | 
| 296 |  |  | int | 
| 297 | schorsch | 2.9 | buildxf(                /* build transform for marker */ | 
| 298 |  |  | register char   *xf, | 
| 299 |  |  | double  markscale, | 
| 300 |  |  | FILE    *fin | 
| 301 |  |  | ) | 
| 302 | greg | 1.1 | { | 
| 303 |  |  | static FVECT    vlist[MAXVERT]; | 
| 304 |  |  | static EDGE     elist[MAXVERT]; | 
| 305 |  |  | FVECT   xvec, yvec, zvec; | 
| 306 |  |  | double  xlen; | 
| 307 |  |  | int     n; | 
| 308 |  |  | register int    i; | 
| 309 |  |  | /* | 
| 310 |  |  | * Read and sort vectors:  longest is hypotenuse, | 
| 311 |  |  | *      second longest is x' axis, | 
| 312 |  |  | *      third longest is y' axis (approximate), | 
| 313 |  |  | *      other vectors are ignored. | 
| 314 |  |  | * It is an error if the x' and y' axes do | 
| 315 |  |  | *      not share a common vertex (their origin). | 
| 316 |  |  | */ | 
| 317 |  |  | if (fscanf(fin, "%*d %*d %d", &n) != 1) | 
| 318 |  |  | return(-1); | 
| 319 |  |  | if (n%3 != 0) | 
| 320 |  |  | return(-1); | 
| 321 |  |  | n /= 3; | 
| 322 |  |  | if (n < 3 || n > MAXVERT) | 
| 323 |  |  | return(-1); | 
| 324 |  |  | /* sort edges in descending order */ | 
| 325 |  |  | for (i = 0; i < n; i++) { | 
| 326 |  |  | if (fscanf(fin, "%lf %lf %lf", &vlist[i][0], | 
| 327 |  |  | &vlist[i][1], &vlist[i][2]) != 3) | 
| 328 |  |  | return(-1); | 
| 329 |  |  | if (i) { | 
| 330 |  |  | elist[i].beg = i-1; | 
| 331 |  |  | elist[i].end = i; | 
| 332 |  |  | elist[i].len2 = dist2(vlist[i-1],vlist[i]); | 
| 333 |  |  | } | 
| 334 |  |  | } | 
| 335 |  |  | elist[0].beg = n-1; | 
| 336 |  |  | elist[0].end = 0; | 
| 337 |  |  | elist[0].len2 = dist2(vlist[n-1],vlist[0]); | 
| 338 |  |  | qsort(elist, n, sizeof(EDGE), edgecmp); | 
| 339 |  |  | /* find x' and y' */ | 
| 340 |  |  | if (elist[1].end == elist[2].beg || elist[1].end == elist[2].end) { | 
| 341 |  |  | i = elist[1].beg; | 
| 342 |  |  | elist[1].beg = elist[1].end; | 
| 343 |  |  | elist[1].end = i; | 
| 344 |  |  | } | 
| 345 |  |  | if (elist[2].end == elist[1].beg) { | 
| 346 |  |  | i = elist[2].beg; | 
| 347 |  |  | elist[2].beg = elist[2].end; | 
| 348 |  |  | elist[2].end = i; | 
| 349 |  |  | } | 
| 350 |  |  | if (elist[1].beg != elist[2].beg) | 
| 351 |  |  | return(-1);             /* x' and y' not connected! */ | 
| 352 |  |  | for (i = 0; i < 3; i++) { | 
| 353 |  |  | xvec[i] = vlist[elist[1].end][i] - vlist[elist[1].beg][i]; | 
| 354 |  |  | yvec[i] = vlist[elist[2].end][i] - vlist[elist[2].beg][i]; | 
| 355 |  |  | } | 
| 356 |  |  | if ((xlen = normalize(xvec)) == 0.0) | 
| 357 |  |  | return(-1); | 
| 358 |  |  | fcross(zvec, xvec, yvec); | 
| 359 |  |  | if (normalize(zvec) == 0.0) | 
| 360 |  |  | return(-1); | 
| 361 |  |  | fcross(yvec, zvec, xvec); | 
| 362 |  |  | n = 0;                          /* start transformation... */ | 
| 363 |  |  | if (markscale > 0.0) {          /* add scale factor */ | 
| 364 |  |  | sprintf(xf, " -s %f", xlen*markscale); | 
| 365 |  |  | n += 2; | 
| 366 | greg | 2.5 | while (*xf) ++xf; | 
| 367 | greg | 1.1 | } | 
| 368 |  |  | /* add rotation */ | 
| 369 |  |  | n += addrot(xf, xvec, yvec, zvec); | 
| 370 | greg | 2.5 | while (*xf) ++xf; | 
| 371 | greg | 1.1 | /* add translation */ | 
| 372 |  |  | n += 4; | 
| 373 |  |  | sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0], | 
| 374 |  |  | vlist[elist[1].beg][1], vlist[elist[1].beg][2]); | 
| 375 |  |  | return(n);                      /* all done */ | 
| 376 |  |  | } | 
| 377 |  |  |  | 
| 378 |  |  |  | 
| 379 | schorsch | 2.9 | int | 
| 380 |  |  | addrot(         /* compute rotation (x,y,z) => (xp,yp,zp) */ | 
| 381 |  |  | register char   *xf, | 
| 382 |  |  | FVECT xp, | 
| 383 |  |  | FVECT yp, | 
| 384 |  |  | FVECT zp | 
| 385 |  |  | ) | 
| 386 | greg | 1.1 | { | 
| 387 | greg | 1.2 | int     n; | 
| 388 |  |  | double  theta; | 
| 389 | greg | 1.1 |  | 
| 390 | greg | 1.2 | n = 0; | 
| 391 |  |  | theta = atan2(yp[2], zp[2]); | 
| 392 |  |  | if (!FEQ(theta,0.0)) { | 
| 393 |  |  | sprintf(xf, " -rx %f", theta*(180./PI)); | 
| 394 | greg | 2.5 | while (*xf) ++xf; | 
| 395 | greg | 1.2 | n += 2; | 
| 396 |  |  | } | 
| 397 |  |  | theta = asin(-xp[2]); | 
| 398 |  |  | if (!FEQ(theta,0.0)) { | 
| 399 |  |  | sprintf(xf, " -ry %f", theta*(180./PI)); | 
| 400 | greg | 2.5 | while (*xf) ++xf; | 
| 401 | greg | 1.2 | n += 2; | 
| 402 |  |  | } | 
| 403 |  |  | theta = atan2(xp[1], xp[0]); | 
| 404 |  |  | if (!FEQ(theta,0.0)) { | 
| 405 |  |  | sprintf(xf, " -rz %f", theta*(180./PI)); | 
| 406 | greg | 2.5 | /* while (*xf) ++xf; */ | 
| 407 | greg | 1.2 | n += 2; | 
| 408 |  |  | } | 
| 409 |  |  | return(n); | 
| 410 | greg | 1.1 | } |