| 1 | /* Copyright (c) 1994 Regents of the University of California */ | 
| 2 |  | 
| 3 | #ifndef lint | 
| 4 | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 | #endif | 
| 6 |  | 
| 7 | /* | 
| 8 | * Parse an MGF file, converting or discarding unsupported entities | 
| 9 | */ | 
| 10 |  | 
| 11 | #include <stdio.h> | 
| 12 | #include <math.h> | 
| 13 | #include <ctype.h> | 
| 14 | #include <string.h> | 
| 15 | #include "parser.h" | 
| 16 | #include "lookup.h" | 
| 17 | #include "messages.h" | 
| 18 |  | 
| 19 | /* | 
| 20 | * Global definitions of variables declared in parser.h | 
| 21 | */ | 
| 22 | /* entity names */ | 
| 23 |  | 
| 24 | char    mg_ename[MG_NENTITIES][MG_MAXELEN] = MG_NAMELIST; | 
| 25 |  | 
| 26 | /* Handler routines for each entity */ | 
| 27 |  | 
| 28 | int     (*mg_ehand[MG_NENTITIES])(); | 
| 29 |  | 
| 30 | /* error messages */ | 
| 31 |  | 
| 32 | char    *mg_err[MG_NERRS] = MG_ERRLIST; | 
| 33 |  | 
| 34 | MG_FCTXT        *mg_file;       /* current file context pointer */ | 
| 35 |  | 
| 36 | int     mg_nqcdivs = MG_NQCD;   /* number of divisions per quarter circle */ | 
| 37 |  | 
| 38 | /* | 
| 39 | * The idea with this parser is to compensate for any missing entries in | 
| 40 | * mg_ehand with alternate handlers that express these entities in terms | 
| 41 | * of others that the calling program can handle. | 
| 42 | * | 
| 43 | * In some cases, no alternate handler is possible because the entity | 
| 44 | * has no approximate equivalent.  These entities are simply discarded. | 
| 45 | * | 
| 46 | * Certain entities are dependent on others, and mg_init() will fail | 
| 47 | * if the supported entities are not consistent. | 
| 48 | * | 
| 49 | * Some alternate entity handlers require that earlier entities be | 
| 50 | * noted in some fashion, and we therefore keep another array of | 
| 51 | * parallel support handlers to assist in this effort. | 
| 52 | */ | 
| 53 |  | 
| 54 | /* temporary settings for testing */ | 
| 55 | #define e_ies e_any_toss | 
| 56 | /* alternate handler routines */ | 
| 57 |  | 
| 58 | static int      e_any_toss(),           /* discard unneeded entity */ | 
| 59 | e_ies(),                /* IES luminaire file */ | 
| 60 | e_include(),            /* include file */ | 
| 61 | e_sph(),                /* sphere */ | 
| 62 | e_cmix(),               /* color mixtures */ | 
| 63 | e_cspec(),              /* color spectra */ | 
| 64 | e_cyl(),                /* cylinder */ | 
| 65 | e_cone(),               /* cone */ | 
| 66 | e_prism(),              /* prism */ | 
| 67 | e_ring(),               /* ring */ | 
| 68 | e_torus();              /* torus */ | 
| 69 |  | 
| 70 | /* alternate handler support functions */ | 
| 71 |  | 
| 72 | static int      (*e_supp[MG_NENTITIES])(); | 
| 73 |  | 
| 74 | static char     FLTFMT[] = "%.12g"; | 
| 75 |  | 
| 76 | static int      warpconends;            /* hack for generating good normals */ | 
| 77 |  | 
| 78 |  | 
| 79 | void | 
| 80 | mg_init()                       /* initialize alternate entity handlers */ | 
| 81 | { | 
| 82 | unsigned long   ineed = 0, uneed = 0; | 
| 83 | register int    i; | 
| 84 | /* pick up slack */ | 
| 85 | if (mg_ehand[MG_E_IES] == NULL) | 
| 86 | mg_ehand[MG_E_IES] = e_ies; | 
| 87 | if (mg_ehand[MG_E_INCLUDE] == NULL) | 
| 88 | mg_ehand[MG_E_INCLUDE] = e_include; | 
| 89 | if (mg_ehand[MG_E_SPH] == NULL) { | 
| 90 | mg_ehand[MG_E_SPH] = e_sph; | 
| 91 | ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX; | 
| 92 | } else | 
| 93 | uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; | 
| 94 | if (mg_ehand[MG_E_CYL] == NULL) { | 
| 95 | mg_ehand[MG_E_CYL] = e_cyl; | 
| 96 | ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX; | 
| 97 | } else | 
| 98 | uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; | 
| 99 | if (mg_ehand[MG_E_CONE] == NULL) { | 
| 100 | mg_ehand[MG_E_CONE] = e_cone; | 
| 101 | ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX; | 
| 102 | } else | 
| 103 | uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; | 
| 104 | if (mg_ehand[MG_E_RING] == NULL) { | 
| 105 | mg_ehand[MG_E_RING] = e_ring; | 
| 106 | ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX; | 
| 107 | } else | 
| 108 | uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF; | 
| 109 | if (mg_ehand[MG_E_PRISM] == NULL) { | 
| 110 | mg_ehand[MG_E_PRISM] = e_prism; | 
| 111 | ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX; | 
| 112 | } else | 
| 113 | uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; | 
| 114 | if (mg_ehand[MG_E_TORUS] == NULL) { | 
| 115 | mg_ehand[MG_E_TORUS] = e_torus; | 
| 116 | ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX; | 
| 117 | } else | 
| 118 | uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF; | 
| 119 | if (mg_ehand[MG_E_COLOR] != NULL) { | 
| 120 | if (mg_ehand[MG_E_CMIX] == NULL) { | 
| 121 | mg_ehand[MG_E_CMIX] = e_cmix; | 
| 122 | ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX; | 
| 123 | } | 
| 124 | if (mg_ehand[MG_E_CSPEC] == NULL) { | 
| 125 | mg_ehand[MG_E_CSPEC] = e_cspec; | 
| 126 | ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX; | 
| 127 | } | 
| 128 | } | 
| 129 | /* check for consistency */ | 
| 130 | if (mg_ehand[MG_E_FACE] != NULL) | 
| 131 | uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; | 
| 132 | if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL || | 
| 133 | mg_ehand[MG_E_CMIX] != NULL) | 
| 134 | uneed |= 1<<MG_E_COLOR; | 
| 135 | if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL || | 
| 136 | mg_ehand[MG_E_ED] != NULL || | 
| 137 | mg_ehand[MG_E_RS] != NULL || | 
| 138 | mg_ehand[MG_E_TS] != NULL || | 
| 139 | mg_ehand[MG_E_SIDES] != NULL) | 
| 140 | uneed |= 1<<MG_E_MATERIAL; | 
| 141 | for (i = 0; i < MG_NENTITIES; i++) | 
| 142 | if (uneed & 1<<i && mg_ehand[i] == NULL) { | 
| 143 | fprintf(stderr, "Missing support for \"%s\" entity\n", | 
| 144 | mg_ename[i]); | 
| 145 | exit(1); | 
| 146 | } | 
| 147 | /* add support as needed */ | 
| 148 | if (ineed & 1<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex) | 
| 149 | e_supp[MG_E_VERTEX] = c_hvertex; | 
| 150 | if (ineed & 1<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex) | 
| 151 | e_supp[MG_E_POINT] = c_hvertex; | 
| 152 | if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex) | 
| 153 | e_supp[MG_E_NORMAL] = c_hvertex; | 
| 154 | if (ineed & 1<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor) | 
| 155 | e_supp[MG_E_COLOR] = c_hcolor; | 
| 156 | if (ineed & 1<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor) | 
| 157 | e_supp[MG_E_CXY] = c_hcolor; | 
| 158 | if (ineed & 1<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor) | 
| 159 | e_supp[MG_E_CSPEC] = c_hcolor; | 
| 160 | if (ineed & 1<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor) | 
| 161 | e_supp[MG_E_CMIX] = c_hcolor; | 
| 162 | /* discard remaining entities */ | 
| 163 | for (i = 0; i < MG_NENTITIES; i++) | 
| 164 | if (mg_ehand[i] == NULL) | 
| 165 | mg_ehand[i] = e_any_toss; | 
| 166 | } | 
| 167 |  | 
| 168 |  | 
| 169 |  | 
| 170 | int | 
| 171 | mg_entity(name)                 /* get entity number from its name */ | 
| 172 | char    *name; | 
| 173 | { | 
| 174 | static LUTAB    ent_tab = LU_SINIT(NULL,NULL);  /* lookup table */ | 
| 175 | register char   *cp; | 
| 176 |  | 
| 177 | if (!ent_tab.tsiz) {            /* initialize hash table */ | 
| 178 | if (!lu_init(&ent_tab, MG_NENTITIES)) | 
| 179 | return(-1);             /* what to do? */ | 
| 180 | for (cp = mg_ename[MG_NENTITIES-1]; cp >= mg_ename[0]; | 
| 181 | cp -= sizeof(mg_ename[0])) | 
| 182 | lu_find(&ent_tab, cp)->key = cp; | 
| 183 | } | 
| 184 | cp = lu_find(&ent_tab, name)->key; | 
| 185 | if (cp == NULL) | 
| 186 | return(-1); | 
| 187 | return((cp - mg_ename[0])/sizeof(mg_ename[0])); | 
| 188 | } | 
| 189 |  | 
| 190 |  | 
| 191 | int | 
| 192 | mg_handle(en, ac, av)           /* pass entity to appropriate handler */ | 
| 193 | register int    en; | 
| 194 | int     ac; | 
| 195 | char    **av; | 
| 196 | { | 
| 197 | int     rv; | 
| 198 |  | 
| 199 | if (en < 0 && (en = mg_entity(av[0])) < 0) | 
| 200 | return(MG_EUNK); | 
| 201 | if (e_supp[en] != NULL) { | 
| 202 | if ((rv = (*e_supp[en])(ac, av)) != MG_OK) | 
| 203 | return(rv); | 
| 204 | } | 
| 205 | return((*mg_ehand[en])(ac, av)); | 
| 206 | } | 
| 207 |  | 
| 208 |  | 
| 209 | int | 
| 210 | mg_open(ctx, fn)                        /* open new input file */ | 
| 211 | register MG_FCTXT       *ctx; | 
| 212 | char    *fn; | 
| 213 | { | 
| 214 | static int      nfids; | 
| 215 | int     olen; | 
| 216 | register char   *cp; | 
| 217 |  | 
| 218 | ctx->fid = ++nfids; | 
| 219 | ctx->lineno = 0; | 
| 220 | if (fn == NULL) { | 
| 221 | strcpy(ctx->fname, "<stdin>"); | 
| 222 | ctx->fp = stdin; | 
| 223 | ctx->prev = mg_file; | 
| 224 | mg_file = ctx; | 
| 225 | return(MG_OK); | 
| 226 | } | 
| 227 | /* get name relative to this context */ | 
| 228 | if (mg_file != NULL && | 
| 229 | (cp = strrchr(mg_file->fname, '/')) != NULL) | 
| 230 | olen = cp - mg_file->fname + 1; | 
| 231 | else | 
| 232 | olen = 0; | 
| 233 | if (olen) | 
| 234 | strcpy(ctx->fname, mg_file->fname); | 
| 235 | strcpy(ctx->fname+olen, fn); | 
| 236 | ctx->fp = fopen(ctx->fname, "r"); | 
| 237 | if (ctx->fp == NULL) | 
| 238 | return(MG_ENOFILE); | 
| 239 | ctx->prev = mg_file;            /* establish new context */ | 
| 240 | mg_file = ctx; | 
| 241 | return(MG_OK); | 
| 242 | } | 
| 243 |  | 
| 244 |  | 
| 245 | void | 
| 246 | mg_close()                      /* close input file */ | 
| 247 | { | 
| 248 | register MG_FCTXT       *ctx = mg_file; | 
| 249 |  | 
| 250 | mg_file = ctx->prev;            /* restore enclosing context */ | 
| 251 | if (ctx->fp == stdin) | 
| 252 | return;                 /* don't close standard input */ | 
| 253 | fclose(ctx->fp); | 
| 254 | } | 
| 255 |  | 
| 256 |  | 
| 257 | void | 
| 258 | mg_fgetpos(pos)                 /* get current position in input file */ | 
| 259 | register MG_FPOS        *pos; | 
| 260 | { | 
| 261 | extern long     ftell(); | 
| 262 |  | 
| 263 | pos->fid = mg_file->fid; | 
| 264 | pos->lineno = mg_file->lineno; | 
| 265 | pos->offset = ftell(mg_file->fp); | 
| 266 | } | 
| 267 |  | 
| 268 |  | 
| 269 | int | 
| 270 | mg_fgoto(pos)                   /* reposition input file pointer */ | 
| 271 | register MG_FPOS        *pos; | 
| 272 | { | 
| 273 | if (pos->fid != mg_file->fid) | 
| 274 | return(MG_ESEEK); | 
| 275 | if (pos->lineno == mg_file->lineno) | 
| 276 | return(MG_OK); | 
| 277 | if (mg_file->fp == stdin) | 
| 278 | return(MG_ESEEK);       /* cannot seek on standard input */ | 
| 279 | if (fseek(mg_file->fp, pos->offset, 0) == EOF) | 
| 280 | return(MG_ESEEK); | 
| 281 | mg_file->lineno = pos->lineno; | 
| 282 | return(MG_OK); | 
| 283 | } | 
| 284 |  | 
| 285 |  | 
| 286 | int | 
| 287 | mg_read()                       /* read next line from file */ | 
| 288 | { | 
| 289 | register int    len = 0; | 
| 290 |  | 
| 291 | do { | 
| 292 | if (fgets(mg_file->inpline+len, | 
| 293 | MG_MAXLINE-len, mg_file->fp) == NULL) | 
| 294 | return(len); | 
| 295 | mg_file->lineno++; | 
| 296 | len += strlen(mg_file->inpline+len); | 
| 297 | if (len > 1 && mg_file->inpline[len-2] == '\\') | 
| 298 | mg_file->inpline[--len-1] = ' '; | 
| 299 | } while (mg_file->inpline[len]); | 
| 300 |  | 
| 301 | return(len); | 
| 302 | } | 
| 303 |  | 
| 304 |  | 
| 305 | int | 
| 306 | mg_parse()                      /* parse current input line */ | 
| 307 | { | 
| 308 | char    abuf[MG_MAXLINE]; | 
| 309 | char    *argv[MG_MAXARGC]; | 
| 310 | int     en; | 
| 311 | register char   *cp, **ap; | 
| 312 |  | 
| 313 | strcpy(cp=abuf, mg_file->inpline); | 
| 314 | ap = argv;                      /* break into words */ | 
| 315 | for ( ; ; ) { | 
| 316 | while (isspace(*cp)) | 
| 317 | *cp++ = '\0'; | 
| 318 | if (!*cp) | 
| 319 | break; | 
| 320 | if (ap - argv >= MG_MAXARGC-1) | 
| 321 | return(MG_EARGC); | 
| 322 | *ap++ = cp; | 
| 323 | while (*++cp && !isspace(*cp)) | 
| 324 | ; | 
| 325 | } | 
| 326 | if (ap == argv) | 
| 327 | return(MG_OK);          /* no words in line */ | 
| 328 | *ap = NULL; | 
| 329 | /* else handle it */ | 
| 330 | return(mg_handle(-1, ap-argv, argv)); | 
| 331 | } | 
| 332 |  | 
| 333 |  | 
| 334 | int | 
| 335 | mg_load(fn)                     /* load an MGF file */ | 
| 336 | char    *fn; | 
| 337 | { | 
| 338 | MG_FCTXT        cntxt; | 
| 339 | int     rval; | 
| 340 |  | 
| 341 | if ((rval = mg_open(&cntxt, fn)) != MG_OK) { | 
| 342 | fprintf(stderr, "%s: %s\n", fn, mg_err[rval]); | 
| 343 | return(rval); | 
| 344 | } | 
| 345 | while (mg_read())               /* parse each line */ | 
| 346 | if ((rval = mg_parse()) != MG_OK) { | 
| 347 | fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname, | 
| 348 | cntxt.lineno, mg_err[rval], | 
| 349 | cntxt.inpline); | 
| 350 | break; | 
| 351 | } | 
| 352 | mg_close(); | 
| 353 | return(rval); | 
| 354 | } | 
| 355 |  | 
| 356 |  | 
| 357 | void | 
| 358 | mg_clear()                      /* clear parser history */ | 
| 359 | { | 
| 360 | c_clearall();                   /* clear context tables */ | 
| 361 | mg_file = NULL;                 /* reset our context */ | 
| 362 | } | 
| 363 |  | 
| 364 |  | 
| 365 | /**************************************************************************** | 
| 366 | *      The following routines handle unsupported entities | 
| 367 | */ | 
| 368 |  | 
| 369 |  | 
| 370 | static int | 
| 371 | e_any_toss(ac, av)              /* discard an unwanted entity */ | 
| 372 | int     ac; | 
| 373 | char    **av; | 
| 374 | { | 
| 375 | return(MG_OK); | 
| 376 | } | 
| 377 |  | 
| 378 |  | 
| 379 | static int | 
| 380 | e_include(ac, av)               /* include file */ | 
| 381 | int     ac; | 
| 382 | char    **av; | 
| 383 | { | 
| 384 | char    *xfarg[MG_MAXARGC]; | 
| 385 | MG_FCTXT        ictx; | 
| 386 | int     rv; | 
| 387 |  | 
| 388 | if (ac < 2) | 
| 389 | return(MG_EARGC); | 
| 390 | if ((rv = mg_open(&ictx, av[1])) != MG_OK) | 
| 391 | return(rv); | 
| 392 | if (ac > 2) { | 
| 393 | register int    i; | 
| 394 |  | 
| 395 | xfarg[0] = mg_ename[MG_E_XF]; | 
| 396 | for (i = 1; i < ac-1; i++) | 
| 397 | xfarg[i] = av[i+1]; | 
| 398 | xfarg[ac-1] = NULL; | 
| 399 | if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) | 
| 400 | return(rv); | 
| 401 | } | 
| 402 | while (!feof(mg_file->fp)) { | 
| 403 | while (mg_read()) | 
| 404 | if ((rv = mg_parse()) != MG_OK) { | 
| 405 | fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, | 
| 406 | ictx.lineno, mg_err[rv], | 
| 407 | ictx.inpline); | 
| 408 | mg_close(); | 
| 409 | return(MG_EINCL); | 
| 410 | } | 
| 411 | if (ac > 2) | 
| 412 | if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) | 
| 413 | return(rv); | 
| 414 | } | 
| 415 | mg_close(); | 
| 416 | return(MG_OK); | 
| 417 | } | 
| 418 |  | 
| 419 |  | 
| 420 | static void | 
| 421 | make_axes(u, v, w)              /* compute u and v given w (normalized) */ | 
| 422 | FVECT   u, v, w; | 
| 423 | { | 
| 424 | register int    i; | 
| 425 |  | 
| 426 | v[0] = v[1] = v[2] = 0.; | 
| 427 | for (i = 0; i < 3; i++) | 
| 428 | if (w[i] < .6 && w[i] > -.6) | 
| 429 | break; | 
| 430 | v[i] = 1.; | 
| 431 | fcross(u, v, w); | 
| 432 | normalize(u); | 
| 433 | fcross(v, w, u); | 
| 434 | } | 
| 435 |  | 
| 436 |  | 
| 437 | static int | 
| 438 | e_sph(ac, av)                   /* expand a sphere into cones */ | 
| 439 | int     ac; | 
| 440 | char    **av; | 
| 441 | { | 
| 442 | static char     p2x[24], p2y[24], p2z[24], r1[24], r2[24]; | 
| 443 | static char     *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"}; | 
| 444 | static char     *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="}; | 
| 445 | static char     *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z}; | 
| 446 | static char     *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2}; | 
| 447 | register C_VERTEX       *cv; | 
| 448 | register int    i; | 
| 449 | int     rval; | 
| 450 | double  rad; | 
| 451 | double  theta; | 
| 452 |  | 
| 453 | if (ac != 3) | 
| 454 | return(MG_EARGC); | 
| 455 | if ((cv = c_getvert(av[1])) == NULL) | 
| 456 | return(MG_EUNDEF); | 
| 457 | if (!isflt(av[2])) | 
| 458 | return(MG_ETYPE); | 
| 459 | rad = atof(av[2]); | 
| 460 | /* initialize */ | 
| 461 | warpconends = 1; | 
| 462 | if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK) | 
| 463 | return(rval); | 
| 464 | sprintf(p2x, FLTFMT, cv->p[0]); | 
| 465 | sprintf(p2y, FLTFMT, cv->p[1]); | 
| 466 | sprintf(p2z, FLTFMT, cv->p[2]+rad); | 
| 467 | if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) | 
| 468 | return(rval); | 
| 469 | r2[0] = '0'; r2[1] = '\0'; | 
| 470 | for (i = 1; i <= 2*mg_nqcdivs; i++) { | 
| 471 | theta = i*(PI/2)/mg_nqcdivs; | 
| 472 | if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) | 
| 473 | return(rval); | 
| 474 | sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta)); | 
| 475 | if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) | 
| 476 | return(rval); | 
| 477 | if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) | 
| 478 | return(rval); | 
| 479 | strcpy(r1, r2); | 
| 480 | sprintf(r2, FLTFMT, rad*sin(theta)); | 
| 481 | if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) | 
| 482 | return(rval); | 
| 483 | } | 
| 484 | warpconends = 0; | 
| 485 | return(MG_OK); | 
| 486 | } | 
| 487 |  | 
| 488 |  | 
| 489 | static int | 
| 490 | e_torus(ac, av)                 /* expand a torus into cones */ | 
| 491 | int     ac; | 
| 492 | char    **av; | 
| 493 | { | 
| 494 | static char     p2[3][24], r1[24], r2[24]; | 
| 495 | static char     *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"}; | 
| 496 | static char     *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="}; | 
| 497 | static char     *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]}; | 
| 498 | static char     *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2}; | 
| 499 | register C_VERTEX       *cv; | 
| 500 | register int    i, j; | 
| 501 | int     rval; | 
| 502 | int     sgn; | 
| 503 | double  minrad, maxrad, avgrad; | 
| 504 | double  theta; | 
| 505 |  | 
| 506 | if (ac != 4) | 
| 507 | return(MG_EARGC); | 
| 508 | if ((cv = c_getvert(av[1])) == NULL) | 
| 509 | return(MG_EUNDEF); | 
| 510 | if (is0vect(cv->n)) | 
| 511 | return(MG_EILL); | 
| 512 | if (!isflt(av[2]) || !isflt(av[3])) | 
| 513 | return(MG_ETYPE); | 
| 514 | minrad = atof(av[2]); | 
| 515 | round0(minrad); | 
| 516 | maxrad = atof(av[3]); | 
| 517 | /* check orientation */ | 
| 518 | if (minrad > 0.) | 
| 519 | sgn = 1; | 
| 520 | else if (minrad < 0.) | 
| 521 | sgn = -1; | 
| 522 | else if (maxrad > 0.) | 
| 523 | sgn = 1; | 
| 524 | else if (maxrad < 0.) | 
| 525 | sgn = -1; | 
| 526 | else | 
| 527 | return(MG_EILL); | 
| 528 | if (sgn*(maxrad-minrad) <= 0.) | 
| 529 | return(MG_EILL); | 
| 530 | /* initialize */ | 
| 531 | warpconends = 1; | 
| 532 | v2ent[3] = av[1]; | 
| 533 | for (j = 0; j < 3; j++) | 
| 534 | sprintf(p2[j], FLTFMT, cv->p[j] + | 
| 535 | .5*sgn*(maxrad-minrad)*cv->n[j]); | 
| 536 | if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) | 
| 537 | return(rval); | 
| 538 | if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) | 
| 539 | return(rval); | 
| 540 | sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad)); | 
| 541 | /* run outer section */ | 
| 542 | for (i = 1; i <= 2*mg_nqcdivs; i++) { | 
| 543 | theta = i*(PI/2)/mg_nqcdivs; | 
| 544 | if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) | 
| 545 | return(rval); | 
| 546 | for (j = 0; j < 3; j++) | 
| 547 | sprintf(p2[j], FLTFMT, cv->p[j] + | 
| 548 | .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); | 
| 549 | if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) | 
| 550 | return(rval); | 
| 551 | if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) | 
| 552 | return(rval); | 
| 553 | strcpy(r1, r2); | 
| 554 | sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta)); | 
| 555 | if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) | 
| 556 | return(rval); | 
| 557 | } | 
| 558 | /* run inner section */ | 
| 559 | sprintf(r2, FLTFMT, -.5*(minrad+maxrad)); | 
| 560 | for ( ; i <= 4*mg_nqcdivs; i++) { | 
| 561 | theta = i*(PI/2)/mg_nqcdivs; | 
| 562 | for (j = 0; j < 3; j++) | 
| 563 | sprintf(p2[j], FLTFMT, cv->p[j] + | 
| 564 | .5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); | 
| 565 | if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) | 
| 566 | return(rval); | 
| 567 | if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) | 
| 568 | return(rval); | 
| 569 | if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) | 
| 570 | return(rval); | 
| 571 | strcpy(r1, r2); | 
| 572 | sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta)); | 
| 573 | if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) | 
| 574 | return(rval); | 
| 575 | } | 
| 576 | warpconends = 0; | 
| 577 | return(MG_OK); | 
| 578 | } | 
| 579 |  | 
| 580 |  | 
| 581 | static int | 
| 582 | e_cyl(ac, av)                   /* replace a cylinder with equivalent cone */ | 
| 583 | int     ac; | 
| 584 | char    **av; | 
| 585 | { | 
| 586 | static char     *avnew[6] = {mg_ename[MG_E_CONE]}; | 
| 587 |  | 
| 588 | if (ac != 4) | 
| 589 | return(MG_EARGC); | 
| 590 | avnew[1] = av[1]; | 
| 591 | avnew[2] = av[2]; | 
| 592 | avnew[3] = av[3]; | 
| 593 | avnew[4] = av[2]; | 
| 594 | return(mg_handle(MG_E_CONE, 5, avnew)); | 
| 595 | } | 
| 596 |  | 
| 597 |  | 
| 598 | static int | 
| 599 | e_ring(ac, av)                  /* turn a ring into polygons */ | 
| 600 | int     ac; | 
| 601 | char    **av; | 
| 602 | { | 
| 603 | static char     p3[3][24], p4[3][24]; | 
| 604 | static char     *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"}; | 
| 605 | static char     *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="}; | 
| 606 | static char     *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"}; | 
| 607 | static char     *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="}; | 
| 608 | static char     *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]}; | 
| 609 | static char     *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="}; | 
| 610 | static char     *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; | 
| 611 | static char     *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"}; | 
| 612 | register C_VERTEX       *cv; | 
| 613 | register int    i, j; | 
| 614 | FVECT   u, v; | 
| 615 | double  minrad, maxrad; | 
| 616 | int     rv; | 
| 617 | double  theta, d; | 
| 618 |  | 
| 619 | if (ac != 4) | 
| 620 | return(MG_EARGC); | 
| 621 | if ((cv = c_getvert(av[1])) == NULL) | 
| 622 | return(MG_EUNDEF); | 
| 623 | if (is0vect(cv->n)) | 
| 624 | return(MG_EILL); | 
| 625 | if (!isflt(av[2]) || !isflt(av[3])) | 
| 626 | return(MG_ETYPE); | 
| 627 | minrad = atof(av[2]); | 
| 628 | round0(minrad); | 
| 629 | maxrad = atof(av[3]); | 
| 630 | if (minrad < 0. || maxrad <= minrad) | 
| 631 | return(MG_EILL); | 
| 632 | /* initialize */ | 
| 633 | make_axes(u, v, cv->n); | 
| 634 | for (j = 0; j < 3; j++) | 
| 635 | sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]); | 
| 636 | if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK) | 
| 637 | return(rv); | 
| 638 | if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) | 
| 639 | return(rv); | 
| 640 | if (minrad == 0.) {             /* closed */ | 
| 641 | v1ent[3] = av[1]; | 
| 642 | if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) | 
| 643 | return(rv); | 
| 644 | if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK) | 
| 645 | return(rv); | 
| 646 | for (i = 1; i <= 4*mg_nqcdivs; i++) { | 
| 647 | theta = i*(PI/2)/mg_nqcdivs; | 
| 648 | if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) | 
| 649 | return(rv); | 
| 650 | for (j = 0; j < 3; j++) | 
| 651 | sprintf(p3[j], FLTFMT, cv->p[j] + | 
| 652 | maxrad*u[j]*cos(theta) + | 
| 653 | maxrad*v[j]*sin(theta)); | 
| 654 | if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) | 
| 655 | return(rv); | 
| 656 | if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) | 
| 657 | return(rv); | 
| 658 | if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK) | 
| 659 | return(rv); | 
| 660 | } | 
| 661 | } else {                        /* open */ | 
| 662 | if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK) | 
| 663 | return(rv); | 
| 664 | for (j = 0; j < 3; j++) | 
| 665 | sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]); | 
| 666 | if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) | 
| 667 | return(rv); | 
| 668 | v1ent[3] = "_rv4"; | 
| 669 | for (i = 1; i <= 4*mg_nqcdivs; i++) { | 
| 670 | theta = i*(PI/2)/mg_nqcdivs; | 
| 671 | if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) | 
| 672 | return(rv); | 
| 673 | if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) | 
| 674 | return(rv); | 
| 675 | for (j = 0; j < 3; j++) { | 
| 676 | d = u[j]*cos(theta) + v[j]*sin(theta); | 
| 677 | sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d); | 
| 678 | sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d); | 
| 679 | } | 
| 680 | if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) | 
| 681 | return(rv); | 
| 682 | if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) | 
| 683 | return(rv); | 
| 684 | if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK) | 
| 685 | return(rv); | 
| 686 | if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) | 
| 687 | return(rv); | 
| 688 | if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK) | 
| 689 | return(rv); | 
| 690 | } | 
| 691 | } | 
| 692 | return(MG_OK); | 
| 693 | } | 
| 694 |  | 
| 695 |  | 
| 696 | static int | 
| 697 | e_cone(ac, av)                  /* turn a cone into polygons */ | 
| 698 | int     ac; | 
| 699 | char    **av; | 
| 700 | { | 
| 701 | static char     p3[3][24], p4[3][24], n3[3][24], n4[3][24]; | 
| 702 | static char     *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="}; | 
| 703 | static char     *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"}; | 
| 704 | static char     *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="}; | 
| 705 | static char     *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]}; | 
| 706 | static char     *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]}; | 
| 707 | static char     *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="}; | 
| 708 | static char     *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; | 
| 709 | static char     *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]}; | 
| 710 | static char     *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"}; | 
| 711 | register C_VERTEX       *cv1, *cv2; | 
| 712 | register int    i, j; | 
| 713 | FVECT   u, v, w; | 
| 714 | double  rad1, rad2; | 
| 715 | int     sgn; | 
| 716 | double  n1off, n2off; | 
| 717 | double  d; | 
| 718 | int     rv; | 
| 719 | double  theta; | 
| 720 |  | 
| 721 | if (ac != 5) | 
| 722 | return(MG_EARGC); | 
| 723 | if ((cv1 = c_getvert(av[1])) == NULL || | 
| 724 | (cv2 = c_getvert(av[3])) == NULL) | 
| 725 | return(MG_EUNDEF); | 
| 726 | if (!isflt(av[2]) || !isflt(av[4])) | 
| 727 | return(MG_ETYPE); | 
| 728 | rad1 = atof(av[2]); | 
| 729 | round0(rad1); | 
| 730 | rad2 = atof(av[4]); | 
| 731 | round0(rad2); | 
| 732 | if (rad1 == 0.) { | 
| 733 | if (rad2 == 0.) | 
| 734 | return(MG_EILL); | 
| 735 | } else if (rad2 != 0.) { | 
| 736 | if (rad1 < 0. ^ rad2 < 0.) | 
| 737 | return(MG_EILL); | 
| 738 | } else {                        /* swap */ | 
| 739 | C_VERTEX        *cv; | 
| 740 |  | 
| 741 | cv = cv1; | 
| 742 | cv1 = cv2; | 
| 743 | cv2 = cv; | 
| 744 | d = rad1; | 
| 745 | rad1 = rad2; | 
| 746 | rad2 = d; | 
| 747 | } | 
| 748 | sgn = rad2 < 0. ? -1 : 1; | 
| 749 | /* initialize */ | 
| 750 | for (j = 0; j < 3; j++) | 
| 751 | w[j] = cv1->p[j] - cv2->p[j]; | 
| 752 | if ((d = normalize(w)) == 0.) | 
| 753 | return(MG_EILL); | 
| 754 | n1off = n2off = (rad2 - rad1)/d; | 
| 755 | if (warpconends) {              /* hack for e_sph and e_torus */ | 
| 756 | d = atan(n2off) - (PI/4)/mg_nqcdivs; | 
| 757 | if (d <= -PI/2+FTINY) | 
| 758 | n2off = -FHUGE; | 
| 759 | else | 
| 760 | n2off = tan(d); | 
| 761 | } | 
| 762 | make_axes(u, v, w); | 
| 763 | for (j = 0; j < 3; j++) { | 
| 764 | sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]); | 
| 765 | if (n2off <= -FHUGE) | 
| 766 | sprintf(n3[j], FLTFMT, -w[j]); | 
| 767 | else | 
| 768 | sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off); | 
| 769 | } | 
| 770 | if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK) | 
| 771 | return(rv); | 
| 772 | if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) | 
| 773 | return(rv); | 
| 774 | if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) | 
| 775 | return(rv); | 
| 776 | if (rad1 == 0.) {               /* triangles */ | 
| 777 | v1ent[3] = av[1]; | 
| 778 | if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) | 
| 779 | return(rv); | 
| 780 | for (j = 0; j < 3; j++) | 
| 781 | sprintf(n4[j], FLTFMT, w[j]); | 
| 782 | if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) | 
| 783 | return(rv); | 
| 784 | for (i = 1; i <= 4*mg_nqcdivs; i++) { | 
| 785 | theta = sgn*i*(PI/2)/mg_nqcdivs; | 
| 786 | if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) | 
| 787 | return(rv); | 
| 788 | for (j = 0; j < 3; j++) { | 
| 789 | d = u[j]*cos(theta) + v[j]*sin(theta); | 
| 790 | sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d); | 
| 791 | if (n2off > -FHUGE) | 
| 792 | sprintf(n3[j], FLTFMT, d + w[j]*n2off); | 
| 793 | } | 
| 794 | if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) | 
| 795 | return(rv); | 
| 796 | if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) | 
| 797 | return(rv); | 
| 798 | if (n2off > -FHUGE && | 
| 799 | (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) | 
| 800 | return(rv); | 
| 801 | if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK) | 
| 802 | return(rv); | 
| 803 | } | 
| 804 | } else {                        /* quads */ | 
| 805 | v1ent[3] = "_cv4"; | 
| 806 | if (warpconends) {              /* hack for e_sph and e_torus */ | 
| 807 | d = atan(n1off) + (PI/4)/mg_nqcdivs; | 
| 808 | if (d >= PI/2-FTINY) | 
| 809 | n1off = FHUGE; | 
| 810 | else | 
| 811 | n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs); | 
| 812 | } | 
| 813 | for (j = 0; j < 3; j++) { | 
| 814 | sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]); | 
| 815 | if (n1off >= FHUGE) | 
| 816 | sprintf(n4[j], FLTFMT, w[j]); | 
| 817 | else | 
| 818 | sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off); | 
| 819 | } | 
| 820 | if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK) | 
| 821 | return(rv); | 
| 822 | if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) | 
| 823 | return(rv); | 
| 824 | if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) | 
| 825 | return(rv); | 
| 826 | for (i = 1; i <= 4*mg_nqcdivs; i++) { | 
| 827 | theta = sgn*i*(PI/2)/mg_nqcdivs; | 
| 828 | if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) | 
| 829 | return(rv); | 
| 830 | if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) | 
| 831 | return(rv); | 
| 832 | for (j = 0; j < 3; j++) { | 
| 833 | d = u[j]*cos(theta) + v[j]*sin(theta); | 
| 834 | sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d); | 
| 835 | if (n2off > -FHUGE) | 
| 836 | sprintf(n3[j], FLTFMT, d + w[j]*n2off); | 
| 837 | sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d); | 
| 838 | if (n1off < FHUGE) | 
| 839 | sprintf(n4[j], FLTFMT, d + w[j]*n1off); | 
| 840 | } | 
| 841 | if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) | 
| 842 | return(rv); | 
| 843 | if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) | 
| 844 | return(rv); | 
| 845 | if (n2off > -FHUGE && | 
| 846 | (rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) | 
| 847 | return(rv); | 
| 848 | if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK) | 
| 849 | return(rv); | 
| 850 | if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) | 
| 851 | return(rv); | 
| 852 | if (n1off < FHUGE && | 
| 853 | (rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) | 
| 854 | return(rv); | 
| 855 | if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK) | 
| 856 | return(rv); | 
| 857 | } | 
| 858 | } | 
| 859 | return(MG_OK); | 
| 860 | } | 
| 861 |  | 
| 862 |  | 
| 863 | static int | 
| 864 | e_prism(ac, av)                 /* turn a prism into polygons */ | 
| 865 | int     ac; | 
| 866 | char    **av; | 
| 867 | { | 
| 868 | static char     p[3][24]; | 
| 869 | static char     *vent[4] = {mg_ename[MG_E_VERTEX],NULL,"="}; | 
| 870 | static char     *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]}; | 
| 871 | char    *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8]; | 
| 872 | double  length; | 
| 873 | FVECT   v1, v2, v3, norm; | 
| 874 | register C_VERTEX       *cv; | 
| 875 | C_VERTEX        *cv0; | 
| 876 | int     rv; | 
| 877 | register int    i, j; | 
| 878 |  | 
| 879 | if (ac < 5) | 
| 880 | return(MG_EARGC); | 
| 881 | if (!isflt(av[ac-1])) | 
| 882 | return(MG_ETYPE); | 
| 883 | length = atof(av[ac-1]); | 
| 884 | if (length <= FTINY && length >= -FTINY) | 
| 885 | return(MG_EILL); | 
| 886 | /* do bottom face */ | 
| 887 | newav[0] = mg_ename[MG_E_FACE]; | 
| 888 | for (i = 1; i < ac-1; i++) | 
| 889 | newav[i] = av[i]; | 
| 890 | newav[i] = NULL; | 
| 891 | if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK) | 
| 892 | return(rv); | 
| 893 | /* compute face normal */ | 
| 894 | if ((cv0 = c_getvert(av[1])) == NULL) | 
| 895 | return(MG_EUNDEF); | 
| 896 | norm[0] = norm[1] = norm[2] = 0.; | 
| 897 | v1[0] = v1[1] = v1[2] = 0.; | 
| 898 | for (i = 2; i < ac-1; i++) { | 
| 899 | if ((cv = c_getvert(av[i])) == NULL) | 
| 900 | return(MG_EUNDEF); | 
| 901 | v2[0] = cv->p[0] - cv0->p[0]; | 
| 902 | v2[1] = cv->p[1] - cv0->p[1]; | 
| 903 | v2[2] = cv->p[2] - cv0->p[2]; | 
| 904 | fcross(v3, v1, v2); | 
| 905 | norm[0] += v3[0]; | 
| 906 | norm[1] += v3[1]; | 
| 907 | norm[2] += v3[2]; | 
| 908 | VCOPY(v1, v2); | 
| 909 | } | 
| 910 | if (normalize(norm) == 0.) | 
| 911 | return(MG_EILL); | 
| 912 | /* create moved vertices */ | 
| 913 | for (i = 1; i < ac-1; i++) { | 
| 914 | sprintf(nvn[i-1], "_pv%d", i); | 
| 915 | vent[1] = nvn[i-1]; | 
| 916 | if ((rv = mg_handle(MG_E_VERTEX, 3, vent)) != MG_OK) | 
| 917 | return(rv); | 
| 918 | cv = c_getvert(av[i]);          /* checked above */ | 
| 919 | for (j = 0; j < 3; j++) | 
| 920 | sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]); | 
| 921 | if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK) | 
| 922 | return(rv); | 
| 923 | newav[ac-1-i] = nvn[i-1];       /* reverse */ | 
| 924 | } | 
| 925 | /* do top face */ | 
| 926 | if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK) | 
| 927 | return(rv); | 
| 928 | /* do the side faces */ | 
| 929 | newav[5] = NULL; | 
| 930 | newav[3] = av[ac-2]; | 
| 931 | newav[4] = nvn[ac-3]; | 
| 932 | for (i = 1; i < ac-1; i++) { | 
| 933 | newav[1] = nvn[i-1]; | 
| 934 | newav[2] = av[i]; | 
| 935 | if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK) | 
| 936 | return(rv); | 
| 937 | newav[3] = newav[2]; | 
| 938 | newav[4] = newav[1]; | 
| 939 | } | 
| 940 | return(MG_OK); | 
| 941 | } | 
| 942 |  | 
| 943 |  | 
| 944 | static int | 
| 945 | e_cspec(ac, av)                 /* handle spectral color */ | 
| 946 | int     ac; | 
| 947 | char    **av; | 
| 948 | { | 
| 949 | static char     xbuf[24], ybuf[24]; | 
| 950 | static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf}; | 
| 951 | int     rv; | 
| 952 |  | 
| 953 | c_ccvt(c_ccolor, C_CSXY); | 
| 954 | /* if it's really their handler, use it */ | 
| 955 | if (mg_ehand[MG_E_CXY] != c_hcolor) { | 
| 956 | sprintf(xbuf, "%.4f", c_ccolor->cx); | 
| 957 | sprintf(ybuf, "%.4f", c_ccolor->cy); | 
| 958 | if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK) | 
| 959 | return(rv); | 
| 960 | } | 
| 961 | return(MG_OK); | 
| 962 | } | 
| 963 |  | 
| 964 |  | 
| 965 | static int | 
| 966 | e_cmix(ac, av)                  /* handle mixing of colors */ | 
| 967 | int     ac; | 
| 968 | char    **av; | 
| 969 | { | 
| 970 | char    wl[2][6], vbuf[C_CNSS][24]; | 
| 971 | char    *newav[C_CNSS+4]; | 
| 972 | int     rv; | 
| 973 | register int    i; | 
| 974 | /* | 
| 975 | * Contorted logic works as follows: | 
| 976 | *      1. the colors are already mixed in c_hcolor() support function | 
| 977 | *      2. if we would handle a spectral result, make sure it's not | 
| 978 | *      3. if c_hcolor() would handle a spectral result, don't bother | 
| 979 | *      4. otherwise, make cspec entity and pass it to their handler | 
| 980 | *      5. if we have only xy results, handle it as c_spec() would | 
| 981 | */ | 
| 982 | if (mg_ehand[MG_E_CSPEC] == e_cspec) | 
| 983 | c_ccvt(c_ccolor, C_CSXY); | 
| 984 | else if (c_ccolor->flags & C_CDSPEC) { | 
| 985 | if (mg_ehand[MG_E_CSPEC] != c_hcolor) { | 
| 986 | sprintf(wl[0], "%d", C_CMINWL); | 
| 987 | sprintf(wl[1], "%d", C_CMAXWL); | 
| 988 | newav[0] = mg_ename[MG_E_CSPEC]; | 
| 989 | newav[1] = wl[0]; | 
| 990 | newav[2] = wl[1]; | 
| 991 | for (i = 0; i < C_CNSS; i++) { | 
| 992 | sprintf(vbuf[i], "%.6f", | 
| 993 | (double)c_ccolor->ssamp[i] / | 
| 994 | c_ccolor->ssum); | 
| 995 | newav[i+3] = vbuf[i]; | 
| 996 | } | 
| 997 | newav[C_CNSS+3] = NULL; | 
| 998 | if ((rv = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK) | 
| 999 | return(rv); | 
| 1000 | } | 
| 1001 | return(MG_OK); | 
| 1002 | } | 
| 1003 | if (mg_ehand[MG_E_CXY] != c_hcolor) { | 
| 1004 | sprintf(vbuf[0], "%.4f", c_ccolor->cx); | 
| 1005 | sprintf(vbuf[1], "%.4f", c_ccolor->cy); | 
| 1006 | newav[0] = mg_ename[MG_E_CXY]; | 
| 1007 | newav[1] = vbuf[0]; | 
| 1008 | newav[2] = vbuf[1]; | 
| 1009 | newav[3] = NULL; | 
| 1010 | if ((rv = mg_handle(MG_E_CXY, 3, newav)) != MG_OK) | 
| 1011 | return(rv); | 
| 1012 | } | 
| 1013 | return(MG_OK); | 
| 1014 | } |