1 |
greg |
2.1 |
#ifndef lint |
2 |
|
|
static const char RCSid[] = "$Id$"; |
3 |
|
|
#endif |
4 |
|
|
/* |
5 |
|
|
* Mesh support routines |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "standard.h" |
9 |
|
|
#include "octree.h" |
10 |
|
|
#include "object.h" |
11 |
|
|
#include "mesh.h" |
12 |
|
|
#include "lookup.h" |
13 |
|
|
|
14 |
|
|
/* An encoded mesh vertex */ |
15 |
|
|
typedef struct { |
16 |
|
|
int fl; |
17 |
|
|
uint4 xyz[3]; |
18 |
|
|
int4 norm; |
19 |
greg |
2.2 |
uint4 uv[2]; |
20 |
greg |
2.1 |
} MCVERT; |
21 |
|
|
|
22 |
|
|
#define MPATCHBLKSIZ 128 /* patch allocation block size */ |
23 |
|
|
|
24 |
|
|
#define IO_LEGAL (IO_BOUNDS|IO_TREE|IO_SCENE) |
25 |
|
|
|
26 |
|
|
static MESH *mlist = NULL; /* list of loaded meshes */ |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
static unsigned long |
30 |
|
|
cvhash(cvp) /* hash an encoded vertex */ |
31 |
|
|
MCVERT *cvp; |
32 |
|
|
{ |
33 |
|
|
unsigned long hval; |
34 |
|
|
|
35 |
|
|
if (!(cvp->fl & MT_V)) |
36 |
|
|
return(0); |
37 |
|
|
hval = cvp->xyz[0] ^ cvp->xyz[1] << 11 ^ cvp->xyz[2] << 22; |
38 |
|
|
if (cvp->fl & MT_N) |
39 |
|
|
hval ^= cvp->norm; |
40 |
|
|
if (cvp->fl & MT_UV) |
41 |
|
|
hval ^= cvp->uv[0] ^ cvp->uv[1] << 16; |
42 |
|
|
return(hval); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
static int |
47 |
|
|
cvcmp(v1, v2) /* compare encoded vertices */ |
48 |
|
|
register MCVERT *v1, *v2; |
49 |
|
|
{ |
50 |
|
|
if (v1->fl != v2->fl) |
51 |
|
|
return(1); |
52 |
|
|
if (v1->xyz[0] != v2->xyz[0]) |
53 |
|
|
return(1); |
54 |
|
|
if (v1->xyz[1] != v2->xyz[1]) |
55 |
|
|
return(1); |
56 |
|
|
if (v1->xyz[2] != v2->xyz[2]) |
57 |
|
|
return(1); |
58 |
|
|
if (v1->fl & MT_N && v1->norm != v2->norm) |
59 |
|
|
return(1); |
60 |
|
|
if (v1->fl & MT_UV) { |
61 |
|
|
if (v1->uv[0] != v2->uv[0]) |
62 |
|
|
return(1); |
63 |
|
|
if (v1->uv[1] != v2->uv[1]) |
64 |
|
|
return(1); |
65 |
|
|
} |
66 |
|
|
return(0); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
|
70 |
|
|
MESH * |
71 |
|
|
getmesh(mname, flags) /* get mesh data */ |
72 |
|
|
char *mname; |
73 |
|
|
int flags; |
74 |
|
|
{ |
75 |
|
|
char *pathname; |
76 |
|
|
register MESH *ms; |
77 |
|
|
|
78 |
|
|
flags &= IO_LEGAL; |
79 |
|
|
for (ms = mlist; ms != NULL; ms = ms->next) |
80 |
|
|
if (!strcmp(mname, ms->name)) { |
81 |
|
|
if ((ms->ldflags & flags) == flags) { |
82 |
|
|
ms->nref++; |
83 |
|
|
return(ms); /* loaded */ |
84 |
|
|
} |
85 |
|
|
break; /* load the rest */ |
86 |
|
|
} |
87 |
|
|
if (ms == NULL) { |
88 |
|
|
ms = (MESH *)calloc(1, sizeof(MESH)); |
89 |
|
|
if (ms == NULL) |
90 |
|
|
error(SYSTEM, "out of memory in getmesh"); |
91 |
|
|
ms->name = savestr(mname); |
92 |
|
|
ms->nref = 1; |
93 |
|
|
ms->mcube.cutree = EMPTY; |
94 |
|
|
ms->next = mlist; |
95 |
|
|
mlist = ms; |
96 |
|
|
} |
97 |
|
|
if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) { |
98 |
|
|
sprintf(errmsg, "cannot find mesh file \"%s\"", mname); |
99 |
|
|
error(USER, errmsg); |
100 |
|
|
} |
101 |
|
|
flags &= ~ms->ldflags; |
102 |
|
|
if (flags) |
103 |
|
|
readmesh(ms, pathname, flags); |
104 |
|
|
return(ms); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
|
108 |
|
|
MESHINST * |
109 |
|
|
getmeshinst(o, flags) /* create mesh instance */ |
110 |
|
|
OBJREC *o; |
111 |
|
|
int flags; |
112 |
|
|
{ |
113 |
|
|
register MESHINST *ins; |
114 |
|
|
|
115 |
|
|
flags &= IO_LEGAL; |
116 |
|
|
if ((ins = (MESHINST *)o->os) == NULL) { |
117 |
|
|
if ((ins = (MESHINST *)malloc(sizeof(MESHINST))) == NULL) |
118 |
|
|
error(SYSTEM, "out of memory in getmeshinst"); |
119 |
|
|
if (o->oargs.nsargs < 1) |
120 |
|
|
objerror(o, USER, "bad # of arguments"); |
121 |
|
|
if (fullxf(&ins->x, o->oargs.nsargs-1, |
122 |
|
|
o->oargs.sarg+1) != o->oargs.nsargs-1) |
123 |
|
|
objerror(o, USER, "bad transform"); |
124 |
|
|
if (ins->x.f.sca < 0.0) { |
125 |
|
|
ins->x.f.sca = -ins->x.f.sca; |
126 |
|
|
ins->x.b.sca = -ins->x.b.sca; |
127 |
|
|
} |
128 |
|
|
ins->msh = NULL; |
129 |
|
|
o->os = (char *)ins; |
130 |
|
|
} |
131 |
|
|
if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags) |
132 |
|
|
ins->msh = getmesh(o->oargs.sarg[0], flags); |
133 |
|
|
return(ins); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
int |
138 |
|
|
getmeshtrivid(tvid, mp, ti) /* get triangle vertex ID's */ |
139 |
|
|
int4 tvid[3]; |
140 |
|
|
register MESH *mp; |
141 |
|
|
OBJECT ti; |
142 |
|
|
{ |
143 |
|
|
int pn = ti >> 10; |
144 |
|
|
|
145 |
|
|
if (pn >= mp->npatches) |
146 |
|
|
return(0); |
147 |
|
|
ti &= 0x3ff; |
148 |
|
|
if (!(ti & 0x200)) { /* local triangle */ |
149 |
|
|
struct PTri *tp; |
150 |
|
|
if (ti >= mp->patch[pn].ntris) |
151 |
|
|
return(0); |
152 |
|
|
tp = &mp->patch[pn].tri[ti]; |
153 |
|
|
tvid[0] = tvid[1] = tvid[2] = pn << 8; |
154 |
|
|
tvid[0] |= tp->v1; |
155 |
|
|
tvid[1] |= tp->v2; |
156 |
|
|
tvid[2] |= tp->v3; |
157 |
|
|
return(1); |
158 |
|
|
} |
159 |
|
|
ti &= ~0x200; |
160 |
|
|
if (!(ti & 0x100)) { /* single link vertex */ |
161 |
|
|
struct PJoin1 *tp1; |
162 |
|
|
if (ti >= mp->patch[pn].nj1tris) |
163 |
|
|
return(0); |
164 |
|
|
tp1 = &mp->patch[pn].j1tri[ti]; |
165 |
|
|
tvid[0] = tp1->v1j; |
166 |
|
|
tvid[1] = tvid[2] = pn << 8; |
167 |
|
|
tvid[1] |= tp1->v2; |
168 |
|
|
tvid[2] |= tp1->v3; |
169 |
|
|
return(1); |
170 |
|
|
} |
171 |
|
|
ti &= ~0x100; |
172 |
|
|
{ /* double link vertex */ |
173 |
|
|
struct PJoin2 *tp2; |
174 |
|
|
if (ti >= mp->patch[pn].nj2tris) |
175 |
|
|
return(0); |
176 |
|
|
tp2 = &mp->patch[pn].j2tri[ti]; |
177 |
|
|
tvid[0] = tp2->v1j; |
178 |
|
|
tvid[1] = tp2->v2j; |
179 |
|
|
tvid[2] = pn << 8 | tp2->v3; |
180 |
|
|
} |
181 |
|
|
return(1); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
|
185 |
|
|
int |
186 |
|
|
getmeshvert(vp, mp, vid, what) /* get triangle vertex from ID */ |
187 |
|
|
MESHVERT *vp; |
188 |
|
|
register MESH *mp; |
189 |
|
|
int4 vid; |
190 |
|
|
int what; |
191 |
|
|
{ |
192 |
|
|
int pn = vid >> 8; |
193 |
|
|
MESHPATCH *pp; |
194 |
|
|
double vres; |
195 |
|
|
register int i; |
196 |
|
|
|
197 |
|
|
vp->fl = 0; |
198 |
|
|
if (pn >= mp->npatches) |
199 |
|
|
return(0); |
200 |
|
|
pp = &mp->patch[pn]; |
201 |
|
|
vid &= 0xff; |
202 |
|
|
if (vid >= pp->nverts) |
203 |
|
|
return(0); |
204 |
|
|
/* get location */ |
205 |
|
|
if (what & MT_V) { |
206 |
|
|
vres = (1./4294967296.)*mp->mcube.cusize; |
207 |
|
|
for (i = 0; i < 3; i++) |
208 |
|
|
vp->v[i] = mp->mcube.cuorg[i] + |
209 |
|
|
(pp->xyz[vid][i] + .5)*vres; |
210 |
|
|
vp->fl |= MT_V; |
211 |
|
|
} |
212 |
|
|
/* get normal */ |
213 |
|
|
if (what & MT_N && pp->norm != NULL && pp->norm[vid]) { |
214 |
|
|
decodedir(vp->n, pp->norm[vid]); |
215 |
|
|
vp->fl |= MT_N; |
216 |
|
|
} |
217 |
|
|
/* get (u,v) */ |
218 |
|
|
if (what & MT_UV && pp->uv != NULL && pp->uv[vid][0]) { |
219 |
|
|
for (i = 0; i < 2; i++) |
220 |
greg |
2.2 |
vp->uv[i] = mp->uvlim[0][i] + |
221 |
|
|
(mp->uvlim[1][i] - mp->uvlim[0][i])* |
222 |
greg |
2.1 |
(pp->uv[vid][i] + .5)*(1./4294967296.); |
223 |
|
|
vp->fl |= MT_UV; |
224 |
|
|
} |
225 |
|
|
return(vp->fl); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
|
229 |
|
|
int |
230 |
|
|
getmeshtri(tv, mp, ti, what) /* get triangle vertices */ |
231 |
|
|
MESHVERT tv[3]; |
232 |
|
|
MESH *mp; |
233 |
|
|
OBJECT ti; |
234 |
|
|
int what; |
235 |
|
|
{ |
236 |
|
|
int4 tvid[3]; |
237 |
|
|
|
238 |
|
|
if (!getmeshtrivid(tvid, mp, ti)) |
239 |
|
|
return(0); |
240 |
|
|
|
241 |
|
|
getmeshvert(&tv[0], mp, tvid[0], what); |
242 |
|
|
getmeshvert(&tv[1], mp, tvid[1], what); |
243 |
|
|
getmeshvert(&tv[2], mp, tvid[2], what); |
244 |
|
|
|
245 |
|
|
return(tv[0].fl & tv[1].fl & tv[2].fl); |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
|
249 |
|
|
int4 |
250 |
|
|
addmeshvert(mp, vp) /* find/add a mesh vertex */ |
251 |
|
|
register MESH *mp; |
252 |
|
|
MESHVERT *vp; |
253 |
|
|
{ |
254 |
|
|
LUTAB *ltp; |
255 |
|
|
LUENT *lvp; |
256 |
|
|
MCVERT cv; |
257 |
|
|
register int i; |
258 |
|
|
|
259 |
|
|
if (!(vp->fl & MT_V)) |
260 |
|
|
return(-1); |
261 |
|
|
/* encode vertex */ |
262 |
|
|
for (i = 0; i < 3; i++) { |
263 |
|
|
if (vp->v[i] < mp->mcube.cuorg[i]) |
264 |
|
|
return(-1); |
265 |
|
|
if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize) |
266 |
|
|
return(-1); |
267 |
|
|
cv.xyz[i] = (uint4)(4294967296. * |
268 |
|
|
(vp->v[i] - mp->mcube.cuorg[i]) / |
269 |
|
|
mp->mcube.cusize); |
270 |
|
|
} |
271 |
|
|
if (vp->fl & MT_N) |
272 |
|
|
cv.norm = encodedir(vp->n); |
273 |
|
|
if (vp->fl & MT_UV) |
274 |
|
|
for (i = 0; i < 2; i++) { |
275 |
greg |
2.2 |
if (vp->uv[i] <= mp->uvlim[0][i]) |
276 |
greg |
2.1 |
return(-1); |
277 |
greg |
2.2 |
if (vp->uv[i] >= mp->uvlim[1][i]) |
278 |
greg |
2.1 |
return(-1); |
279 |
|
|
cv.uv[i] = (uint4)(4294967296. * |
280 |
greg |
2.2 |
(vp->uv[i] - mp->uvlim[0][i]) / |
281 |
|
|
(mp->uvlim[1][i] - mp->uvlim[0][i])); |
282 |
greg |
2.1 |
} |
283 |
|
|
cv.fl = vp->fl; |
284 |
|
|
ltp = (LUTAB *)mp->cdata; /* get lookup table */ |
285 |
|
|
if (ltp == NULL) { |
286 |
|
|
ltp = (LUTAB *)calloc(1, sizeof(LUTAB)); |
287 |
|
|
if (ltp == NULL) |
288 |
|
|
goto nomem; |
289 |
|
|
ltp->hashf = cvhash; |
290 |
|
|
ltp->keycmp = cvcmp; |
291 |
|
|
ltp->freek = free; |
292 |
|
|
if (!lu_init(ltp, 50000)) |
293 |
|
|
goto nomem; |
294 |
|
|
mp->cdata = (char *)ltp; |
295 |
|
|
} |
296 |
|
|
/* find entry */ |
297 |
|
|
lvp = lu_find(ltp, (char *)&cv); |
298 |
|
|
if (lvp == NULL) |
299 |
|
|
goto nomem; |
300 |
|
|
if (lvp->key == NULL) { |
301 |
|
|
lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4)); |
302 |
|
|
bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); |
303 |
|
|
} |
304 |
|
|
if (lvp->data == NULL) { /* new vertex */ |
305 |
|
|
register MESHPATCH *pp; |
306 |
|
|
if (mp->npatches <= 0) { |
307 |
|
|
mp->patch = (MESHPATCH *)calloc(MPATCHBLKSIZ, |
308 |
|
|
sizeof(MESHPATCH)); |
309 |
|
|
if (mp->patch == NULL) |
310 |
|
|
goto nomem; |
311 |
|
|
mp->npatches = 1; |
312 |
|
|
} else if (mp->patch[mp->npatches-1].nverts >= 256) { |
313 |
|
|
if (mp->npatches % MPATCHBLKSIZ == 0) { |
314 |
|
|
mp->patch = (MESHPATCH *)realloc( |
315 |
|
|
(void *)mp->patch, |
316 |
|
|
(mp->npatches + MPATCHBLKSIZ)* |
317 |
|
|
sizeof(MESHPATCH)); |
318 |
|
|
bzero((void *)(mp->patch + mp->npatches), |
319 |
|
|
MPATCHBLKSIZ*sizeof(MESHPATCH)); |
320 |
|
|
} |
321 |
greg |
2.2 |
if (mp->npatches++ >= 1L<<22) |
322 |
greg |
2.1 |
error(INTERNAL, "too many mesh patches"); |
323 |
|
|
} |
324 |
|
|
pp = &mp->patch[mp->npatches-1]; |
325 |
|
|
if (pp->xyz == NULL) { |
326 |
|
|
pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4)); |
327 |
|
|
if (pp->xyz == NULL) |
328 |
|
|
goto nomem; |
329 |
|
|
} |
330 |
|
|
for (i = 0; i < 3; i++) |
331 |
|
|
pp->xyz[pp->nverts][i] = cv.xyz[i]; |
332 |
|
|
if (cv.fl & MT_N) { |
333 |
|
|
if (pp->norm == NULL) { |
334 |
|
|
pp->norm = (int4 *)calloc(256, sizeof(int4)); |
335 |
|
|
if (pp->norm == NULL) |
336 |
|
|
goto nomem; |
337 |
|
|
} |
338 |
|
|
pp->norm[pp->nverts] = cv.norm; |
339 |
|
|
} |
340 |
|
|
if (cv.fl & MT_UV) { |
341 |
|
|
if (pp->uv == NULL) { |
342 |
|
|
pp->uv = (uint4 (*)[2])calloc(256, |
343 |
|
|
2*sizeof(uint4)); |
344 |
|
|
if (pp->uv == NULL) |
345 |
|
|
goto nomem; |
346 |
|
|
} |
347 |
|
|
for (i = 0; i < 2; i++) |
348 |
|
|
pp->uv[pp->nverts][i] = cv.uv[i]; |
349 |
|
|
} |
350 |
|
|
pp->nverts++; |
351 |
|
|
lvp->data = lvp->key + sizeof(MCVERT); |
352 |
|
|
*(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); |
353 |
|
|
} |
354 |
|
|
return(*(int4 *)lvp->data); |
355 |
|
|
nomem: |
356 |
|
|
error(SYSTEM, "out of memory in addmeshvert"); |
357 |
|
|
return(-1); |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
|
361 |
|
|
OBJECT |
362 |
|
|
addmeshtri(mp, tv) /* add a new mesh triangle */ |
363 |
|
|
MESH *mp; |
364 |
|
|
MESHVERT tv[3]; |
365 |
|
|
{ |
366 |
|
|
int4 vid[3], t; |
367 |
|
|
int pn[3], i; |
368 |
|
|
register MESHPATCH *pp; |
369 |
|
|
|
370 |
|
|
if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V)) |
371 |
|
|
return(OVOID); |
372 |
|
|
/* find/allocate patch vertices */ |
373 |
|
|
for (i = 0; i < 3; i++) { |
374 |
|
|
if ((vid[i] = addmeshvert(mp, &tv[i])) < 0) |
375 |
|
|
return(OVOID); |
376 |
|
|
pn[i] = vid[i] >> 8; |
377 |
|
|
} |
378 |
|
|
/* assign triangle */ |
379 |
|
|
if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ |
380 |
|
|
pp = &mp->patch[pn[0]]; |
381 |
|
|
if (pp->tri == NULL) { |
382 |
|
|
pp->tri = (struct PTri *)malloc( |
383 |
|
|
512*sizeof(struct PTri)); |
384 |
|
|
if (pp->tri == NULL) |
385 |
|
|
goto nomem; |
386 |
|
|
} |
387 |
greg |
2.2 |
if (pp->ntris < 512) { |
388 |
|
|
pp->tri[pp->ntris].v1 = vid[0] & 0xff; |
389 |
|
|
pp->tri[pp->ntris].v2 = vid[1] & 0xff; |
390 |
|
|
pp->tri[pp->ntris].v3 = vid[2] & 0xff; |
391 |
|
|
return(pn[0] << 10 | pp->ntris++); |
392 |
|
|
} |
393 |
greg |
2.1 |
} |
394 |
|
|
if (pn[0] == pn[1]) { |
395 |
greg |
2.2 |
t = vid[2]; vid[2] = vid[1]; vid[1] = vid[0]; vid[0] = t; |
396 |
|
|
i = pn[2]; pn[2] = pn[1]; pn[1] = pn[0]; pn[0] = i; |
397 |
greg |
2.1 |
} else if (pn[0] == pn[2]) { |
398 |
greg |
2.2 |
t = vid[0]; vid[0] = vid[1]; vid[1] = vid[2]; vid[2] = t; |
399 |
|
|
i = pn[0]; pn[0] = pn[1]; pn[1] = pn[2]; pn[2] = i; |
400 |
greg |
2.1 |
} |
401 |
|
|
if (pn[1] == pn[2]) { /* single link */ |
402 |
|
|
pp = &mp->patch[pn[1]]; |
403 |
|
|
if (pp->j1tri == NULL) { |
404 |
|
|
pp->j1tri = (struct PJoin1 *)malloc( |
405 |
|
|
256*sizeof(struct PJoin1)); |
406 |
|
|
if (pp->j1tri == NULL) |
407 |
|
|
goto nomem; |
408 |
|
|
} |
409 |
greg |
2.2 |
if (pp->nj1tris < 256) { |
410 |
|
|
pp->j1tri[pp->nj1tris].v1j = vid[0]; |
411 |
|
|
pp->j1tri[pp->nj1tris].v2 = vid[1] & 0xff; |
412 |
|
|
pp->j1tri[pp->nj1tris].v3 = vid[2] & 0xff; |
413 |
|
|
return(pn[1] << 10 | 0x200 | pp->nj1tris++); |
414 |
|
|
} |
415 |
greg |
2.1 |
} |
416 |
|
|
/* double link */ |
417 |
|
|
pp = &mp->patch[pn[2]]; |
418 |
|
|
if (pp->j2tri == NULL) { |
419 |
|
|
pp->j2tri = (struct PJoin2 *)malloc( |
420 |
|
|
256*sizeof(struct PJoin2)); |
421 |
|
|
if (pp->j2tri == NULL) |
422 |
|
|
goto nomem; |
423 |
|
|
} |
424 |
|
|
if (pp->nj2tris >= 256) |
425 |
greg |
2.2 |
error(INTERNAL, "too many patch triangles in addmeshtri"); |
426 |
|
|
pp->j2tri[pp->nj2tris].v1j = vid[0]; |
427 |
|
|
pp->j2tri[pp->nj2tris].v2j = vid[1]; |
428 |
greg |
2.1 |
pp->j2tri[pp->nj2tris].v3 = vid[2] & 0xff; |
429 |
|
|
return(pn[2] << 10 | 0x300 | pp->nj2tris++); |
430 |
|
|
nomem: |
431 |
|
|
error(SYSTEM, "out of memory in addmeshtri"); |
432 |
|
|
return(OVOID); |
433 |
|
|
} |
434 |
|
|
|
435 |
|
|
|
436 |
|
|
static void |
437 |
|
|
tallyoctree(ot, ecp, lcp, ocp) /* tally octree size */ |
438 |
|
|
OCTREE ot; |
439 |
|
|
int *ecp, *lcp, *ocp; |
440 |
|
|
{ |
441 |
|
|
int i; |
442 |
|
|
|
443 |
|
|
if (isempty(ot)) { |
444 |
|
|
(*ecp)++; |
445 |
|
|
return; |
446 |
|
|
} |
447 |
|
|
if (isfull(ot)) { |
448 |
|
|
OBJECT oset[MAXSET+1]; |
449 |
|
|
(*lcp)++; |
450 |
|
|
objset(oset, ot); |
451 |
|
|
*ocp += oset[0]; |
452 |
|
|
return; |
453 |
|
|
} |
454 |
|
|
for (i = 0; i < 8; i++) |
455 |
|
|
tallyoctree(octkid(ot, i), ecp, lcp, ocp); |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
|
459 |
|
|
void |
460 |
|
|
printmeshstats(ms, fp) /* print out mesh statistics */ |
461 |
|
|
MESH *ms; |
462 |
|
|
FILE *fp; |
463 |
|
|
{ |
464 |
|
|
int lfcnt=0, lecnt=0, locnt=0; |
465 |
|
|
int vcnt=0, ncnt=0, uvcnt=0; |
466 |
|
|
int nscnt=0, uvscnt=0; |
467 |
|
|
int tcnt=0, t1cnt=0, t2cnt=0; |
468 |
|
|
int i, j; |
469 |
|
|
|
470 |
|
|
tallyoctree(ms->mcube.cutree, &lecnt, &lfcnt, &locnt); |
471 |
|
|
for (i = 0; i < ms->npatches; i++) { |
472 |
|
|
register MESHPATCH *pp = &ms->patch[i]; |
473 |
|
|
vcnt += pp->nverts; |
474 |
|
|
if (pp->norm != NULL) { |
475 |
|
|
for (j = pp->nverts; j--; ) |
476 |
|
|
if (pp->norm[j]) |
477 |
|
|
ncnt++; |
478 |
|
|
nscnt += pp->nverts; |
479 |
|
|
} |
480 |
|
|
if (pp->uv != NULL) { |
481 |
|
|
for (j = pp->nverts; j--; ) |
482 |
|
|
if (pp->uv[j][0]) |
483 |
|
|
uvcnt++; |
484 |
|
|
uvscnt += pp->nverts; |
485 |
|
|
} |
486 |
|
|
tcnt += pp->ntris; |
487 |
|
|
t1cnt += pp->nj1tris; |
488 |
|
|
t2cnt += pp->nj2tris; |
489 |
|
|
} |
490 |
|
|
fprintf(fp, "Mesh statistics:\n"); |
491 |
|
|
fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, |
492 |
|
|
(ms->npatches*sizeof(MESHPATCH) + |
493 |
|
|
vcnt*3*sizeof(uint4) + |
494 |
|
|
nscnt*sizeof(int4) + |
495 |
|
|
uvscnt*2*sizeof(uint4) + |
496 |
|
|
tcnt*sizeof(struct PTri) + |
497 |
|
|
t1cnt*sizeof(struct PJoin1) + |
498 |
|
|
t2cnt*sizeof(struct PJoin2))/(1024.*1024.)); |
499 |
|
|
fprintf(fp, "\t%d vertices (%.1f%% w/ normals, %.1f%% w/ uv)\n", |
500 |
|
|
vcnt, 100.*ncnt/vcnt, 100.*uvcnt/vcnt); |
501 |
|
|
fprintf(fp, "\t%d triangles (%.1f%% local, %.1f%% joiner)\n", |
502 |
|
|
tcnt+t1cnt+t2cnt, |
503 |
|
|
100.*tcnt/(tcnt+t1cnt+t2cnt), |
504 |
|
|
100.*t1cnt/(tcnt+t1cnt+t2cnt)); |
505 |
|
|
fprintf(fp, |
506 |
|
|
"\t%d leaves in octree (%.1f%% empty, %.2f avg. set size)\n", |
507 |
|
|
lfcnt+lecnt, 100.*lecnt/(lfcnt+lecnt), |
508 |
|
|
(double)locnt/lfcnt); |
509 |
|
|
} |
510 |
|
|
|
511 |
|
|
|
512 |
|
|
void |
513 |
|
|
freemesh(ms) /* free mesh data */ |
514 |
|
|
MESH *ms; |
515 |
|
|
{ |
516 |
|
|
MESH mhead; |
517 |
|
|
MESH *msp; |
518 |
|
|
|
519 |
|
|
if (ms == NULL) |
520 |
|
|
return; |
521 |
|
|
if (ms->nref <= 0) |
522 |
|
|
error(CONSISTENCY, "unreferenced mesh in freemesh"); |
523 |
|
|
ms->nref--; |
524 |
|
|
if (ms->nref) /* still in use */ |
525 |
|
|
return; |
526 |
|
|
/* else remove from list */ |
527 |
|
|
mhead.next = mlist; |
528 |
|
|
for (msp = &mhead; msp->next != NULL; msp = msp->next) |
529 |
|
|
if (msp->next == ms) { |
530 |
|
|
msp->next = ms->next; |
531 |
|
|
ms->next = NULL; |
532 |
|
|
break; |
533 |
|
|
} |
534 |
|
|
if (ms->next != NULL) /* can't be in list anymore */ |
535 |
|
|
error(CONSISTENCY, "unlisted mesh in freemesh"); |
536 |
|
|
mlist = mhead.next; |
537 |
|
|
/* free mesh data */ |
538 |
|
|
freestr(ms->name); |
539 |
|
|
octfree(ms->mcube.cutree); |
540 |
|
|
if (ms->cdata != NULL) |
541 |
|
|
lu_done((LUTAB *)ms->cdata); |
542 |
|
|
if (ms->npatches > 0) { |
543 |
|
|
register MESHPATCH *pp = ms->patch + ms->npatches; |
544 |
|
|
while (pp-- > ms->patch) { |
545 |
|
|
if (pp->j2tri != NULL) |
546 |
|
|
free((void *)pp->j2tri); |
547 |
|
|
if (pp->j1tri != NULL) |
548 |
|
|
free((void *)pp->j1tri); |
549 |
|
|
if (pp->tri != NULL) |
550 |
|
|
free((void *)pp->tri); |
551 |
|
|
if (pp->uv != NULL) |
552 |
|
|
free((void *)pp->uv); |
553 |
|
|
if (pp->norm != NULL) |
554 |
|
|
free((void *)pp->norm); |
555 |
|
|
if (pp->xyz != NULL) |
556 |
|
|
free((void *)pp->xyz); |
557 |
|
|
} |
558 |
|
|
free((void *)ms->patch); |
559 |
|
|
} |
560 |
|
|
free((void *)ms); |
561 |
|
|
} |
562 |
|
|
|
563 |
|
|
|
564 |
|
|
void |
565 |
|
|
freemeshinst(o) /* free mesh instance */ |
566 |
|
|
OBJREC *o; |
567 |
|
|
{ |
568 |
|
|
if (o->os == NULL) |
569 |
|
|
return; |
570 |
|
|
freemesh((*(MESHINST *)o->os).msh); |
571 |
|
|
free((void *)o->os); |
572 |
|
|
o->os = NULL; |
573 |
|
|
} |