ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genworm.c
Revision: 2.7
Committed: Sun Nov 16 10:29:38 2003 UTC (20 years, 4 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.6: +17 -8 lines
Log Message:
Continued ANSIfication and reduced other compile warnings.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.7 static const char RCSid[] = "$Id: genworm.c,v 2.6 2003/08/04 22:37:53 greg Exp $";
3 greg 1.1 #endif
4 greg 1.2 /*
5 greg 1.1 * 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 schorsch 2.7 #include <stdlib.h>
15 greg 1.1 #include <stdio.h>
16 greg 2.2 #include <math.h>
17 schorsch 2.7 #include <string.h>
18    
19 greg 2.6 #include "calcomp.h"
20 greg 1.1 #include "fvect.h"
21    
22 greg 1.9 #define XNAME "X`SYS`" /* x function name */
23     #define YNAME "Y`SYS`" /* y function name */
24     #define ZNAME "Z`SYS`" /* z function name */
25     #define RNAME "R`SYS`" /* r function name */
26 greg 1.1
27     #define PI 3.14159265358979323846
28    
29     #define max(a,b) ((a) > (b) ? (a) : (b))
30    
31    
32 schorsch 2.7 /* XXX redundant, move to library */
33 greg 2.6 double l_hermite(char *), l_bezier(char *), l_bspline(char *);
34 greg 1.1
35    
36 schorsch 2.7 static void printhead(int ac, char **av);
37    
38    
39     int
40 greg 1.1 main(argc, argv)
41     int argc;
42     char *argv[];
43     {
44     char stmp[256];
45 schorsch 2.7 double t, f, lastr = 0, r;
46 greg 1.1 FVECT lastp, p;
47     int i, nseg;
48    
49 greg 1.7 varset("PI", ':', PI);
50 greg 1.8 funset("hermite", 5, ':', l_hermite);
51     funset("bezier", 5, ':', l_bezier);
52     funset("bspline", 5, ':', l_bspline);
53 greg 1.1
54     if (argc < 8)
55     goto userror;
56    
57     for (i = 8; i < argc; i++)
58     if (!strcmp(argv[i], "-e"))
59 greg 1.6 scompile(argv[++i], NULL, 0);
60 greg 1.1 else if (!strcmp(argv[i], "-f"))
61     fcompile(argv[++i]);
62     else
63     goto userror;
64    
65     sprintf(stmp, "%s(t)=%s;", XNAME, argv[3]);
66 greg 1.6 scompile(stmp, NULL, 0);
67 greg 1.1 sprintf(stmp, "%s(t)=%s;", YNAME, argv[4]);
68 greg 1.6 scompile(stmp, NULL, 0);
69 greg 1.1 sprintf(stmp, "%s(t)=%s;", ZNAME, argv[5]);
70 greg 1.6 scompile(stmp, NULL, 0);
71 greg 1.1 sprintf(stmp, "%s(t)=%s;", RNAME, argv[6]);
72 greg 1.6 scompile(stmp, NULL, 0);
73 greg 1.1 nseg = atoi(argv[7]);
74     if (nseg <= 0)
75     goto userror;
76    
77     printhead(argc, argv);
78 greg 1.5 eclock = 0;
79 greg 1.1
80     for (i = 0; i <= nseg; i++) {
81     t = (double)i/nseg;
82     p[0] = funvalue(XNAME, 1, &t);
83     p[1] = funvalue(YNAME, 1, &t);
84     p[2] = funvalue(ZNAME, 1, &t);
85     r = funvalue(RNAME, 1, &t);
86 schorsch 2.5 if (i) {
87 greg 1.1 if (lastr <= r+FTINY && lastr >= r-FTINY) {
88     printf("\n%s cylinder %s.c%d\n",
89     argv[1], argv[2], i);
90     printf("0\n0\n7\n");
91     printf("%18.12g %18.12g %18.12g\n",
92     lastp[0], lastp[1], lastp[2]);
93     printf("%18.12g %18.12g %18.12g\n",
94     p[0], p[1], p[2]);
95     printf("%18.12g\n", r);
96     } else {
97     printf("\n%s cone %s.c%d\n",
98     argv[1], argv[2], i);
99     printf("0\n0\n8\n");
100     f = (lastr - r)/dist2(lastp,p);
101     printf("%18.12g %18.12g %18.12g\n",
102     lastp[0] + f*lastr*(p[0] - lastp[0]),
103     lastp[1] + f*lastr*(p[1] - lastp[1]),
104     lastp[2] + f*lastr*(p[2] - lastp[2]));
105     printf("%18.12g %18.12g %18.12g\n",
106     p[0] + f*r*(p[0] - lastp[0]),
107     p[1] + f*r*(p[1] - lastp[1]),
108     p[2] + f*r*(p[2] - lastp[2]));
109     f = 1.0 - (lastr-r)*f;
110     f = f <= 0.0 ? 0.0 : sqrt(f);
111     printf("%18.12g %18.12g\n", f*lastr, f*r);
112     }
113 schorsch 2.5 }
114 greg 1.1 printf("\n%s sphere %s.s%d\n", argv[1], argv[2], i);
115     printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n",
116     p[0], p[1], p[2], r);
117     VCOPY(lastp, p);
118     lastr = r;
119     }
120 schorsch 2.7 return 0;
121 greg 1.1
122     userror:
123     fprintf(stderr,
124     "Usage: %s material name x(t) y(t) z(t) r(t) nseg [-e expr] [-f file]\n",
125     argv[0]);
126 schorsch 2.7 return 1;
127 greg 1.1 }
128    
129    
130 greg 2.4 void
131 greg 1.1 eputs(msg)
132     char *msg;
133     {
134     fputs(msg, stderr);
135     }
136    
137    
138 greg 2.4 void
139 greg 1.1 wputs(msg)
140     char *msg;
141     {
142     eputs(msg);
143     }
144    
145    
146 greg 2.4 void
147 greg 1.1 quit(code)
148 greg 2.3 int code;
149 greg 1.1 {
150     exit(code);
151     }
152    
153    
154 schorsch 2.7 void
155     printhead( /* print command header */
156     register int ac,
157     register char **av
158     )
159 greg 1.1 {
160     putchar('#');
161     while (ac--) {
162     putchar(' ');
163     fputs(*av++, stdout);
164     }
165     putchar('\n');
166     }
167    
168    
169     double
170 greg 2.6 l_hermite(char *nm)
171 greg 1.1 {
172     double t;
173    
174     t = argument(5);
175     return( argument(1)*((2.0*t-3.0)*t*t+1.0) +
176     argument(2)*(-2.0*t+3.0)*t*t +
177     argument(3)*((t-2.0)*t+1.0)*t +
178     argument(4)*(t-1.0)*t*t );
179 greg 1.3 }
180    
181    
182     double
183 greg 2.6 l_bezier(char *nm)
184 greg 1.3 {
185     double t;
186    
187     t = argument(5);
188     return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
189     argument(2) * 3.*t*(1.+t*(-2.+t)) +
190     argument(3) * 3.*t*t*(1.-t) +
191     argument(4) * t*t*t );
192 greg 1.4 }
193    
194    
195     double
196 greg 2.6 l_bspline(char *nm)
197 greg 1.4 {
198     double t;
199    
200     t = argument(5);
201     return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
202     argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
203     argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
204     argument(4) * (1./6.*t*t*t) );
205 greg 1.1 }