1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: obj2mesh.c,v 2.5 2003/06/05 19:29:34 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Main program to compile a Wavefront .OBJ file into a Radiance mesh |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
#include "platform.h" |
10 |
#include "standard.h" |
11 |
#include "cvmesh.h" |
12 |
#include "otypes.h" |
13 |
|
14 |
extern int o_face(); |
15 |
|
16 |
int o_default() { return(O_MISS); } |
17 |
|
18 |
FUN ofun[NUMOTYPE] = INIT_OTYPE; /* needed for link resolution */ |
19 |
|
20 |
char *progname; /* argv[0] */ |
21 |
|
22 |
int nowarn = 0; /* supress warnings? */ |
23 |
|
24 |
int objlim = 15; /* # of objects before split */ |
25 |
|
26 |
int resolu = 16384; /* octree resolution limit */ |
27 |
|
28 |
double mincusize; /* minimum cube size from resolu */ |
29 |
|
30 |
|
31 |
main(argc, argv) /* compile a .OBJ file into a mesh */ |
32 |
int argc; |
33 |
char *argv[]; |
34 |
{ |
35 |
int nmatf = 0; |
36 |
char *matinp[32]; |
37 |
int i, j; |
38 |
|
39 |
progname = argv[0]; |
40 |
ofun[OBJ_FACE].funp = o_face; |
41 |
|
42 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
43 |
switch (argv[i][1]) { |
44 |
case 'n': /* set limit */ |
45 |
objlim = atoi(argv[++i]); |
46 |
break; |
47 |
case 'r': /* resolution limit */ |
48 |
resolu = atoi(argv[++i]); |
49 |
break; |
50 |
case 'a': /* material file */ |
51 |
matinp[nmatf++] = argv[++i]; |
52 |
break; |
53 |
case 'w': /* supress warnings */ |
54 |
nowarn = 1; |
55 |
break; |
56 |
default: |
57 |
sprintf(errmsg, "unknown option: '%s'", argv[i]); |
58 |
error(USER, errmsg); |
59 |
break; |
60 |
} |
61 |
/* initialize mesh */ |
62 |
cvinit(i==argc-2 ? argv[i+1] : "<stdout>"); |
63 |
/* load material input */ |
64 |
for (j = 0; j < nmatf; j++) |
65 |
readobj(matinp[j]); |
66 |
/* read .OBJ file into triangles */ |
67 |
if (i == argc) |
68 |
wfreadobj(NULL); |
69 |
else |
70 |
wfreadobj(argv[i]); |
71 |
|
72 |
cvmeshbounds(); /* set octree boundaries */ |
73 |
|
74 |
if (i == argc-2) /* open output file */ |
75 |
if (freopen(argv[i+1], "w", stdout) == NULL) |
76 |
error(SYSTEM, "cannot open output file"); |
77 |
SET_FILE_BINARY(stdout); |
78 |
newheader("RADIANCE", stdout); /* new binary file header */ |
79 |
printargs(i<argc ? i+1 : argc, argv, stdout); |
80 |
fputformat(MESHFMT, stdout); |
81 |
fputc('\n', stdout); |
82 |
|
83 |
mincusize = ourmesh->mcube.cusize / resolu - FTINY; |
84 |
|
85 |
for (i = 0; i < nobjects; i++) /* add triangles to octree */ |
86 |
if (objptr(i)->otype == OBJ_FACE) |
87 |
addface(&ourmesh->mcube, i); |
88 |
|
89 |
/* optimize octree */ |
90 |
ourmesh->mcube.cutree = combine(ourmesh->mcube.cutree); |
91 |
|
92 |
if (ourmesh->mcube.cutree == EMPTY) |
93 |
error(WARNING, "mesh is empty"); |
94 |
|
95 |
cvmesh(); /* convert mesh and leaf nodes */ |
96 |
|
97 |
writemesh(ourmesh, stdout); /* write mesh to output */ |
98 |
|
99 |
/* printmeshstats(ourmesh, stderr); */ |
100 |
|
101 |
quit(0); |
102 |
} |
103 |
|
104 |
|
105 |
void |
106 |
quit(code) /* exit program */ |
107 |
int code; |
108 |
{ |
109 |
exit(code); |
110 |
} |
111 |
|
112 |
|
113 |
void |
114 |
cputs() /* interactive error */ |
115 |
{ |
116 |
/* referenced, but not used */ |
117 |
} |
118 |
|
119 |
|
120 |
void |
121 |
wputs(s) /* warning message */ |
122 |
char *s; |
123 |
{ |
124 |
if (!nowarn) |
125 |
eputs(s); |
126 |
} |
127 |
|
128 |
|
129 |
void |
130 |
eputs(s) /* put string to stderr */ |
131 |
register char *s; |
132 |
{ |
133 |
static int inln = 0; |
134 |
|
135 |
if (!inln++) { |
136 |
fputs(progname, stderr); |
137 |
fputs(": ", stderr); |
138 |
} |
139 |
fputs(s, stderr); |
140 |
if (*s && s[strlen(s)-1] == '\n') |
141 |
inln = 0; |
142 |
} |
143 |
|
144 |
|
145 |
addface(cu, obj) /* add a face to a cube */ |
146 |
register CUBE *cu; |
147 |
OBJECT obj; |
148 |
{ |
149 |
|
150 |
if (o_face(objptr(obj), cu) == O_MISS) |
151 |
return; |
152 |
|
153 |
if (istree(cu->cutree)) { |
154 |
CUBE cukid; /* do children */ |
155 |
int i, j; |
156 |
cukid.cusize = cu->cusize * 0.5; |
157 |
for (i = 0; i < 8; i++) { |
158 |
cukid.cutree = octkid(cu->cutree, i); |
159 |
for (j = 0; j < 3; j++) { |
160 |
cukid.cuorg[j] = cu->cuorg[j]; |
161 |
if ((1<<j) & i) |
162 |
cukid.cuorg[j] += cukid.cusize; |
163 |
} |
164 |
addface(&cukid, obj); |
165 |
octkid(cu->cutree, i) = cukid.cutree; |
166 |
} |
167 |
return; |
168 |
} |
169 |
if (isempty(cu->cutree)) { |
170 |
OBJECT oset[2]; /* singular set */ |
171 |
oset[0] = 1; oset[1] = obj; |
172 |
cu->cutree = fullnode(oset); |
173 |
return; |
174 |
} |
175 |
/* add to full node */ |
176 |
add2full(cu, obj); |
177 |
} |
178 |
|
179 |
|
180 |
add2full(cu, obj) /* add object to full node */ |
181 |
register CUBE *cu; |
182 |
OBJECT obj; |
183 |
{ |
184 |
OCTREE ot; |
185 |
OBJECT oset[MAXSET+1]; |
186 |
CUBE cukid; |
187 |
register int i, j; |
188 |
|
189 |
objset(oset, cu->cutree); |
190 |
cukid.cusize = cu->cusize * 0.5; |
191 |
|
192 |
if (oset[0] < objlim || cukid.cusize < mincusize) { |
193 |
/* add to set */ |
194 |
if (oset[0] >= MAXSET) { |
195 |
sprintf(errmsg, "set overflow in addobject (%s)", |
196 |
objptr(obj)->oname); |
197 |
error(INTERNAL, errmsg); |
198 |
} |
199 |
insertelem(oset, obj); |
200 |
cu->cutree = fullnode(oset); |
201 |
return; |
202 |
} |
203 |
/* subdivide cube */ |
204 |
if ((ot = octalloc()) == EMPTY) |
205 |
error(SYSTEM, "out of octree space"); |
206 |
/* assign subcubes */ |
207 |
for (i = 0; i < 8; i++) { |
208 |
cukid.cutree = EMPTY; |
209 |
for (j = 0; j < 3; j++) { |
210 |
cukid.cuorg[j] = cu->cuorg[j]; |
211 |
if ((1<<j) & i) |
212 |
cukid.cuorg[j] += cukid.cusize; |
213 |
} |
214 |
for (j = 1; j <= oset[0]; j++) |
215 |
addface(&cukid, oset[j]); |
216 |
addface(&cukid, obj); |
217 |
/* returned node */ |
218 |
octkid(ot, i) = cukid.cutree; |
219 |
} |
220 |
cu->cutree = ot; |
221 |
} |