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.3 by greg, Wed Oct 18 15:01:23 1989 UTC vs.
Revision 1.10 by greg, Fri Jul 20 14:35:59 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>
20 < #include  "fvect.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  
28 #define  FTINY          1e-7
29
27   #define  pvect(p)       printf(vformat, (p)[0], (p)[1], (p)[2])
28  
29   char  vformat[] = "%15.9g %15.9g %15.9g\n";
# Line 37 | Line 34 | int  smooth = 0;               /* apply smoothing? */
34  
35   char  *modname, *surfname;
36  
37 < double  funvalue(), l_hermite(), argument(), fabs();
37 > double  funvalue(), l_hermite(), l_bezier(), l_bspline(), argument();
38  
39   typedef struct {
40          FVECT  p;       /* vertex position */
# Line 49 | Line 46 | main(argc, argv)
46   int  argc;
47   char  *argv[];
48   {
49 +        extern long     eclock;
50          POINT  *row0, *row1, *row2, *rp;
51          int  i, j, m, n;
52          char  stmp[256];
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"))
# Line 72 | Line 72 | char  *argv[];
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 = (POINT *)malloc((n+1)*sizeof(POINT));
86 <        row1 = (POINT *)malloc((n+1)*sizeof(POINT));
87 <        row2 = (POINT *)malloc((n+1)*sizeof(POINT));
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 +        row0++; row1++; row2++;
93                                                  /* print header */
94          printhead(argc, argv);
95 <                                                /* compute first two rows */
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(row1, row1, row2, n);
100 >        compnorms(row0, row1, row2, n);
101                                                  /* for each row */
102          for (i = 0; i < m; i++) {
103                                                  /* compute next row */
# Line 102 | Line 105 | char  *argv[];
105                  row0 = row1;
106                  row1 = row2;
107                  row2 = rp;
108 <                if (i+2 <= m) {
109 <                        comprow((double)(i+2)/m, row2, n);
107 <                        compnorms(row0, row1, row2, n);
108 <                } else
109 <                        compnorms(row0, row1, row1, n);
108 >                comprow((double)(i+2)/m, row2, n);
109 >                compnorms(row0, row1, row2, n);
110  
111                  for (j = 0; j < n; j++) {
112                                                          /* put polygons */
# Line 220 | Line 220 | double  s;
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;
239 <        while (siz-- >= 0) {
240 <                row->p[0] = funvalue(XNAME, 2, st);
241 <                row->p[1] = funvalue(YNAME, 2, st);
242 <                row->p[2] = funvalue(ZNAME, 2, st);
232 <                row++;
233 <                st[1] += step;
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  
# Line 240 | Line 249 | register POINT  *r0, *r1, *r2;
249   int  siz;
250   {
251          FVECT  v1, v2, vc;
252 +        register int  i;
253  
254          if (!smooth)                    /* not needed if no smoothing */
255                  return;
246                                        /* compute first point */
247        fvsum(v1, r2[0].p, r1[0].p, -1.0);
248        fvsum(v2, r1[1].p, r1[0].p, -1.0);
249        fcross(r1[0].n, v1, v2);
250        fvsum(v1, r0[0].p, r1[0].p, -1.0);
251        fcross(vc, v2, v1);
252        fvsum(r1[0].n, r1[0].n, vc, 1.0);
253        normalize(r1[0].n);
254        r0++; r1++; r2++;
256                                          /* compute middle points */
257 <        while (--siz > 0) {
257 >        while (siz-- >= 0) {
258                  fvsum(v1, r2[0].p, r1[0].p, -1.0);
259                  fvsum(v2, r1[1].p, r1[0].p, -1.0);
260                  fcross(r1[0].n, v1, v2);
# Line 269 | Line 270 | int  siz;
270                  normalize(r1[0].n);
271                  r0++; r1++; r2++;
272          }
272                                        /* compute end point */
273        fvsum(v1, r0[0].p, r1[0].p, -1.0);
274        fvsum(v2, r1[-1].p, r1[0].p, -1.0);
275        fcross(r1[0].n, v1, v2);
276        fvsum(v1, r2[0].p, r1[0].p, -1.0);
277        fcross(vc, v2, v1);
278        fvsum(r1[0].n, r1[0].n, vc, 1.0);
279        normalize(r1[0].n);
273   }
274  
275  
# Line 289 | Line 282 | POINT  *p0, *p1, *p2, *p3;
282   #define v  ((ax+2)%3)
283  
284          register int  ax;
285 <        double  eqnmat[4][4], solmat[4][4];
285 >        double  eqnmat[4][4];
286          FVECT  v1;
287          register int  i, j;
288  
# Line 300 | Line 293 | POINT  *p0, *p1, *p2, *p3;
293          fvsum(v1, v1, p1->n, 1.0);
294          fvsum(v1, v1, p2->n, 1.0);
295          fvsum(v1, v1, p3->n, 1.0);
296 <        ax = fabs(v1[0]) > fabs(v1[1]) ? 0 : 1;
297 <        ax = fabs(v1[ax]) > fabs(v1[2]) ? ax : 2;
296 >        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
297 >        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
298                                          /* assign equation matrix */
299          eqnmat[0][0] = p0->p[u]*p0->p[v];
300          eqnmat[0][1] = p0->p[u];
# Line 320 | Line 313 | POINT  *p0, *p1, *p2, *p3;
313          eqnmat[3][2] = p3->p[v];
314          eqnmat[3][3] = 1.0;
315                                          /* invert matrix (solve system) */
316 <        if (!invmat(solmat, eqnmat))
316 >        if (!invmat(eqnmat, eqnmat))
317                  return(-1);                     /* no solution */
318                                          /* compute result matrix */
319          for (j = 0; j < 4; j++)
320                  for (i = 0; i < 3; i++)
321 <                        resmat[j][i] =  solmat[j][0]*p0->n[i] +
322 <                                        solmat[j][1]*p1->n[i] +
323 <                                        solmat[j][2]*p2->n[i] +
324 <                                        solmat[j][3]*p3->n[i];
321 >                        resmat[j][i] =  eqnmat[j][0]*p0->n[i] +
322 >                                        eqnmat[j][1]*p1->n[i] +
323 >                                        eqnmat[j][2]*p2->n[i] +
324 >                                        eqnmat[j][3]*p3->n[i];
325          return(ax);
326  
327   #undef u
# Line 336 | Line 329 | POINT  *p0, *p1, *p2, *p3;
329   }
330  
331  
339 static double  m4tmp[4][4];             /* for efficiency */
340
341 #define  copymat4(m4a,m4b)      bcopy((char *)m4b,(char *)m4a,sizeof(m4tmp))
342
343
344 setident4(m4)
345 double  m4[4][4];
346 {
347        static double  ident[4][4] = {
348                1.,0.,0.,0.,
349                0.,1.,0.,0.,
350                0.,0.,1.,0.,
351                0.,0.,0.,1.,
352        };
353        copymat4(m4, ident);
354 }
355
332   /*
333   * invmat - computes the inverse of mat into inverse.  Returns 1
334   * if there exists an inverse, 0 otherwise.  It uses Gaussian Elimination
# Line 364 | Line 340 | double mat[4][4],inverse[4][4];
340   {
341   #define SWAP(a,b,t) (t=a,a=b,b=t)
342  
343 +        double  m4tmp[4][4];
344          register int i,j,k;
345          register double temp;
346  
347 <        setident4(inverse);
348 <        copymat4(m4tmp, mat);
347 >        bcopy((char *)mat, (char *)m4tmp, sizeof(m4tmp));
348 >                                        /* set inverse to identity */
349 >        for (i = 0; i < 4; i++)
350 >                for (j = 0; j < 4; j++)
351 >                        inverse[i][j] = i==j ? 1.0 : 0.0;
352  
353          for(i = 0; i < 4; i++) {
354 <                if(m4tmp[i][i] == 0) {    /* Pivot is zero */
355 <                        /* Look for a raw with pivot != 0 and swap raws */
356 <                        for(j = i + 1; j < 4; j++)
357 <                                if(m4tmp[j][i] != 0) {
358 <                                        for( k = 0; k < 4; k++) {
359 <                                                SWAP(m4tmp[i][k],m4tmp[j][k],temp);
360 <                                                SWAP(inverse[i][k],inverse[j][k],temp);
361 <                                                }
362 <                                        break;
363 <                                        }
364 <                        if(j == 4)      /* No replacing raw -> no inverse */
365 <                                return(0);
366 <                        }
354 >                /* Look for raw with largest pivot and swap raws */
355 >                temp = FTINY; j = -1;
356 >                for(k = i; k < 4; k++)
357 >                        if(ABS(m4tmp[k][i]) > temp) {
358 >                                temp = ABS(m4tmp[k][i]);
359 >                                j = k;
360 >                                }
361 >                if(j == -1)     /* No replacing raw -> no inverse */
362 >                        return(0);
363 >                if (j != i)
364 >                        for(k = 0; k < 4; k++) {
365 >                                SWAP(m4tmp[i][k],m4tmp[j][k],temp);
366 >                                SWAP(inverse[i][k],inverse[j][k],temp);
367 >                                }
368  
369                  temp = m4tmp[i][i];
370                  for(k = 0; k < 4; k++) {
# Line 401 | Line 382 | double mat[4][4],inverse[4][4];
382                          }
383                  }
384          return(1);
385 +
386   #undef SWAP
387   }
388  
# Line 448 | Line 430 | l_hermite()                    
430                  argument(2)*(-2.0*t+3.0)*t*t +
431                  argument(3)*((t-2.0)*t+1.0)*t +
432                  argument(4)*(t-1.0)*t*t );
433 + }
434 +
435 +
436 + double
437 + l_bezier()
438 + {
439 +        double  t;
440 +
441 +        t = argument(5);
442 +        return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
443 +                argument(2) * 3.*t*(1.+t*(-2.+t)) +
444 +                argument(3) * 3.*t*t*(1.-t) +
445 +                argument(4) * t*t*t );
446 + }
447 +
448 +
449 + double
450 + l_bspline()
451 + {
452 +        double  t;
453 +
454 +        t = argument(5);
455 +        return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
456 +                argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
457 +                argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
458 +                argument(4) * (1./6.*t*t*t) );
459   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines