ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genbranch.c
(Generate patch)

Comparing ray/src/gen/genbranch.c (file contents):
Revision 1.1 by greg, Thu Feb 2 11:16:27 1989 UTC vs.
Revision 2.6 by schorsch, Sun Jun 8 12:03:09 2003 UTC

# Line 1 | Line 1
1 /*
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
4 + /*
5   *  genbranch.c - program to generate 3D Christmas tree branches.
6   *
7   *     8/23/86
# Line 10 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9  
10   #include  <stdio.h>
11  
12 + #include <stdlib.h>
13 +
14 + #include  <math.h>
15 +
16   #include  "random.h"
17  
18   #define  errf()         (var*(0.5-frandom()))
# Line 29 | Line 32 | int  rdepth = 3;                       /* recursion depth */
32   double  var = 0.3;                      /* variability */
33  
34  
35 + 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   main(argc, argv)
120   int  argc;
121   char  *argv[];
122   {
36        double  atof();
123          int  i, j;
124  
125          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 106 | Line 192 | unkopt:                        fprintf(stderr, "%s: unknown option: %s\n",
192          return(0);
193   }
194  
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 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines