ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genworm.c
Revision: 1.3
Committed: Fri Mar 2 17:24:05 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +15 -1 lines
Log Message:
Added Bezier cubic function

File Contents

# User Rev Content
1 greg 1.2 /* Copyright (c) 1989 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6 greg 1.2
7     /*
8 greg 1.1 * genworm.c - program to generate worms (strings with varying thickness).
9     *
10     * The program takes as input the functions of t for x, y,
11     * z and r (the radius). Cylinders and cones will be
12     * joined by spheres. Negative radii are forbidden.
13     *
14     * 9/24/87
15     */
16    
17     #include <stdio.h>
18    
19     #include "fvect.h"
20    
21     #define XNAME "X_" /* x function name */
22     #define YNAME "Y_" /* y function name */
23     #define ZNAME "Z_" /* z function name */
24     #define RNAME "R_" /* r function name */
25    
26     #define PI 3.14159265358979323846
27    
28     #define FTINY 1e-7
29    
30     #define max(a,b) ((a) > (b) ? (a) : (b))
31    
32    
33 greg 1.3 double funvalue(), l_hermite(), l_bezier(), argument();
34 greg 1.1
35    
36     main(argc, argv)
37     int argc;
38     char *argv[];
39     {
40     char stmp[256];
41     double t, f, lastr, r;
42     FVECT lastp, p;
43     int i, nseg;
44    
45     varset("PI", PI, NULL);
46     funset("hermite", 5, l_hermite);
47 greg 1.3 funset("bezier", 5, l_bezier);
48 greg 1.1
49     if (argc < 8)
50     goto userror;
51    
52     for (i = 8; i < argc; i++)
53     if (!strcmp(argv[i], "-e"))
54     scompile(NULL, argv[++i]);
55     else if (!strcmp(argv[i], "-f"))
56     fcompile(argv[++i]);
57     else
58     goto userror;
59    
60     sprintf(stmp, "%s(t)=%s;", XNAME, argv[3]);
61     scompile(NULL, stmp);
62     sprintf(stmp, "%s(t)=%s;", YNAME, argv[4]);
63     scompile(NULL, stmp);
64     sprintf(stmp, "%s(t)=%s;", ZNAME, argv[5]);
65     scompile(NULL, stmp);
66     sprintf(stmp, "%s(t)=%s;", RNAME, argv[6]);
67     scompile(NULL, stmp);
68     nseg = atoi(argv[7]);
69     if (nseg <= 0)
70     goto userror;
71    
72     printhead(argc, argv);
73    
74     for (i = 0; i <= nseg; i++) {
75     t = (double)i/nseg;
76     p[0] = funvalue(XNAME, 1, &t);
77     p[1] = funvalue(YNAME, 1, &t);
78     p[2] = funvalue(ZNAME, 1, &t);
79     r = funvalue(RNAME, 1, &t);
80     if (i)
81     if (lastr <= r+FTINY && lastr >= r-FTINY) {
82     printf("\n%s cylinder %s.c%d\n",
83     argv[1], argv[2], i);
84     printf("0\n0\n7\n");
85     printf("%18.12g %18.12g %18.12g\n",
86     lastp[0], lastp[1], lastp[2]);
87     printf("%18.12g %18.12g %18.12g\n",
88     p[0], p[1], p[2]);
89     printf("%18.12g\n", r);
90     } else {
91     printf("\n%s cone %s.c%d\n",
92     argv[1], argv[2], i);
93     printf("0\n0\n8\n");
94     f = (lastr - r)/dist2(lastp,p);
95     printf("%18.12g %18.12g %18.12g\n",
96     lastp[0] + f*lastr*(p[0] - lastp[0]),
97     lastp[1] + f*lastr*(p[1] - lastp[1]),
98     lastp[2] + f*lastr*(p[2] - lastp[2]));
99     printf("%18.12g %18.12g %18.12g\n",
100     p[0] + f*r*(p[0] - lastp[0]),
101     p[1] + f*r*(p[1] - lastp[1]),
102     p[2] + f*r*(p[2] - lastp[2]));
103     f = 1.0 - (lastr-r)*f;
104     f = f <= 0.0 ? 0.0 : sqrt(f);
105     printf("%18.12g %18.12g\n", f*lastr, f*r);
106     }
107     printf("\n%s sphere %s.s%d\n", argv[1], argv[2], i);
108     printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n",
109     p[0], p[1], p[2], r);
110     VCOPY(lastp, p);
111     lastr = r;
112     }
113     quit(0);
114    
115     userror:
116     fprintf(stderr,
117     "Usage: %s material name x(t) y(t) z(t) r(t) nseg [-e expr] [-f file]\n",
118     argv[0]);
119     quit(1);
120     }
121    
122    
123     eputs(msg)
124     char *msg;
125     {
126     fputs(msg, stderr);
127     }
128    
129    
130     wputs(msg)
131     char *msg;
132     {
133     eputs(msg);
134     }
135    
136    
137     quit(code)
138     {
139     exit(code);
140     }
141    
142    
143     printhead(ac, av) /* print command header */
144     register int ac;
145     register char **av;
146     {
147     putchar('#');
148     while (ac--) {
149     putchar(' ');
150     fputs(*av++, stdout);
151     }
152     putchar('\n');
153     }
154    
155    
156     double
157     l_hermite()
158     {
159     double t;
160    
161     t = argument(5);
162     return( argument(1)*((2.0*t-3.0)*t*t+1.0) +
163     argument(2)*(-2.0*t+3.0)*t*t +
164     argument(3)*((t-2.0)*t+1.0)*t +
165     argument(4)*(t-1.0)*t*t );
166 greg 1.3 }
167    
168    
169     double
170     l_bezier()
171     {
172     double t;
173    
174     t = argument(5);
175     return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
176     argument(2) * 3.*t*(1.+t*(-2.+t)) +
177     argument(3) * 3.*t*t*(1.-t) +
178     argument(4) * t*t*t );
179 greg 1.1 }