- for(int j=0; j<ngears; j++) {
- meshing[i][j] = abs(i - j) & 1 ? true : false;
- if(meshing[i][j]) {
- printf("connecting %d - %d\n", i, j);
+ ppos[i] = gears[i]->get_planar_position();
+ }
+
+ for(int i=0; i<ngears; i++) {
+ for(int j=i; j<ngears; j++) {
+ meshing[i][j] = meshing[j][i] = false;
+
+ if(i == j) continue;
+
+ if(1.0 - fabs(dot(gears[i]->axis, gears[j]->axis)) < 1e-5) {
+ // co-planar, just check Z range after inverse-transforming to the XY plane
+ if(fabs(ppos[i].z - ppos[j].z) > (gears[i]->thickness + gears[j]->thickness) / 2.0) {
+ continue;
+ }
+ // Z interval match, check distance
+ float dsq = length_sq(ppos[i].xy() - ppos[j].xy());
+
+ float outer_rad_sum = gears[i]->radius + gears[j]->radius;
+ float inner_rad_sum = outer_rad_sum - gears[i]->teeth_length - gears[j]->teeth_length;
+
+ if(dsq <= outer_rad_sum * outer_rad_sum && dsq >= inner_rad_sum * inner_rad_sum) {
+ printf("connecting co-planar gears %d - %d\n", i, j);
+ meshing[i][j] = meshing[j][i] = true;
+ }
+
+ } else {
+ /* TODO: not co-planar
+ * - calc line of intersection between the two planes
+ * - find distance of each gear to that line
+ * - profit...
+ */