ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genbranch.c
Revision: 2.6
Committed: Sun Jun 8 12:03:09 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.5: +85 -82 lines
Log Message:
Reduced compile warnings/errors on Windows.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.6 static const char RCSid[] = "$Id: genbranch.c,v 2.5 2003/02/22 02:07:23 greg 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     printhead(ac, av) /* print command header */
107     register int ac;
108     register char **av;
109     {
110     putchar('#');
111     while (ac--) {
112     putchar(' ');
113     fputs(*av++, stdout);
114     }
115     putchar('\n');
116     }
117    
118    
119 greg 1.1 main(argc, argv)
120     int argc;
121     char *argv[];
122     {
123     int i, j;
124    
125     for (i = 1; i < argc && argv[i][0] == '-'; i++)
126     switch (argv[i][1]) {
127     case 'b': /* branch */
128     switch (argv[i][2]) {
129     case 's': /* start */
130     bstart[0] = atof(argv[++i]);
131     bstart[1] = atof(argv[++i]);
132     bstart[2] = atof(argv[++i]);
133     break;
134     case 'e': /* end */
135     bend[0] = atof(argv[++i]);
136     bend[1] = atof(argv[++i]);
137     bend[2] = atof(argv[++i]);
138     break;
139     case 't': /* thickness */
140     bthick = atof(argv[++i]);
141     break;
142     case 'n': /* narrow */
143     bnarrow = atof(argv[++i]);
144     break;
145     case 'r': /* ratio */
146     bratio = atof(argv[++i]);
147     break;
148     case 'm': /* material */
149     branchmat = argv[++i];
150     break;
151     default:
152     goto unkopt;
153     }
154     break;
155     case 'l': /* leaf */
156     switch (argv[i][2]) {
157     case 'm': /* material */
158     leafmat = argv[++i];
159     break;
160     default:
161     goto unkopt;
162     }
163     break;
164     case 'n': /* number of offshoots */
165     nshoots = atoi(argv[++i]);
166     break;
167     case 'r': /* recursion depth */
168     rdepth = atoi(argv[++i]);
169     break;
170     case 's': /* seed */
171     j = atoi(argv[++i]);
172     while (j-- > 0)
173     frandom();
174     break;
175     case 'v': /* variability */
176     var = atof(argv[++i]);
177     break;
178     default:;
179     unkopt: fprintf(stderr, "%s: unknown option: %s\n",
180     argv[0], argv[i]);
181     exit(1);
182     }
183    
184     if (i != argc) {
185     fprintf(stderr, "%s: bad argument\n", argv[0]);
186     exit(1);
187     }
188     printhead(argc, argv);
189    
190     branch(bstart, bend, bthick, rdepth);
191    
192     return(0);
193     }
194