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