1 |
< |
/* Copyright (c) 1994 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
27 |
|
|
28 |
|
int (*mg_ehand[MG_NENTITIES])(); |
29 |
|
|
30 |
+ |
/* Handler routine for unknown entities */ |
31 |
+ |
|
32 |
+ |
int (*mg_uhand)() = mg_defuhand; |
33 |
+ |
|
34 |
+ |
unsigned mg_nunknown; /* count of unknown entities */ |
35 |
+ |
|
36 |
|
/* error messages */ |
37 |
|
|
38 |
|
char *mg_err[MG_NERRS] = MG_ERRLIST; |
63 |
|
|
64 |
|
static int e_any_toss(), /* discard unneeded entity */ |
65 |
|
e_ies(), /* IES luminaire file */ |
66 |
< |
e_include(), /* include file */ |
67 |
< |
e_sph(), /* sphere */ |
68 |
< |
e_cyl(), /* cylinder */ |
63 |
< |
e_cone(), /* cone */ |
64 |
< |
e_ring(), /* ring */ |
65 |
< |
e_torus(); /* torus */ |
66 |
> |
e_cct(), /* color temperature */ |
67 |
> |
e_cmix(), /* color mixtures */ |
68 |
> |
e_cspec(); /* color spectra */ |
69 |
|
|
70 |
|
/* alternate handler support functions */ |
71 |
|
|
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; |
91 |
> |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
92 |
|
} else |
93 |
< |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
93 |
> |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<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; |
96 |
> |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
97 |
|
} else |
98 |
< |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
98 |
> |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<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; |
101 |
> |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
102 |
|
} else |
103 |
< |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
103 |
> |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<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; |
106 |
> |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX; |
107 |
|
} else |
108 |
< |
uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF; |
108 |
> |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
109 |
> |
if (mg_ehand[MG_E_PRISM] == NULL) { |
110 |
> |
mg_ehand[MG_E_PRISM] = e_prism; |
111 |
> |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX; |
112 |
> |
} else |
113 |
> |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<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; |
116 |
> |
ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX; |
117 |
|
} else |
118 |
< |
uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF; |
118 |
> |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
119 |
> |
if (mg_ehand[MG_E_FACE] == NULL) |
120 |
> |
mg_ehand[MG_E_FACE] = mg_ehand[MG_E_FACEH]; |
121 |
> |
else if (mg_ehand[MG_E_FACEH] == NULL) |
122 |
> |
mg_ehand[MG_E_FACEH] = e_faceh; |
123 |
> |
if (mg_ehand[MG_E_COLOR] != NULL) { |
124 |
> |
if (mg_ehand[MG_E_CMIX] == NULL) { |
125 |
> |
mg_ehand[MG_E_CMIX] = e_cmix; |
126 |
> |
ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT; |
127 |
> |
} |
128 |
> |
if (mg_ehand[MG_E_CSPEC] == NULL) { |
129 |
> |
mg_ehand[MG_E_CSPEC] = e_cspec; |
130 |
> |
ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT; |
131 |
> |
} |
132 |
> |
if (mg_ehand[MG_E_CCT] == NULL) { |
133 |
> |
mg_ehand[MG_E_CCT] = e_cct; |
134 |
> |
ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT; |
135 |
> |
} |
136 |
> |
} |
137 |
|
/* check for consistency */ |
138 |
|
if (mg_ehand[MG_E_FACE] != NULL) |
139 |
< |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
140 |
< |
if (mg_ehand[MG_E_CXY] != NULL) |
141 |
< |
uneed |= 1<<MG_E_COLOR; |
139 |
> |
uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
140 |
> |
if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL || |
141 |
> |
mg_ehand[MG_E_CMIX] != NULL) |
142 |
> |
uneed |= 1L<<MG_E_COLOR; |
143 |
|
if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL || |
144 |
+ |
mg_ehand[MG_E_IR] != NULL || |
145 |
|
mg_ehand[MG_E_ED] != NULL || |
146 |
|
mg_ehand[MG_E_RS] != NULL || |
147 |
< |
mg_ehand[MG_E_TS] != NULL) |
148 |
< |
uneed |= 1<<MG_E_MATERIAL; |
147 |
> |
mg_ehand[MG_E_TS] != NULL || |
148 |
> |
mg_ehand[MG_E_SIDES] != NULL) |
149 |
> |
uneed |= 1L<<MG_E_MATERIAL; |
150 |
|
for (i = 0; i < MG_NENTITIES; i++) |
151 |
< |
if (uneed & 1<<i && mg_ehand[i] == NULL) { |
151 |
> |
if (uneed & 1L<<i && mg_ehand[i] == NULL) { |
152 |
|
fprintf(stderr, "Missing support for \"%s\" entity\n", |
153 |
|
mg_ename[i]); |
154 |
|
exit(1); |
155 |
|
} |
156 |
|
/* add support as needed */ |
157 |
< |
if (ineed & 1<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex) |
157 |
> |
if (ineed & 1L<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex) |
158 |
|
e_supp[MG_E_VERTEX] = c_hvertex; |
159 |
< |
if (ineed & 1<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex) |
159 |
> |
if (ineed & 1L<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex) |
160 |
|
e_supp[MG_E_POINT] = c_hvertex; |
161 |
< |
if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex) |
161 |
> |
if (ineed & 1L<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex) |
162 |
|
e_supp[MG_E_NORMAL] = c_hvertex; |
163 |
+ |
if (ineed & 1L<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor) |
164 |
+ |
e_supp[MG_E_COLOR] = c_hcolor; |
165 |
+ |
if (ineed & 1L<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor) |
166 |
+ |
e_supp[MG_E_CXY] = c_hcolor; |
167 |
+ |
if (ineed & 1L<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor) |
168 |
+ |
e_supp[MG_E_CSPEC] = c_hcolor; |
169 |
+ |
if (ineed & 1L<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor) |
170 |
+ |
e_supp[MG_E_CMIX] = c_hcolor; |
171 |
+ |
if (ineed & 1L<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor) |
172 |
+ |
e_supp[MG_E_CCT] = c_hcolor; |
173 |
|
/* discard remaining entities */ |
174 |
|
for (i = 0; i < MG_NENTITIES; i++) |
175 |
|
if (mg_ehand[i] == NULL) |
177 |
|
} |
178 |
|
|
179 |
|
|
141 |
– |
|
180 |
|
int |
181 |
|
mg_entity(name) /* get entity number from its name */ |
182 |
|
char *name; |
183 |
|
{ |
184 |
< |
static LUTAB ent_tab; /* entity lookup table */ |
184 |
> |
static LUTAB ent_tab = LU_SINIT(NULL,NULL); /* lookup table */ |
185 |
|
register char *cp; |
186 |
|
|
187 |
|
if (!ent_tab.tsiz) { /* initialize hash table */ |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
< |
static int |
202 |
< |
handle_it(en, ac, av) /* pass entity to appropriate handler */ |
201 |
> |
int |
202 |
> |
mg_handle(en, ac, av) /* pass entity to appropriate handler */ |
203 |
|
register int en; |
204 |
|
int ac; |
205 |
|
char **av; |
206 |
|
{ |
207 |
|
int rv; |
208 |
|
|
209 |
< |
if (en < 0 && (en = mg_entity(av[0])) < 0) |
209 |
> |
if (en < 0 && (en = mg_entity(av[0])) < 0) { /* unknown entity */ |
210 |
> |
if (mg_uhand != NULL) |
211 |
> |
return((*mg_uhand)(ac, av)); |
212 |
|
return(MG_EUNK); |
213 |
< |
if (e_supp[en] != NULL) { |
213 |
> |
} |
214 |
> |
if (e_supp[en] != NULL) { /* support handler */ |
215 |
|
if ((rv = (*e_supp[en])(ac, av)) != MG_OK) |
216 |
|
return(rv); |
217 |
|
} |
218 |
< |
return((*mg_ehand[en])(ac, av)); |
218 |
> |
return((*mg_ehand[en])(ac, av)); /* assigned handler */ |
219 |
|
} |
220 |
|
|
221 |
|
|
224 |
|
register MG_FCTXT *ctx; |
225 |
|
char *fn; |
226 |
|
{ |
227 |
< |
int olen; |
227 |
> |
static int nfids; |
228 |
|
register char *cp; |
229 |
|
|
230 |
+ |
ctx->fid = ++nfids; |
231 |
|
ctx->lineno = 0; |
232 |
|
if (fn == NULL) { |
233 |
< |
ctx->fname = "<stdin>"; |
233 |
> |
strcpy(ctx->fname, "<stdin>"); |
234 |
|
ctx->fp = stdin; |
235 |
|
ctx->prev = mg_file; |
236 |
|
mg_file = ctx; |
237 |
|
return(MG_OK); |
238 |
|
} |
239 |
|
/* get name relative to this context */ |
240 |
< |
if (mg_file != NULL && |
199 |
< |
(cp = strrchr(mg_file->fname, '/')) != NULL) |
200 |
< |
olen = cp - mg_file->fname + 1; |
201 |
< |
else |
202 |
< |
olen = 0; |
203 |
< |
ctx->fname = (char *)malloc(olen+strlen(fn)+1); |
204 |
< |
if (ctx->fname == NULL) |
205 |
< |
return(MG_EMEM); |
206 |
< |
if (olen) |
240 |
> |
if (mg_file != NULL && (cp = strrchr(mg_file->fname, '/')) != NULL) { |
241 |
|
strcpy(ctx->fname, mg_file->fname); |
242 |
< |
strcpy(ctx->fname+olen, fn); |
242 |
> |
strcpy(ctx->fname+(cp-mg_file->fname+1), fn); |
243 |
> |
} else |
244 |
> |
strcpy(ctx->fname, fn); |
245 |
|
ctx->fp = fopen(ctx->fname, "r"); |
246 |
< |
if (ctx->fp == NULL) { |
211 |
< |
free((MEM_PTR)ctx->fname); |
246 |
> |
if (ctx->fp == NULL) |
247 |
|
return(MG_ENOFILE); |
213 |
– |
} |
248 |
|
ctx->prev = mg_file; /* establish new context */ |
249 |
|
mg_file = ctx; |
250 |
|
return(MG_OK); |
257 |
|
register MG_FCTXT *ctx = mg_file; |
258 |
|
|
259 |
|
mg_file = ctx->prev; /* restore enclosing context */ |
260 |
< |
if (ctx->fp == stdin) |
261 |
< |
return; /* don't close standard input */ |
228 |
< |
fclose(ctx->fp); |
229 |
< |
free((MEM_PTR)ctx->fname); |
260 |
> |
if (ctx->fp != stdin) /* close file if it's a file */ |
261 |
> |
fclose(ctx->fp); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
+ |
void |
266 |
+ |
mg_fgetpos(pos) /* get current position in input file */ |
267 |
+ |
register MG_FPOS *pos; |
268 |
+ |
{ |
269 |
+ |
extern long ftell(); |
270 |
+ |
|
271 |
+ |
pos->fid = mg_file->fid; |
272 |
+ |
pos->lineno = mg_file->lineno; |
273 |
+ |
pos->offset = ftell(mg_file->fp); |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
|
277 |
|
int |
278 |
< |
mg_rewind() /* rewind input file */ |
278 |
> |
mg_fgoto(pos) /* reposition input file pointer */ |
279 |
> |
register MG_FPOS *pos; |
280 |
|
{ |
281 |
< |
if (mg_file->lineno == 0) |
281 |
> |
if (pos->fid != mg_file->fid) |
282 |
> |
return(MG_ESEEK); |
283 |
> |
if (pos->lineno == mg_file->lineno) |
284 |
|
return(MG_OK); |
285 |
|
if (mg_file->fp == stdin) |
286 |
|
return(MG_ESEEK); /* cannot seek on standard input */ |
287 |
< |
if (fseek(mg_file->fp, 0L, 0) == EOF) |
287 |
> |
if (fseek(mg_file->fp, pos->offset, 0) == EOF) |
288 |
|
return(MG_ESEEK); |
289 |
< |
mg_file->lineno = 0; |
289 |
> |
mg_file->lineno = pos->lineno; |
290 |
|
return(MG_OK); |
291 |
|
} |
292 |
|
|
300 |
|
if (fgets(mg_file->inpline+len, |
301 |
|
MG_MAXLINE-len, mg_file->fp) == NULL) |
302 |
|
return(len); |
256 |
– |
mg_file->lineno++; |
303 |
|
len += strlen(mg_file->inpline+len); |
304 |
< |
if (len > 1 && mg_file->inpline[len-2] == '\\') |
305 |
< |
mg_file->inpline[--len-1] = ' '; |
306 |
< |
} while (mg_file->inpline[len]); |
304 |
> |
if (len >= MG_MAXLINE-1) |
305 |
> |
return(len); |
306 |
> |
mg_file->lineno++; |
307 |
> |
} while (len > 1 && mg_file->inpline[len-2] == '\\'); |
308 |
|
|
309 |
|
return(len); |
310 |
|
} |
315 |
|
{ |
316 |
|
char abuf[MG_MAXLINE]; |
317 |
|
char *argv[MG_MAXARGC]; |
318 |
< |
int en; |
319 |
< |
register char *cp, **ap; |
320 |
< |
|
321 |
< |
strcpy(cp=abuf, mg_file->inpline); |
322 |
< |
ap = argv; /* break into words */ |
318 |
> |
register char *cp, *cp2, **ap; |
319 |
> |
/* copy line, removing escape chars */ |
320 |
> |
cp = abuf; cp2 = mg_file->inpline; |
321 |
> |
while ((*cp++ = *cp2++)) |
322 |
> |
if (cp2[0] == '\n' && cp2[-1] == '\\') |
323 |
> |
cp--; |
324 |
> |
cp = abuf; ap = argv; /* break into words */ |
325 |
|
for ( ; ; ) { |
326 |
|
while (isspace(*cp)) |
327 |
|
*cp++ = '\0'; |
337 |
|
return(MG_OK); /* no words in line */ |
338 |
|
*ap = NULL; |
339 |
|
/* else handle it */ |
340 |
< |
return(handle_it(-1, ap-argv, argv)); |
340 |
> |
return(mg_handle(-1, ap-argv, argv)); |
341 |
|
} |
342 |
|
|
343 |
|
|
347 |
|
{ |
348 |
|
MG_FCTXT cntxt; |
349 |
|
int rval; |
350 |
+ |
register int nbr; |
351 |
|
|
352 |
|
if ((rval = mg_open(&cntxt, fn)) != MG_OK) { |
353 |
|
fprintf(stderr, "%s: %s\n", fn, mg_err[rval]); |
354 |
|
return(rval); |
355 |
|
} |
356 |
< |
while (mg_read()) /* parse each line */ |
356 |
> |
while ((nbr = mg_read()) > 0) { /* parse each line */ |
357 |
> |
if (nbr >= MG_MAXLINE-1) { |
358 |
> |
fprintf(stderr, "%s: %d: %s\n", cntxt.fname, |
359 |
> |
cntxt.lineno, mg_err[rval=MG_ELINE]); |
360 |
> |
break; |
361 |
> |
} |
362 |
|
if ((rval = mg_parse()) != MG_OK) { |
363 |
|
fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname, |
364 |
|
cntxt.lineno, mg_err[rval], |
365 |
|
cntxt.inpline); |
366 |
|
break; |
367 |
|
} |
368 |
+ |
} |
369 |
|
mg_close(); |
370 |
|
return(rval); |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
< |
void |
375 |
< |
mg_clear() /* clear parser history */ |
374 |
> |
int |
375 |
> |
mg_defuhand(ac, av) /* default handler for unknown entities */ |
376 |
> |
int ac; |
377 |
> |
char **av; |
378 |
|
{ |
379 |
< |
c_clearall(); /* clear context tables */ |
380 |
< |
mg_file = NULL; /* reset our context */ |
379 |
> |
if (mg_nunknown++ == 0) /* report first incident */ |
380 |
> |
fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname, |
381 |
> |
mg_file->lineno, mg_err[MG_EUNK], av[0]); |
382 |
> |
return(MG_OK); |
383 |
|
} |
384 |
|
|
385 |
|
|
386 |
< |
int |
387 |
< |
mg_iterate(ac, av, f) /* iterate on statement */ |
328 |
< |
int ac; |
329 |
< |
register char **av; |
330 |
< |
int (*f)(); |
386 |
> |
void |
387 |
> |
mg_clear() /* clear parser history */ |
388 |
|
{ |
389 |
< |
int niter, rval; |
390 |
< |
register int i, j; |
391 |
< |
char *argv[MG_MAXARGC]; |
335 |
< |
char cntbuf[10]; |
336 |
< |
/* build partial transformation */ |
337 |
< |
for (i = 0; i < ac; i++) { |
338 |
< |
if (av[i][0] == '-' && av[i][1] == 'a' && av[i][2] == '\0') |
339 |
< |
break; |
340 |
< |
argv[i+1] = av[i]; |
341 |
< |
} |
342 |
< |
argv[i+1] = NULL; |
343 |
< |
if (i) { /* handle transformation */ |
344 |
< |
argv[0] = mg_ename[MG_E_XF]; |
345 |
< |
if ((rval = handle_it(MG_E_XF, i+1, argv)) != MG_OK) |
346 |
< |
return(rval); |
347 |
< |
} |
348 |
< |
if (i < ac) { /* run array */ |
349 |
< |
if (i+1 >= ac || !isint(av[i+1])) |
350 |
< |
return(MG_ETYPE); |
351 |
< |
niter = atoi(av[i+1]); |
352 |
< |
argv[0] = mg_ename[MG_E_OBJECT]; |
353 |
< |
argv[1] = cntbuf; |
354 |
< |
for (j = 2; j+i < ac; j++) |
355 |
< |
argv[j] = av[j+i]; |
356 |
< |
argv[j] = NULL; |
357 |
< |
for (j = 0; j < niter; j++) { |
358 |
< |
sprintf(cntbuf, "%d", j); |
359 |
< |
if ((rval = handle_it(MG_E_OBJECT, 2, argv)) != MG_OK) |
360 |
< |
return(rval); |
361 |
< |
argv[0] = "-i"; |
362 |
< |
if ((rval = mg_iterate(ac-i, argv, f)) != MG_OK) |
363 |
< |
return(rval); |
364 |
< |
argv[0] = mg_ename[MG_E_OBJECT]; |
365 |
< |
if ((rval = handle_it(MG_E_OBJECT, 1, argv)) != MG_OK) |
366 |
< |
return(rval); |
367 |
< |
} |
368 |
< |
} else if ((rval = (*f)()) != MG_OK) /* else do this instance */ |
369 |
< |
return(rval); |
370 |
< |
if (i) { /* reset the transform */ |
371 |
< |
argv[0] = mg_ename[MG_E_XF]; |
372 |
< |
argv[1] = NULL; |
373 |
< |
(void)handle_it(MG_E_XF, 1, argv); |
374 |
< |
} |
375 |
< |
return(MG_OK); |
389 |
> |
c_clearall(); /* clear context tables */ |
390 |
> |
while (mg_file != NULL) /* reset our file context */ |
391 |
> |
mg_close(); |
392 |
|
} |
393 |
|
|
394 |
|
|
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
< |
static int |
394 |
< |
reload_file() /* reload current MGF file */ |
395 |
< |
{ |
396 |
< |
register int rval; |
397 |
< |
|
398 |
< |
if ((rval = mg_rewind()) != MG_OK) |
399 |
< |
return(rval); |
400 |
< |
while (mg_read()) |
401 |
< |
if ((rval = mg_parse()) != MG_OK) |
402 |
< |
return(rval); |
403 |
< |
return(MG_OK); |
404 |
< |
} |
405 |
< |
|
406 |
< |
|
407 |
< |
static int |
409 |
> |
int |
410 |
|
e_include(ac, av) /* include file */ |
411 |
|
int ac; |
412 |
|
char **av; |
413 |
|
{ |
414 |
+ |
char *xfarg[MG_MAXARGC]; |
415 |
|
MG_FCTXT ictx; |
416 |
< |
int rv; |
416 |
> |
XF_SPEC *xf_orig = xf_context; |
417 |
> |
register int rv; |
418 |
|
|
419 |
|
if (ac < 2) |
420 |
|
return(MG_EARGC); |
421 |
|
if ((rv = mg_open(&ictx, av[1])) != MG_OK) |
422 |
|
return(rv); |
423 |
< |
if ((rv = mg_iterate(ac-2, av+2, reload_file)) != MG_OK) { |
424 |
< |
fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, |
425 |
< |
ictx.lineno, mg_err[rv], ictx.inpline); |
426 |
< |
mg_close(); |
427 |
< |
return(MG_EINCL); |
423 |
> |
if (ac > 2) { |
424 |
> |
register int i; |
425 |
> |
|
426 |
> |
xfarg[0] = mg_ename[MG_E_XF]; |
427 |
> |
for (i = 1; i < ac-1; i++) |
428 |
> |
xfarg[i] = av[i+1]; |
429 |
> |
xfarg[ac-1] = NULL; |
430 |
> |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) { |
431 |
> |
mg_close(); |
432 |
> |
return(rv); |
433 |
> |
} |
434 |
|
} |
435 |
+ |
do { |
436 |
+ |
while ((rv = mg_read()) > 0) { |
437 |
+ |
if (rv >= MG_MAXLINE-1) { |
438 |
+ |
fprintf(stderr, "%s: %d: %s\n", ictx.fname, |
439 |
+ |
ictx.lineno, mg_err[MG_ELINE]); |
440 |
+ |
mg_close(); |
441 |
+ |
return(MG_EINCL); |
442 |
+ |
} |
443 |
+ |
if ((rv = mg_parse()) != MG_OK) { |
444 |
+ |
fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, |
445 |
+ |
ictx.lineno, mg_err[rv], |
446 |
+ |
ictx.inpline); |
447 |
+ |
mg_close(); |
448 |
+ |
return(MG_EINCL); |
449 |
+ |
} |
450 |
+ |
} |
451 |
+ |
if (ac > 2) |
452 |
+ |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) { |
453 |
+ |
mg_close(); |
454 |
+ |
return(rv); |
455 |
+ |
} |
456 |
+ |
} while (xf_context != xf_orig); |
457 |
|
mg_close(); |
458 |
|
return(MG_OK); |
459 |
|
} |
460 |
|
|
461 |
|
|
462 |
+ |
int |
463 |
+ |
e_faceh(ac, av) /* replace face+holes with single contour */ |
464 |
+ |
int ac; |
465 |
+ |
char **av; |
466 |
+ |
{ |
467 |
+ |
char *newav[MG_MAXARGC]; |
468 |
+ |
int lastp = 0; |
469 |
+ |
register int i, j; |
470 |
+ |
|
471 |
+ |
newav[0] = mg_ename[MG_E_FACE]; |
472 |
+ |
for (i = 1; i < ac; i++) |
473 |
+ |
if (av[i][0] == '-') { |
474 |
+ |
if (i < 4) |
475 |
+ |
return(MG_EARGC); |
476 |
+ |
if (i >= ac-1) |
477 |
+ |
break; |
478 |
+ |
if (!lastp) |
479 |
+ |
lastp = i-1; |
480 |
+ |
for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++) |
481 |
+ |
; |
482 |
+ |
if (j - i < 3) |
483 |
+ |
return(MG_EARGC); |
484 |
+ |
newav[i] = av[j]; /* connect hole loop */ |
485 |
+ |
} else |
486 |
+ |
newav[i] = av[i]; /* hole or perimeter vertex */ |
487 |
+ |
if (lastp) |
488 |
+ |
newav[i++] = av[lastp]; /* finish seam to outside */ |
489 |
+ |
newav[i] = NULL; |
490 |
+ |
return(mg_handle(MG_E_FACE, i, newav)); |
491 |
+ |
} |
492 |
+ |
|
493 |
+ |
|
494 |
|
static void |
495 |
|
make_axes(u, v, w) /* compute u and v given w (normalized) */ |
496 |
|
FVECT u, v, w; |
508 |
|
} |
509 |
|
|
510 |
|
|
511 |
< |
static int |
511 |
> |
int |
512 |
|
e_sph(ac, av) /* expand a sphere into cones */ |
513 |
|
int ac; |
514 |
|
char **av; |
533 |
|
rad = atof(av[2]); |
534 |
|
/* initialize */ |
535 |
|
warpconends = 1; |
536 |
< |
if ((rval = handle_it(MG_E_VERTEX, 3, v2ent)) != MG_OK) |
536 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 3, v2ent)) != MG_OK) |
537 |
|
return(rval); |
538 |
|
sprintf(p2x, FLTFMT, cv->p[0]); |
539 |
|
sprintf(p2y, FLTFMT, cv->p[1]); |
540 |
|
sprintf(p2z, FLTFMT, cv->p[2]+rad); |
541 |
< |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
541 |
> |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
542 |
|
return(rval); |
543 |
|
r2[0] = '0'; r2[1] = '\0'; |
544 |
|
for (i = 1; i <= 2*mg_nqcdivs; i++) { |
545 |
|
theta = i*(PI/2)/mg_nqcdivs; |
546 |
< |
if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
546 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
547 |
|
return(rval); |
548 |
|
sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta)); |
549 |
< |
if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
549 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
550 |
|
return(rval); |
551 |
< |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
551 |
> |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
552 |
|
return(rval); |
553 |
|
strcpy(r1, r2); |
554 |
|
sprintf(r2, FLTFMT, rad*sin(theta)); |
555 |
< |
if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK) |
555 |
> |
if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) |
556 |
|
return(rval); |
557 |
|
} |
558 |
|
warpconends = 0; |
560 |
|
} |
561 |
|
|
562 |
|
|
563 |
< |
static int |
563 |
> |
int |
564 |
|
e_torus(ac, av) /* expand a torus into cones */ |
565 |
|
int ac; |
566 |
|
char **av; |
607 |
|
for (j = 0; j < 3; j++) |
608 |
|
sprintf(p2[j], FLTFMT, cv->p[j] + |
609 |
|
.5*sgn*(maxrad-minrad)*cv->n[j]); |
610 |
< |
if ((rval = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
610 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
611 |
|
return(rval); |
612 |
< |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
612 |
> |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
613 |
|
return(rval); |
614 |
|
sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad)); |
615 |
|
/* run outer section */ |
616 |
|
for (i = 1; i <= 2*mg_nqcdivs; i++) { |
617 |
|
theta = i*(PI/2)/mg_nqcdivs; |
618 |
< |
if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
618 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
619 |
|
return(rval); |
620 |
|
for (j = 0; j < 3; j++) |
621 |
|
sprintf(p2[j], FLTFMT, cv->p[j] + |
622 |
|
.5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); |
623 |
< |
if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
623 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
624 |
|
return(rval); |
625 |
< |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
625 |
> |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
626 |
|
return(rval); |
627 |
|
strcpy(r1, r2); |
628 |
|
sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta)); |
629 |
< |
if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK) |
629 |
> |
if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) |
630 |
|
return(rval); |
631 |
|
} |
632 |
|
/* run inner section */ |
636 |
|
for (j = 0; j < 3; j++) |
637 |
|
sprintf(p2[j], FLTFMT, cv->p[j] + |
638 |
|
.5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); |
639 |
< |
if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
639 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
640 |
|
return(rval); |
641 |
< |
if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
641 |
> |
if ((rval = mg_handle(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
642 |
|
return(rval); |
643 |
< |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
643 |
> |
if ((rval = mg_handle(MG_E_POINT, 4, p2ent)) != MG_OK) |
644 |
|
return(rval); |
645 |
|
strcpy(r1, r2); |
646 |
|
sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta)); |
647 |
< |
if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK) |
647 |
> |
if ((rval = mg_handle(MG_E_CONE, 5, conent)) != MG_OK) |
648 |
|
return(rval); |
649 |
|
} |
650 |
|
warpconends = 0; |
652 |
|
} |
653 |
|
|
654 |
|
|
655 |
< |
static int |
655 |
> |
int |
656 |
|
e_cyl(ac, av) /* replace a cylinder with equivalent cone */ |
657 |
|
int ac; |
658 |
|
char **av; |
665 |
|
avnew[2] = av[2]; |
666 |
|
avnew[3] = av[3]; |
667 |
|
avnew[4] = av[2]; |
668 |
< |
return(handle_it(MG_E_CONE, 5, avnew)); |
668 |
> |
return(mg_handle(MG_E_CONE, 5, avnew)); |
669 |
|
} |
670 |
|
|
671 |
|
|
672 |
< |
static int |
672 |
> |
int |
673 |
|
e_ring(ac, av) /* turn a ring into polygons */ |
674 |
|
int ac; |
675 |
|
char **av; |
707 |
|
make_axes(u, v, cv->n); |
708 |
|
for (j = 0; j < 3; j++) |
709 |
|
sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]); |
710 |
< |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
710 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
711 |
|
return(rv); |
712 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
712 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
713 |
|
return(rv); |
714 |
|
if (minrad == 0.) { /* closed */ |
715 |
|
v1ent[3] = av[1]; |
716 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
716 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
717 |
|
return(rv); |
718 |
< |
if ((rv = handle_it(MG_E_NORMAL, 4, nzent)) != MG_OK) |
718 |
> |
if ((rv = mg_handle(MG_E_NORMAL, 4, nzent)) != MG_OK) |
719 |
|
return(rv); |
720 |
|
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
721 |
|
theta = i*(PI/2)/mg_nqcdivs; |
722 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
722 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
723 |
|
return(rv); |
724 |
|
for (j = 0; j < 3; j++) |
725 |
|
sprintf(p3[j], FLTFMT, cv->p[j] + |
726 |
|
maxrad*u[j]*cos(theta) + |
727 |
|
maxrad*v[j]*sin(theta)); |
728 |
< |
if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
728 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
729 |
|
return(rv); |
730 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
730 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
731 |
|
return(rv); |
732 |
< |
if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK) |
732 |
> |
if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK) |
733 |
|
return(rv); |
734 |
|
} |
735 |
|
} else { /* open */ |
736 |
< |
if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
736 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
737 |
|
return(rv); |
738 |
|
for (j = 0; j < 3; j++) |
739 |
|
sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]); |
740 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
740 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
741 |
|
return(rv); |
742 |
|
v1ent[3] = "_rv4"; |
743 |
|
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
744 |
|
theta = i*(PI/2)/mg_nqcdivs; |
745 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
745 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
746 |
|
return(rv); |
747 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
747 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
748 |
|
return(rv); |
749 |
|
for (j = 0; j < 3; j++) { |
750 |
|
d = u[j]*cos(theta) + v[j]*sin(theta); |
751 |
|
sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d); |
752 |
|
sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d); |
753 |
|
} |
754 |
< |
if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
754 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
755 |
|
return(rv); |
756 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
756 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
757 |
|
return(rv); |
758 |
< |
if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK) |
758 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK) |
759 |
|
return(rv); |
760 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
760 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
761 |
|
return(rv); |
762 |
< |
if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK) |
762 |
> |
if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK) |
763 |
|
return(rv); |
764 |
|
} |
765 |
|
} |
767 |
|
} |
768 |
|
|
769 |
|
|
770 |
< |
static int |
770 |
> |
int |
771 |
|
e_cone(ac, av) /* turn a cone into polygons */ |
772 |
|
int ac; |
773 |
|
char **av; |
782 |
|
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
783 |
|
static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]}; |
784 |
|
static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"}; |
785 |
+ |
char *v1n; |
786 |
|
register C_VERTEX *cv1, *cv2; |
787 |
|
register int i, j; |
788 |
|
FVECT u, v, w; |
798 |
|
if ((cv1 = c_getvert(av[1])) == NULL || |
799 |
|
(cv2 = c_getvert(av[3])) == NULL) |
800 |
|
return(MG_EUNDEF); |
801 |
+ |
v1n = av[1]; |
802 |
|
if (!isflt(av[2]) || !isflt(av[4])) |
803 |
|
return(MG_ETYPE); |
804 |
|
rad1 = atof(av[2]); |
809 |
|
if (rad2 == 0.) |
810 |
|
return(MG_EILL); |
811 |
|
} else if (rad2 != 0.) { |
812 |
< |
if (rad1 < 0. ^ rad2 < 0.) |
812 |
> |
if ((rad1 < 0.) ^ (rad2 < 0.)) |
813 |
|
return(MG_EILL); |
814 |
|
} else { /* swap */ |
815 |
|
C_VERTEX *cv; |
817 |
|
cv = cv1; |
818 |
|
cv1 = cv2; |
819 |
|
cv2 = cv; |
820 |
+ |
v1n = av[3]; |
821 |
|
d = rad1; |
822 |
|
rad1 = rad2; |
823 |
|
rad2 = d; |
844 |
|
else |
845 |
|
sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off); |
846 |
|
} |
847 |
< |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
847 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
848 |
|
return(rv); |
849 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
849 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
850 |
|
return(rv); |
851 |
< |
if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
851 |
> |
if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
852 |
|
return(rv); |
853 |
|
if (rad1 == 0.) { /* triangles */ |
854 |
< |
v1ent[3] = av[1]; |
855 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
854 |
> |
v1ent[3] = v1n; |
855 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
856 |
|
return(rv); |
857 |
|
for (j = 0; j < 3; j++) |
858 |
|
sprintf(n4[j], FLTFMT, w[j]); |
859 |
< |
if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
859 |
> |
if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
860 |
|
return(rv); |
861 |
|
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
862 |
|
theta = sgn*i*(PI/2)/mg_nqcdivs; |
863 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
863 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
864 |
|
return(rv); |
865 |
|
for (j = 0; j < 3; j++) { |
866 |
|
d = u[j]*cos(theta) + v[j]*sin(theta); |
868 |
|
if (n2off > -FHUGE) |
869 |
|
sprintf(n3[j], FLTFMT, d + w[j]*n2off); |
870 |
|
} |
871 |
< |
if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
871 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
872 |
|
return(rv); |
873 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
873 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
874 |
|
return(rv); |
875 |
|
if (n2off > -FHUGE && |
876 |
< |
(rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
876 |
> |
(rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
877 |
|
return(rv); |
878 |
< |
if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK) |
878 |
> |
if ((rv = mg_handle(MG_E_FACE, 4, fent)) != MG_OK) |
879 |
|
return(rv); |
880 |
|
} |
881 |
|
} else { /* quads */ |
894 |
|
else |
895 |
|
sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off); |
896 |
|
} |
897 |
< |
if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
897 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
898 |
|
return(rv); |
899 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
899 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
900 |
|
return(rv); |
901 |
< |
if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
901 |
> |
if ((rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
902 |
|
return(rv); |
903 |
|
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
904 |
|
theta = sgn*i*(PI/2)/mg_nqcdivs; |
905 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
905 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
906 |
|
return(rv); |
907 |
< |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
907 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
908 |
|
return(rv); |
909 |
|
for (j = 0; j < 3; j++) { |
910 |
|
d = u[j]*cos(theta) + v[j]*sin(theta); |
915 |
|
if (n1off < FHUGE) |
916 |
|
sprintf(n4[j], FLTFMT, d + w[j]*n1off); |
917 |
|
} |
918 |
< |
if ((rv = handle_it(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
918 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 2, v3ent)) != MG_OK) |
919 |
|
return(rv); |
920 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
920 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p3ent)) != MG_OK) |
921 |
|
return(rv); |
922 |
|
if (n2off > -FHUGE && |
923 |
< |
(rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
923 |
> |
(rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
924 |
|
return(rv); |
925 |
< |
if ((rv = handle_it(MG_E_VERTEX, 2, v4ent)) != MG_OK) |
925 |
> |
if ((rv = mg_handle(MG_E_VERTEX, 2, v4ent)) != MG_OK) |
926 |
|
return(rv); |
927 |
< |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
927 |
> |
if ((rv = mg_handle(MG_E_POINT, 4, p4ent)) != MG_OK) |
928 |
|
return(rv); |
929 |
|
if (n1off < FHUGE && |
930 |
< |
(rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
930 |
> |
(rv = mg_handle(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
931 |
|
return(rv); |
932 |
< |
if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK) |
932 |
> |
if ((rv = mg_handle(MG_E_FACE, 5, fent)) != MG_OK) |
933 |
|
return(rv); |
934 |
|
} |
935 |
|
} |
936 |
+ |
return(MG_OK); |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
|
940 |
+ |
int |
941 |
+ |
e_prism(ac, av) /* turn a prism into polygons */ |
942 |
+ |
int ac; |
943 |
+ |
char **av; |
944 |
+ |
{ |
945 |
+ |
static char p[3][24]; |
946 |
+ |
static char *vent[5] = {mg_ename[MG_E_VERTEX],NULL,"="}; |
947 |
+ |
static char *pent[5] = {mg_ename[MG_E_POINT],p[0],p[1],p[2]}; |
948 |
+ |
static char *znorm[5] = {mg_ename[MG_E_NORMAL],"0","0","0"}; |
949 |
+ |
char *newav[MG_MAXARGC], nvn[MG_MAXARGC-1][8]; |
950 |
+ |
double length; |
951 |
+ |
int hasnorm; |
952 |
+ |
FVECT v1, v2, v3, norm; |
953 |
+ |
register C_VERTEX *cv; |
954 |
+ |
C_VERTEX *cv0; |
955 |
+ |
int rv; |
956 |
+ |
register int i, j; |
957 |
+ |
/* check arguments */ |
958 |
+ |
if (ac < 5) |
959 |
+ |
return(MG_EARGC); |
960 |
+ |
if (!isflt(av[ac-1])) |
961 |
+ |
return(MG_ETYPE); |
962 |
+ |
length = atof(av[ac-1]); |
963 |
+ |
if (length <= FTINY && length >= -FTINY) |
964 |
+ |
return(MG_EILL); |
965 |
+ |
/* compute face normal */ |
966 |
+ |
if ((cv0 = c_getvert(av[1])) == NULL) |
967 |
+ |
return(MG_EUNDEF); |
968 |
+ |
hasnorm = 0; |
969 |
+ |
norm[0] = norm[1] = norm[2] = 0.; |
970 |
+ |
v1[0] = v1[1] = v1[2] = 0.; |
971 |
+ |
for (i = 2; i < ac-1; i++) { |
972 |
+ |
if ((cv = c_getvert(av[i])) == NULL) |
973 |
+ |
return(MG_EUNDEF); |
974 |
+ |
hasnorm += !is0vect(cv->n); |
975 |
+ |
v2[0] = cv->p[0] - cv0->p[0]; |
976 |
+ |
v2[1] = cv->p[1] - cv0->p[1]; |
977 |
+ |
v2[2] = cv->p[2] - cv0->p[2]; |
978 |
+ |
fcross(v3, v1, v2); |
979 |
+ |
norm[0] += v3[0]; |
980 |
+ |
norm[1] += v3[1]; |
981 |
+ |
norm[2] += v3[2]; |
982 |
+ |
VCOPY(v1, v2); |
983 |
+ |
} |
984 |
+ |
if (normalize(norm) == 0.) |
985 |
+ |
return(MG_EILL); |
986 |
+ |
/* create moved vertices */ |
987 |
+ |
for (i = 1; i < ac-1; i++) { |
988 |
+ |
sprintf(nvn[i-1], "_pv%d", i); |
989 |
+ |
vent[1] = nvn[i-1]; |
990 |
+ |
vent[3] = av[i]; |
991 |
+ |
if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK) |
992 |
+ |
return(rv); |
993 |
+ |
cv = c_getvert(av[i]); /* checked above */ |
994 |
+ |
for (j = 0; j < 3; j++) |
995 |
+ |
sprintf(p[j], FLTFMT, cv->p[j] - length*norm[j]); |
996 |
+ |
if ((rv = mg_handle(MG_E_POINT, 4, pent)) != MG_OK) |
997 |
+ |
return(rv); |
998 |
+ |
} |
999 |
+ |
/* make faces */ |
1000 |
+ |
newav[0] = mg_ename[MG_E_FACE]; |
1001 |
+ |
/* do the side faces */ |
1002 |
+ |
newav[5] = NULL; |
1003 |
+ |
newav[3] = av[ac-2]; |
1004 |
+ |
newav[4] = nvn[ac-3]; |
1005 |
+ |
for (i = 1; i < ac-1; i++) { |
1006 |
+ |
newav[1] = nvn[i-1]; |
1007 |
+ |
newav[2] = av[i]; |
1008 |
+ |
if ((rv = mg_handle(MG_E_FACE, 5, newav)) != MG_OK) |
1009 |
+ |
return(rv); |
1010 |
+ |
newav[3] = newav[2]; |
1011 |
+ |
newav[4] = newav[1]; |
1012 |
+ |
} |
1013 |
+ |
/* do top face */ |
1014 |
+ |
for (i = 1; i < ac-1; i++) { |
1015 |
+ |
if (hasnorm) { /* zero normals */ |
1016 |
+ |
vent[1] = nvn[i-1]; |
1017 |
+ |
if ((rv = mg_handle(MG_E_VERTEX, 2, vent)) != MG_OK) |
1018 |
+ |
return(rv); |
1019 |
+ |
if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK) |
1020 |
+ |
return(rv); |
1021 |
+ |
} |
1022 |
+ |
newav[ac-1-i] = nvn[i-1]; /* reverse */ |
1023 |
+ |
} |
1024 |
+ |
if ((rv = mg_handle(MG_E_FACE, ac-1, newav)) != MG_OK) |
1025 |
+ |
return(rv); |
1026 |
+ |
/* do bottom face */ |
1027 |
+ |
if (hasnorm) |
1028 |
+ |
for (i = 1; i < ac-1; i++) { |
1029 |
+ |
vent[1] = nvn[i-1]; |
1030 |
+ |
vent[3] = av[i]; |
1031 |
+ |
if ((rv = mg_handle(MG_E_VERTEX, 4, vent)) != MG_OK) |
1032 |
+ |
return(rv); |
1033 |
+ |
if ((rv = mg_handle(MG_E_NORMAL, 4, znorm)) != MG_OK) |
1034 |
+ |
return(rv); |
1035 |
+ |
newav[i] = nvn[i-1]; |
1036 |
+ |
} |
1037 |
+ |
else |
1038 |
+ |
for (i = 1; i < ac-1; i++) |
1039 |
+ |
newav[i] = av[i]; |
1040 |
+ |
newav[i] = NULL; |
1041 |
+ |
if ((rv = mg_handle(MG_E_FACE, i, newav)) != MG_OK) |
1042 |
+ |
return(rv); |
1043 |
+ |
return(MG_OK); |
1044 |
+ |
} |
1045 |
+ |
|
1046 |
+ |
|
1047 |
+ |
static int |
1048 |
+ |
put_cxy() /* put out current xy chromaticities */ |
1049 |
+ |
{ |
1050 |
+ |
static char xbuf[24], ybuf[24]; |
1051 |
+ |
static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf}; |
1052 |
+ |
|
1053 |
+ |
sprintf(xbuf, "%.4f", c_ccolor->cx); |
1054 |
+ |
sprintf(ybuf, "%.4f", c_ccolor->cy); |
1055 |
+ |
return(mg_handle(MG_E_CXY, 3, ccom)); |
1056 |
+ |
} |
1057 |
+ |
|
1058 |
+ |
|
1059 |
+ |
static int |
1060 |
+ |
put_cspec() /* put out current color spectrum */ |
1061 |
+ |
{ |
1062 |
+ |
char wl[2][6], vbuf[C_CNSS][24]; |
1063 |
+ |
char *newav[C_CNSS+4]; |
1064 |
+ |
double sf; |
1065 |
+ |
register int i; |
1066 |
+ |
|
1067 |
+ |
if (mg_ehand[MG_E_CSPEC] != c_hcolor) { |
1068 |
+ |
sprintf(wl[0], "%d", C_CMINWL); |
1069 |
+ |
sprintf(wl[1], "%d", C_CMAXWL); |
1070 |
+ |
newav[0] = mg_ename[MG_E_CSPEC]; |
1071 |
+ |
newav[1] = wl[0]; |
1072 |
+ |
newav[2] = wl[1]; |
1073 |
+ |
sf = (double)C_CNSS / c_ccolor->ssum; |
1074 |
+ |
for (i = 0; i < C_CNSS; i++) { |
1075 |
+ |
sprintf(vbuf[i], "%.4f", sf*c_ccolor->ssamp[i]); |
1076 |
+ |
newav[i+3] = vbuf[i]; |
1077 |
+ |
} |
1078 |
+ |
newav[C_CNSS+3] = NULL; |
1079 |
+ |
if ((i = mg_handle(MG_E_CSPEC, C_CNSS+3, newav)) != MG_OK) |
1080 |
+ |
return(i); |
1081 |
+ |
} |
1082 |
+ |
return(MG_OK); |
1083 |
+ |
} |
1084 |
+ |
|
1085 |
+ |
|
1086 |
+ |
static int |
1087 |
+ |
e_cspec(ac, av) /* handle spectral color */ |
1088 |
+ |
int ac; |
1089 |
+ |
char **av; |
1090 |
+ |
{ |
1091 |
+ |
/* convert to xy chromaticity */ |
1092 |
+ |
c_ccvt(c_ccolor, C_CSXY); |
1093 |
+ |
/* if it's really their handler, use it */ |
1094 |
+ |
if (mg_ehand[MG_E_CXY] != c_hcolor) |
1095 |
+ |
return(put_cxy()); |
1096 |
+ |
return(MG_OK); |
1097 |
+ |
} |
1098 |
+ |
|
1099 |
+ |
|
1100 |
+ |
static int |
1101 |
+ |
e_cmix(ac, av) /* handle mixing of colors */ |
1102 |
+ |
int ac; |
1103 |
+ |
char **av; |
1104 |
+ |
{ |
1105 |
+ |
/* |
1106 |
+ |
* Contorted logic works as follows: |
1107 |
+ |
* 1. the colors are already mixed in c_hcolor() support function |
1108 |
+ |
* 2. if we would handle a spectral result, make sure it's not |
1109 |
+ |
* 3. if c_hcolor() would handle a spectral result, don't bother |
1110 |
+ |
* 4. otherwise, make cspec entity and pass it to their handler |
1111 |
+ |
* 5. if we have only xy results, handle it as c_spec() would |
1112 |
+ |
*/ |
1113 |
+ |
if (mg_ehand[MG_E_CSPEC] == e_cspec) |
1114 |
+ |
c_ccvt(c_ccolor, C_CSXY); |
1115 |
+ |
else if (c_ccolor->flags & C_CDSPEC) |
1116 |
+ |
return(put_cspec()); |
1117 |
+ |
if (mg_ehand[MG_E_CXY] != c_hcolor) |
1118 |
+ |
return(put_cxy()); |
1119 |
+ |
return(MG_OK); |
1120 |
+ |
} |
1121 |
+ |
|
1122 |
+ |
|
1123 |
+ |
static int |
1124 |
+ |
e_cct(ac, av) /* handle color temperature */ |
1125 |
+ |
int ac; |
1126 |
+ |
char **av; |
1127 |
+ |
{ |
1128 |
+ |
/* |
1129 |
+ |
* Logic is similar to e_cmix here. Support handler has already |
1130 |
+ |
* converted temperature to spectral color. Put it out as such |
1131 |
+ |
* if they support it, otherwise convert to xy chromaticity and |
1132 |
+ |
* put it out if they handle it. |
1133 |
+ |
*/ |
1134 |
+ |
if (mg_ehand[MG_E_CSPEC] != e_cspec) |
1135 |
+ |
return(put_cspec()); |
1136 |
+ |
c_ccvt(c_ccolor, C_CSXY); |
1137 |
+ |
if (mg_ehand[MG_E_CXY] != c_hcolor) |
1138 |
+ |
return(put_cxy()); |
1139 |
|
return(MG_OK); |
1140 |
|
} |