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.8 by greg, Sat May 26 19:44:40 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];
39 <        double  *row0, *row1, *dp;
40 <        double  v1[3], v2[3], vc1[3], vc2[3];
41 <        double  a1, a2;
49 >        POINT  *row0, *row1, *row2, *rp;
50          int  i, j, m, n;
51          char  stmp[256];
44        double  d;
45        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 55 | 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 69 | 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) {
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 <                        }
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 155 | 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 >        int  end;
223 >        register int  i;
224 >        
225 >        if (smooth) {
226 >                i = -1;                 /* compute one past each end */
227 >                end = siz+1;
228 >        } else {
229 >                if (s < -FTINY || s > 1.0+FTINY)
230 >                        return;
231 >                i = 0;
232 >                end = siz;
233 >        }
234          st[0] = s;
235 <        st[1] = 0.0;
236 <        step = 1.0 / siz;
235 >        while (i <= end) {
236 >                st[1] = (double)i/siz;
237 >                row[i].p[0] = funvalue(XNAME, 2, st);
238 >                row[i].p[1] = funvalue(YNAME, 2, st);
239 >                row[i].p[2] = funvalue(ZNAME, 2, st);
240 >                i++;
241 >        }
242 > }
243 >
244 >
245 > compnorms(r0, r1, r2, siz)              /* compute row of averaged normals */
246 > register POINT  *r0, *r1, *r2;
247 > int  siz;
248 > {
249 >        FVECT  v1, v2, vc;
250 >        register int  i;
251 >
252 >        if (!smooth)                    /* not needed if no smoothing */
253 >                return;
254 >                                        /* compute middle points */
255          while (siz-- >= 0) {
256 <                *row++ = funvalue(XNAME, 2, st);
257 <                *row++ = funvalue(YNAME, 2, st);
258 <                *row++ = funvalue(ZNAME, 2, st);
259 <                st[1] += step;
256 >                fvsum(v1, r2[0].p, r1[0].p, -1.0);
257 >                fvsum(v2, r1[1].p, r1[0].p, -1.0);
258 >                fcross(r1[0].n, v1, v2);
259 >                fvsum(v1, r0[0].p, r1[0].p, -1.0);
260 >                fcross(vc, v2, v1);
261 >                fvsum(r1[0].n, r1[0].n, vc, 1.0);
262 >                fvsum(v2, r1[-1].p, r1[0].p, -1.0);
263 >                fcross(vc, v1, v2);
264 >                fvsum(r1[0].n, r1[0].n, vc, 1.0);
265 >                fvsum(v1, r2[0].p, r1[0].p, -1.0);
266 >                fcross(vc, v2, v1);
267 >                fvsum(r1[0].n, r1[0].n, vc, 1.0);
268 >                normalize(r1[0].n);
269 >                r0++; r1++; r2++;
270          }
271   }
272  
273  
274 + int
275 + norminterp(resmat, p0, p1, p2, p3)      /* compute normal interpolation */
276 + register FVECT  resmat[4];
277 + POINT  *p0, *p1, *p2, *p3;
278 + {
279 + #define u  ((ax+1)%3)
280 + #define v  ((ax+2)%3)
281 +
282 +        register int  ax;
283 +        double  eqnmat[4][4];
284 +        FVECT  v1;
285 +        register int  i, j;
286 +
287 +        if (!smooth)                    /* no interpolation if no smoothing */
288 +                return(-1);
289 +                                        /* find dominant axis */
290 +        VCOPY(v1, p0->n);
291 +        fvsum(v1, v1, p1->n, 1.0);
292 +        fvsum(v1, v1, p2->n, 1.0);
293 +        fvsum(v1, v1, p3->n, 1.0);
294 +        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
295 +        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
296 +                                        /* assign equation matrix */
297 +        eqnmat[0][0] = p0->p[u]*p0->p[v];
298 +        eqnmat[0][1] = p0->p[u];
299 +        eqnmat[0][2] = p0->p[v];
300 +        eqnmat[0][3] = 1.0;
301 +        eqnmat[1][0] = p1->p[u]*p1->p[v];
302 +        eqnmat[1][1] = p1->p[u];
303 +        eqnmat[1][2] = p1->p[v];
304 +        eqnmat[1][3] = 1.0;
305 +        eqnmat[2][0] = p2->p[u]*p2->p[v];
306 +        eqnmat[2][1] = p2->p[u];
307 +        eqnmat[2][2] = p2->p[v];
308 +        eqnmat[2][3] = 1.0;
309 +        eqnmat[3][0] = p3->p[u]*p3->p[v];
310 +        eqnmat[3][1] = p3->p[u];
311 +        eqnmat[3][2] = p3->p[v];
312 +        eqnmat[3][3] = 1.0;
313 +                                        /* invert matrix (solve system) */
314 +        if (!invmat(eqnmat, eqnmat))
315 +                return(-1);                     /* no solution */
316 +                                        /* compute result matrix */
317 +        for (j = 0; j < 4; j++)
318 +                for (i = 0; i < 3; i++)
319 +                        resmat[j][i] =  eqnmat[j][0]*p0->n[i] +
320 +                                        eqnmat[j][1]*p1->n[i] +
321 +                                        eqnmat[j][2]*p2->n[i] +
322 +                                        eqnmat[j][3]*p3->n[i];
323 +        return(ax);
324 +
325 + #undef u
326 + #undef v
327 + }
328 +
329 +
330 + /*
331 + * invmat - computes the inverse of mat into inverse.  Returns 1
332 + * if there exists an inverse, 0 otherwise.  It uses Gaussian Elimination
333 + * method.
334 + */
335 +
336 + invmat(inverse,mat)
337 + double mat[4][4],inverse[4][4];
338 + {
339 + #define SWAP(a,b,t) (t=a,a=b,b=t)
340 +
341 +        double  m4tmp[4][4];
342 +        register int i,j,k;
343 +        register double temp;
344 +
345 +        bcopy((char *)mat, (char *)m4tmp, sizeof(m4tmp));
346 +                                        /* set inverse to identity */
347 +        for (i = 0; i < 4; i++)
348 +                for (j = 0; j < 4; j++)
349 +                        inverse[i][j] = i==j ? 1.0 : 0.0;
350 +
351 +        for(i = 0; i < 4; i++) {
352 +                /* Look for raw with largest pivot and swap raws */
353 +                temp = FTINY; j = -1;
354 +                for(k = i; k < 4; k++)
355 +                        if(ABS(m4tmp[k][i]) > temp) {
356 +                                temp = ABS(m4tmp[k][i]);
357 +                                j = k;
358 +                                }
359 +                if(j == -1)     /* No replacing raw -> no inverse */
360 +                        return(0);
361 +                if (j != i)
362 +                        for(k = 0; k < 4; k++) {
363 +                                SWAP(m4tmp[i][k],m4tmp[j][k],temp);
364 +                                SWAP(inverse[i][k],inverse[j][k],temp);
365 +                                }
366 +
367 +                temp = m4tmp[i][i];
368 +                for(k = 0; k < 4; k++) {
369 +                        m4tmp[i][k] /= temp;
370 +                        inverse[i][k] /= temp;
371 +                        }
372 +                for(j = 0; j < 4; j++) {
373 +                        if(j != i) {
374 +                                temp = m4tmp[j][i];
375 +                                for(k = 0; k < 4; k++) {
376 +                                        m4tmp[j][k] -= m4tmp[i][k]*temp;
377 +                                        inverse[j][k] -= inverse[i][k]*temp;
378 +                                        }
379 +                                }
380 +                        }
381 +                }
382 +        return(1);
383 +
384 + #undef SWAP
385 + }
386 +
387 +
388   eputs(msg)
389   char  *msg;
390   {
# Line 222 | Line 428 | l_hermite()                    
428                  argument(2)*(-2.0*t+3.0)*t*t +
429                  argument(3)*((t-2.0)*t+1.0)*t +
430                  argument(4)*(t-1.0)*t*t );
431 + }
432 +
433 +
434 + double
435 + l_bezier()
436 + {
437 +        double  t;
438 +
439 +        t = argument(5);
440 +        return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
441 +                argument(2) * 3.*t*(1.+t*(-2.+t)) +
442 +                argument(3) * 3.*t*t*(1.-t) +
443 +                argument(4) * t*t*t );
444 + }
445 +
446 +
447 + double
448 + l_bspline()
449 + {
450 +        double  t;
451 +
452 +        t = argument(5);
453 +        return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
454 +                argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
455 +                argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
456 +                argument(4) * (1./6.*t*t*t) );
457   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines