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_cyl(), /* cylinder */ |
63 |
e_cone(), /* cone */ |
64 |
e_ring(), /* ring */ |
65 |
e_torus(); /* torus */ |
66 |
|
67 |
/* alternate handler support functions */ |
68 |
|
69 |
static int (*e_supp[MG_NENTITIES])(); |
70 |
|
71 |
static char FLTFMT[] = "%.12g"; |
72 |
|
73 |
static int warpconends; /* hack for generating good normals */ |
74 |
|
75 |
|
76 |
void |
77 |
mg_init() /* initialize alternate entity handlers */ |
78 |
{ |
79 |
unsigned long ineed = 0, uneed = 0; |
80 |
register int i; |
81 |
/* pick up slack */ |
82 |
if (mg_ehand[MG_E_IES] == NULL) |
83 |
mg_ehand[MG_E_IES] = e_ies; |
84 |
if (mg_ehand[MG_E_INCLUDE] == NULL) |
85 |
mg_ehand[MG_E_INCLUDE] = e_include; |
86 |
if (mg_ehand[MG_E_SPH] == NULL) { |
87 |
mg_ehand[MG_E_SPH] = e_sph; |
88 |
ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX; |
89 |
} else |
90 |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
91 |
if (mg_ehand[MG_E_CYL] == NULL) { |
92 |
mg_ehand[MG_E_CYL] = e_cyl; |
93 |
ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX; |
94 |
} else |
95 |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
96 |
if (mg_ehand[MG_E_CONE] == NULL) { |
97 |
mg_ehand[MG_E_CONE] = e_cone; |
98 |
ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX; |
99 |
} else |
100 |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
101 |
if (mg_ehand[MG_E_RING] == NULL) { |
102 |
mg_ehand[MG_E_RING] = e_ring; |
103 |
ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX; |
104 |
} else |
105 |
uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF; |
106 |
if (mg_ehand[MG_E_TORUS] == NULL) { |
107 |
mg_ehand[MG_E_TORUS] = e_torus; |
108 |
ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX; |
109 |
} else |
110 |
uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF; |
111 |
/* check for consistency */ |
112 |
if (mg_ehand[MG_E_FACE] != NULL) |
113 |
uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF; |
114 |
if (mg_ehand[MG_E_CXY] != NULL) |
115 |
uneed |= 1<<MG_E_COLOR; |
116 |
if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL || |
117 |
mg_ehand[MG_E_ED] != NULL || |
118 |
mg_ehand[MG_E_RS] != NULL || |
119 |
mg_ehand[MG_E_TS] != NULL) |
120 |
uneed |= 1<<MG_E_MATERIAL; |
121 |
for (i = 0; i < MG_NENTITIES; i++) |
122 |
if (uneed & 1<<i && mg_ehand[i] == NULL) { |
123 |
fprintf(stderr, "Missing support for \"%s\" entity\n", |
124 |
mg_ename[i]); |
125 |
exit(1); |
126 |
} |
127 |
/* add support as needed */ |
128 |
if (ineed & 1<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex) |
129 |
e_supp[MG_E_VERTEX] = c_hvertex; |
130 |
if (ineed & 1<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex) |
131 |
e_supp[MG_E_POINT] = c_hvertex; |
132 |
if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex) |
133 |
e_supp[MG_E_NORMAL] = c_hvertex; |
134 |
/* discard remaining entities */ |
135 |
for (i = 0; i < MG_NENTITIES; i++) |
136 |
if (mg_ehand[i] == NULL) |
137 |
mg_ehand[i] = e_any_toss; |
138 |
} |
139 |
|
140 |
|
141 |
|
142 |
int |
143 |
mg_entity(name) /* get entity number from its name */ |
144 |
char *name; |
145 |
{ |
146 |
static LUTAB ent_tab; /* entity lookup table */ |
147 |
register char *cp; |
148 |
|
149 |
if (!ent_tab.tsiz) { /* initialize hash table */ |
150 |
if (!lu_init(&ent_tab, MG_NENTITIES)) |
151 |
return(-1); /* what to do? */ |
152 |
for (cp = mg_ename[MG_NENTITIES-1]; cp >= mg_ename[0]; |
153 |
cp -= sizeof(mg_ename[0])) |
154 |
lu_find(&ent_tab, cp)->key = cp; |
155 |
} |
156 |
cp = lu_find(&ent_tab, name)->key; |
157 |
if (cp == NULL) |
158 |
return(-1); |
159 |
return((cp - mg_ename[0])/sizeof(mg_ename[0])); |
160 |
} |
161 |
|
162 |
|
163 |
static int |
164 |
handle_it(en, ac, av) /* pass entity to appropriate handler */ |
165 |
register int en; |
166 |
int ac; |
167 |
char **av; |
168 |
{ |
169 |
int rv; |
170 |
|
171 |
if (en < 0 && (en = mg_entity(av[0])) < 0) |
172 |
return(MG_EUNK); |
173 |
if (e_supp[en] != NULL) { |
174 |
if ((rv = (*e_supp[en])(ac, av)) != MG_OK) |
175 |
return(rv); |
176 |
} |
177 |
return((*mg_ehand[en])(ac, av)); |
178 |
} |
179 |
|
180 |
|
181 |
int |
182 |
mg_open(ctx, fn) /* open new input file */ |
183 |
register MG_FCTXT *ctx; |
184 |
char *fn; |
185 |
{ |
186 |
int olen; |
187 |
register char *cp; |
188 |
|
189 |
ctx->lineno = 0; |
190 |
if (fn == NULL) { |
191 |
ctx->fname = "<stdin>"; |
192 |
ctx->fp = stdin; |
193 |
ctx->prev = mg_file; |
194 |
mg_file = ctx; |
195 |
return(MG_OK); |
196 |
} |
197 |
/* get name relative to this context */ |
198 |
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) |
207 |
strcpy(ctx->fname, mg_file->fname); |
208 |
strcpy(ctx->fname+olen, fn); |
209 |
ctx->fp = fopen(ctx->fname, "r"); |
210 |
if (ctx->fp == NULL) { |
211 |
free((MEM_PTR)ctx->fname); |
212 |
return(MG_ENOFILE); |
213 |
} |
214 |
ctx->prev = mg_file; /* establish new context */ |
215 |
mg_file = ctx; |
216 |
return(MG_OK); |
217 |
} |
218 |
|
219 |
|
220 |
void |
221 |
mg_close() /* close input file */ |
222 |
{ |
223 |
register MG_FCTXT *ctx = mg_file; |
224 |
|
225 |
mg_file = ctx->prev; /* restore enclosing context */ |
226 |
if (ctx->fp == stdin) |
227 |
return; /* don't close standard input */ |
228 |
fclose(ctx->fp); |
229 |
free((MEM_PTR)ctx->fname); |
230 |
} |
231 |
|
232 |
|
233 |
int |
234 |
mg_rewind() /* rewind input file */ |
235 |
{ |
236 |
if (mg_file->lineno == 0) |
237 |
return(MG_OK); |
238 |
if (mg_file->fp == stdin) |
239 |
return(MG_ESEEK); /* cannot seek on standard input */ |
240 |
if (fseek(mg_file->fp, 0L, 0) == EOF) |
241 |
return(MG_ESEEK); |
242 |
mg_file->lineno = 0; |
243 |
return(MG_OK); |
244 |
} |
245 |
|
246 |
|
247 |
int |
248 |
mg_read() /* read next line from file */ |
249 |
{ |
250 |
register int len = 0; |
251 |
|
252 |
do { |
253 |
if (fgets(mg_file->inpline+len, |
254 |
MG_MAXLINE-len, mg_file->fp) == NULL) |
255 |
return(len); |
256 |
mg_file->lineno++; |
257 |
len += strlen(mg_file->inpline+len); |
258 |
if (len > 1 && mg_file->inpline[len-2] == '\\') |
259 |
mg_file->inpline[--len-1] = ' '; |
260 |
} while (mg_file->inpline[len]); |
261 |
|
262 |
return(len); |
263 |
} |
264 |
|
265 |
|
266 |
int |
267 |
mg_parse() /* parse current input line */ |
268 |
{ |
269 |
char abuf[MG_MAXLINE]; |
270 |
char *argv[MG_MAXARGC]; |
271 |
int en; |
272 |
register char *cp, **ap; |
273 |
|
274 |
strcpy(cp=abuf, mg_file->inpline); |
275 |
ap = argv; /* break into words */ |
276 |
for ( ; ; ) { |
277 |
while (isspace(*cp)) |
278 |
*cp++ = '\0'; |
279 |
if (!*cp) |
280 |
break; |
281 |
if (ap - argv >= MG_MAXARGC-1) |
282 |
return(MG_EARGC); |
283 |
*ap++ = cp; |
284 |
while (*++cp && !isspace(*cp)) |
285 |
; |
286 |
} |
287 |
if (ap == argv) |
288 |
return(MG_OK); /* no words in line */ |
289 |
*ap = NULL; |
290 |
/* else handle it */ |
291 |
return(handle_it(-1, ap-argv, argv)); |
292 |
} |
293 |
|
294 |
|
295 |
int |
296 |
mg_load(fn) /* load an MGF file */ |
297 |
char *fn; |
298 |
{ |
299 |
MG_FCTXT cntxt; |
300 |
int rval; |
301 |
|
302 |
if ((rval = mg_open(&cntxt, fn)) != MG_OK) { |
303 |
fprintf(stderr, "%s: %s\n", fn, mg_err[rval]); |
304 |
return(rval); |
305 |
} |
306 |
while (mg_read()) /* parse each line */ |
307 |
if ((rval = mg_parse()) != MG_OK) { |
308 |
fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname, |
309 |
cntxt.lineno, mg_err[rval], |
310 |
cntxt.inpline); |
311 |
break; |
312 |
} |
313 |
mg_close(); |
314 |
return(rval); |
315 |
} |
316 |
|
317 |
|
318 |
void |
319 |
mg_clear() /* clear parser history */ |
320 |
{ |
321 |
c_clearall(); /* clear context tables */ |
322 |
mg_file = NULL; /* reset our context */ |
323 |
} |
324 |
|
325 |
|
326 |
int |
327 |
mg_iterate(ac, av, f) /* iterate on statement */ |
328 |
int ac; |
329 |
register char **av; |
330 |
int (*f)(); |
331 |
{ |
332 |
int niter, rval; |
333 |
register int i, j; |
334 |
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); |
376 |
} |
377 |
|
378 |
|
379 |
/**************************************************************************** |
380 |
* The following routines handle unsupported entities |
381 |
*/ |
382 |
|
383 |
|
384 |
static int |
385 |
e_any_toss(ac, av) /* discard an unwanted entity */ |
386 |
int ac; |
387 |
char **av; |
388 |
{ |
389 |
return(MG_OK); |
390 |
} |
391 |
|
392 |
|
393 |
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 |
408 |
e_include(ac, av) /* include file */ |
409 |
int ac; |
410 |
char **av; |
411 |
{ |
412 |
MG_FCTXT ictx; |
413 |
int rv; |
414 |
|
415 |
if (ac < 2) |
416 |
return(MG_EARGC); |
417 |
if ((rv = mg_open(&ictx, av[1])) != MG_OK) |
418 |
return(rv); |
419 |
if ((rv = mg_iterate(ac-2, av+2, reload_file)) != MG_OK) { |
420 |
fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, |
421 |
ictx.lineno, mg_err[rv], ictx.inpline); |
422 |
mg_close(); |
423 |
return(MG_EINCL); |
424 |
} |
425 |
mg_close(); |
426 |
return(MG_OK); |
427 |
} |
428 |
|
429 |
|
430 |
static void |
431 |
make_axes(u, v, w) /* compute u and v given w (normalized) */ |
432 |
FVECT u, v, w; |
433 |
{ |
434 |
register int i; |
435 |
|
436 |
v[0] = v[1] = v[2] = 0.; |
437 |
for (i = 0; i < 3; i++) |
438 |
if (w[i] < .6 && w[i] > -.6) |
439 |
break; |
440 |
v[i] = 1.; |
441 |
fcross(u, v, w); |
442 |
normalize(u); |
443 |
fcross(v, w, u); |
444 |
} |
445 |
|
446 |
|
447 |
static int |
448 |
e_sph(ac, av) /* expand a sphere into cones */ |
449 |
int ac; |
450 |
char **av; |
451 |
{ |
452 |
static char p2x[24], p2y[24], p2z[24], r1[24], r2[24]; |
453 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_sv1","=","_sv2"}; |
454 |
static char *v2ent[4] = {mg_ename[MG_E_VERTEX],"_sv2","="}; |
455 |
static char *p2ent[5] = {mg_ename[MG_E_POINT],p2x,p2y,p2z}; |
456 |
static char *conent[6] = {mg_ename[MG_E_CONE],"_sv1",r1,"_sv2",r2}; |
457 |
register C_VERTEX *cv; |
458 |
register int i; |
459 |
int rval; |
460 |
double rad; |
461 |
double theta; |
462 |
|
463 |
if (ac != 3) |
464 |
return(MG_EARGC); |
465 |
if ((cv = c_getvert(av[1])) == NULL) |
466 |
return(MG_EUNDEF); |
467 |
if (!isflt(av[2])) |
468 |
return(MG_ETYPE); |
469 |
rad = atof(av[2]); |
470 |
/* initialize */ |
471 |
warpconends = 1; |
472 |
if ((rval = handle_it(MG_E_VERTEX, 3, v2ent)) != MG_OK) |
473 |
return(rval); |
474 |
sprintf(p2x, FLTFMT, cv->p[0]); |
475 |
sprintf(p2y, FLTFMT, cv->p[1]); |
476 |
sprintf(p2z, FLTFMT, cv->p[2]+rad); |
477 |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
478 |
return(rval); |
479 |
r2[0] = '0'; r2[1] = '\0'; |
480 |
for (i = 1; i <= 2*mg_nqcdivs; i++) { |
481 |
theta = i*(PI/2)/mg_nqcdivs; |
482 |
if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
483 |
return(rval); |
484 |
sprintf(p2z, FLTFMT, cv->p[2]+rad*cos(theta)); |
485 |
if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
486 |
return(rval); |
487 |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
488 |
return(rval); |
489 |
strcpy(r1, r2); |
490 |
sprintf(r2, FLTFMT, rad*sin(theta)); |
491 |
if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK) |
492 |
return(rval); |
493 |
} |
494 |
warpconends = 0; |
495 |
return(MG_OK); |
496 |
} |
497 |
|
498 |
|
499 |
static int |
500 |
e_torus(ac, av) /* expand a torus into cones */ |
501 |
int ac; |
502 |
char **av; |
503 |
{ |
504 |
static char p2[3][24], r1[24], r2[24]; |
505 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_tv1","=","_tv2"}; |
506 |
static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_tv2","="}; |
507 |
static char *p2ent[5] = {mg_ename[MG_E_POINT],p2[0],p2[1],p2[2]}; |
508 |
static char *conent[6] = {mg_ename[MG_E_CONE],"_tv1",r1,"_tv2",r2}; |
509 |
register C_VERTEX *cv; |
510 |
register int i, j; |
511 |
int rval; |
512 |
int sgn; |
513 |
double minrad, maxrad, avgrad; |
514 |
double theta; |
515 |
|
516 |
if (ac != 4) |
517 |
return(MG_EARGC); |
518 |
if ((cv = c_getvert(av[1])) == NULL) |
519 |
return(MG_EUNDEF); |
520 |
if (cv->n[0]==0. && cv->n[1]==0. && cv->n[2]==0.) |
521 |
return(MG_EILL); |
522 |
if (!isflt(av[2]) || !isflt(av[3])) |
523 |
return(MG_ETYPE); |
524 |
minrad = atof(av[2]); |
525 |
maxrad = atof(av[3]); |
526 |
/* check orientation */ |
527 |
if (minrad > 0.) |
528 |
sgn = 1; |
529 |
else if (minrad < 0.) |
530 |
sgn = -1; |
531 |
else if (maxrad > 0.) |
532 |
sgn = 1; |
533 |
else if (maxrad < 0.) |
534 |
sgn = -1; |
535 |
else |
536 |
return(MG_EILL); |
537 |
if (sgn*(maxrad-minrad) <= 0.) |
538 |
return(MG_EILL); |
539 |
/* initialize */ |
540 |
warpconends = 1; |
541 |
v2ent[3] = av[1]; |
542 |
for (j = 0; j < 3; j++) |
543 |
sprintf(p2[j], FLTFMT, cv->p[j] + |
544 |
.5*sgn*(maxrad-minrad)*cv->n[j]); |
545 |
if ((rval = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
546 |
return(rval); |
547 |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
548 |
return(rval); |
549 |
sprintf(r2, FLTFMT, avgrad=.5*(minrad+maxrad)); |
550 |
/* run outer section */ |
551 |
for (i = 1; i <= 2*mg_nqcdivs; i++) { |
552 |
theta = i*(PI/2)/mg_nqcdivs; |
553 |
if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
554 |
return(rval); |
555 |
for (j = 0; j < 3; j++) |
556 |
sprintf(p2[j], FLTFMT, cv->p[j] + |
557 |
.5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); |
558 |
if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
559 |
return(rval); |
560 |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
561 |
return(rval); |
562 |
strcpy(r1, r2); |
563 |
sprintf(r2, FLTFMT, avgrad + .5*(maxrad-minrad)*sin(theta)); |
564 |
if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK) |
565 |
return(rval); |
566 |
} |
567 |
/* run inner section */ |
568 |
sprintf(r2, FLTFMT, -.5*(minrad+maxrad)); |
569 |
for ( ; i <= 4*mg_nqcdivs; i++) { |
570 |
theta = i*(PI/2)/mg_nqcdivs; |
571 |
for (j = 0; j < 3; j++) |
572 |
sprintf(p2[j], FLTFMT, cv->p[j] + |
573 |
.5*sgn*(maxrad-minrad)*cos(theta)*cv->n[j]); |
574 |
if ((rval = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
575 |
return(rval); |
576 |
if ((rval = handle_it(MG_E_VERTEX, 2, v2ent)) != MG_OK) |
577 |
return(rval); |
578 |
if ((rval = handle_it(MG_E_POINT, 4, p2ent)) != MG_OK) |
579 |
return(rval); |
580 |
strcpy(r1, r2); |
581 |
sprintf(r2, FLTFMT, -avgrad - .5*(maxrad-minrad)*sin(theta)); |
582 |
if ((rval = handle_it(MG_E_CONE, 5, conent)) != MG_OK) |
583 |
return(rval); |
584 |
} |
585 |
warpconends = 0; |
586 |
return(MG_OK); |
587 |
} |
588 |
|
589 |
|
590 |
static int |
591 |
e_cyl(ac, av) /* replace a cylinder with equivalent cone */ |
592 |
int ac; |
593 |
char **av; |
594 |
{ |
595 |
static char *avnew[6] = {mg_ename[MG_E_CONE]}; |
596 |
|
597 |
if (ac != 4) |
598 |
return(MG_EARGC); |
599 |
avnew[1] = av[1]; |
600 |
avnew[2] = av[2]; |
601 |
avnew[3] = av[3]; |
602 |
avnew[4] = av[2]; |
603 |
return(handle_it(MG_E_CONE, 5, avnew)); |
604 |
} |
605 |
|
606 |
|
607 |
static int |
608 |
e_ring(ac, av) /* turn a ring into polygons */ |
609 |
int ac; |
610 |
char **av; |
611 |
{ |
612 |
static char p3[3][24], p4[3][24]; |
613 |
static char *nzent[5] = {mg_ename[MG_E_NORMAL],"0","0","0"}; |
614 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_rv1","="}; |
615 |
static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_rv2","=","_rv3"}; |
616 |
static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_rv3","="}; |
617 |
static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]}; |
618 |
static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_rv4","="}; |
619 |
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
620 |
static char *fent[6] = {mg_ename[MG_E_FACE],"_rv1","_rv2","_rv3","_rv4"}; |
621 |
register C_VERTEX *cv; |
622 |
register int i, j; |
623 |
FVECT u, v; |
624 |
double minrad, maxrad; |
625 |
int rv; |
626 |
double theta, d; |
627 |
|
628 |
if (ac != 4) |
629 |
return(MG_EARGC); |
630 |
if ((cv = c_getvert(av[1])) == NULL) |
631 |
return(MG_EUNDEF); |
632 |
if (cv->n[0]==0. && cv->n[1]==0. && cv->n[2]==0.) |
633 |
return(MG_EILL); |
634 |
if (!isflt(av[2]) || !isflt(av[3])) |
635 |
return(MG_ETYPE); |
636 |
minrad = atof(av[2]); |
637 |
maxrad = atof(av[3]); |
638 |
if (minrad < 0. || maxrad <= minrad) |
639 |
return(MG_EILL); |
640 |
/* initialize */ |
641 |
make_axes(u, v, cv->n); |
642 |
for (j = 0; j < 3; j++) |
643 |
sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*u[j]); |
644 |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
645 |
return(rv); |
646 |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
647 |
return(rv); |
648 |
if (minrad == 0.) { /* closed */ |
649 |
v1ent[3] = av[1]; |
650 |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
651 |
return(rv); |
652 |
if ((rv = handle_it(MG_E_NORMAL, 4, nzent)) != MG_OK) |
653 |
return(rv); |
654 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
655 |
theta = i*(PI/2)/mg_nqcdivs; |
656 |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
657 |
return(rv); |
658 |
for (j = 0; j < 3; j++) |
659 |
sprintf(p3[j], FLTFMT, cv->p[j] + |
660 |
maxrad*u[j]*cos(theta) + |
661 |
maxrad*v[j]*sin(theta)); |
662 |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
663 |
return(rv); |
664 |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
665 |
return(rv); |
666 |
if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK) |
667 |
return(rv); |
668 |
} |
669 |
} else { /* open */ |
670 |
if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
671 |
return(rv); |
672 |
for (j = 0; j < 3; j++) |
673 |
sprintf(p4[j], FLTFMT, cv->p[j] + minrad*u[j]); |
674 |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
675 |
return(rv); |
676 |
v1ent[3] = "_rv4"; |
677 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
678 |
theta = i*(PI/2)/mg_nqcdivs; |
679 |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
680 |
return(rv); |
681 |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
682 |
return(rv); |
683 |
for (j = 0; j < 3; j++) { |
684 |
d = u[j]*cos(theta) + v[j]*sin(theta); |
685 |
sprintf(p3[j], FLTFMT, cv->p[j] + maxrad*d); |
686 |
sprintf(p4[j], FLTFMT, cv->p[j] + minrad*d); |
687 |
} |
688 |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
689 |
return(rv); |
690 |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
691 |
return(rv); |
692 |
if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
693 |
return(rv); |
694 |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
695 |
return(rv); |
696 |
if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK) |
697 |
return(rv); |
698 |
} |
699 |
} |
700 |
return(MG_OK); |
701 |
} |
702 |
|
703 |
|
704 |
static int |
705 |
e_cone(ac, av) /* turn a cone into polygons */ |
706 |
int ac; |
707 |
char **av; |
708 |
{ |
709 |
static char p3[3][24], p4[3][24], n3[3][24], n4[3][24]; |
710 |
static char *v1ent[5] = {mg_ename[MG_E_VERTEX],"_cv1","="}; |
711 |
static char *v2ent[5] = {mg_ename[MG_E_VERTEX],"_cv2","=","_cv3"}; |
712 |
static char *v3ent[4] = {mg_ename[MG_E_VERTEX],"_cv3","="}; |
713 |
static char *p3ent[5] = {mg_ename[MG_E_POINT],p3[0],p3[1],p3[2]}; |
714 |
static char *n3ent[5] = {mg_ename[MG_E_NORMAL],n3[0],n3[1],n3[2]}; |
715 |
static char *v4ent[4] = {mg_ename[MG_E_VERTEX],"_cv4","="}; |
716 |
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
717 |
static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]}; |
718 |
static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"}; |
719 |
register C_VERTEX *cv1, *cv2; |
720 |
register int i, j; |
721 |
FVECT u, v, w; |
722 |
double rad1, rad2; |
723 |
int sgn; |
724 |
double n1off, n2off; |
725 |
double d; |
726 |
int rv; |
727 |
double theta; |
728 |
|
729 |
if (ac != 5) |
730 |
return(MG_EARGC); |
731 |
if ((cv1 = c_getvert(av[1])) == NULL || |
732 |
(cv2 = c_getvert(av[3])) == NULL) |
733 |
return(MG_EUNDEF); |
734 |
if (!isflt(av[2]) || !isflt(av[4])) |
735 |
return(MG_ETYPE); |
736 |
rad1 = atof(av[2]); |
737 |
rad2 = atof(av[4]); |
738 |
if (rad1 == 0.) { |
739 |
if (rad2 == 0.) |
740 |
return(MG_EILL); |
741 |
} else if (rad2 != 0.) { |
742 |
if (rad1 < 0. ^ rad2 < 0.) |
743 |
return(MG_EILL); |
744 |
} else { /* swap */ |
745 |
C_VERTEX *cv; |
746 |
|
747 |
cv = cv1; |
748 |
cv1 = cv2; |
749 |
cv2 = cv; |
750 |
d = rad1; |
751 |
rad1 = rad2; |
752 |
rad2 = d; |
753 |
} |
754 |
sgn = rad2 < 0. ? -1 : 1; |
755 |
/* initialize */ |
756 |
for (j = 0; j < 3; j++) |
757 |
w[j] = cv1->p[j] - cv2->p[j]; |
758 |
if ((d = normalize(w)) == 0.) |
759 |
return(MG_EILL); |
760 |
n1off = n2off = (rad2 - rad1)/d; |
761 |
if (warpconends) /* hack for e_sph and e_torus */ |
762 |
n2off = tan(atan(n2off)-(PI/4)/mg_nqcdivs); |
763 |
n2off = sgn*n2off; |
764 |
make_axes(u, v, w); |
765 |
for (j = 0; j < 3; j++) { |
766 |
sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*u[j]); |
767 |
sprintf(n3[j], FLTFMT, u[j] + w[j]*n2off); |
768 |
} |
769 |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
770 |
return(rv); |
771 |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
772 |
return(rv); |
773 |
if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
774 |
return(rv); |
775 |
if (rad1 == 0.) { /* triangles */ |
776 |
v1ent[3] = av[1]; |
777 |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
778 |
return(rv); |
779 |
for (j = 0; j < 3; j++) |
780 |
sprintf(n4[j], FLTFMT, w[j]); |
781 |
if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
782 |
return(rv); |
783 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
784 |
theta = sgn*i*(PI/2)/mg_nqcdivs; |
785 |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
786 |
return(rv); |
787 |
for (j = 0; j < 3; j++) { |
788 |
d = u[j]*cos(theta) + v[j]*sin(theta); |
789 |
sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d); |
790 |
sprintf(n3[j], FLTFMT, d + w[j]*n2off); |
791 |
} |
792 |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
793 |
return(rv); |
794 |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
795 |
return(rv); |
796 |
if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
797 |
return(rv); |
798 |
if ((rv = handle_it(MG_E_FACE, 4, fent)) != MG_OK) |
799 |
return(rv); |
800 |
} |
801 |
} else { /* quads */ |
802 |
v1ent[3] = "_cv4"; |
803 |
if (warpconends) /* hack for e_sph and e_torus */ |
804 |
n1off = tan(atan(n1off)+(PI/4)/mg_nqcdivs); |
805 |
n1off = sgn*n1off; |
806 |
for (j = 0; j < 3; j++) { |
807 |
sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*u[j]); |
808 |
sprintf(n4[j], FLTFMT, u[j] + w[j]*n1off); |
809 |
} |
810 |
if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
811 |
return(rv); |
812 |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
813 |
return(rv); |
814 |
if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
815 |
return(rv); |
816 |
for (i = 1; i <= 4*mg_nqcdivs; i++) { |
817 |
theta = sgn*i*(PI/2)/mg_nqcdivs; |
818 |
if ((rv = handle_it(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
819 |
return(rv); |
820 |
if ((rv = handle_it(MG_E_VERTEX, 4, v2ent)) != MG_OK) |
821 |
return(rv); |
822 |
for (j = 0; j < 3; j++) { |
823 |
d = u[j]*cos(theta) + v[j]*sin(theta); |
824 |
sprintf(p3[j], FLTFMT, cv2->p[j] + rad2*d); |
825 |
sprintf(n3[j], FLTFMT, d + w[j]*n2off); |
826 |
sprintf(p4[j], FLTFMT, cv1->p[j] + rad1*d); |
827 |
sprintf(n4[j], FLTFMT, d + w[j]*n1off); |
828 |
} |
829 |
if ((rv = handle_it(MG_E_VERTEX, 3, v3ent)) != MG_OK) |
830 |
return(rv); |
831 |
if ((rv = handle_it(MG_E_POINT, 4, p3ent)) != MG_OK) |
832 |
return(rv); |
833 |
if ((rv = handle_it(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
834 |
return(rv); |
835 |
if ((rv = handle_it(MG_E_VERTEX, 3, v4ent)) != MG_OK) |
836 |
return(rv); |
837 |
if ((rv = handle_it(MG_E_POINT, 4, p4ent)) != MG_OK) |
838 |
return(rv); |
839 |
if ((rv = handle_it(MG_E_NORMAL, 4, n4ent)) != MG_OK) |
840 |
return(rv); |
841 |
if ((rv = handle_it(MG_E_FACE, 5, fent)) != MG_OK) |
842 |
return(rv); |
843 |
} |
844 |
} |
845 |
return(MG_OK); |
846 |
} |