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