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