ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genworm.c
Revision: 1.5
Committed: Mon Jul 9 09:55:50 1990 UTC (33 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +2 -0 lines
Log Message:
set eclock variable for more efficient computations

File Contents

# Content
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 * 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 double funvalue(), l_hermite(), l_bezier(), l_bspline(), argument();
34
35
36 main(argc, argv)
37 int argc;
38 char *argv[];
39 {
40 extern long eclock;
41 char stmp[256];
42 double t, f, lastr, r;
43 FVECT lastp, p;
44 int i, nseg;
45
46 varset("PI", PI, NULL);
47 funset("hermite", 5, l_hermite);
48 funset("bezier", 5, l_bezier);
49 funset("bspline", 5, l_bspline);
50
51 if (argc < 8)
52 goto userror;
53
54 for (i = 8; i < argc; i++)
55 if (!strcmp(argv[i], "-e"))
56 scompile(NULL, argv[++i]);
57 else if (!strcmp(argv[i], "-f"))
58 fcompile(argv[++i]);
59 else
60 goto userror;
61
62 sprintf(stmp, "%s(t)=%s;", XNAME, argv[3]);
63 scompile(NULL, stmp);
64 sprintf(stmp, "%s(t)=%s;", YNAME, argv[4]);
65 scompile(NULL, stmp);
66 sprintf(stmp, "%s(t)=%s;", ZNAME, argv[5]);
67 scompile(NULL, stmp);
68 sprintf(stmp, "%s(t)=%s;", RNAME, argv[6]);
69 scompile(NULL, stmp);
70 nseg = atoi(argv[7]);
71 if (nseg <= 0)
72 goto userror;
73
74 printhead(argc, argv);
75 eclock = 0;
76
77 for (i = 0; i <= nseg; i++) {
78 t = (double)i/nseg;
79 p[0] = funvalue(XNAME, 1, &t);
80 p[1] = funvalue(YNAME, 1, &t);
81 p[2] = funvalue(ZNAME, 1, &t);
82 r = funvalue(RNAME, 1, &t);
83 if (i)
84 if (lastr <= r+FTINY && lastr >= r-FTINY) {
85 printf("\n%s cylinder %s.c%d\n",
86 argv[1], argv[2], i);
87 printf("0\n0\n7\n");
88 printf("%18.12g %18.12g %18.12g\n",
89 lastp[0], lastp[1], lastp[2]);
90 printf("%18.12g %18.12g %18.12g\n",
91 p[0], p[1], p[2]);
92 printf("%18.12g\n", r);
93 } else {
94 printf("\n%s cone %s.c%d\n",
95 argv[1], argv[2], i);
96 printf("0\n0\n8\n");
97 f = (lastr - r)/dist2(lastp,p);
98 printf("%18.12g %18.12g %18.12g\n",
99 lastp[0] + f*lastr*(p[0] - lastp[0]),
100 lastp[1] + f*lastr*(p[1] - lastp[1]),
101 lastp[2] + f*lastr*(p[2] - lastp[2]));
102 printf("%18.12g %18.12g %18.12g\n",
103 p[0] + f*r*(p[0] - lastp[0]),
104 p[1] + f*r*(p[1] - lastp[1]),
105 p[2] + f*r*(p[2] - lastp[2]));
106 f = 1.0 - (lastr-r)*f;
107 f = f <= 0.0 ? 0.0 : sqrt(f);
108 printf("%18.12g %18.12g\n", f*lastr, f*r);
109 }
110 printf("\n%s sphere %s.s%d\n", argv[1], argv[2], i);
111 printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n",
112 p[0], p[1], p[2], r);
113 VCOPY(lastp, p);
114 lastr = r;
115 }
116 quit(0);
117
118 userror:
119 fprintf(stderr,
120 "Usage: %s material name x(t) y(t) z(t) r(t) nseg [-e expr] [-f file]\n",
121 argv[0]);
122 quit(1);
123 }
124
125
126 eputs(msg)
127 char *msg;
128 {
129 fputs(msg, stderr);
130 }
131
132
133 wputs(msg)
134 char *msg;
135 {
136 eputs(msg);
137 }
138
139
140 quit(code)
141 {
142 exit(code);
143 }
144
145
146 printhead(ac, av) /* print command header */
147 register int ac;
148 register char **av;
149 {
150 putchar('#');
151 while (ac--) {
152 putchar(' ');
153 fputs(*av++, stdout);
154 }
155 putchar('\n');
156 }
157
158
159 double
160 l_hermite()
161 {
162 double t;
163
164 t = argument(5);
165 return( argument(1)*((2.0*t-3.0)*t*t+1.0) +
166 argument(2)*(-2.0*t+3.0)*t*t +
167 argument(3)*((t-2.0)*t+1.0)*t +
168 argument(4)*(t-1.0)*t*t );
169 }
170
171
172 double
173 l_bezier()
174 {
175 double t;
176
177 t = argument(5);
178 return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
179 argument(2) * 3.*t*(1.+t*(-2.+t)) +
180 argument(3) * 3.*t*t*(1.-t) +
181 argument(4) * t*t*t );
182 }
183
184
185 double
186 l_bspline()
187 {
188 double t;
189
190 t = argument(5);
191 return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
192 argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
193 argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
194 argument(4) * (1./6.*t*t*t) );
195 }