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.7 by greg, Wed Mar 7 11:14:36 1990 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines