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

Comparing ray/src/cv/tmesh2rad.c (file contents):
Revision 2.4 by greg, Tue Mar 22 08:54:23 1994 UTC vs.
Revision 2.16 by greg, Mon Nov 10 19:08:18 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Convert a trianglular mesh into a Radiance description.
6   *
# Line 32 | Line 29 | static char SCCSid[] = "$SunId$ LBL";
29   * It is not necessary for the normal vectors to have unit length.
30   */
31  
32 < #include "standard.h"
32 > #include <stdlib.h>
33 > #include <stdio.h>
34  
35 + #include "rtmath.h"
36 +
37 + #include "tmesh.h"
38 +
39   #define VOIDID          "void"          /* this is defined in object.h */
40  
39 #define CALNAME         "tmesh.cal"     /* the name of our auxiliary file */
41   #define PATNAME         "T-pat"         /* triangle pattern name (reused) */
42   #define TEXNAME         "T-nor"         /* triangle texture name (reused) */
43  
# Line 54 | Line 55 | typedef struct {
55   VERTEX  *vlist = NULL;          /* our vertex list */
56   int     nverts = 0;             /* number of vertices in our list */
57  
58 < typedef FLOAT   BARYCCM[3][4];
58 > #define novert(i)       (((i)<0)|((i)>=nverts) || !(vlist[i].flags&V_DEFINED))
59  
59 #define novert(i)       ((i)<0|(i)>=nverts || !(vlist[i].flags&V_DEFINED))
60
60   #define CHUNKSIZ        128     /* vertex allocation chunk size */
61  
62   extern VERTEX   *vnew();        /* allocate a vertex (never freed) */
# Line 66 | Line 65 | char   *defmat = VOIDID;       /* default (starting) material
65   char    *defpat = "";           /* default (starting) picture name */
66   char    *defobj = "T";          /* default (starting) object name */
67  
68 + void convert(char *fname, FILE *fp);
69 + void triangle(char *pn,char *mod,char *obj, VERTEX *v1,VERTEX *v2,VERTEX *v3);
70 + VERTEX *vnew(int id, double x, double y, double z);
71 + void syntax(char *fn, FILE *fp, char *er);
72  
73 < main(argc, argv)                /* read in T-mesh files and convert */
74 < int     argc;
75 < char    *argv[];
73 >
74 > int
75 > main(           /* read in T-mesh files and convert */
76 >        int     argc,
77 >        char    *argv[]
78 > )
79   {
80          FILE    *fp;
81          int     i;
# Line 87 | Line 93 | char   *argv[];
93                          break;
94                  default:
95                          fprintf(stderr,
96 <                        "Usage: %s [-o obj][-m mat][-p pic] [file ..]\n",
96 >                        "Usage: %s [-o obj][-m mat][-p hdr] [file ..]\n",
97                                          argv[0]);
98                          exit(1);
99                  }
# Line 106 | Line 112 | char   *argv[];
112   }
113  
114  
115 < convert(fname, fp)              /* convert a T-mesh */
116 < char    *fname;
117 < FILE    *fp;
115 > void
116 > convert(                /* convert a T-mesh */
117 >        char    *fname,
118 >        FILE    *fp
119 > )
120   {
121          char    typ[4];
122          int     id[3];
# Line 199 | Line 207 | FILE   *fp;
207   }
208  
209  
210 < triangle(pn, mod, obj, v1, v2, v3)      /* put out a triangle */
211 < char    *pn, *mod, *obj;
212 < register VERTEX *v1, *v2, *v3;
210 > void
211 > triangle(       /* put out a triangle */
212 >        char    *pn,
213 >        char    *mod,
214 >        char    *obj,
215 >        register VERTEX *v1,
216 >        register VERTEX *v2,
217 >        register VERTEX *v3
218 > )
219   {
220 +        static char     vfmt[] = "%18.12g %18.12g %18.12g\n";
221          static int      ntri = 0;
222 +        int             flatness = ISFLAT;
223          BARYCCM bvecs;
224 +        RREAL   bvm[3][3];
225 +        register int    i;
226                                          /* compute barycentric coordinates */
227          if (v1->flags & v2->flags & v3->flags & (V_HASINDX|V_HASNORM))
228 <                if (comp_baryc(bvecs, v1->pos, v2->pos, v3->pos) < 0)
228 >                if (comp_baryc(&bvecs, v1->pos, v2->pos, v3->pos) < 0)
229                          return;
230 <                                        /* put out texture (if any) */
230 >                                        /* check flatness */
231          if (v1->flags & v2->flags & v3->flags & V_HASNORM) {
232 +                flatness = flat_tri(v1->pos, v2->pos, v3->pos,
233 +                                        v1->nor, v2->nor, v3->nor);
234 +                if (flatness == DEGEN)
235 +                        return;
236 +        }
237 +                                        /* put out texture (if any) */
238 +        if (flatness == ISBENT || flatness == RVBENT) {
239                  printf("\n%s texfunc %s\n", mod, TEXNAME);
240                  mod = TEXNAME;
241 <                printf("4 dx dy dz %s\n", CALNAME);
242 <                printf("0\n21\n");
243 <                put_baryc(bvecs);
244 <                printf("\t%14.12g %14.12g %14.12g\n",
245 <                                v1->nor[0], v2->nor[0], v3->nor[0]);
246 <                printf("\t%14.12g %14.12g %14.12g\n",
247 <                                v1->nor[1], v2->nor[1], v3->nor[1]);
248 <                printf("\t%14.12g %14.12g %14.12g\n",
224 <                                v1->nor[2], v2->nor[2], v3->nor[2]);
241 >                printf("4 dx dy dz %s\n", TCALNAME);
242 >                printf("0\n");
243 >                for (i = 0; i < 3; i++) {
244 >                        bvm[i][0] = v1->nor[i];
245 >                        bvm[i][1] = v2->nor[i];
246 >                        bvm[i][2] = v3->nor[i];
247 >                }
248 >                put_baryc(&bvecs, bvm, 3);
249          }
250                                          /* put out pattern (if any) */
251          if (*pn && (v1->flags & v2->flags & v3->flags & V_HASINDX)) {
252                  printf("\n%s colorpict %s\n", mod, PATNAME);
253                  mod = PATNAME;
254 <                printf("7 noneg noneg noneg %s %s u v\n", pn, CALNAME);
255 <                printf("0\n18\n");
256 <                put_baryc(bvecs);
257 <                printf("\t%f %f %f\n", v1->ndx[0], v2->ndx[0], v3->ndx[0]);
258 <                printf("\t%f %f %f\n", v1->ndx[1], v2->ndx[1], v3->ndx[1]);
254 >                printf("7 noneg noneg noneg %s %s u v\n", pn, TCALNAME);
255 >                printf("0\n");
256 >                for (i = 0; i < 2; i++) {
257 >                        bvm[i][0] = v1->ndx[i];
258 >                        bvm[i][1] = v2->ndx[i];
259 >                        bvm[i][2] = v3->ndx[i];
260 >                }
261 >                put_baryc(&bvecs, bvm, 2);
262          }
263 <                                        /* put out triangle */
263 >                                        /* put out (reversed) triangle */
264          printf("\n%s polygon %s.%d\n", mod, obj, ++ntri);
265          printf("0\n0\n9\n");
266 <        printf("%18.12g %18.12g %18.12g\n", v1->pos[0],v1->pos[1],v1->pos[2]);
267 <        printf("%18.12g %18.12g %18.12g\n", v2->pos[0],v2->pos[1],v2->pos[2]);
268 <        printf("%18.12g %18.12g %18.12g\n", v3->pos[0],v3->pos[1],v3->pos[2]);
269 < }
270 <
271 <
272 < int
273 < comp_baryc(bcm, v1, v2, v3)             /* compute barycentric vectors */
247 < register BARYCCM        bcm;
248 < FLOAT   *v1, *v2, *v3;
249 < {
250 <        FLOAT   *vt;
251 <        FVECT   va, vab, vcb;
252 <        double  d;
253 <        register int    i, j;
254 <
255 <        for (j = 0; j < 3; j++) {
256 <                for (i = 0; i < 3; i++) {
257 <                        vab[i] = v1[i] - v2[i];
258 <                        vcb[i] = v3[i] - v2[i];
259 <                }
260 <                d = DOT(vcb,vcb);
261 <                if (d <= FTINY)
262 <                        return(-1);
263 <                d = DOT(vcb,vab)/d;
264 <                for (i = 0; i < 3; i++)
265 <                        va[i] = vab[i] - vcb[i]*d;
266 <                d = DOT(va,va);
267 <                if (d <= FTINY)
268 <                        return(-1);
269 <                for (i = 0; i < 3; i++) {
270 <                        va[i] /= d;
271 <                        bcm[j][i] = va[i];
272 <                }
273 <                bcm[j][3] = -DOT(v2,va);
274 <                                        /* rotate vertices */
275 <                vt = v1;
276 <                v1 = v2;
277 <                v2 = v3;
278 <                v3 = vt;
266 >        if (flatness == RVFLAT || flatness == RVBENT) {
267 >                printf(vfmt, v3->pos[0],v3->pos[1],v3->pos[2]);
268 >                printf(vfmt, v2->pos[0],v2->pos[1],v2->pos[2]);
269 >                printf(vfmt, v1->pos[0],v1->pos[1],v1->pos[2]);
270 >        } else {
271 >                printf(vfmt, v1->pos[0],v1->pos[1],v1->pos[2]);
272 >                printf(vfmt, v2->pos[0],v2->pos[1],v2->pos[2]);
273 >                printf(vfmt, v3->pos[0],v3->pos[1],v3->pos[2]);
274          }
280        return(0);
275   }
276  
277  
284 put_baryc(bcm)                          /* put barycentric coord. vectors */
285 register BARYCCM        bcm;
286 {
287        register int    i;
288
289        for (i = 0; i < 3; i++)
290                printf("%14.8f %14.8f %14.8f %14.8f\n",
291                                bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
292 }
293
294
278   VERTEX *
279 < vnew(id, x, y, z)                       /* create a new vertex */
280 < register int    id;
281 < double  x, y, z;
279 > vnew(                   /* create a new vertex */
280 >        register int    id,
281 >        double  x,
282 >        double  y,
283 >        double  z
284 > )
285   {
286          register int    i;
287  
# Line 305 | Line 291 | double x, y, z;
291                  if (vlist == NULL)
292                          vlist = (VERTEX *)malloc(nverts*sizeof(VERTEX));
293                  else
294 <                        vlist = (VERTEX *)realloc((char *)vlist,
294 >                        vlist = (VERTEX *)realloc((void *)vlist,
295                                          nverts*sizeof(VERTEX));
296                  if (vlist == NULL) {
297                          fprintf(stderr,
# Line 325 | Line 311 | double x, y, z;
311   }
312  
313  
314 < syntax(fn, fp, er)                      /* report syntax error and exit */
315 < char    *fn;
316 < register FILE   *fp;
317 < char    *er;
314 > void
315 > syntax(                 /* report syntax error and exit */
316 >        char    *fn,
317 >        register FILE   *fp,
318 >        char    *er
319 > )
320   {
321          extern long     ftell();
322          register long   cpos;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines