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 2.2 by greg, Thu Dec 19 15:09:06 1991 UTC vs.
Revision 2.7 by schorsch, Sun Nov 16 10:29:38 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1989 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  genbranch.c - program to generate 3D Christmas tree branches.
6   *
# Line 12 | 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()))
19  
19 #ifndef atof
20 extern double  atof();
21 #endif
22
20   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 */
# Line 35 | 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 + void
107 + 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 + int
121   main(argc, argv)
122   int  argc;
123   char  *argv[];
# Line 111 | Line 194 | unkopt:                        fprintf(stderr, "%s: unknown option: %s\n",
194          return(0);
195   }
196  
114
115 branch(beg, end, rad, lvl)              /* generate branch recursively */
116 double  beg[3], end[3];
117 double  rad;
118 int  lvl;
119 {
120        double  sqrt();
121        double  newbeg[3], newend[3];
122        double  t;
123        int  i, j;
124        
125        if (lvl == 0) {
126                stick(leafmat, beg, end, rad);
127                return;
128        }
129
130        stick(branchmat, beg, end, rad);
131
132        for (i = 1; i <= nshoots; i++) {
133                                                /* right branch */
134                t = (i+errf())/(nshoots+2);
135                t = (t + sqrt(t))/2.0;
136                for (j = 0; j < 3; j++) {
137                        newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j];
138                        newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2);
139                }
140                newend[0] += (end[2]-newbeg[2])*bratio*(1+errf());
141                newend[1] += (end[1]-newbeg[1])*bratio*(1+errf());
142                newend[2] -= (end[0]-newbeg[0])*bratio*(1+errf());
143                branch(newbeg, newend,
144                                (1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1);
145                
146                                                /* left branch */
147                t = (i+errf())/(nshoots+2);
148                t = (t + sqrt(t))/2.0;
149                for (j = 0; j < 3; j++) {
150                        newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j];
151                        newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2);
152                }
153                newend[0] -= (end[2]-newbeg[2])*bratio*(1+errf());
154                newend[1] += (end[1]-newbeg[1])*bratio*(1+errf());
155                newend[2] += (end[0]-newbeg[0])*bratio*(1+errf());
156                branch(newbeg, newend,
157                                (1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1);
158        }
159 }
160
161
162 stick(mat, beg, end, rad)               /* output a branch or leaf */
163 char  *mat;
164 double  beg[3], end[3];
165 double  rad;
166 {
167        static int  nsticks = 0;
168        
169        printf("\n%s cone s%d\n", mat, nsticks);
170        printf("0\n0\n8\n");
171        printf("\t%18.12g\t%18.12g\t%18.12g\n", beg[0], beg[1], beg[2]);
172        printf("\t%18.12g\t%18.12g\t%18.12g\n", end[0], end[1], end[2]);
173        printf("\t%18.12g\t%18.12g\n", rad, bnarrow*rad);
174
175        printf("\n%s sphere e%d\n", mat, nsticks);
176        printf("0\n0\n4");
177        printf("\t%18.12g\t%18.12g\t%18.12g\%18.12g\n",
178                        end[0], end[1], end[2], bnarrow*rad);
179
180        nsticks++;
181 }
182
183
184 printhead(ac, av)               /* print command header */
185 register int  ac;
186 register char  **av;
187 {
188        putchar('#');
189        while (ac--) {
190                putchar(' ');
191                fputs(*av++, stdout);
192        }
193        putchar('\n');
194 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines