ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/ot/wfconv.c
(Generate patch)

Comparing ray/src/ot/wfconv.c (file contents):
Revision 2.2 by greg, Wed Mar 12 04:59:04 2003 UTC vs.
Revision 2.11 by greg, Fri Jan 24 01:26:44 2014 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9   #include "copyright.h"
10   #include "standard.h"
11   #include "cvmesh.h"
12 + #include "triangulate.h"
13   #include <ctype.h>
14  
14 FVECT   *vlist;                 /* our vertex list */
15 int     nvs;                    /* number of vertices in our list */
16 FVECT   *vnlist;                /* vertex normal list */
17 int     nvns;
18 FLOAT   (*vtlist)[2];           /* map vertex list */
19 int     nvts;
20
15   typedef int     VNDX[3];        /* vertex index (point,map,normal) */
16  
17   #define CHUNKSIZ        1024    /* vertex allocation chunk size */
18  
19 < #define MAXARG          64      /* maximum # arguments in a statement */
19 > #define MAXARG          512     /* maximum # arguments in a statement */
20  
21 < char    *inpfile;               /* input file name */
22 < int     lineno;                 /* current line number */
23 < int     faceno;                 /* current face number */
21 > static FVECT    *vlist;         /* our vertex list */
22 > static int      nvs;            /* number of vertices in our list */
23 > static FVECT    *vnlist;        /* vertex normal list */
24 > static int      nvns;
25 > static RREAL    (*vtlist)[2];   /* map vertex list */
26 > static int      nvts;
27  
28 + static char     *inpfile;       /* input file name */
29 + static int      havemats;       /* materials available? */
30 + static char     material[256];  /* current material name */
31 + static char     group[256];     /* current group name */
32 + static int      lineno;         /* current line number */
33 + static int      faceno;         /* current face number */
34  
35 < wfreadobj(objfn)                /* read in .OBJ file and convert */
36 < char    *objfn;
35 > static int getstmt(char *av[MAXARG], FILE       *fp);
36 > static int cvtndx(VNDX  vi, char        *vs);
37 > static int putface(int  ac, char        **av);
38 > static OBJECT getmod(void);
39 > static int puttri(char  *v1, char       *v2, char       *v3);
40 > static void freeverts(void);
41 > static int newv(double  x, double       y, double       z);
42 > static int newvn(double x, double       y, double       z);
43 > static int newvt(double x, double       y);
44 > static void syntax(char *er);
45 >
46 >
47 > void
48 > wfreadobj(              /* read in .OBJ file and convert */
49 >        char    *objfn
50 > )
51   {
52          FILE    *fp;
53          char    *argv[MAXARG];
54          int     argc;
55          int     nstats, nunknown;
39        int     i;
56  
57          if (objfn == NULL) {
58                  inpfile = "<stdin>";
# Line 45 | Line 61 | char   *objfn;
61                  sprintf(errmsg, "cannot open \"%s\"", inpfile);
62                  error(USER, errmsg);
63          }
64 +        havemats = (nobjects > 0);
65          nstats = nunknown = 0;
66 +        material[0] = '\0';
67 +        group[0] = '\0';
68          lineno = 0; faceno = 0;
69                                          /* scan until EOF */
70 <        while (argc = getstmt(argv, fp)) {
70 >        while ( (argc = getstmt(argv, fp)) ) {
71                  switch (argv[0][0]) {
72                  case 'v':               /* vertex */
73                          switch (argv[0][1]) {
# Line 96 | Line 115 | char   *objfn;
115                                  break;
116                          }
117                          break;
118 <                case 'u':
119 <                        if (strcmp(argv[0], "usemtl") &&
120 <                                        strcmp(argv[0], "usemap"))
118 >                case 'u':                               /* usemtl/usemap */
119 >                        if (!strcmp(argv[0], "usemap"))
120 >                                break;
121 >                        if (strcmp(argv[0], "usemtl"))
122                                  goto unknown;
123 +                        if (argc > 1)
124 +                                strcpy(material, argv[1]);
125 +                        else
126 +                                material[0] = '\0';
127                          break;
128                  case 'o':               /* object name */
129                          if (argv[0][1])
130                                  goto unknown;
131                          break;
132 <                case 'g':               /* group name(s) */
132 >                case 'g':               /* group name */
133                          if (argv[0][1])
134                                  goto unknown;
135 +                        if (argc > 1)
136 +                                strcpy(group, argv[1]);
137 +                        else
138 +                                group[0] = '\0';
139                          break;
140                  case '#':               /* comment */
141                          break;
# Line 129 | Line 157 | char   *objfn;
157   }
158  
159  
160 < int
161 < getstmt(av, fp)                         /* read the next statement from fp */
162 < register char   *av[MAXARG];
163 < FILE    *fp;
160 > static int
161 > getstmt(                                /* read the next statement from fp */
162 >        char    *av[MAXARG],
163 >        FILE    *fp
164 > )
165   {
166 <        static char     sbuf[MAXARG*10];
167 <        register char   *cp;
168 <        register int    i;
166 >        static char     sbuf[MAXARG*16];
167 >        char    *cp;
168 >        int     i;
169  
170          do {
171                  if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL)
# Line 148 | Line 177 | FILE   *fp;
177                                          lineno++;
178                                  *cp++ = '\0';
179                          }
180 <                        if (!*cp || i >= MAXARG-1)
180 >                        if (!*cp)
181                                  break;
182 +                        if (i >= MAXARG-1) {
183 +                                sprintf(errmsg,
184 +                        "%s: too many arguments near line %d (limit %d)\n",
185 +                                        inpfile, lineno+1, MAXARG-1);
186 +                                break;
187 +                        }
188                          av[i++] = cp;
189                          while (*++cp && !isspace(*cp))
190                                  ;
# Line 162 | Line 197 | FILE   *fp;
197   }
198  
199  
200 < cvtndx(vi, vs)                          /* convert vertex string to index */
201 < register VNDX   vi;
202 < register char   *vs;
200 > static int
201 > cvtndx(                         /* convert vertex string to index */
202 >        VNDX    vi,
203 >        char    *vs
204 > )
205   {
206                                          /* get point */
207          vi[0] = atoi(vs);
# Line 208 | Line 245 | register char  *vs;
245          return(1);
246   }
247  
248 + /* determine dominant axis for triangle */
249 + static int
250 + dominant_axis(char *v1, char *v2, char *v3)
251 + {
252 +        int     v1i = atoi(v1), v2i = atoi(v2), v3i = atoi(v3);
253 +        FVECT   e1, e2, vn;
254 +        int     i, imax;
255  
256 < putface(ac, av)                         /* put out an N-sided polygon */
257 < int     ac;
258 < register char   **av;
256 >        VSUB(e1, vlist[v2i], vlist[v1i]);
257 >        VSUB(e2, vlist[v3i], vlist[v2i]);
258 >        VCROSS(vn, e1, e2);
259 >        for (i = imax = 2; i--; )
260 >                if (vn[i]*vn[i] > vn[imax]*vn[imax])
261 >                        imax = i;
262 >        return(vn[imax]*vn[imax] > FTINY ? imax : -1);
263 > }
264 >
265 > /* callback for triangle output from polygon */
266 > static int
267 > tri_out(const Vert2_list *tp, int a, int b, int c)
268   {
269 <        char            *cp;
270 <        register int    i;
269 >        return( puttri( ((char **)tp->p)[a],
270 >                        ((char **)tp->p)[b],
271 >                        ((char **)tp->p)[c] ) );
272 > }
273  
274 <        while (ac > 3) {                /* break into triangles */
275 <                if (!puttri(av[0], av[1], av[2]))
274 > static int
275 > putface(                                /* put out an N-sided polygon */
276 >        int     ac,
277 >        char    **av
278 > )
279 > {
280 >        Vert2_list      *poly = polyAlloc(ac);
281 >        int             i, ax, ay;
282 >
283 >        if (poly == NULL)
284 >                return(0);
285 >        poly->p = (void *)av;
286 >        for (i = ac-3; i >= 0; i--)     /* identify dominant axis */
287 >                if ((ax = dominant_axis(av[i], av[i+1], av[i+2])) >= 0)
288 >                        break;
289 >        if (ax < 0)
290 >                return(0);
291 >        if (++ax >= 3) ax = 0;
292 >        ay = ax;
293 >        if (++ay >= 3) ay = 0;
294 >        for (i = 0; i < ac; i++) {      /* convert to 2-D polygon */
295 >                VNDX    vi;
296 >                if (!cvtndx(vi, av[i])) {
297 >                        error(WARNING, "bad vertex reference");
298 >                        polyFree(poly);
299                          return(0);
300 <                ac--;                   /* remove vertex & rotate */
301 <                cp = av[0];
302 <                for (i = 0; i < ac-1; i++)
225 <                        av[i] = av[i+2];
226 <                av[i] = cp;
300 >                }
301 >                poly->v[i].mX = vlist[vi[0]][ax];
302 >                poly->v[i].mY = vlist[vi[0]][ay];
303          }
304 <        return(puttri(av[0], av[1], av[2]));
304 >                                        /* break into triangles & output */
305 >        if (!polyTriangulate(poly, &tri_out))
306 >                return(0);
307 >        polyFree(poly);
308 >        return(1);
309   }
310  
311  
312 < puttri(v1, v2, v3)                      /* convert a triangle */
313 < char    *v1, *v2, *v3;
312 > static OBJECT
313 > getmod(void)                            /* get current modifier ID */
314   {
315 +        char    *mnam;
316 +        OBJECT  mod;
317 +
318 +        if (!havemats)
319 +                return(OVOID);
320 +        if (!strcmp(material, VOIDID))
321 +                return(OVOID);
322 +        if (material[0])                /* prefer usemtl statements */
323 +                mnam = material;
324 +        else if (group[0])              /* else use group name */
325 +                mnam = group;
326 +        else
327 +                return(OVOID);
328 +        mod = modifier(mnam);
329 +        if (mod == OVOID) {
330 +                sprintf(errmsg, "%s: undefined modifier \"%s\"",
331 +                                inpfile, mnam);
332 +                error(USER, errmsg);
333 +        }
334 +        return(mod);
335 + }
336 +
337 +
338 + static int
339 + puttri(                 /* convert a triangle */
340 +        char    *v1,
341 +        char    *v2,
342 +        char    *v3
343 + )
344 + {
345          VNDX    v1i, v2i, v3i;
346 <        FLOAT   *v1c, *v2c, *v3c;
347 <        FLOAT   *v1n, *v2n, *v3n;
346 >        RREAL   *v1c, *v2c, *v3c;
347 >        RREAL   *v1n, *v2n, *v3n;
348          
349 <        if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3))
349 >        if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) {
350 >                error(WARNING, "bad vertex reference");
351                  return(0);
352 <
352 >        }
353          if (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0) {
354                  v1c = vtlist[v1i[1]];
355                  v2c = vtlist[v2i[1]];
# Line 253 | Line 364 | char   *v1, *v2, *v3;
364          } else
365                  v1n = v2n = v3n = NULL;
366          
367 <        return(cvtri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
367 >        return(cvtri(getmod(), vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]],
368                          v1n, v2n, v3n, v1c, v2c, v3c) >= 0);
369   }
370  
371  
372 < freeverts()                     /* free all vertices */
372 > static void
373 > freeverts(void)                 /* free all vertices */
374   {
375          if (nvs) {
376                  free((void *)vlist);
# Line 275 | Line 387 | freeverts()                    /* free all vertices */
387   }
388  
389  
390 < int
391 < newv(x, y, z)                   /* create a new vertex */
392 < double  x, y, z;
390 > static int
391 > newv(                   /* create a new vertex */
392 >        double  x,
393 >        double  y,
394 >        double  z
395 > )
396   {
397          if (!(nvs%CHUNKSIZ)) {          /* allocate next block */
398                  if (nvs == 0)
399                          vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
400                  else
401 <                        vlist = (FVECT *)realloc((char *)vlist,
401 >                        vlist = (FVECT *)realloc((void *)vlist,
402                                          (nvs+CHUNKSIZ)*sizeof(FVECT));
403                  if (vlist == NULL)
404                          error(SYSTEM, "out of memory in newv");
# Line 296 | Line 411 | double x, y, z;
411   }
412  
413  
414 < int
415 < newvn(x, y, z)                  /* create a new vertex normal */
416 < double  x, y, z;
414 > static int
415 > newvn(                  /* create a new vertex normal */
416 >        double  x,
417 >        double  y,
418 >        double  z
419 > )
420   {
421          if (!(nvns%CHUNKSIZ)) {         /* allocate next block */
422                  if (nvns == 0)
423                          vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT));
424                  else
425 <                        vnlist = (FVECT *)realloc((char *)vnlist,
425 >                        vnlist = (FVECT *)realloc((void *)vnlist,
426                                          (nvns+CHUNKSIZ)*sizeof(FVECT));
427                  if (vnlist == NULL)
428                          error(SYSTEM, "out of memory in newvn");
# Line 319 | Line 437 | double x, y, z;
437   }
438  
439  
440 < int
441 < newvt(x, y)                     /* create a new texture map vertex */
442 < double  x, y;
440 > static int
441 > newvt(                  /* create a new texture map vertex */
442 >        double  x,
443 >        double  y
444 > )
445   {
446          if (!(nvts%CHUNKSIZ)) {         /* allocate next block */
447                  if (nvts == 0)
448 <                        vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT));
448 >                        vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL));
449                  else
450 <                        vtlist = (FLOAT (*)[2])realloc((char *)vtlist,
451 <                                        (nvts+CHUNKSIZ)*2*sizeof(FLOAT));
450 >                        vtlist = (RREAL (*)[2])realloc((void *)vtlist,
451 >                                        (nvts+CHUNKSIZ)*2*sizeof(RREAL));
452                  if (vtlist == NULL)
453                          error(SYSTEM, "out of memory in newvt");
454          }
# Line 339 | Line 459 | double x, y;
459   }
460  
461  
462 < syntax(er)                      /* report syntax error and exit */
463 < char    *er;
462 > static void
463 > syntax(                 /* report syntax error and exit */
464 >        char    *er
465 > )
466   {
467          sprintf(errmsg, "%s: Wavefront syntax error near line %d: %s\n",
468                          inpfile, lineno, er);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines