1 |
/* |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
* genbranch.c - program to generate 3D Christmas tree branches. |
7 |
* |
8 |
* 8/23/86 |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
|
13 |
#include "random.h" |
14 |
|
15 |
#define errf() (var*(0.5-frandom())) |
16 |
|
17 |
double bstart[3] = {0.0, 0.0, 0.0}; /* start of branch */ |
18 |
double bend[3] = {28.0, 8.0, 0.0}; /* end of branch */ |
19 |
double bthick = .6; /* branch radius at base */ |
20 |
double bnarrow = .4; /* ratio of tip radius to base */ |
21 |
double bratio = .4; /* ratio of limb to branch */ |
22 |
char *branchmat = "m_bark"; /* bark material */ |
23 |
|
24 |
char *leafmat = "m_leaf"; /* leaf material */ |
25 |
|
26 |
int nshoots = 7; /* number of offshoots */ |
27 |
int rdepth = 3; /* recursion depth */ |
28 |
|
29 |
double var = 0.3; /* variability */ |
30 |
|
31 |
|
32 |
main(argc, argv) |
33 |
int argc; |
34 |
char *argv[]; |
35 |
{ |
36 |
double atof(); |
37 |
int i, j; |
38 |
|
39 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
40 |
switch (argv[i][1]) { |
41 |
case 'b': /* branch */ |
42 |
switch (argv[i][2]) { |
43 |
case 's': /* start */ |
44 |
bstart[0] = atof(argv[++i]); |
45 |
bstart[1] = atof(argv[++i]); |
46 |
bstart[2] = atof(argv[++i]); |
47 |
break; |
48 |
case 'e': /* end */ |
49 |
bend[0] = atof(argv[++i]); |
50 |
bend[1] = atof(argv[++i]); |
51 |
bend[2] = atof(argv[++i]); |
52 |
break; |
53 |
case 't': /* thickness */ |
54 |
bthick = atof(argv[++i]); |
55 |
break; |
56 |
case 'n': /* narrow */ |
57 |
bnarrow = atof(argv[++i]); |
58 |
break; |
59 |
case 'r': /* ratio */ |
60 |
bratio = atof(argv[++i]); |
61 |
break; |
62 |
case 'm': /* material */ |
63 |
branchmat = argv[++i]; |
64 |
break; |
65 |
default: |
66 |
goto unkopt; |
67 |
} |
68 |
break; |
69 |
case 'l': /* leaf */ |
70 |
switch (argv[i][2]) { |
71 |
case 'm': /* material */ |
72 |
leafmat = argv[++i]; |
73 |
break; |
74 |
default: |
75 |
goto unkopt; |
76 |
} |
77 |
break; |
78 |
case 'n': /* number of offshoots */ |
79 |
nshoots = atoi(argv[++i]); |
80 |
break; |
81 |
case 'r': /* recursion depth */ |
82 |
rdepth = atoi(argv[++i]); |
83 |
break; |
84 |
case 's': /* seed */ |
85 |
j = atoi(argv[++i]); |
86 |
while (j-- > 0) |
87 |
frandom(); |
88 |
break; |
89 |
case 'v': /* variability */ |
90 |
var = atof(argv[++i]); |
91 |
break; |
92 |
default:; |
93 |
unkopt: fprintf(stderr, "%s: unknown option: %s\n", |
94 |
argv[0], argv[i]); |
95 |
exit(1); |
96 |
} |
97 |
|
98 |
if (i != argc) { |
99 |
fprintf(stderr, "%s: bad argument\n", argv[0]); |
100 |
exit(1); |
101 |
} |
102 |
printhead(argc, argv); |
103 |
|
104 |
branch(bstart, bend, bthick, rdepth); |
105 |
|
106 |
return(0); |
107 |
} |
108 |
|
109 |
|
110 |
branch(beg, end, rad, lvl) /* generate branch recursively */ |
111 |
double beg[3], end[3]; |
112 |
double rad; |
113 |
int lvl; |
114 |
{ |
115 |
double sqrt(); |
116 |
double newbeg[3], newend[3]; |
117 |
double t; |
118 |
int i, j; |
119 |
|
120 |
if (lvl == 0) { |
121 |
stick(leafmat, beg, end, rad); |
122 |
return; |
123 |
} |
124 |
|
125 |
stick(branchmat, beg, end, rad); |
126 |
|
127 |
for (i = 1; i <= nshoots; i++) { |
128 |
/* right branch */ |
129 |
t = (i+errf())/(nshoots+2); |
130 |
t = (t + sqrt(t))/2.0; |
131 |
for (j = 0; j < 3; j++) { |
132 |
newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; |
133 |
newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); |
134 |
} |
135 |
newend[0] += (end[2]-newbeg[2])*bratio*(1+errf()); |
136 |
newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); |
137 |
newend[2] -= (end[0]-newbeg[0])*bratio*(1+errf()); |
138 |
branch(newbeg, newend, |
139 |
(1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); |
140 |
|
141 |
/* left branch */ |
142 |
t = (i+errf())/(nshoots+2); |
143 |
t = (t + sqrt(t))/2.0; |
144 |
for (j = 0; j < 3; j++) { |
145 |
newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; |
146 |
newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); |
147 |
} |
148 |
newend[0] -= (end[2]-newbeg[2])*bratio*(1+errf()); |
149 |
newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); |
150 |
newend[2] += (end[0]-newbeg[0])*bratio*(1+errf()); |
151 |
branch(newbeg, newend, |
152 |
(1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); |
153 |
} |
154 |
} |
155 |
|
156 |
|
157 |
stick(mat, beg, end, rad) /* output a branch or leaf */ |
158 |
char *mat; |
159 |
double beg[3], end[3]; |
160 |
double rad; |
161 |
{ |
162 |
static int nsticks = 0; |
163 |
|
164 |
printf("\n%s cone s%d\n", mat, nsticks); |
165 |
printf("0\n0\n8\n"); |
166 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", beg[0], beg[1], beg[2]); |
167 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", end[0], end[1], end[2]); |
168 |
printf("\t%18.12g\t%18.12g\n", rad, bnarrow*rad); |
169 |
|
170 |
printf("\n%s sphere e%d\n", mat, nsticks); |
171 |
printf("0\n0\n4"); |
172 |
printf("\t%18.12g\t%18.12g\t%18.12g\%18.12g\n", |
173 |
end[0], end[1], end[2], bnarrow*rad); |
174 |
|
175 |
nsticks++; |
176 |
} |
177 |
|
178 |
|
179 |
printhead(ac, av) /* print command header */ |
180 |
register int ac; |
181 |
register char **av; |
182 |
{ |
183 |
putchar('#'); |
184 |
while (ac--) { |
185 |
putchar(' '); |
186 |
fputs(*av++, stdout); |
187 |
} |
188 |
putchar('\n'); |
189 |
} |