ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genworm.c
Revision: 1.1
Committed: Thu Feb 2 11:16:31 1989 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

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