ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genworm.c
Revision: 2.11
Committed: Sat Dec 7 02:21:42 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +2 -2 lines
Log Message:
Changed gen{worm,surf,rev} to accept expressions for number of segments

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: genworm.c,v 2.10 2018/05/04 23:56:49 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 <stdlib.h>
15 #include <stdio.h>
16 #include <math.h>
17 #include <string.h>
18
19 #include "calcomp.h"
20 #include "rtio.h"
21 #include "resolu.h"
22 #include "rterror.h"
23 #include "fvect.h"
24
25 #define XNAME "X`SYS`" /* x function name */
26 #define YNAME "Y`SYS`" /* y function name */
27 #define ZNAME "Z`SYS`" /* z function name */
28 #define RNAME "R`SYS`" /* r function name */
29
30 #define PI 3.14159265358979323846
31
32 #define max(a,b) ((a) > (b) ? (a) : (b))
33
34
35 /* XXX redundant, move to library */
36 double l_hermite(char *), l_bezier(char *), l_bspline(char *);
37
38
39 int
40 main(argc, argv)
41 int argc;
42 char *argv[];
43 {
44 char stmp[256];
45 double t, f, lastr = 0, r;
46 FVECT lastp, p;
47 int i, nseg;
48
49 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
50 esupport &= ~(E_OUTCHAN|E_INCHAN);
51 varset("PI", ':', PI);
52 funset("hermite", 5, ':', l_hermite);
53 funset("bezier", 5, ':', l_bezier);
54 funset("bspline", 5, ':', l_bspline);
55
56 if (argc < 8)
57 goto userror;
58
59 for (i = 8; i < argc; i++)
60 if (!strcmp(argv[i], "-e"))
61 scompile(argv[++i], NULL, 0);
62 else if (!strcmp(argv[i], "-f")) {
63 char *fpath = getpath(argv[++i], getrlibpath(), 0);
64 if (fpath == NULL) {
65 fprintf(stderr, "%s: cannot find file '%s'\n",
66 argv[0], argv[i]);
67 quit(1);
68 }
69 fcompile(fpath);
70 } else
71 goto userror;
72
73 sprintf(stmp, "%s(t)=%s;", XNAME, argv[3]);
74 scompile(stmp, NULL, 0);
75 sprintf(stmp, "%s(t)=%s;", YNAME, argv[4]);
76 scompile(stmp, NULL, 0);
77 sprintf(stmp, "%s(t)=%s;", ZNAME, argv[5]);
78 scompile(stmp, NULL, 0);
79 sprintf(stmp, "%s(t)=%s;", RNAME, argv[6]);
80 scompile(stmp, NULL, 0);
81 nseg = eval(argv[7]) + .5;
82 if (nseg <= 0)
83 goto userror;
84
85 fputs("# ", stdout);
86 printargs(argc, argv, stdout);
87 eclock = 0;
88
89 for (i = 0; i <= nseg; i++) {
90 t = (double)i/nseg;
91 p[0] = funvalue(XNAME, 1, &t);
92 p[1] = funvalue(YNAME, 1, &t);
93 p[2] = funvalue(ZNAME, 1, &t);
94 r = funvalue(RNAME, 1, &t);
95 if (i) {
96 if (lastr <= r+FTINY && lastr >= r-FTINY) {
97 printf("\n%s cylinder %s.c%d\n",
98 argv[1], argv[2], i);
99 printf("0\n0\n7\n");
100 printf("%18.12g %18.12g %18.12g\n",
101 lastp[0], lastp[1], lastp[2]);
102 printf("%18.12g %18.12g %18.12g\n",
103 p[0], p[1], p[2]);
104 printf("%18.12g\n", r);
105 } else {
106 printf("\n%s cone %s.c%d\n",
107 argv[1], argv[2], i);
108 printf("0\n0\n8\n");
109 f = (lastr - r)/dist2(lastp,p);
110 printf("%18.12g %18.12g %18.12g\n",
111 lastp[0] + f*lastr*(p[0] - lastp[0]),
112 lastp[1] + f*lastr*(p[1] - lastp[1]),
113 lastp[2] + f*lastr*(p[2] - lastp[2]));
114 printf("%18.12g %18.12g %18.12g\n",
115 p[0] + f*r*(p[0] - lastp[0]),
116 p[1] + f*r*(p[1] - lastp[1]),
117 p[2] + f*r*(p[2] - lastp[2]));
118 f = 1.0 - (lastr-r)*f;
119 f = f <= 0.0 ? 0.0 : sqrt(f);
120 printf("%18.12g %18.12g\n", f*lastr, f*r);
121 }
122 }
123 printf("\n%s sphere %s.s%d\n", argv[1], argv[2], i);
124 printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n",
125 p[0], p[1], p[2], r);
126 VCOPY(lastp, p);
127 lastr = r;
128 }
129 return 0;
130
131 userror:
132 fprintf(stderr,
133 "Usage: %s material name x(t) y(t) z(t) r(t) nseg [-e expr] [-f file]\n",
134 argv[0]);
135 return 1;
136 }
137
138
139 double
140 l_hermite(char *nm)
141 {
142 double t;
143
144 t = argument(5);
145 return( argument(1)*((2.0*t-3.0)*t*t+1.0) +
146 argument(2)*(-2.0*t+3.0)*t*t +
147 argument(3)*((t-2.0)*t+1.0)*t +
148 argument(4)*(t-1.0)*t*t );
149 }
150
151
152 double
153 l_bezier(char *nm)
154 {
155 double t;
156
157 t = argument(5);
158 return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
159 argument(2) * 3.*t*(1.+t*(-2.+t)) +
160 argument(3) * 3.*t*t*(1.-t) +
161 argument(4) * t*t*t );
162 }
163
164
165 double
166 l_bspline(char *nm)
167 {
168 double t;
169
170 t = argument(5);
171 return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
172 argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
173 argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
174 argument(4) * (1./6.*t*t*t) );
175 }