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.1 by greg, Thu Feb 2 11:16:30 1989 UTC vs.
Revision 1.11 by greg, Thu Aug 2 13:42:56 1990 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines