/* Make a new gear, place it next to its parent in the scene, with its teeth meshed and the proper velocity. Returns the gear; or 0 if it didn't work. (Call this a bunch of times until either it works, or you decide it's probably not going to.) [Mostly lifted from pinion.c] */ static gear * place_new_gear (ModeInfo *mi, gear *parent) { gears_configuration *bp = &bps[MI_SCREEN(mi)]; int loop_count = 0; gear *g = 0; while (1) { loop_count++; if (loop_count >= 100) { if (g) free_gear (g); g = 0; break; } g = new_gear (mi, parent); if (!g) return 0; /* out of memory? */ if (place_gear (mi, g, parent)) break; } if (! g) return 0; /* We got a gear, and it is properly positioned. Insert it in the scene. */ bp->gears[bp->ngears++] = g; return g; }
/* Make a new gear, place it next to its parent in the scene, with its teeth meshed and the proper velocity. Returns the gear; or 0 if it didn't work. (Call this a bunch of times until either it works, or you decide it's probably not going to.) */ static gear * place_new_gear (ModeInfo *mi, gear *parent, Bool coaxial_p) { pinion_configuration *pp = &pps[MI_SCREEN(mi)]; int loop_count = 0; gear *g = 0; while (1) { loop_count++; if (loop_count >= 100) { if (g) free_gear (g); g = 0; break; } g = new_gear (mi, parent, coaxial_p); if (!g) return 0; /* out of memory? */ if (place_gear (mi, g, parent, coaxial_p)) break; } if (! g) return 0; /* We got a gear, and it is properly positioned. Insert it in the scene. */ if (pp->ngears + 2 >= pp->gears_size) { pp->gears_size += 100; pp->gears = (gear **) realloc (pp->gears, pp->gears_size * sizeof (*pp->gears)); if (! pp->gears) { fprintf (stderr, "%s: out of memory (%d gears)\n", progname, pp->gears_size); } } pp->gears[pp->ngears++] = g; return g; }
static void planetary_gears (ModeInfo *mi) { gears_configuration *bp = &bps[MI_SCREEN(mi)]; gear *g0, *g1, *g2, *g3, *g4; GLfloat distance = 2.02; bp->planetary_p = True; g0 = new_gear (mi, 0); g1 = new_gear (mi, 0); g2 = new_gear (mi, 0); g3 = new_gear (mi, 0); g4 = new_gear (mi, 0); if (! place_gear (mi, g0, 0)) abort(); if (! place_gear (mi, g1, 0)) abort(); if (! place_gear (mi, g2, 0)) abort(); if (! place_gear (mi, g3, 0)) abort(); if (! place_gear (mi, g4, 0)) abort(); g0->nteeth = 12 + (3 * (random() % 10)); /* must be multiple of 3 */ g0->tooth_w = g0->r / g0->nteeth; g0->tooth_h = g0->tooth_w * 2.8; # define COPY(F) g4->F = g3->F = g2->F = g1->F = g0->F COPY(r); COPY(th); COPY(nteeth); COPY(tooth_w); COPY(tooth_h); COPY(tooth_slope); COPY(inner_r); COPY(inner_r2); COPY(inner_r3); COPY(thickness); COPY(thickness2); COPY(thickness3); COPY(ratio); COPY(size); # undef COPY g1->x = cos (M_PI * 2 / 3) * g1->r * distance; g1->y = sin (M_PI * 2 / 3) * g1->r * distance; g2->x = cos (M_PI * 4 / 3) * g2->r * distance; g2->y = sin (M_PI * 4 / 3) * g2->r * distance; g3->x = cos (M_PI * 6 / 3) * g3->r * distance; g3->y = sin (M_PI * 6 / 3) * g3->r * distance; g4->x = 0; g4->y = 0; g4->th = -g3->th; /* rotate central gear 1/2 tooth-size if odd number of teeth */ if (g4->nteeth & 1) g4->th -= (180.0 / g4->nteeth); g0->inverted_p = True; g0->x = 0; g0->y = 0; g0->nteeth = g1->nteeth * 3; g0->r = g1->r * 3.05; g0->inner_r = g0->r * 0.8; g0->inner_r2 = 0; g0->inner_r3 = 0; g0->th = g1->th + (180 / g0->nteeth); g0->ratio = g1->ratio / 3; g0->tooth_slope = 0; g0->nubs = 3; g0->spokes = 0; g0->size = INVOLUTE_LARGE; bp->gears = (gear **) calloc (6, sizeof(**bp->gears)); bp->ngears = 0; bp->gears[bp->ngears++] = g1; bp->gears[bp->ngears++] = g2; bp->gears[bp->ngears++] = g3; bp->gears[bp->ngears++] = g4; bp->gears[bp->ngears++] = g0; }