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

Comparing ray/src/gen/gensurf.c (file contents):
Revision 1.2 by greg, Sun Sep 10 16:45:16 1989 UTC vs.
Revision 1.4 by greg, Wed Oct 18 18:49:09 1989 UTC

# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17   */
18  
19   #include  <stdio.h>
20 + #include  "fvect.h"
21  
22   #define  XNAME          "X_"                    /* x function name */
23   #define  YNAME          "Y_"                    /* y function name */
# Line 26 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27  
28   #define  FTINY          1e-7
29  
30 < #define  vertex(p)      printf(vformat, (p)[0], (p)[1], (p)[2])
30 > #define  ABS(x)         ((x)>=0 ? (x) : -(x))
31  
32 + #define  pvect(p)       printf(vformat, (p)[0], (p)[1], (p)[2])
33 +
34   char  vformat[] = "%15.9g %15.9g %15.9g\n";
35 + char  tsargs[] = "4 surf_dx surf_dy surf_dz surf.cal\n";
36 + char  texname[] = "Phong";
37  
38 < double  funvalue(), dist2(), fdot(), l_hermite(), argument();
38 > int  smooth = 0;                /* apply smoothing? */
39  
40 + char  *modname, *surfname;
41  
42 + double  funvalue(), l_hermite(), argument();
43 +
44 + typedef struct {
45 +        FVECT  p;       /* vertex position */
46 +        FVECT  n;       /* average normal */
47 + } POINT;
48 +
49 +
50   main(argc, argv)
51   int  argc;
52   char  *argv[];
53   {
54 <        static double  *xyz[4];
41 <        double  *row0, *row1, *dp;
42 <        double  v1[3], v2[3], vc1[3], vc2[3];
43 <        double  a1, a2;
54 >        POINT  *row0, *row1, *row2, *rp;
55          int  i, j, m, n;
56          char  stmp[256];
46        double  d;
47        register int  k;
57  
58          varset("PI", PI);
59          funset("hermite", 5, l_hermite);
# Line 57 | Line 66 | char  *argv[];
66                          scompile(NULL, argv[++i]);
67                  else if (!strcmp(argv[i], "-f"))
68                          fcompile(argv[++i]);
69 +                else if (!strcmp(argv[i], "-s"))
70 +                        smooth++;
71                  else
72                          goto userror;
73  
74 +        modname = argv[1];
75 +        surfname = argv[2];
76          sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]);
77          scompile(NULL, stmp);
78          sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]);
# Line 71 | Line 84 | char  *argv[];
84          if (m <= 0 || n <= 0)
85                  goto userror;
86  
87 <        row0 = (double *)malloc((n+1)*3*sizeof(double));
88 <        row1 = (double *)malloc((n+1)*3*sizeof(double));
89 <        if (row0 == NULL || row1 == NULL) {
87 >        row0 = (POINT *)malloc((n+3)*sizeof(POINT));
88 >        row1 = (POINT *)malloc((n+3)*sizeof(POINT));
89 >        row2 = (POINT *)malloc((n+3)*sizeof(POINT));
90 >        if (row0 == NULL || row1 == NULL || row2 == NULL) {
91                  fprintf(stderr, "%s: out of memory\n", argv[0]);
92                  quit(1);
93          }
94 <
94 >        row0++; row1++; row2++;
95 >                                                /* print header */
96          printhead(argc, argv);
97 <
98 <        comprow(0.0, row1, n);                  /* compute zeroeth row */
99 <
97 >                                                /* initialize */
98 >        comprow(-1.0/m, row0, n);
99 >        comprow(0.0, row1, n);
100 >        comprow(1.0/m, row2, n);
101 >        compnorms(row0, row1, row2, n);
102 >                                                /* for each row */
103          for (i = 0; i < m; i++) {
104                                                  /* compute next row */
105 <                dp = row0;
105 >                rp = row0;
106                  row0 = row1;
107 <                row1 = dp;
108 <                comprow((double)(i+1)/m, row1, n);
107 >                row1 = row2;
108 >                row2 = rp;
109 >                comprow((double)(i+2)/m, row2, n);
110 >                compnorms(row0, row1, row2, n);
111  
112                  for (j = 0; j < n; j++) {
113 <                                                        /* get vertices */
114 <                        xyz[0] = row0 + 3*j;
115 <                        xyz[1] = row1 + 3*j;
116 <                        xyz[2] = xyz[0] + 3;
117 <                        xyz[3] = xyz[1] + 3;
118 <                                                        /* rotate vertices */
119 <                        if (dist2(xyz[0],xyz[3]) < dist2(xyz[1],xyz[2])-FTINY) {
100 <                                dp = xyz[0];
101 <                                xyz[0] = xyz[1];
102 <                                xyz[1] = xyz[3];
103 <                                xyz[3] = xyz[2];
104 <                                xyz[2] = dp;
105 <                        }
106 <                                                        /* get normals */
107 <                        for (k = 0; k < 3; k++) {
108 <                                v1[k] = xyz[1][k] - xyz[0][k];
109 <                                v2[k] = xyz[2][k] - xyz[0][k];
110 <                        }
111 <                        fcross(vc1, v1, v2);
112 <                        a1 = fdot(vc1, vc1);
113 <                        for (k = 0; k < 3; k++) {
114 <                                v1[k] = xyz[2][k] - xyz[3][k];
115 <                                v2[k] = xyz[1][k] - xyz[3][k];
116 <                        }
117 <                        fcross(vc2, v1, v2);
118 <                        a2 = fdot(vc2, vc2);
119 <                                                        /* check coplanar */
120 <                        if (a1 > FTINY*FTINY && a2 > FTINY*FTINY) {
121 <                                d = fdot(vc1, vc2);
122 <                                if (d*d/a1/a2 >= 1.0-FTINY*FTINY) {
123 <                                        if (d > 0.0) {  /* coplanar */
124 <                                                printf(
125 <                                                "\n%s polygon %s.%d.%d\n",
126 <                                                argv[1], argv[2], i+1, j+1);
127 <                                                printf("0\n0\n12\n");
128 <                                                vertex(xyz[0]);
129 <                                                vertex(xyz[1]);
130 <                                                vertex(xyz[3]);
131 <                                                vertex(xyz[2]);
132 <                                        }               /* else overlapped */
133 <                                        continue;
134 <                                }                       /* else bent */
135 <                        }
136 <                                                        /* check triangles */
137 <                        if (a1 > FTINY*FTINY) {
138 <                                printf("\n%s polygon %s.%da%d\n",
139 <                                        argv[1], argv[2], i+1, j+1);
140 <                                printf("0\n0\n9\n");
141 <                                vertex(xyz[0]);
142 <                                vertex(xyz[1]);
143 <                                vertex(xyz[2]);
144 <                        }
145 <                        if (a2 > FTINY*FTINY) {
146 <                                printf("\n%s polygon %s.%db%d\n",
147 <                                        argv[1], argv[2], i+1, j+1);
148 <                                printf("0\n0\n9\n");
149 <                                vertex(xyz[2]);
150 <                                vertex(xyz[1]);
151 <                                vertex(xyz[3]);
152 <                        }
113 >                                                        /* put polygons */
114 >                        if ((i+j) & 1)
115 >                                putsquare(&row0[j], &row1[j],
116 >                                                &row0[j+1], &row1[j+1]);
117 >                        else
118 >                                putsquare(&row1[j], &row1[j+1],
119 >                                                &row0[j], &row0[j+1]);
120                  }
121          }
122  
# Line 157 | Line 124 | char  *argv[];
124  
125   userror:
126          fprintf(stderr, "Usage: %s material name ", argv[0]);
127 <        fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-e expr] [-f file]\n");
127 >        fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-e expr][-f file]\n");
128          quit(1);
129   }
130  
131  
132 + putsquare(p0, p1, p2, p3)               /* put out a square */
133 + POINT  *p0, *p1, *p2, *p3;
134 + {
135 +        static int  nout = 0;
136 +        FVECT  norm[4];
137 +        int  axis;
138 +        FVECT  v1, v2, vc1, vc2;
139 +        int  ok1, ok2;
140 +                                        /* compute exact normals */
141 +        fvsum(v1, p1->p, p0->p, -1.0);
142 +        fvsum(v2, p2->p, p0->p, -1.0);
143 +        fcross(vc1, v1, v2);
144 +        ok1 = normalize(vc1) != 0.0;
145 +        fvsum(v1, p2->p, p3->p, -1.0);
146 +        fvsum(v2, p1->p, p3->p, -1.0);
147 +        fcross(vc2, v1, v2);
148 +        ok2 = normalize(vc2) != 0.0;
149 +        if (!(ok1 | ok2))
150 +                return;
151 +                                        /* compute normal interpolation */
152 +        axis = norminterp(norm, p0, p1, p2, p3);
153 +
154 +                                        /* put out quadrilateral? */
155 +        if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
156 +                printf("\n%s ", modname);
157 +                if (axis != -1) {
158 +                        printf("texfunc %s\n", texname);
159 +                        printf(tsargs);
160 +                        printf("0\n13\t%d\n", axis);
161 +                        pvect(norm[0]);
162 +                        pvect(norm[1]);
163 +                        pvect(norm[2]);
164 +                        fvsum(v1, norm[3], vc1, -0.5);
165 +                        fvsum(v1, v1, vc2, -0.5);
166 +                        pvect(v1);
167 +                        printf("\n%s ", texname);
168 +                }
169 +                printf("polygon %s.%d\n", surfname, ++nout);
170 +                printf("0\n0\n12\n");
171 +                pvect(p0->p);
172 +                pvect(p1->p);
173 +                pvect(p3->p);
174 +                pvect(p2->p);
175 +                return;
176 +        }
177 +                                        /* put out triangles? */
178 +        if (ok1) {
179 +                printf("\n%s ", modname);
180 +                if (axis != -1) {
181 +                        printf("texfunc %s\n", texname);
182 +                        printf(tsargs);
183 +                        printf("0\n13\t%d\n", axis);
184 +                        pvect(norm[0]);
185 +                        pvect(norm[1]);
186 +                        pvect(norm[2]);
187 +                        fvsum(v1, norm[3], vc1, -1.0);
188 +                        pvect(v1);
189 +                        printf("\n%s ", texname);
190 +                }
191 +                printf("polygon %s.%d\n", surfname, ++nout);
192 +                printf("0\n0\n9\n");
193 +                pvect(p0->p);
194 +                pvect(p1->p);
195 +                pvect(p2->p);
196 +        }
197 +        if (ok2) {
198 +                printf("\n%s ", modname);
199 +                if (axis != -1) {
200 +                        printf("texfunc %s\n", texname);
201 +                        printf(tsargs);
202 +                        printf("0\n13\t%d\n", axis);
203 +                        pvect(norm[0]);
204 +                        pvect(norm[1]);
205 +                        pvect(norm[2]);
206 +                        fvsum(v2, norm[3], vc2, -1.0);
207 +                        pvect(v2);
208 +                        printf("\n%s ", texname);
209 +                }
210 +                printf("polygon %s.%d\n", surfname, ++nout);
211 +                printf("0\n0\n9\n");
212 +                pvect(p2->p);
213 +                pvect(p1->p);
214 +                pvect(p3->p);
215 +        }
216 + }
217 +
218 +
219   comprow(s, row, siz)                    /* compute row of values */
220   double  s;
221 < register double  *row;
221 > register POINT  *row;
222   int  siz;
223   {
224 <        double  st[2], step;
225 <
224 >        double  st[2];
225 >        register int  i;
226 >                                        /* compute one past each end */
227          st[0] = s;
228 <        st[1] = 0.0;
229 <        step = 1.0 / siz;
228 >        for (i = -1; i <= siz+1; i++) {
229 >                st[1] = (double)i/siz;
230 >                row[i].p[0] = funvalue(XNAME, 2, st);
231 >                row[i].p[1] = funvalue(YNAME, 2, st);
232 >                row[i].p[2] = funvalue(ZNAME, 2, st);
233 >        }
234 > }
235 >
236 >
237 > compnorms(r0, r1, r2, siz)              /* compute row of averaged normals */
238 > register POINT  *r0, *r1, *r2;
239 > int  siz;
240 > {
241 >        FVECT  v1, v2, vc;
242 >        register int  i;
243 >
244 >        if (!smooth)                    /* not needed if no smoothing */
245 >                return;
246 >                                        /* compute middle points */
247          while (siz-- >= 0) {
248 <                *row++ = funvalue(XNAME, 2, st);
249 <                *row++ = funvalue(YNAME, 2, st);
250 <                *row++ = funvalue(ZNAME, 2, st);
251 <                st[1] += step;
248 >                fvsum(v1, r2[0].p, r1[0].p, -1.0);
249 >                fvsum(v2, r1[1].p, r1[0].p, -1.0);
250 >                fcross(r1[0].n, v1, v2);
251 >                fvsum(v1, r0[0].p, r1[0].p, -1.0);
252 >                fcross(vc, v2, v1);
253 >                fvsum(r1[0].n, r1[0].n, vc, 1.0);
254 >                fvsum(v2, r1[-1].p, r1[0].p, -1.0);
255 >                fcross(vc, v1, v2);
256 >                fvsum(r1[0].n, r1[0].n, vc, 1.0);
257 >                fvsum(v1, r2[0].p, r1[0].p, -1.0);
258 >                fcross(vc, v2, v1);
259 >                fvsum(r1[0].n, r1[0].n, vc, 1.0);
260 >                normalize(r1[0].n);
261 >                r0++; r1++; r2++;
262          }
263 + }
264 +
265 +
266 + int
267 + norminterp(resmat, p0, p1, p2, p3)      /* compute normal interpolation */
268 + register FVECT  resmat[4];
269 + POINT  *p0, *p1, *p2, *p3;
270 + {
271 + #define u  ((ax+1)%3)
272 + #define v  ((ax+2)%3)
273 +
274 +        register int  ax;
275 +        double  eqnmat[4][4];
276 +        FVECT  v1;
277 +        register int  i, j;
278 +
279 +        if (!smooth)                    /* no interpolation if no smoothing */
280 +                return(-1);
281 +                                        /* find dominant axis */
282 +        VCOPY(v1, p0->n);
283 +        fvsum(v1, v1, p1->n, 1.0);
284 +        fvsum(v1, v1, p2->n, 1.0);
285 +        fvsum(v1, v1, p3->n, 1.0);
286 +        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
287 +        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
288 +                                        /* assign equation matrix */
289 +        eqnmat[0][0] = p0->p[u]*p0->p[v];
290 +        eqnmat[0][1] = p0->p[u];
291 +        eqnmat[0][2] = p0->p[v];
292 +        eqnmat[0][3] = 1.0;
293 +        eqnmat[1][0] = p1->p[u]*p1->p[v];
294 +        eqnmat[1][1] = p1->p[u];
295 +        eqnmat[1][2] = p1->p[v];
296 +        eqnmat[1][3] = 1.0;
297 +        eqnmat[2][0] = p2->p[u]*p2->p[v];
298 +        eqnmat[2][1] = p2->p[u];
299 +        eqnmat[2][2] = p2->p[v];
300 +        eqnmat[2][3] = 1.0;
301 +        eqnmat[3][0] = p3->p[u]*p3->p[v];
302 +        eqnmat[3][1] = p3->p[u];
303 +        eqnmat[3][2] = p3->p[v];
304 +        eqnmat[3][3] = 1.0;
305 +                                        /* invert matrix (solve system) */
306 +        if (!invmat(eqnmat, eqnmat))
307 +                return(-1);                     /* no solution */
308 +                                        /* compute result matrix */
309 +        for (j = 0; j < 4; j++)
310 +                for (i = 0; i < 3; i++)
311 +                        resmat[j][i] =  eqnmat[j][0]*p0->n[i] +
312 +                                        eqnmat[j][1]*p1->n[i] +
313 +                                        eqnmat[j][2]*p2->n[i] +
314 +                                        eqnmat[j][3]*p3->n[i];
315 +        return(ax);
316 +
317 + #undef u
318 + #undef v
319 + }
320 +
321 +
322 + /*
323 + * invmat - computes the inverse of mat into inverse.  Returns 1
324 + * if there exists an inverse, 0 otherwise.  It uses Gaussian Elimination
325 + * method.
326 + */
327 +
328 + invmat(inverse,mat)
329 + double mat[4][4],inverse[4][4];
330 + {
331 + #define SWAP(a,b,t) (t=a,a=b,b=t)
332 +
333 +        double  m4tmp[4][4];
334 +        register int i,j,k;
335 +        register double temp;
336 +
337 +        bcopy(mat, m4tmp, sizeof(m4tmp));
338 +                                        /* set inverse to identity */
339 +        for (i = 0; i < 4; i++)
340 +                for (j = 0; j < 4; j++)
341 +                        inverse[i][j] = i==j ? 1.0 : 0.0;
342 +
343 +        for(i = 0; i < 4; i++) {
344 +                /* Look for raw with largest pivot and swap raws */
345 +                temp = FTINY; j = -1;
346 +                for(k = i; k < 4; k++)
347 +                        if(ABS(m4tmp[k][i]) > temp) {
348 +                                temp = ABS(m4tmp[k][i]);
349 +                                j = k;
350 +                                }
351 +                if(j == -1)     /* No replacing raw -> no inverse */
352 +                        return(0);
353 +                if (j != i)
354 +                        for(k = 0; k < 4; k++) {
355 +                                SWAP(m4tmp[i][k],m4tmp[j][k],temp);
356 +                                SWAP(inverse[i][k],inverse[j][k],temp);
357 +                                }
358 +
359 +                temp = m4tmp[i][i];
360 +                for(k = 0; k < 4; k++) {
361 +                        m4tmp[i][k] /= temp;
362 +                        inverse[i][k] /= temp;
363 +                        }
364 +                for(j = 0; j < 4; j++) {
365 +                        if(j != i) {
366 +                                temp = m4tmp[j][i];
367 +                                for(k = 0; k < 4; k++) {
368 +                                        m4tmp[j][k] -= m4tmp[i][k]*temp;
369 +                                        inverse[j][k] -= inverse[i][k]*temp;
370 +                                        }
371 +                                }
372 +                        }
373 +                }
374 +        return(1);
375 +
376 + #undef SWAP
377   }
378  
379  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines