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 2.19 by greg, Wed Dec 8 21:37:51 2010 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";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  gensurf.c - program to generate functional surfaces
6   *
# Line 14 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11   *  rule applied to (s,t).
12   *
13   *      4/3/87
14 + *
15 + *      4/16/02 Added conditional vertex output
16   */
17  
18 < #include  <stdio.h>
18 > #include  "standard.h"
19  
20 < #define  XNAME          "X_"                    /* x function name */
21 < #define  YNAME          "Y_"                    /* y function name */
22 < #define  ZNAME          "Z_"                    /* z function name */
20 > #include  "paths.h"
21 > #include  "resolu.h"
22 > #include  "rterror.h"
23 > #include  "calcomp.h"
24  
25 < #define  PI             3.14159265358979323846
25 > char  XNAME[] =         "X`SYS";                /* x function name */
26 > char  YNAME[] =         "Y`SYS";                /* y function name */
27 > char  ZNAME[] =         "Z`SYS";                /* z function name */
28  
29 < #define  FTINY          1e-7
29 > char  VNAME[] =         "valid";                /* valid vertex name */
30  
31 < #define  vertex(p)      printf(vformat, (p)[0], (p)[1], (p)[2])
31 > #define  ABS(x)         ((x)>=0 ? (x) : -(x))
32  
33 < char  vformat[] = "%15.9g %15.9g %15.9g\n";
33 > #define  ZEROVECT(v)    (DOT(v,v) <= FTINY*FTINY)
34  
35 < double  funvalue(), dist2(), fdot(), l_hermite(), argument();
35 > #define  pvect(p)       printf(vformat, (p)[0], (p)[1], (p)[2])
36  
37 + char  vformat[] = "%18.12g %18.12g %18.12g\n";
38 + char  tsargs[] = "4 surf_dx surf_dy surf_dz surf.cal";
39 + char  texname[] = "Phong";
40  
41 + int  smooth = 0;                /* apply smoothing? */
42 + int  objout = 0;                /* output .OBJ format? */
43 +
44 + char  *modname, *surfname;
45 +
46 +                                /* recorded data flags */
47 + #define  HASBORDER      01
48 + #define  TRIPLETS       02
49 +                                /* a data structure */
50 + struct {
51 +        int     flags;                  /* data type */
52 +        short   m, n;                   /* number of s and t values */
53 +        RREAL   *data;                  /* the data itself, s major sort */
54 + } datarec;                      /* our recorded data */
55 +
56 + /* XXX this is redundant with rt/noise3.c, should go to a library */
57 + double  l_hermite(), l_bezier(), l_bspline(), l_dataval();
58 +
59 + typedef struct {
60 +        int  valid;     /* point is valid (vertex number) */
61 +        FVECT  p;       /* vertex position */
62 +        FVECT  n;       /* average normal */
63 +        RREAL  uv[2];   /* (u,v) position */
64 + } POINT;
65 +
66 +
67 + void loaddata(char *file, int m, int n, int pointsize);
68 + double l_dataval(char *nam);
69 + void putobjrow(POINT *rp, int n);
70 + void putsquare(POINT *p0, POINT *p1, POINT *p2, POINT *p3);
71 + void comprow(double s, POINT *row, int siz);
72 + void compnorms(POINT *r0, POINT *r1, POINT *r2, int siz);
73 + int norminterp(FVECT resmat[4], POINT *p0, POINT *p1, POINT *p2, POINT *p3);
74 +
75 +
76 + int
77   main(argc, argv)
78   int  argc;
79   char  *argv[];
80   {
81 <        static double  *xyz[4];
41 <        double  *row0, *row1, *dp;
42 <        double  v1[3], v2[3], vc1[3], vc2[3];
43 <        double  a1, a2;
81 >        POINT  *row0, *row1, *row2, *rp;
82          int  i, j, m, n;
83          char  stmp[256];
46        double  d;
47        register int  k;
84  
85 <        varset("PI", PI);
86 <        funset("hermite", 5, l_hermite);
85 >        varset("PI", ':', PI);
86 >        funset("hermite", 5, ':', l_hermite);
87 >        funset("bezier", 5, ':', l_bezier);
88 >        funset("bspline", 5, ':', l_bspline);
89  
90          if (argc < 8)
91                  goto userror;
92  
93          for (i = 8; i < argc; i++)
94                  if (!strcmp(argv[i], "-e"))
95 <                        scompile(NULL, argv[++i]);
95 >                        scompile(argv[++i], NULL, 0);
96                  else if (!strcmp(argv[i], "-f"))
97                          fcompile(argv[++i]);
98 +                else if (!strcmp(argv[i], "-s"))
99 +                        smooth++;
100 +                else if (!strcmp(argv[i], "-o"))
101 +                        objout++;
102                  else
103                          goto userror;
104  
105 <        sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]);
106 <        scompile(NULL, stmp);
65 <        sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]);
66 <        scompile(NULL, stmp);
67 <        sprintf(stmp, "%s(s,t)=%s;", ZNAME, argv[5]);
68 <        scompile(NULL, stmp);
105 >        modname = argv[1];
106 >        surfname = argv[2];
107          m = atoi(argv[6]);
108          n = atoi(argv[7]);
109          if (m <= 0 || n <= 0)
110                  goto userror;
111 <
112 <        row0 = (double *)malloc((n+1)*3*sizeof(double));
113 <        row1 = (double *)malloc((n+1)*3*sizeof(double));
114 <        if (row0 == NULL || row1 == NULL) {
111 >        if (!strcmp(argv[5], "-") || access(argv[5], 4) == 0) { /* file? */
112 >                funset(ZNAME, 2, ':', l_dataval);
113 >                if (!strcmp(argv[5],argv[3]) && !strcmp(argv[5],argv[4])) {
114 >                        loaddata(argv[5], m, n, 3);
115 >                        funset(XNAME, 2, ':', l_dataval);
116 >                        funset(YNAME, 2, ':', l_dataval);
117 >                } else {
118 >                        loaddata(argv[5], m, n, 1);
119 >                        sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]);
120 >                        scompile(stmp, NULL, 0);
121 >                        sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]);
122 >                        scompile(stmp, NULL, 0);
123 >                }
124 >        } else {
125 >                sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]);
126 >                scompile(stmp, NULL, 0);
127 >                sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]);
128 >                scompile(stmp, NULL, 0);
129 >                sprintf(stmp, "%s(s,t)=%s;", ZNAME, argv[5]);
130 >                scompile(stmp, NULL, 0);
131 >        }
132 >        row0 = (POINT *)malloc((n+3)*sizeof(POINT));
133 >        row1 = (POINT *)malloc((n+3)*sizeof(POINT));
134 >        row2 = (POINT *)malloc((n+3)*sizeof(POINT));
135 >        if (row0 == NULL || row1 == NULL || row2 == NULL) {
136                  fprintf(stderr, "%s: out of memory\n", argv[0]);
137                  quit(1);
138          }
139 <
140 <        printhead(argc, argv);
141 <
142 <        comprow(0.0, row1, n);                  /* compute zeroeth row */
143 <
139 >        row0++; row1++; row2++;
140 >                                                /* print header */
141 >        fputs("# ", stdout);
142 >        printargs(argc, argv, stdout);
143 >        eclock = 0;
144 >                                                /* initialize */
145 >        comprow(-1.0/m, row0, n);
146 >        comprow(0.0, row1, n);
147 >        comprow(1.0/m, row2, n);
148 >        compnorms(row0, row1, row2, n);
149 >        if (objout) {
150 >                printf("\nusemtl %s\n\n", modname);
151 >                putobjrow(row1, n);
152 >        }
153 >                                                /* for each row */
154          for (i = 0; i < m; i++) {
155                                                  /* compute next row */
156 <                dp = row0;
156 >                rp = row0;
157                  row0 = row1;
158 <                row1 = dp;
159 <                comprow((double)(i+1)/m, row1, n);
158 >                row1 = row2;
159 >                row2 = rp;
160 >                comprow((double)(i+2)/m, row2, n);
161 >                compnorms(row0, row1, row2, n);
162 >                if (objout)
163 >                        putobjrow(row1, n);
164  
165                  for (j = 0; j < n; j++) {
166 <                                                        /* get vertices */
167 <                        xyz[0] = row0 + 3*j;
168 <                        xyz[1] = row1 + 3*j;
169 <                        xyz[2] = xyz[0] + 3;
170 <                        xyz[3] = xyz[1] + 3;
171 <                                                        /* rotate vertices */
172 <                        if (dist2(xyz[0],xyz[3]) < dist2(xyz[1],xyz[2])-FTINY) {
173 <                                dp = xyz[0];
174 <                                xyz[0] = xyz[1];
175 <                                xyz[1] = xyz[3];
176 <                                xyz[3] = xyz[2];
177 <                                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 <                        }
166 >                        int  orient = (j & 1);
167 >                                                        /* put polygons */
168 >                        if (!(row0[j].valid && row1[j+1].valid))
169 >                                orient = 1;
170 >                        else if (!(row1[j].valid && row0[j+1].valid))
171 >                                orient = 0;
172 >                        if (orient)
173 >                                putsquare(&row0[j], &row1[j],
174 >                                                &row0[j+1], &row1[j+1]);
175 >                        else
176 >                                putsquare(&row1[j], &row1[j+1],
177 >                                                &row0[j], &row0[j+1]);
178                  }
179          }
180  
181 <        quit(0);
181 >        return 0;
182  
183   userror:
184          fprintf(stderr, "Usage: %s material name ", argv[0]);
185 <        fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-e expr] [-f file]\n");
186 <        quit(1);
185 >        fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-o][-e expr][-f file]\n");
186 >        return 1;
187   }
188  
189  
190 < comprow(s, row, siz)                    /* compute row of values */
191 < double  s;
192 < register double  *row;
193 < int  siz;
190 > void
191 > loaddata(               /* load point data from file */
192 >        char  *file,
193 >        int  m,
194 >        int  n,
195 >        int  pointsize
196 > )
197   {
198 <        double  st[2], step;
198 >        FILE  *fp;
199 >        char  word[64];
200 >        register int  size;
201 >        register RREAL  *dp;
202  
203 <        st[0] = s;
204 <        st[1] = 0.0;
205 <        step = 1.0 / siz;
206 <        while (siz-- >= 0) {
207 <                *row++ = funvalue(XNAME, 2, st);
208 <                *row++ = funvalue(YNAME, 2, st);
209 <                *row++ = funvalue(ZNAME, 2, st);
210 <                st[1] += step;
203 >        datarec.flags = HASBORDER;              /* assume border values */
204 >        datarec.m = m+1;
205 >        datarec.n = n+1;
206 >        size = datarec.m*datarec.n*pointsize;
207 >        if (pointsize == 3)
208 >                datarec.flags |= TRIPLETS;
209 >        dp = (RREAL *)malloc(size*sizeof(RREAL));
210 >        if ((datarec.data = dp) == NULL) {
211 >                fputs("Out of memory\n", stderr);
212 >                exit(1);
213          }
214 +        if (!strcmp(file, "-")) {
215 +                file = "<stdin>";
216 +                fp = stdin;
217 +        } else if ((fp = fopen(file, "r")) == NULL) {
218 +                fputs(file, stderr);
219 +                fputs(": cannot open\n", stderr);
220 +                exit(1);
221 +        }
222 +        while (size > 0 && fgetword(word, sizeof(word), fp) != NULL) {
223 +                if (!isflt(word)) {
224 +                        fprintf(stderr, "%s: garbled data value: %s\n",
225 +                                        file, word);
226 +                        exit(1);
227 +                }
228 +                *dp++ = atof(word);
229 +                size--;
230 +        }
231 +        if (size == (m+n+1)*pointsize) {        /* no border after all */
232 +                dp = (RREAL *)realloc((void *)datarec.data,
233 +                                m*n*pointsize*sizeof(RREAL));
234 +                if (dp != NULL)
235 +                        datarec.data = dp;
236 +                datarec.flags &= ~HASBORDER;
237 +                datarec.m = m;
238 +                datarec.n = n;
239 +                size = 0;
240 +        }
241 +        if (datarec.m < 2 || datarec.n < 2 || size != 0 ||
242 +                        fgetword(word, sizeof(word), fp) != NULL) {
243 +                fputs(file, stderr);
244 +                fputs(": bad number of data points\n", stderr);
245 +                exit(1);
246 +        }
247 +        fclose(fp);
248   }
249  
250  
251 < eputs(msg)
252 < char  *msg;
251 > double
252 > l_dataval(                              /* return recorded data value */
253 >        char  *nam
254 > )
255   {
256 <        fputs(msg, stderr);
256 >        double  u, v;
257 >        register int  i, j;
258 >        register RREAL  *dp;
259 >        double  d00, d01, d10, d11;
260 >                                                /* compute coordinates */
261 >        u = argument(1); v = argument(2);
262 >        if (datarec.flags & HASBORDER) {
263 >                i = u *= datarec.m-1;
264 >                j = v *= datarec.n-1;
265 >        } else {
266 >                i = u = u*datarec.m - .5;
267 >                j = v = v*datarec.n - .5;
268 >        }
269 >        if (i < 0) i = 0;
270 >        else if (i > datarec.m-2) i = datarec.m-2;
271 >        if (j < 0) j = 0;
272 >        else if (j > datarec.n-2) j = datarec.n-2;
273 >                                                /* compute value */
274 >        if (datarec.flags & TRIPLETS) {
275 >                dp = datarec.data + 3*(j*datarec.m + i);
276 >                if (nam == ZNAME)
277 >                        dp += 2;
278 >                else if (nam == YNAME)
279 >                        dp++;
280 >                d00 = dp[0]; d01 = dp[3];
281 >                dp += 3*datarec.m;
282 >                d10 = dp[0]; d11 = dp[3];
283 >        } else {
284 >                dp = datarec.data + j*datarec.m + i;
285 >                d00 = dp[0]; d01 = dp[1];
286 >                dp += datarec.m;
287 >                d10 = dp[0]; d11 = dp[1];
288 >        }
289 >                                                /* bilinear interpolation */
290 >        return((j+1-v)*((i+1-u)*d00+(u-i)*d01)+(v-j)*((i+1-u)*d10+(u-i)*d11));
291   }
292  
293  
294 < wputs(msg)
295 < char  *msg;
294 > void
295 > putobjrow(                      /* output vertex row to .OBJ */
296 >        register POINT  *rp,
297 >        int  n
298 > )
299   {
300 <        eputs(msg);
300 >        static int      nverts = 0;
301 >
302 >        for ( ; n-- >= 0; rp++) {
303 >                if (!rp->valid)
304 >                        continue;
305 >                fputs("v ", stdout);
306 >                pvect(rp->p);
307 >                if (smooth && !ZEROVECT(rp->n))
308 >                        printf("\tvn %.9g %.9g %.9g\n",
309 >                                        rp->n[0], rp->n[1], rp->n[2]);
310 >                printf("\tvt %.9g %.9g\n", rp->uv[0], rp->uv[1]);
311 >                rp->valid = ++nverts;
312 >        }
313   }
314  
315  
316 < quit(code)
316 > void
317 > putsquare(              /* put out a square */
318 >        POINT *p0,
319 >        POINT *p1,
320 >        POINT *p2,
321 >        POINT *p3
322 > )
323   {
324 <        exit(code);
324 >        static int  nout = 0;
325 >        FVECT  norm[4];
326 >        int  axis;
327 >        FVECT  v1, v2, vc1, vc2;
328 >        int  ok1, ok2;
329 >                                        /* compute exact normals */
330 >        ok1 = (p0->valid && p1->valid && p2->valid);
331 >        if (ok1) {
332 >                VSUB(v1, p1->p, p0->p);
333 >                VSUB(v2, p2->p, p0->p);
334 >                fcross(vc1, v1, v2);
335 >                ok1 = (normalize(vc1) != 0.0);
336 >        }
337 >        ok2 = (p1->valid && p2->valid && p3->valid);
338 >        if (ok2) {
339 >                VSUB(v1, p2->p, p3->p);
340 >                VSUB(v2, p1->p, p3->p);
341 >                fcross(vc2, v1, v2);
342 >                ok2 = (normalize(vc2) != 0.0);
343 >        }
344 >        if (!(ok1 | ok2))
345 >                return;
346 >        if (objout) {                   /* output .OBJ faces */
347 >                int     p0n=0, p1n=0, p2n=0, p3n=0;
348 >                if (smooth) {
349 >                        if (!ZEROVECT(p0->n))
350 >                                p0n = p0->valid;
351 >                        if (!ZEROVECT(p1->n))
352 >                                p1n = p1->valid;
353 >                        if (!ZEROVECT(p2->n))
354 >                                p2n = p2->valid;
355 >                        if (!ZEROVECT(p3->n))
356 >                                p3n = p3->valid;
357 >                }
358 >                if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
359 >                        printf("f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d\n",
360 >                                        p0->valid, p0->valid, p0n,
361 >                                        p1->valid, p1->valid, p1n,
362 >                                        p3->valid, p3->valid, p3n,
363 >                                        p2->valid, p2->valid, p2n);
364 >                        return;
365 >                }
366 >                if (ok1)
367 >                        printf("f %d/%d/%d %d/%d/%d %d/%d/%d\n",
368 >                                        p0->valid, p0->valid, p0n,
369 >                                        p1->valid, p1->valid, p1n,
370 >                                        p2->valid, p2->valid, p2n);
371 >                if (ok2)
372 >                        printf("f %d/%d/%d %d/%d/%d %d/%d/%d\n",
373 >                                        p2->valid, p2->valid, p2n,
374 >                                        p1->valid, p1->valid, p1n,
375 >                                        p3->valid, p3->valid, p3n);
376 >                return;
377 >        }
378 >                                        /* compute normal interpolation */
379 >        axis = norminterp(norm, p0, p1, p2, p3);
380 >
381 >                                        /* put out quadrilateral? */
382 >        if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
383 >                printf("\n%s ", modname);
384 >                if (axis != -1) {
385 >                        printf("texfunc %s\n%s\n", texname, tsargs);
386 >                        printf("0\n13\t%d\n", axis);
387 >                        pvect(norm[0]);
388 >                        pvect(norm[1]);
389 >                        pvect(norm[2]);
390 >                        fvsum(v1, norm[3], vc1, -0.5);
391 >                        fvsum(v1, v1, vc2, -0.5);
392 >                        pvect(v1);
393 >                        printf("\n%s ", texname);
394 >                }
395 >                printf("polygon %s.%d\n", surfname, ++nout);
396 >                printf("0\n0\n12\n");
397 >                pvect(p0->p);
398 >                pvect(p1->p);
399 >                pvect(p3->p);
400 >                pvect(p2->p);
401 >                return;
402 >        }
403 >                                        /* put out triangles? */
404 >        if (ok1) {
405 >                printf("\n%s ", modname);
406 >                if (axis != -1) {
407 >                        printf("texfunc %s\n%s\n", texname, tsargs);
408 >                        printf("0\n13\t%d\n", axis);
409 >                        pvect(norm[0]);
410 >                        pvect(norm[1]);
411 >                        pvect(norm[2]);
412 >                        fvsum(v1, norm[3], vc1, -1.0);
413 >                        pvect(v1);
414 >                        printf("\n%s ", texname);
415 >                }
416 >                printf("polygon %s.%d\n", surfname, ++nout);
417 >                printf("0\n0\n9\n");
418 >                pvect(p0->p);
419 >                pvect(p1->p);
420 >                pvect(p2->p);
421 >        }
422 >        if (ok2) {
423 >                printf("\n%s ", modname);
424 >                if (axis != -1) {
425 >                        printf("texfunc %s\n%s\n", texname, tsargs);
426 >                        printf("0\n13\t%d\n", axis);
427 >                        pvect(norm[0]);
428 >                        pvect(norm[1]);
429 >                        pvect(norm[2]);
430 >                        fvsum(v2, norm[3], vc2, -1.0);
431 >                        pvect(v2);
432 >                        printf("\n%s ", texname);
433 >                }
434 >                printf("polygon %s.%d\n", surfname, ++nout);
435 >                printf("0\n0\n9\n");
436 >                pvect(p2->p);
437 >                pvect(p1->p);
438 >                pvect(p3->p);
439 >        }
440   }
441  
442  
443 < printhead(ac, av)               /* print command header */
444 < register int  ac;
445 < register char  **av;
443 > void
444 > comprow(                        /* compute row of values */
445 >        double  s,
446 >        register POINT  *row,
447 >        int  siz
448 > )
449   {
450 <        putchar('#');
451 <        while (ac--) {
452 <                putchar(' ');
453 <                fputs(*av++, stdout);
450 >        double  st[2];
451 >        int  end;
452 >        int  checkvalid;
453 >        register int  i;
454 >        
455 >        if (smooth) {
456 >                i = -1;                 /* compute one past each end */
457 >                end = siz+1;
458 >        } else {
459 >                if (s < -FTINY || s > 1.0+FTINY)
460 >                        return;
461 >                i = 0;
462 >                end = siz;
463          }
464 <        putchar('\n');
464 >        st[0] = s;
465 >        checkvalid = (fundefined(VNAME) == 2);
466 >        while (i <= end) {
467 >                st[1] = (double)i/siz;
468 >                if (checkvalid && funvalue(VNAME, 2, st) <= 0.0) {
469 >                        row[i].valid = 0;
470 >                        row[i].p[0] = row[i].p[1] = row[i].p[2] = 0.0;
471 >                        row[i].uv[0] = row[i].uv[1] = 0.0;
472 >                } else {
473 >                        row[i].valid = 1;
474 >                        row[i].p[0] = funvalue(XNAME, 2, st);
475 >                        row[i].p[1] = funvalue(YNAME, 2, st);
476 >                        row[i].p[2] = funvalue(ZNAME, 2, st);
477 >                        row[i].uv[0] = st[0];
478 >                        row[i].uv[1] = st[1];
479 >                }
480 >                i++;
481 >        }
482   }
483  
484  
485 + void
486 + compnorms(              /* compute row of averaged normals */
487 +        register POINT  *r0,
488 +        register POINT  *r1,
489 +        register POINT  *r2,
490 +        int  siz
491 + )
492 + {
493 +        FVECT  v1, v2;
494 +
495 +        if (!smooth)                    /* not needed if no smoothing */
496 +                return;
497 +                                        /* compute row 1 normals */
498 +        while (siz-- >= 0) {
499 +                if (!r1[0].valid)
500 +                        continue;
501 +                if (!r0[0].valid) {
502 +                        if (!r2[0].valid) {
503 +                                r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0;
504 +                                continue;
505 +                        }
506 +                        fvsum(v1, r2[0].p, r1[0].p, -1.0);
507 +                } else if (!r2[0].valid)
508 +                        fvsum(v1, r1[0].p, r0[0].p, -1.0);
509 +                else
510 +                        fvsum(v1, r2[0].p, r0[0].p, -1.0);
511 +                if (!r1[-1].valid) {
512 +                        if (!r1[1].valid) {
513 +                                r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0;
514 +                                continue;
515 +                        }
516 +                        fvsum(v2, r1[1].p, r1[0].p, -1.0);
517 +                } else if (!r1[1].valid)
518 +                        fvsum(v2, r1[0].p, r1[-1].p, -1.0);
519 +                else
520 +                        fvsum(v2, r1[1].p, r1[-1].p, -1.0);
521 +                fcross(r1[0].n, v1, v2);
522 +                normalize(r1[0].n);
523 +                r0++; r1++; r2++;
524 +        }
525 + }
526 +
527 +
528 + int
529 + norminterp(     /* compute normal interpolation */
530 +        register FVECT  resmat[4],
531 +        POINT  *p0,
532 +        POINT  *p1,
533 +        POINT  *p2,
534 +        POINT  *p3
535 + )
536 + {
537 + #define u  ((ax+1)%3)
538 + #define v  ((ax+2)%3)
539 +
540 +        register int  ax;
541 +        MAT4  eqnmat;
542 +        FVECT  v1;
543 +        register int  i, j;
544 +
545 +        if (!smooth)                    /* no interpolation if no smoothing */
546 +                return(-1);
547 +                                        /* find dominant axis */
548 +        VCOPY(v1, p0->n);
549 +        fvsum(v1, v1, p1->n, 1.0);
550 +        fvsum(v1, v1, p2->n, 1.0);
551 +        fvsum(v1, v1, p3->n, 1.0);
552 +        ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
553 +        ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
554 +                                        /* assign equation matrix */
555 +        eqnmat[0][0] = p0->p[u]*p0->p[v];
556 +        eqnmat[0][1] = p0->p[u];
557 +        eqnmat[0][2] = p0->p[v];
558 +        eqnmat[0][3] = 1.0;
559 +        eqnmat[1][0] = p1->p[u]*p1->p[v];
560 +        eqnmat[1][1] = p1->p[u];
561 +        eqnmat[1][2] = p1->p[v];
562 +        eqnmat[1][3] = 1.0;
563 +        eqnmat[2][0] = p2->p[u]*p2->p[v];
564 +        eqnmat[2][1] = p2->p[u];
565 +        eqnmat[2][2] = p2->p[v];
566 +        eqnmat[2][3] = 1.0;
567 +        eqnmat[3][0] = p3->p[u]*p3->p[v];
568 +        eqnmat[3][1] = p3->p[u];
569 +        eqnmat[3][2] = p3->p[v];
570 +        eqnmat[3][3] = 1.0;
571 +                                        /* invert matrix (solve system) */
572 +        if (!invmat4(eqnmat, eqnmat))
573 +                return(-1);                     /* no solution */
574 +                                        /* compute result matrix */
575 +        for (j = 0; j < 4; j++)
576 +                for (i = 0; i < 3; i++)
577 +                        resmat[j][i] =  eqnmat[j][0]*p0->n[i] +
578 +                                        eqnmat[j][1]*p1->n[i] +
579 +                                        eqnmat[j][2]*p2->n[i] +
580 +                                        eqnmat[j][3]*p3->n[i];
581 +        return(ax);
582 +
583 + #undef u
584 + #undef v
585 + }
586 +
587 +
588   double
589 < l_hermite()                    
589 > l_hermite(char *nm)
590   {
591          double  t;
592          
# Line 224 | Line 595 | l_hermite()                    
595                  argument(2)*(-2.0*t+3.0)*t*t +
596                  argument(3)*((t-2.0)*t+1.0)*t +
597                  argument(4)*(t-1.0)*t*t );
598 + }
599 +
600 +
601 + double
602 + l_bezier(char *nm)
603 + {
604 +        double  t;
605 +
606 +        t = argument(5);
607 +        return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
608 +                argument(2) * 3.*t*(1.+t*(-2.+t)) +
609 +                argument(3) * 3.*t*t*(1.-t) +
610 +                argument(4) * t*t*t );
611 + }
612 +
613 +
614 + double
615 + l_bspline(char *nm)
616 + {
617 +        double  t;
618 +
619 +        t = argument(5);
620 +        return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
621 +                argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
622 +                argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
623 +                argument(4) * (1./6.*t*t*t) );
624   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines