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