ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readmesh.c
Revision: 2.1
Committed: Tue Mar 11 17:08:55 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
First working version of new "mesh" primitive, obj2mesh converter

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Routines for reading a compiled mesh from a file
6 */
7
8 #include "standard.h"
9 #include "octree.h"
10 #include "object.h"
11 #include "mesh.h"
12
13 static char *meshfn; /* input file name */
14 static FILE *meshfp; /* mesh file pointer */
15 static int objsize; /* sizeof(OBJECT) from writer */
16
17
18 static void
19 mesherror(etyp, msg) /* mesh read error */
20 int etyp;
21 char *msg;
22 {
23 char msgbuf[128];
24
25 sprintf(msgbuf, "(%s): %s", meshfn, msg);
26 error(etyp, msgbuf);
27 }
28
29
30 static long
31 mgetint(siz) /* get a siz-byte integer */
32 int siz;
33 {
34 register long r;
35
36 r = getint(siz, meshfp);
37 if (feof(meshfp))
38 mesherror(USER, "truncated mesh file");
39 return(r);
40 }
41
42
43 static double
44 mgetflt() /* get a floating point number */
45 {
46 double r;
47
48 r = getflt(meshfp);
49 if (feof(meshfp))
50 mesherror(USER, "truncated mesh file");
51 return(r);
52 }
53
54
55 static OCTREE
56 getfullnode() /* get a set, return fullnode */
57 {
58 OBJECT set[MAXSET+1];
59 register int i;
60
61 if ((set[0] = mgetint(objsize)) > MAXSET)
62 mesherror(USER, "bad set in getfullnode");
63 for (i = 1; i <= set[0]; i++)
64 set[i] = mgetint(objsize);
65 return(fullnode(set));
66 }
67
68
69 static OCTREE
70 gettree() /* get a pre-ordered octree */
71 {
72 register OCTREE ot;
73 register int i;
74
75 switch (getc(meshfp)) {
76 case OT_EMPTY:
77 return(EMPTY);
78 case OT_FULL:
79 return(getfullnode());
80 case OT_TREE:
81 if ((ot = octalloc()) == EMPTY)
82 mesherror(SYSTEM, "out of tree space in readmesh");
83 for (i = 0; i < 8; i++)
84 octkid(ot, i) = gettree();
85 return(ot);
86 case EOF:
87 mesherror(USER, "truncated mesh octree");
88 default:
89 mesherror(USER, "damaged mesh octree");
90 }
91 }
92
93
94 static void
95 skiptree() /* skip octree on input */
96 {
97 register int i;
98
99 switch (getc(meshfp)) {
100 case OT_EMPTY:
101 return;
102 case OT_FULL:
103 for (i = mgetint(objsize)*objsize; i-- > 0; )
104 if (getc(meshfp) == EOF)
105 mesherror(USER, "truncated mesh octree");
106 return;
107 case OT_TREE:
108 for (i = 0; i < 8; i++)
109 skiptree();
110 return;
111 case EOF:
112 mesherror(USER, "truncated mesh octree");
113 default:
114 mesherror(USER, "damaged mesh octree");
115 }
116 }
117
118
119 static void
120 getpatch(pp) /* load a mesh patch */
121 register MESHPATCH *pp;
122 {
123 int flags;
124 int i, j;
125 /* vertex flags */
126 flags = mgetint(1);
127 if (!(flags & MT_V) || flags & ~(MT_V|MT_N|MT_UV))
128 mesherror(USER, "bad patch flags");
129 /* allocate vertices */
130 pp->nverts = mgetint(2);
131 if (pp->nverts <= 0 || pp->nverts > 256)
132 mesherror(USER, "bad number of patch vertices");
133 pp->xyz = (uint4 (*)[3])malloc(pp->nverts*3*sizeof(uint4));
134 if (pp->xyz == NULL)
135 goto nomem;
136 if (flags & MT_N) {
137 pp->norm = (int4 *)calloc(pp->nverts, sizeof(int4));
138 if (pp->norm == NULL)
139 goto nomem;
140 } else
141 pp->norm = NULL;
142 if (flags & MT_UV) {
143 pp->uv = (uint4 (*)[2])calloc(pp->nverts, 2*sizeof(uint4));
144 if (pp->uv == NULL)
145 goto nomem;
146 } else
147 pp->uv = NULL;
148 /* vertex xyz locations */
149 for (i = 0; i < pp->nverts; i++)
150 for (j = 0; j < 3; j++)
151 pp->xyz[i][j] = mgetint(4);
152 /* vertex normals */
153 if (flags & MT_N)
154 for (i = 0; i < pp->nverts; i++)
155 pp->norm[i] = mgetint(4);
156 /* uv coordinates */
157 if (flags & MT_UV)
158 for (i = 0; i < pp->nverts; i++)
159 for (j = 0; j < 2; j++)
160 pp->uv[i][j] = mgetint(4);
161 /* local triangles */
162 pp->ntris = mgetint(2);
163 if (pp->ntris < 0 || pp->ntris > 512)
164 mesherror(USER, "bad number of local triangles");
165 if (pp->ntris) {
166 pp->tri = (struct PTri *)malloc(pp->ntris *
167 sizeof(struct PTri));
168 if (pp->tri == NULL)
169 goto nomem;
170 for (i = 0; i < pp->ntris; i++) {
171 pp->tri[i].v1 = mgetint(1);
172 pp->tri[i].v2 = mgetint(1);
173 pp->tri[i].v3 = mgetint(1);
174 }
175 } else
176 pp->tri = NULL;
177 /* joiner triangles */
178 pp->nj1tris = mgetint(2);
179 if (pp->nj1tris < 0 || pp->nj1tris > 512)
180 mesherror(USER, "bad number of joiner triangles");
181 if (pp->nj1tris) {
182 pp->j1tri = (struct PJoin1 *)malloc(pp->nj1tris *
183 sizeof(struct PJoin1));
184 if (pp->j1tri == NULL)
185 goto nomem;
186 for (i = 0; i < pp->nj1tris; i++) {
187 pp->j1tri[i].v1j = mgetint(4);
188 pp->j1tri[i].v2 = mgetint(1);
189 pp->j1tri[i].v3 = mgetint(1);
190 }
191 } else
192 pp->j1tri = NULL;
193 /* double joiner triangles */
194 pp->nj2tris = mgetint(2);
195 if (pp->nj2tris < 0 || pp->nj2tris > 256)
196 mesherror(USER, "bad number of double joiner triangles");
197 if (pp->nj2tris) {
198 pp->j2tri = (struct PJoin2 *)malloc(pp->nj2tris *
199 sizeof(struct PJoin2));
200 if (pp->j2tri == NULL)
201 goto nomem;
202 for (i = 0; i < pp->nj2tris; i++) {
203 pp->j2tri[i].v1j = mgetint(4);
204 pp->j2tri[i].v2j = mgetint(4);
205 pp->j2tri[i].v3 = mgetint(1);
206 }
207 } else
208 pp->j2tri = NULL;
209 return;
210 nomem:
211 error(SYSTEM, "out of mesh memory in getpatch");
212 }
213
214
215 void
216 readmesh(mp, path, flags) /* read in mesh structures */
217 MESH *mp;
218 char *path;
219 int flags;
220 {
221 char sbuf[64];
222 int i;
223 /* check what's loaded */
224 flags &= (IO_INFO|IO_BOUNDS|IO_TREE|IO_SCENE) & ~mp->ldflags;
225 /* open input file */
226 if (path == NULL) {
227 meshfn = "standard input";
228 meshfp = stdin;
229 } else if ((meshfp = fopen(meshfn=path, "r")) == NULL) {
230 sprintf(errmsg, "cannot open mesh file \"%s\"", path);
231 error(SYSTEM, errmsg);
232 }
233 #ifdef MSDOS
234 setmode(fileno(meshfp), O_BINARY);
235 #endif
236 /* read header */
237 checkheader(meshfp, MESHFMT, flags&IO_INFO ? stdout : (FILE *)NULL);
238 /* read format number */
239 objsize = getint(2, meshfp) - MESHMAGIC;
240 if (objsize <= 0 || objsize > MAXOBJSIZ || objsize > sizeof(long))
241 mesherror(USER, "incompatible mesh format");
242 /* read boundaries */
243 if (flags & IO_BOUNDS) {
244 for (i = 0; i < 3; i++)
245 mp->mcube.cuorg[i] = atof(getstr(sbuf, meshfp));
246 mp->mcube.cusize = atof(getstr(sbuf, meshfp));
247 for (i = 0; i < 2; i++) {
248 mp->uvlim[0][i] = mgetflt();
249 mp->uvlim[1][i] = mgetflt();
250 }
251 } else {
252 for (i = 0; i < 4; i++)
253 getstr(sbuf, meshfp);
254 for (i = 0; i < 4; i++)
255 mgetflt();
256 }
257 /* read the octree */
258 if (flags & IO_TREE)
259 mp->mcube.cutree = gettree();
260 else if (flags & IO_SCENE)
261 skiptree();
262 /* read the patches */
263 if (flags & IO_SCENE) {
264 mp->npatches = mgetint(4);
265 mp->patch = (MESHPATCH *)calloc(mp->npatches,
266 sizeof(MESHPATCH));
267 if (mp->patch == NULL)
268 mesherror(SYSTEM, "out of patch memory");
269 for (i = 0; i < mp->npatches; i++)
270 getpatch(&mp->patch[i]);
271 }
272 /* clean up */
273 fclose(meshfp);
274 mp->ldflags |= flags;
275 }