1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Convert MGF (Materials and Geometry Format) to Metafile 2-d graphics |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include <stdlib.h> |
10 |
#include <math.h> |
11 |
#include "random.h" |
12 |
#include "mgflib/parser.h" |
13 |
|
14 |
#define MSIZE ((1<<14)-1) |
15 |
#define MX(v) (int)(MSIZE*(v)[(proj_axis+1)%3]) |
16 |
#define MY(v) (int)(MSIZE*(v)[(proj_axis+2)%3]) |
17 |
|
18 |
int r_face(); |
19 |
int proj_axis; |
20 |
double limit[3][2]; |
21 |
int layer; |
22 |
long rthresh = 1; |
23 |
|
24 |
extern int mg_nqcdivs; |
25 |
|
26 |
|
27 |
main(argc, argv) /* convert files to stdout */ |
28 |
int argc; |
29 |
char *argv[]; |
30 |
{ |
31 |
int i; |
32 |
/* initialize dispatch table */ |
33 |
mg_ehand[MG_E_FACE] = r_face; |
34 |
mg_ehand[MG_E_POINT] = c_hvertex; |
35 |
mg_ehand[MG_E_VERTEX] = c_hvertex; |
36 |
mg_ehand[MG_E_XF] = xf_handler; |
37 |
mg_nqcdivs = 3; /* reduce object subdivision */ |
38 |
mg_init(); /* initialize the parser */ |
39 |
/* get arguments */ |
40 |
if (argc > 9 && !strcmp(argv[1], "-t")) { |
41 |
rthresh = atof(argv[2])*MSIZE + 0.5; |
42 |
rthresh *= rthresh; |
43 |
argv += 2; |
44 |
argc -= 2; |
45 |
} |
46 |
if (argc < 8 || (proj_axis = argv[1][0]-'x') < 0 || proj_axis > 2) |
47 |
goto userr; |
48 |
limit[0][0] = atof(argv[2]); limit[0][1] = atof(argv[3]); |
49 |
limit[1][0] = atof(argv[4]); limit[1][1] = atof(argv[5]); |
50 |
limit[2][0] = atof(argv[6]); limit[2][1] = atof(argv[7]); |
51 |
|
52 |
if (argc == 8) { /* convert stdin */ |
53 |
if (mg_load(NULL) != MG_OK) |
54 |
exit(1); |
55 |
} else /* convert each file */ |
56 |
for (i = 8; i < argc; i++) { |
57 |
if (mg_load(argv[i]) != MG_OK) |
58 |
exit(1); |
59 |
newlayer(); |
60 |
} |
61 |
mendpage(); /* print page */ |
62 |
mdone(); /* close output */ |
63 |
exit(0); |
64 |
userr: |
65 |
fputs("Usage: mgf2meta [-t thresh] {x|y|z} xmin xmax ymin ymax zmin zmax [file.mgf] ..\n", |
66 |
stderr); |
67 |
exit(1); |
68 |
} |
69 |
|
70 |
|
71 |
int |
72 |
r_face(ac, av) /* convert a face */ |
73 |
int ac; |
74 |
char **av; |
75 |
{ |
76 |
static FVECT bbmin = {0,0,0}, bbmax = {1,1,1}; |
77 |
register int i, j; |
78 |
register C_VERTEX *cv; |
79 |
FVECT v1, v2, vo; |
80 |
|
81 |
if (ac < 4) |
82 |
return(MG_EARGC); |
83 |
/* connect to last point */ |
84 |
if ((cv = c_getvert(av[ac-1])) == NULL) |
85 |
return(MG_EUNDEF); |
86 |
xf_xfmpoint(vo, cv->p); |
87 |
for (j = 0; j < 3; j++) |
88 |
vo[j] = (vo[j] - limit[j][0])/(limit[j][1]-limit[j][0]); |
89 |
for (i = 1; i < ac; i++) { /* go around face */ |
90 |
if ((cv = c_getvert(av[i])) == NULL) |
91 |
return(MG_EUNDEF); |
92 |
xf_xfmpoint(v2, cv->p); |
93 |
for (j = 0; j < 3; j++) |
94 |
v2[j] = (v2[j] - limit[j][0])/(limit[j][1]-limit[j][0]); |
95 |
VCOPY(v1, vo); |
96 |
VCOPY(vo, v2); |
97 |
if (clip(v1, v2, bbmin, bbmax)) |
98 |
doline(MX(v1), MY(v1), MX(v2), MY(v2)); |
99 |
} |
100 |
return(MG_OK); |
101 |
} |
102 |
|
103 |
|
104 |
#define HTBLSIZ 16381 /* prime hash table size */ |
105 |
|
106 |
short hshtab[HTBLSIZ][4]; /* done line segments */ |
107 |
|
108 |
#define hash(mx1,my1,mx2,my2) ((long)(mx1)<<15 ^ (long)(my1)<<10 ^ \ |
109 |
(long)(mx2)<<5 ^ (long)(my2)) |
110 |
|
111 |
|
112 |
newlayer() /* start a new layer */ |
113 |
{ |
114 |
#ifdef BSD |
115 |
bzero((char *)hshtab, sizeof(hshtab)); |
116 |
#else |
117 |
(void)memset((char *)hshtab, 0, sizeof(hshtab)); |
118 |
#endif |
119 |
if (++layer >= 16) { |
120 |
mendpage(); |
121 |
layer = 0; |
122 |
} |
123 |
} |
124 |
|
125 |
|
126 |
int |
127 |
doline(v1x, v1y, v2x, v2y) /* draw line conditionally */ |
128 |
int v1x, v1y, v2x, v2y; |
129 |
{ |
130 |
register int h; |
131 |
|
132 |
if (v1x > v2x || (v1x == v2x && v1y > v2y)) { /* sort endpoints */ |
133 |
h=v1x; v1x=v2x; v2x=h; |
134 |
h=v1y; v1y=v2y; v2y=h; |
135 |
} |
136 |
h = hash(v1x, v1y, v2x, v2y) % HTBLSIZ; |
137 |
if (hshtab[h][0] == v1x && hshtab[h][1] == v1y && |
138 |
hshtab[h][2] == v2x && hshtab[h][3] == v2y) |
139 |
return(0); |
140 |
hshtab[h][0] = v1x; hshtab[h][1] = v1y; |
141 |
hshtab[h][2] = v2x; hshtab[h][3] = v2y; |
142 |
if ((long)(v2x-v1x)*(v2x-v1x) + (long)(v2y-v1y)*(v2y-v1y) |
143 |
<= random() % rthresh) |
144 |
return(0); |
145 |
mline(v1x, v1y, layer/4, 0, layer%4); |
146 |
mdraw(v2x, v2y); |
147 |
return(1); |
148 |
} |