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