ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/tmesh2rad.c
Revision: 2.1
Committed: Tue Feb 15 15:58:03 1994 UTC (30 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# Content
1 /* Copyright (c) 1994 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Convert a triangle mesh into a Radiance description.
9 *
10 * Unlike most other converters, we have defined a file
11 * format for the mesh ourselves. It contains eight types,
12 * each of which is identified by a single letter. These are:
13 *
14 * # comment = a comment. Continues until end of line.
15 * v id Px Py Pz = a vertex. The id must be an integer.
16 * n Nx Ny Nz = a normal. Corresponds to most recent vertex.
17 * i Iu Iv = an index. Corresponds to most recent vertex.
18 * p picture = a picture. Used as a pattern what follows.
19 * m material = a material name. Used for what follows.
20 * t id1 id2 id3 = a triangle.
21 *
22 * Only the 't' type results in any output. The others merely set values
23 * to be used in generating triangles. If no picture or "p -" is given,
24 * there will be no pattern associated with the geometry. If no material
25 * or "m -" is given, no material will be associated. (Note that this
26 * only makes sense for a mesh which is to be put into an octree for
27 * instancing.) Using a pattern requires that each vertex have an
28 * associated index value for generating the colorpict primitive.
29 */
30
31 #include "standard.h"
32
33 #define CALNAME "tmesh.cal" /* the name of our auxiliary file */
34 #define PATNAME "tpat" /* triangle pattern name (reused) */
35 #define TEXNAME "tnor" /* triangle texture name (reused) */
36
37 #define V_DEFINED 01 /* this vertex is defined */
38 #define V_HASNORM 02 /* vertex has surface normal */
39 #define V_HASINDX 04 /* vertex has index */
40
41 typedef struct {
42 short flags; /* vertex flags, from above */
43 FVECT pos; /* location */
44 FVECT nor; /* normal */
45 float ndx[2]; /* picture index */
46 } VERTEX;
47
48 VERTEX *vlist = NULL; /* our vertex list */
49 int nverts = 0; /* number of vertices in our list */
50
51 typedef FLOAT BARYCCM[3][4];
52
53 #define novert(i) ((i)<0|(i)>=nverts||!(vlist[i].flags&V_DEFINED))
54
55 #define CHUNKSIZ 128 /* vertex allocation chunk size */
56
57 extern VERTEX *vnew(); /* allocate a vertex (never freed) */
58
59
60 main(argc, argv) /* read in T-mesh files and convert */
61 int argc;
62 char *argv[];
63 {
64 FILE *fp;
65 int i;
66
67 if (argc == 1)
68 convert("<stdin>", stdin);
69 else
70 for (i = 1; i < argc; i++) {
71 if ((fp = fopen(argv[i], "r")) == NULL) {
72 perror(argv[i]);
73 exit(1);
74 }
75 convert(argv[i], fp);
76 fclose(fp);
77 }
78 exit(0);
79 }
80
81
82 convert(fname, fp) /* convert a T-mesh */
83 char *fname;
84 FILE *fp;
85 {
86 char typ[2];
87 int id[3];
88 double vec[3];
89 char picfile[128];
90 char matname[64];
91 char *err;
92 register int c;
93 register VERTEX *lastv = NULL;
94
95 picfile[0] = '\0';
96 strcpy(matname, "void");
97
98 while (fscanf(fp, "%1s", typ) == 1)
99 switch (typ[0]) {
100 case 'v': /* vertex */
101 if (fscanf(fp, "%d %lf %lf %lf", &id[0],
102 &vec[0], &vec[1], &vec[2]) != 4)
103 syntax(fname, fp, "Bad vertex");
104 lastv = vnew(id[0], vec[0], vec[1], vec[2]);
105 break;
106 case 't': /* triangle */
107 if (fscanf(fp, "%d %d %d", &id[0], &id[1], &id[2]) != 3)
108 syntax(fname, fp, "Bad triangle");
109 if (novert(id[0]) | novert(id[1]) | novert(id[2]))
110 syntax(fname, fp, "Undefined triangle vertex");
111 triangle(picfile, matname, &vlist[id[0]],
112 &vlist[id[1]], &vlist[id[2]]);
113 break;
114 case 'n': /* surface normal */
115 if (lastv == NULL)
116 syntax(fname, fp, "No vertex for normal");
117 if (fscanf(fp, "%lf %lf %lf",
118 &vec[0], &vec[1], &vec[2]) != 3)
119 syntax(fname, fp, "Bad vertex normal");
120 lastv->nor[0] = vec[0];
121 lastv->nor[1] = vec[1];
122 lastv->nor[2] = vec[2];
123 if (normalize(lastv->nor) == 0.0)
124 syntax(fname, fp, "Zero vertex normal");
125 lastv->flags |= V_HASNORM;
126 break;
127 case 'i': /* index position */
128 if (lastv == NULL)
129 syntax(fname, fp, "No vertex for index");
130 if (fscanf(fp, "%lf %lf", &vec[0], &vec[1]) != 2)
131 syntax(fname, fp, "Bad index");
132 lastv->ndx[0] = vec[0];
133 lastv->ndx[1] = vec[1];
134 lastv->flags |= V_HASINDX;
135 break;
136 case 'm': /* material */
137 if (fscanf(fp, "%s", matname) != 1)
138 syntax(fname, fp, "Bad material");
139 if (matname[0] == '-' && !matname[1])
140 strcpy(matname, "void");
141 break;
142 case 'p': /* picture */
143 if (fscanf(fp, "%s", picfile) != 1)
144 syntax(fname, fp, "Bad pattern");
145 if (picfile[0] == '-' && !picfile[1])
146 picfile[0] = '\0';
147 break;
148 case '#': /* comment */
149 while ((c = getc(fp)) != EOF && c != '\n')
150 ;
151 break;
152 default:
153 syntax(fname, fp, "Unknown type");
154 break;
155 }
156 }
157
158
159 triangle(pn, mn, v1, v2, v3) /* put out a triangle */
160 char *pn, *mn;
161 register VERTEX *v1, *v2, *v3;
162 {
163 static int ntri = 0;
164 char *mod = mn;
165 BARYCCM bvecs;
166 /* compute barycentric coordinates */
167 if (v1->flags & v2->flags & v3->flags & (V_HASINDX|V_HASNORM))
168 if (comp_baryc(bvecs, v1->pos, v2->pos, v3->pos) < 0)
169 return;
170 /* put out texture (if any) */
171 if (v1->flags & v2->flags & v3->flags & V_HASNORM) {
172 printf("\n%s texfunc %s\n", mod, TEXNAME);
173 mod = TEXNAME;
174 printf("4 dx dy dz %s\n", CALNAME);
175 printf("0\n21\n");
176 put_baryc(bvecs);
177 printf("\t%f %f %f\n", v1->nor[0], v1->nor[1], v1->nor[2]);
178 printf("\t%f %f %f\n", v2->nor[0], v2->nor[1], v2->nor[2]);
179 printf("\t%f %f %f\n", v3->nor[0], v3->nor[1], v3->nor[2]);
180 }
181 /* put out pattern (if any) */
182 if (*pn && (v1->flags & v2->flags & v3->flags & V_HASINDX)) {
183 printf("\n%s colorpict %s\n", mod, PATNAME);
184 mod = PATNAME;
185 printf("7 noneg noneg noneg %s %s u v\n", pn, CALNAME);
186 printf("0\n18\n");
187 put_baryc(bvecs);
188 printf("\t%f %f\n", v1->ndx[0], v1->ndx[1]);
189 printf("\t%f %f\n", v2->ndx[0], v2->ndx[1]);
190 printf("\t%f %f\n", v3->ndx[0], v3->ndx[1]);
191 }
192 /* put out triangle */
193 printf("\n%s polygon t%d\n", mod, ++ntri);
194 printf("0\n0\n9\n");
195 printf("%18.12g %18.12g %18.12g\n", v1->pos[0],v1->pos[1],v1->pos[2]);
196 printf("%18.12g %18.12g %18.12g\n", v2->pos[0],v2->pos[1],v2->pos[2]);
197 printf("%18.12g %18.12g %18.12g\n", v3->pos[0],v3->pos[1],v3->pos[2]);
198 }
199
200
201 int
202 comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */
203 register BARYCCM bcm;
204 FVECT v1, v2, v3;
205 {
206 FLOAT *vt;
207 FVECT va, vab, vcb;
208 double d;
209 register int i, j;
210
211 for (j = 0; j < 3; j++) {
212 for (i = 0; i < 3; i++) {
213 vab[i] = v1[i] - v2[i];
214 vcb[i] = v3[i] - v2[i];
215 }
216 d = DOT(vcb,vcb);
217 if (d <= FTINY)
218 return(-1);
219 d = DOT(vcb,vab)/d;
220 for (i = 0; i < 3; i++)
221 va[i] = vab[i] - vcb[i]*d;
222 d = DOT(va,va);
223 if (d <= FTINY)
224 return(-1);
225 for (i = 0; i < 3; i++) {
226 va[i] /= d;
227 bcm[j][i] = va[i];
228 }
229 bcm[j][3] = -DOT(v2,va);
230 /* rotate vertices */
231 vt = v1;
232 v1 = v2;
233 v2 = v3;
234 v3 = vt;
235 }
236 return(0);
237 }
238
239
240 put_baryc(bcm) /* put barycentric coord. vectors */
241 register BARYCCM bcm;
242 {
243 register int i;
244
245 for (i = 0; i < 3; i++)
246 printf("%14.8f %14.8f %14.8f %14.8f\n",
247 bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]);
248 }
249
250
251 VERTEX *
252 vnew(id, x, y, z) /* create a new vertex */
253 register int id;
254 double x, y, z;
255 {
256 register int i;
257
258 if (id > nverts) { /* get some more */
259 i = nverts;
260 nverts = CHUNKSIZ*((id%CHUNKSIZ)+1);
261 if (vlist == NULL)
262 vlist = (VERTEX *)malloc(nverts*sizeof(VERTEX));
263 else
264 vlist = (VERTEX *)realloc((char *)vlist,
265 nverts*sizeof(VERTEX));
266 if (vlist == NULL) {
267 fprintf(stderr,
268 "Out of memory while allocating vertex %d\n", id);
269 exit(1);
270 }
271 while (i < nverts) /* clear them */
272 vlist[i++].flags = 0;
273 }
274 /* assign it */
275 vlist[id].pos[0] = x;
276 vlist[id].pos[1] = y;
277 vlist[id].pos[2] = z;
278 vlist[id].flags = V_DEFINED;
279 /* return it */
280 return(&vlist[id]);
281 }
282
283
284 syntax(fn, fp, er) /* report syntax error and exit */
285 char *fn;
286 register FILE *fp;
287 char *er;
288 {
289 extern long ftell();
290 register long cpos;
291 register int c;
292 int lineno;
293
294 if (fp == stdin)
295 fprintf(stderr, "%s: syntax error: %s\n", fn, er);
296 else {
297 cpos = ftell(fp);
298 fseek(fp, 0L, 0);
299 lineno = 1;
300 while (cpos-- > 0) {
301 if ((c = getc(fp)) == EOF)
302 break;
303 if (c == '\n')
304 lineno++;
305 }
306 fprintf(stderr, "%s: syntax error at line %d: %s\n",
307 fn, lineno, er);
308 }
309 exit(1);
310 }