ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genworm.c
Revision: 2.5
Committed: Mon Jul 21 22:30:18 2003 UTC (20 years, 8 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.4: +3 -2 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: genworm.c,v 2.4 2003/02/22 02:07:23 greg Exp $";
3 #endif
4 /*
5 * genworm.c - program to generate worms (strings with varying thickness).
6 *
7 * The program takes as input the functions of t for x, y,
8 * z and r (the radius). Cylinders and cones will be
9 * joined by spheres. Negative radii are forbidden.
10 *
11 * 9/24/87
12 */
13
14 #include <stdio.h>
15 #include <math.h>
16 #include "fvect.h"
17
18 #define XNAME "X`SYS`" /* x function name */
19 #define YNAME "Y`SYS`" /* y function name */
20 #define ZNAME "Z`SYS`" /* z function name */
21 #define RNAME "R`SYS`" /* r function name */
22
23 #define PI 3.14159265358979323846
24
25 #define max(a,b) ((a) > (b) ? (a) : (b))
26
27
28 double funvalue(), l_hermite(), l_bezier(), l_bspline(), argument();
29 void quit();
30
31
32 main(argc, argv)
33 int argc;
34 char *argv[];
35 {
36 extern long eclock;
37 char stmp[256];
38 double t, f, lastr, r;
39 FVECT lastp, p;
40 int i, nseg;
41
42 varset("PI", ':', PI);
43 funset("hermite", 5, ':', l_hermite);
44 funset("bezier", 5, ':', l_bezier);
45 funset("bspline", 5, ':', l_bspline);
46
47 if (argc < 8)
48 goto userror;
49
50 for (i = 8; i < argc; i++)
51 if (!strcmp(argv[i], "-e"))
52 scompile(argv[++i], NULL, 0);
53 else if (!strcmp(argv[i], "-f"))
54 fcompile(argv[++i]);
55 else
56 goto userror;
57
58 sprintf(stmp, "%s(t)=%s;", XNAME, argv[3]);
59 scompile(stmp, NULL, 0);
60 sprintf(stmp, "%s(t)=%s;", YNAME, argv[4]);
61 scompile(stmp, NULL, 0);
62 sprintf(stmp, "%s(t)=%s;", ZNAME, argv[5]);
63 scompile(stmp, NULL, 0);
64 sprintf(stmp, "%s(t)=%s;", RNAME, argv[6]);
65 scompile(stmp, NULL, 0);
66 nseg = atoi(argv[7]);
67 if (nseg <= 0)
68 goto userror;
69
70 printhead(argc, argv);
71 eclock = 0;
72
73 for (i = 0; i <= nseg; i++) {
74 t = (double)i/nseg;
75 p[0] = funvalue(XNAME, 1, &t);
76 p[1] = funvalue(YNAME, 1, &t);
77 p[2] = funvalue(ZNAME, 1, &t);
78 r = funvalue(RNAME, 1, &t);
79 if (i) {
80 if (lastr <= r+FTINY && lastr >= r-FTINY) {
81 printf("\n%s cylinder %s.c%d\n",
82 argv[1], argv[2], i);
83 printf("0\n0\n7\n");
84 printf("%18.12g %18.12g %18.12g\n",
85 lastp[0], lastp[1], lastp[2]);
86 printf("%18.12g %18.12g %18.12g\n",
87 p[0], p[1], p[2]);
88 printf("%18.12g\n", r);
89 } else {
90 printf("\n%s cone %s.c%d\n",
91 argv[1], argv[2], i);
92 printf("0\n0\n8\n");
93 f = (lastr - r)/dist2(lastp,p);
94 printf("%18.12g %18.12g %18.12g\n",
95 lastp[0] + f*lastr*(p[0] - lastp[0]),
96 lastp[1] + f*lastr*(p[1] - lastp[1]),
97 lastp[2] + f*lastr*(p[2] - lastp[2]));
98 printf("%18.12g %18.12g %18.12g\n",
99 p[0] + f*r*(p[0] - lastp[0]),
100 p[1] + f*r*(p[1] - lastp[1]),
101 p[2] + f*r*(p[2] - lastp[2]));
102 f = 1.0 - (lastr-r)*f;
103 f = f <= 0.0 ? 0.0 : sqrt(f);
104 printf("%18.12g %18.12g\n", f*lastr, f*r);
105 }
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 void
124 eputs(msg)
125 char *msg;
126 {
127 fputs(msg, stderr);
128 }
129
130
131 void
132 wputs(msg)
133 char *msg;
134 {
135 eputs(msg);
136 }
137
138
139 void
140 quit(code)
141 int code;
142 {
143 exit(code);
144 }
145
146
147 printhead(ac, av) /* print command header */
148 register int ac;
149 register char **av;
150 {
151 putchar('#');
152 while (ac--) {
153 putchar(' ');
154 fputs(*av++, stdout);
155 }
156 putchar('\n');
157 }
158
159
160 double
161 l_hermite()
162 {
163 double t;
164
165 t = argument(5);
166 return( argument(1)*((2.0*t-3.0)*t*t+1.0) +
167 argument(2)*(-2.0*t+3.0)*t*t +
168 argument(3)*((t-2.0)*t+1.0)*t +
169 argument(4)*(t-1.0)*t*t );
170 }
171
172
173 double
174 l_bezier()
175 {
176 double t;
177
178 t = argument(5);
179 return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
180 argument(2) * 3.*t*(1.+t*(-2.+t)) +
181 argument(3) * 3.*t*t*(1.-t) +
182 argument(4) * t*t*t );
183 }
184
185
186 double
187 l_bspline()
188 {
189 double t;
190
191 t = argument(5);
192 return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
193 argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
194 argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
195 argument(4) * (1./6.*t*t*t) );
196 }