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