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