chiark / gitweb /
79e30383e1e032b0ec5c4de7fb5e2a083ccbd8b2
[reprap-play.git] / sewing-table.scad.m4
1 // -*- C -*-
2
3 include <funcs.scad>
4 include <commitid.scad>
5
6 ply_th = 18;
7 ply_hole_dia = 15;
8 ply_edge_min = 10;
9
10 tile_th = 3;
11 post_dia = 8;
12
13 post_shorter = 1;
14
15 screw_dia = 2.2;
16 screw_big_dia = 3.6;
17 screw_big_len = 4.0;
18
19 round_edge_rad = 2.0;
20
21 round_cnr_rad = 10;
22
23 interlock_dia = 10;
24 interlock_fine = 0.66;
25
26 interlock_fine_slope = 1.0;
27 interlock_fine_lenslop = 1.0;
28
29 demo_slop = 0.1;
30
31 // cutout
32
33 machine_rear_to_front = 84 + 0.25 - 1.4;
34
35 cutout_l_end_y_front_slop = 0.5;
36 cutout_l_end_y_rear_slop = 0.5;
37 cutout_l_end_x_slop = 0.25;
38
39 cutout_l_end_x = 22;
40 cutout_l_end_y = machine_rear_to_front;
41 cutout_l_end_curve = 1;
42 cutout_l_end_new_x_slop = -1.4;
43 cutout_l_end_y_total = cutout_l_end_y
44   + cutout_l_end_y_front_slop + cutout_l_end_y_rear_slop;
45
46 tile02_tr = [-250, 0];
47 tile01_tr = [  0, 0];
48
49 cutout_tile01_y = 170 - 147 + cutout_l_end_y_front_slop;
50 cutout_tile11_x = cutout_l_end_x + cutout_l_end_curve;
51 cutout_tile11_y = cutout_l_end_y_total - cutout_tile01_y;
52
53 // front and rear curves
54
55 rearedge_len = 170;
56 frontedge_len = 170;
57
58 rearcurve_strt_len = 52;
59
60 rearcurve_z_slop = -0.50;
61
62 rearcurve_avoid_y = 35;
63
64 rearcurve_double_inrad = 26.10 + 8.04;
65
66 reartablet_z = 2.54;
67 reartablet_x = 5 + 1;
68 reartablet_y = 8;
69
70 frontcurve_side_skew = 3.5 / 72;
71 frontcurve_avoid_y = 70;
72 frontcurve_z_slop = 0.75;
73
74 frontcurve_strt_len = 50;
75 frontcurve_dualcurve_angle = 30;
76
77 teststrapslots_at = [ [ 110, 70 ], [ 110, -35 ],
78                       [ 180, 90 ],
79                       [ 190, -80 ], // do not change index of this one
80                       [   0, 70 ],  [  0, -35 ],
81                       ];
82
83 teststrap = [ 3, 5 ];
84 teststrap_peg = [7.5, 3.5];
85
86 // calculated
87
88 TEST = false;
89
90 ply_edge_hole_dist = ply_edge_min + ply_hole_dia/2;
91
92 echo(str("HOLES IN PLY ctr dist from PLY edge = ", ply_edge_hole_dist));
93
94 hole_slop = (ply_hole_dia - post_dia)/2;
95 tile_hard_edge_hole_dist = ply_edge_hole_dist + hole_slop;
96
97 echo(str("HOLES IN PLY ctr dist from TILE HARD edge = ",
98          tile_hard_edge_hole_dist));
99
100 echo(str("HOLES IN PLY ctr dist from TILE ROUND edge = ",
101          tile_hard_edge_hole_dist + round_edge_rad));
102
103 thehd = [ tile_hard_edge_hole_dist, tile_hard_edge_hole_dist ];
104 thehd_tr = thehd;
105 thehd_tl = [ -thehd_tr[0], thehd_tr[1] ];
106 thehd_bl = -thehd_tr;
107 thehd_br = -thehd_tl;
108
109 tablet_z_slop = 0.25;
110
111 interlock_rad = interlock_dia/2;
112 interlock_negative_rad = interlock_rad + 0.125;
113
114 interlock_sq_adj = 0.2; // arbitrary
115
116 m4_define(`POST_TCROSSSZ',
117           `2*( tile_hard_edge_hole_dist - test_edge + 1 )')
118
119 module Post(){
120   mirror([0,0,1]) {
121     difference(){
122       cylinder(r= post_dia/2, h= tile_th + ply_th - post_shorter);
123       translate([0,0, tile_th]) {
124         cylinder(r= screw_big_dia/2, h= screw_big_len);
125         cylinder(r= screw_dia/2, h= ply_th, $fn=20);
126       }
127     }
128     if (TEST) {
129       translate([0,0, tile_th/2]) {
130         cube([post_dia,      POST_TCROSSSZ, tile_th], center=true);
131         cube([POST_TCROSSSZ, post_dia,      tile_th], center=true);
132       }
133     }
134   }
135 }
136
137 module Posts(posts) {
138   for (p= posts) {
139     translate(concat(p, [0]))
140       Post();
141   }
142 }
143
144 module TileBase(botleft, topright){
145   size = topright - botleft;
146   botleft_post = botleft + thehd_tr;
147   topright_post = topright + thehd_bl;
148   difference(){
149     mirror([0,0,1])
150       translate(concat(botleft, [0]))
151       cube(concat(size, [tile_th]));
152     if (!TEST) {
153       cidsz = topright_post-botleft_post
154         + [-post_dia,-post_dia]
155         + [0, thehd[1]];
156       cidszr = [ min(cidsz[0],50), min(cidsz[1],50) ];
157       echo("CID",cidsz,cidszr);
158       translate( concat(botleft_post, [ -tile_th ])
159                  + 0.5 * [ post_dia, post_dia, 0 ]
160                  + 0.5 * concat( cidsz - cidszr, [ 0 ]) )
161         Commitid_BestCount_M(cidszr);
162     }
163     if (TEST) {
164       crossoff = tile_hard_edge_hole_dist + POST_TCROSSSZ/2;
165       cidsz = [ thehd[0], size[1] - 2*crossoff ];
166       cidszr = [ cidsz[0], min(cidsz[1], 50) ];
167       translate( concat(botleft + [0, crossoff] + (cidsz-cidszr)/2, [0]) )
168         Commitid_BestCount(cidszr);
169       difference(){
170         mirror([0,0,1]) {
171           translate(concat(botleft + [test_edge,test_edge], [test_tile_th]))
172             cube(concat(size - [test_edge,test_edge]*2, [tile_th]));
173           translate(concat(botleft_post, [-1]))
174             cube(concat(topright_post-botleft_post, [tile_th+2]));
175         }
176         shufflesz = max(test_edge, tile_hard_edge_hole_dist)*2;
177         minkowski(){
178           MachineEnvelope();
179           cube(shufflesz, center=true);
180         }
181       }
182     }
183   }
184 }
185
186 m4_dnl   R_EDGE(c,ix)
187 m4_dnl        c is from Rectangle_corners and
188 m4_dnl        ix is a corner number
189 m4_dnl    expands to two comma-separated corners:
190 m4_dnl    that denoted by ix, and the next one anticlockwise
191 m4_define(`R_EDGE',`$1[$2], $1[(($2)+1)%4]')
192
193 m4_dnl   R_CNR(c,ix)
194 m4_dnl        c is from Rectangle_corners and
195 m4_dnl        ix is a corner number
196 m4_dnl    expands to an array of corners as for RoundCorner
197 m4_define(`R_CNR',`[ $1[$2], $1[(($2)+1)%4], $1[(($2)+3)%4] ]')
198
199 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars) { body; }
200 m4_define(`INREFFRAME',`
201   length_vec = ($2) - ($1);
202   length = dist2d([0,0], length_vec);
203   length_uvec = length_vec / length;
204   ortho_uvec = [ -length_uvec[1], length_uvec[0] ];
205   m = [ [ length_uvec[0],  ortho_uvec[0], 0, ($1)[0], ],
206         [ length_uvec[1],  ortho_uvec[1], 0, ($1)[1], ],
207         [ 0,              0,              1,            0, ],
208         [ 0,              0,              0,            1, ] ];
209   $3
210   multmatrix(m)
211 ')
212
213 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars)
214 m4_dnl    INREFFRAME_EDGE { body; }
215 m4_define(`INREFFRAME_EDGE',`
216   translate([0,0, -round_edge_rad])
217 ')
218
219 module RoundEdge(left_cnr, right_cnr) {
220   INREFFRAME(left_cnr, right_cnr)
221     INREFFRAME_EDGE {
222     difference(){
223       rotate([0,90,0])
224         cylinder(r= round_edge_rad, h= length, $fn=50);
225       translate([-1, 0, -20])
226         cube([length+2, 20, 20]);
227     }
228   }
229 }
230
231 m4_define(`ROUNDCORNER_VARS',`
232   this_cnr = ci[0];
233   right_cnr = ci[1];
234   left_cnr = ci[2];
235   bigr= round_cnr_rad - round_edge_rad;
236   l_uvec = unitvector2d(left_cnr - this_cnr);
237   r_uvec = unitvector2d(right_cnr - this_cnr);
238   lp1 = left_cnr  + clockwise2d(l_uvec) * bigr;
239   lp2 = this_cnr  + clockwise2d(l_uvec) * bigr;
240   lp3 = this_cnr  - clockwise2d(r_uvec) * bigr;
241   lp4 = right_cnr - clockwise2d(r_uvec) * bigr;
242   ctr = line_intersection_2d(lp1,lp2,lp3,lp4);
243   ctr3 = concat(ctr,[0])
244 ')
245
246 module RoundCorner_selector(ci, adj) {
247   ROUNDCORNER_VARS;
248   intersection(){
249     union(){
250       INREFFRAME(ctr3,concat(lp1,[4])){
251         translate([0,0,-bigr]) linear_extrude(height=bigr*2) {
252           translate([-bigr*2 + adj, -bigr])
253             square([bigr*2, bigr*3]);
254         }
255       }
256     }
257     union(){
258       INREFFRAME(ctr3,concat(lp4,[0])){
259         translate([0,0,-bigr]) linear_extrude(height=bigr*2) {
260           translate([-bigr*2, -bigr*2])
261             square([bigr*2 + adj, bigr*3]);
262         }
263       }
264     }
265   }
266 }
267
268 module RoundCornerCut(ci) {
269   // ci should be [this_cnr, right_cnr, left_cnr]
270   // where right_cnr is to the right (ie, anticlockwise)
271   ROUNDCORNER_VARS;
272   difference(){
273     RoundCorner_selector(ci, -0.1);
274     translate(ctr3)
275       cylinder(center=true, h=20, r= bigr);
276   }
277 }
278
279 module RoundCornerAdd(ci) {
280   ROUNDCORNER_VARS;
281   intersection(){
282     RoundCorner_selector(ci, +0.1);
283     INREFFRAME_EDGE {
284       translate(ctr3){
285         rotate_extrude(convexity=10, $fn=50)
286           translate([bigr, 0])
287           difference(){
288           circle(r= round_edge_rad, $fn=50);
289           mirror([1,1])
290             square([20,20]);
291         }
292       }
293     }
294   }
295 }
296
297 module InterlockLobePlan(negative) {
298   r = negative ? interlock_negative_rad : interlock_rad;
299   ymir = negative ? 0 : 1;
300
301   dx = sqrt(3) * r;
302   $fn= 80;
303   translate([thehd[0], 0]){
304     mirror([0,ymir]){
305       circle(r=r);
306       difference(){
307         translate([-dx, -0.1])
308           square([ dx*2, r/2 + 0.1 ]);
309         for (xi = [-1, 1]) {
310           translate([ xi*dx, r ])
311             circle(r=r);
312         }
313       }
314     }
315   }
316 }
317
318 module InterlockEdgePlan(negative, nlobes, length, dosquare=true) {
319   for (lobei = [ 0 : nlobes-1 ]) {
320     lobex = (length - thehd[0]*2) * (lobei ? lobei / (nlobes-1) : 0);
321     translate([lobex, 0, 0]) {
322       InterlockLobePlan(negative);
323     }
324   }
325
326   if (dosquare) {
327     iadj = 0;
328     slotshorter = negative ? -0.1 : interlock_fine_lenslop;
329     mirror([0, negative])
330       translate([slotshorter, iadj])
331       square([length - slotshorter*2, interlock_fine + iadj*2]);
332   }
333 }
334
335 module InterlockEdge(left_cnr, right_cnr, negative=0, nlobes=2) {
336   plusth = negative * 1.0;
337   protr = interlock_fine + interlock_sq_adj;
338
339   z2 = -tile_th/2;
340   z1 = -tile_th/2 - protr / interlock_fine_slope;
341   z3 = -tile_th/2 + protr / interlock_fine_slope;
342
343   negsign = negative ? -1 : +1;
344   yprotr = negsign * protr;
345
346   INREFFRAME(left_cnr, right_cnr) {
347     for (vsect = [ // zs0            zs1      ys0,            ys1
348                   [ -tile_th-plusth, plusth,  0,              0],
349                   [ z1,              z2,      0, yprotr],
350                   [ z2,              z3,      yprotr, 0],
351                   ]) {
352       zs0 = vsect[0];
353       zs1 = vsect[1];
354       zsd = zs1-zs0;
355       ys0 = vsect[2];
356       ys1 = vsect[3];
357       ysd = ys1-ys0;
358       sl = ysd/zsd;
359       m = [ [ 1,0,   0,    0 ],
360             [ 0,1, -sl, -ys0 + negsign*interlock_sq_adj ],
361             [ 0,0,   1,  zs0 ],
362             [ 0,0,   0,    1 ] ];
363       multmatrix(m)
364         linear_extrude(height=zsd, convexity=10)
365         InterlockEdgePlan(negative, nlobes, length, !!ysd);
366     }
367   }
368 }
369
370 function TestPiece_holes2corners(holes) =
371   [ holes[0] + thehd_bl,
372     holes[1] + thehd_br,
373     holes[1] + thehd_tr,
374     holes[0] + thehd_tl ];
375
376 module TestPiece1(){ ////toplevel
377   holes = [ [-100, 0],
378             [   0, 0]
379             ];
380   corners = TestPiece_holes2corners(holes);
381   rcs = R_CNR(corners,0);
382   difference(){
383     union(){
384       TileBase(corners[0], corners[2]);
385       Posts(holes);
386       RoundEdge(corners[0], corners[1]);
387       RoundEdge(corners[3], corners[0]);
388     }
389     InterlockEdge(corners[1], corners[2], 1, nlobes=1);
390     RoundCornerCut(rcs);
391   }
392   RoundCornerAdd(rcs);
393 }
394
395 module TestPiece2(){ ////toplevel
396   holes = [ [   0, 0],
397             [  50, 0]
398             ];
399   corners = TestPiece_holes2corners(holes);
400   TileBase(corners[0], corners[2]);
401   Posts(holes);
402   RoundEdge(corners[0], corners[1]);
403   InterlockEdge(corners[3], corners[0], 0, nlobes=1);
404 }
405
406 module TestDemo(){ ////toplevel
407   translate([ -thehd[0], 0 ])
408     color("blue")
409     TestPiece1();
410   translate([ +thehd[0] + demo_slop, 0 ])
411     TestPiece2();
412 }
413
414 module Machine_Arm(){
415   ysz = cutout_l_end_y_total;
416   // assume the round end is arc of a circle
417   chordlen = dist2d([0,0], [ cutout_l_end_y, cutout_l_end_curve ]);
418   endrad = cutout_l_end_y / cutout_l_end_curve * chordlen;
419   
420   translate([0,0,-30]) linear_extrude(height=60) {
421     translate(tile01_tr + [0, (-cutout_tile01_y + cutout_tile11_y)/2]) {
422       intersection(){
423         translate([-50, -ysz/2])
424           square([400, ysz]);
425         translate([ endrad - cutout_tile11_x - cutout_l_end_x_slop, 0 ])
426           circle(r=endrad, $fa=0.01,$fd=5);
427       }
428     }
429   }
430 }
431
432 module Machine_NewRearProfile(){
433   // figures copied out of xfig edit boxes
434   // best not to edit the posbox size if poss - just move it
435   posbox = 10 * ([7.2333,-14.1267] - [-16.2289,40.0289]); // box, Green
436   sideline = -10 * ([-6.2400,13.5600] - [-2.4467,28.2556]); // line, Blue
437   scaleline = 10 * dist2d([-1.1911,-20.4800], [-11.2600,4.0578]); // Green2
438   scaleline_mm = 12+5+10+5+3;
439   sh = -[abs(posbox[0]), abs(posbox[1])];
440   rot = atan2(-sideline[0], sideline[1]);
441   sc = scaleline_mm / scaleline;
442   //echo("SH",sh,rot,sc);
443   scale(sc) rotate(rot) translate(sh){
444     import("sewing-table-rear-profile.dxf", convexity=10); // spline, Pink3
445   }
446 }
447
448 module Machine_NewFrontProfile(){
449   // figures copied out of xfig edit boxes
450   // best not to edit the posbox size if poss - just move it
451   posbox = 10 * ([11.8022,8.0600] - [4.2044,19.1867]); // box, Green
452   refline = 10 * ([7.6778,16.7222] - [27.8689,17.6578]); // line, Blue
453   refline_mm = (11-1)*10;
454   sh = -[abs(posbox[0]), abs(posbox[1])];
455   rot = atan2(-refline[0], refline[1]);
456   sc = refline_mm / vectorlen2d(refline);
457   //echo("SH",sh,rot,sc);
458   mirror([1,0]) scale(sc) rotate(rot+90) translate(sh){
459     import("sewing-table-front-profile.dxf", convexity=10); // spline, Pink3
460   }
461 }
462
463 module Machine_NewEndProfile(){
464   // figures copied out of xfig edit boxes
465   // NB that y coords are reversed - xfig origin is at bottom left
466   posboxs = 10 * [[4.0400,17.7956], [11.6622,32.5511]]; // box, Pink3
467   refline = 10 * ([8.4000,22.6000] - [50.3000,22.2000]); // line, Blue
468   refline_mm = 10 * (11 - 2.5);
469   sidelines = 10 * [[[9.0889,20.6178], [8.9644,14.6889]],
470                     [[50.3800,21.9578], [50.1933,14.4933]]]; // lines, Blue3
471   baseline = 10 * [[8.4000,18.0822], [50.3000,17.6822]]; // line, Green2
472
473   rot_adj = -0.38;
474
475   posbox = [min(posboxs[0][0],posboxs[1][0]),
476             max(posboxs[0][1],posboxs[1][1])];
477
478   m4_define(`MNEP_ELP',
479      `line_intersection_2d(baseline[0],baseline[1],
480                            sidelines[$1][0],sidelines[$1][1])')
481   endline = [MNEP_ELP(0),MNEP_ELP(1)];
482
483   rot = atan2(-refline[1], -refline[0]);
484   sc = refline_mm / vectorlen2d(refline);
485   sh = (0.5 * (endline[0] + endline[1])) - posbox;
486
487   ellen = sc * dist2d(endline[0],endline[1]);
488   scy = cutout_l_end_y_total / ellen;
489
490   scale([scy,1]) scale(sc) rotate(rot + rot_adj) translate(-[sh[0],-sh[1]]){
491
492     mirror([0,1]){
493   //%translate(1 * (posboxs[0] - posbox)) square(50);
494   //%translate(1 * (posboxs[1] - posbox)) square(50);
495 //  %translate(1 * (baseline[0] - posbox)) square([50,10]);
496
497 //  %translate(1 * (endline[0] - posbox)) square([50,10]);
498 //  %translate(1 * (endline[1] - posbox)) square([50,10]);
499
500 //  %translate(1 * (sidelines[0][0] - posbox)) square([10,50]);
501 //  %translate(1 * (sidelines[0][1] - posbox)) square([10,50]);
502 //  %translate(1 * (sidelines[1][0] - posbox)) square([10,50]);
503 //  %translate(1 * (sidelines[1][1] - posbox)) square([10,50]);
504     }
505
506     import("sewing-table-end-profile.dxf", convexity=10); // spline, Pink3
507   }
508 }
509
510 module Machine_NewEndProfileDemo(){ ////toplevel
511     translate([0,5,0])                             Machine_NewEndProfile();
512     translate([0,5,1]) color("blue") mirror([1,0]) Machine_NewEndProfile();
513   mirror([0,1,0]){
514     translate([0,5, 0])                             Machine_NewEndProfile();
515     translate([0,5,-1]) color("blue") mirror([1,0]) Machine_NewEndProfile();
516   }
517 }
518
519 module Machine_NewArm(){
520   translate([0,0,-30]) linear_extrude(height=60) {
521     translate(tile01_tr + [ -cutout_l_end_x + cutout_l_end_new_x_slop,
522                             (-cutout_tile01_y + cutout_tile11_y)/2 ]){
523       rotate(-90){
524         hull(){
525           for (d=[0,400]) 
526             translate([0,d]) Machine_NewEndProfile();
527         }
528       }
529     }
530   }
531 }
532
533 module Machine_NewRearCurve(){
534   slant = atan2(4,210-10);
535   //echo("SL",slant);
536   translate([0,0, rearcurve_double_inrad]) rotate([slant,0,0]){
537     translate([ rearcurve_double_inrad,
538                 0,
539                 -rearcurve_double_inrad + 10 ]){
540       rotate([180,0,0]) rotate([0,0,90]) linear_extrude(height=30){
541         hull(){
542           Machine_NewRearProfile();
543           translate([0,-100]) Machine_NewRearProfile();
544         }
545       }
546     }
547     rotate([0,90,0]) rotate([90,0,0]) {
548       intersection(){
549         rotate_extrude(convexity=10, $fn=64)
550           rotate(90)
551           translate([ 0, -rearcurve_double_inrad ])
552           Machine_NewRearProfile();
553         translate([0,0,-500])
554           cube([500,500,1000]);
555       }
556     }
557     translate([1,0,-rearcurve_double_inrad])
558       rotate([0,-90,0]) rotate([0,0,-90])
559       linear_extrude(height= rearcurve_strt_len + 1)
560       Machine_NewRearProfile();
561   }
562 }
563
564 module Machine_Curves(){ ////toplevel
565   translate([ tile01_tr[0] - cutout_l_end_x + rearedge_len,
566               cutout_tile11_y,
567               0 ]){
568     //%cube([20,20,20]);
569     translate([ -reartablet_x,
570                 -1,
571                 -reartablet_z + tablet_z_slop])
572       mirror([0,0,1])
573       cube([ reartablet_x+1,
574              reartablet_y+1,
575              20 ]);
576   }
577   translate([ tile01_tr[0] - cutout_l_end_x + frontedge_len,
578               cutout_tile11_y,
579               frontcurve_z_slop ]){
580     translate([0, -machine_rear_to_front, 0])
581       multmatrix([[1, -frontcurve_side_skew, 0, 0],
582                   [0,  1,   0, 0],
583                   [0,  0,   1, 0],
584                   [0,  0,   0, 1]])
585       mirror([1,0,0]) rotate([0,-90,0])rotate([0,0,-90])
586       linear_extrude(height= 200)
587       Machine_NewFrontProfile();
588
589     translate([ rearcurve_strt_len,
590                 0,
591                 rearcurve_z_slop ]){
592       Machine_NewRearCurve();
593     }
594   }
595 }
596
597 module TestStrapSlots(){
598   pegwidth = teststrap_peg[0];
599   for (pos = teststrapslots_at) {
600     echo("TSS",pos);
601     translate(concat(pos,[0]))
602       for (mx = [0,1]) mirror([mx,0,0]) {
603           translate([ pegwidth/2, -teststrap[1]/2, -20 ])
604             cube(concat(teststrap,[40]));
605         }
606   }
607 }
608
609 module TestStrapPeg_any(l){ cube(concat([l],teststrap_peg)); }
610
611 module TestStrapPeg_Short(){ ////toplevel
612   TestStrapPeg_any(35);
613 }
614
615 module TestStrapPeg_Long(){ ////toplevel
616   TestStrapPeg_any(60);
617 }
618
619 module Machine(){ ////toplevel
620   Machine_NewArm();
621   Machine_Curves();
622   if (TEST)
623     TestStrapSlots();
624 }
625
626 module MachineEnvelope(){
627   // used for testing
628   p_arm_bl = [-cutout_l_end_x, -cutout_tile01_y];
629   y_arm_t  = cutout_tile11_y;
630   p_crv_fl = p_arm_bl + [frontedge_len, -frontcurve_avoid_y];
631   y_crv_b  = y_arm_t + rearcurve_avoid_y;
632
633   translate([0,0,-50]) linear_extrude(height= 100){
634     translate(p_arm_bl) square([400, y_arm_t] - p_arm_bl);
635     translate(p_crv_fl) square([400, y_crv_b] - p_crv_fl);
636   }
637 }
638
639 function Rectangle_corners(c0, sz) =
640   // returns the corners of a rectangle from c0 to c0+sz
641   // if sz is positive, the corners are anticlockwise starting with c0
642   [ c0 + [ 0,     0     ],
643     c0 + [ sz[0], 0     ],
644     c0 + [ sz[0], sz[1] ],
645     c0 + [ 0,     sz[1] ] ];
646
647 function Rectangle_corners2posts(c) =
648   [ c[0] + thehd_tr,
649     c[1] + thehd_tl,
650     c[2] + thehd_bl,
651     c[3] + thehd_br ];
652
653 module Rectangle_TileBase(c) { TileBase(c[0], c[2]); }
654
655 function Posts_interpolate_one(c0,c1) = [c0, (c0+c1)/2, c1];
656
657 module Tile02(){ ////toplevel
658   sz = [100,170];
659   c0 = tile02_tr + -sz;
660   c = Rectangle_corners(c0, sz);
661   posts = Rectangle_corners2posts(c);
662   rcs = R_CNR(c,0);
663   difference(){
664     union(){
665       Rectangle_TileBase(c);
666       Posts(posts);
667       RoundEdge(R_EDGE(c,0));
668       RoundEdge(R_EDGE(c,3));
669       InterlockEdge(R_EDGE(c,2), 0);
670     }
671     InterlockEdge(R_EDGE(c,1), 1);
672     RoundCornerCut(rcs);
673   }
674   RoundCornerAdd(rcs);
675 }
676
677 module Tile12(){ ////toplevel
678   sz = [100,250];
679   c0 = tile02_tr + [-sz[0], 0];
680   c = Rectangle_corners(c0, sz);
681   posts = Rectangle_corners2posts(c);
682   rcs = R_CNR(c,3);
683   difference(){
684     union(){
685       Rectangle_TileBase(c);
686       Posts(posts);
687       RoundEdge(R_EDGE(c,2));
688       RoundEdge(R_EDGE(c,3));
689     }
690     InterlockEdge(R_EDGE(c,0), 1);
691     InterlockEdge(R_EDGE(c,1), 1);
692     RoundCornerCut(rcs);
693   }
694   RoundCornerAdd(rcs);
695 }
696
697 tile_01_11_cnr = tile01_tr + [-cutout_l_end_x, 0];
698 tile_11_10_cnr = tile01_tr + [0, cutout_tile11_y];
699 tile_01_00_cnr = tile01_tr - [0, cutout_tile01_y];
700
701 module Tile11(){ ////toplevel
702   sz = [250,250];
703   c0 = tile01_tr + [-sz[0],0];
704   c = Rectangle_corners(c0, sz);
705   cnr_posts = Rectangle_corners2posts(c);
706   posts = concat(
707                  Posts_interpolate_one(cnr_posts[0],
708                                        cnr_posts[1] - [cutout_l_end_x, 0]),
709                  [ cnr_posts[1] + [0, cutout_tile11_y],
710                    cnr_posts[2],
711                    cnr_posts[3]
712                    ]);
713   difference(){
714     union(){
715       Rectangle_TileBase(c);
716       Posts(posts);
717       RoundEdge(R_EDGE(c,2));
718       InterlockEdge(R_EDGE(c,3));
719     }
720     InterlockEdge(c[0], tile_01_11_cnr, 1);
721     InterlockEdge(tile_11_10_cnr, c[2], 1);
722     Machine();
723   }
724 }    
725
726 module Tile01(){ ////toplevel
727   sz = [250,170];
728   c0 = tile01_tr + -sz;
729   c = Rectangle_corners(c0, sz);
730   cnr_posts = Rectangle_corners2posts(c);
731   posts = concat(
732                  Posts_interpolate_one(R_EDGE(cnr_posts,0)),
733                  [ cnr_posts[2] + [0, -cutout_tile01_y] ],
734                  Posts_interpolate_one(cnr_posts[2] - [cutout_l_end_x, 0],
735                                        cnr_posts[3])
736                  );
737   difference(){
738     union(){
739       Rectangle_TileBase(c);
740       Posts(posts);
741       RoundEdge(R_EDGE(c,0));
742       InterlockEdge(tile_01_11_cnr, c[3]);
743       InterlockEdge(R_EDGE(c,3));
744     }
745     InterlockEdge(c[1], tile_01_00_cnr, 1);
746     Machine();
747   }
748 }    
749
750 module Tile10(){ ////toplevel
751   sz = [250,250];
752   c0 = tile01_tr + [0,0];
753   c = Rectangle_corners(c0, sz);
754   cnr_posts = Rectangle_corners2posts(c);
755   cty = cutout_tile11_y;
756   rcy = cty + rearcurve_avoid_y;
757   posts = [ cnr_posts[0] + [ 0,                             cty ],
758             cnr_posts[1] + [ -sz[0] + rearedge_len - cutout_l_end_x, cty ],
759             cnr_posts[1] + [ 0,                             rcy ],
760             cnr_posts[2],
761             cnr_posts[3] ];
762   rcs = R_CNR(c,2);
763   difference(){
764     union(){
765       Rectangle_TileBase(c);
766       Posts(posts);
767       RoundEdge(R_EDGE(c,1));
768       RoundEdge(R_EDGE(c,2));
769       InterlockEdge(c[3], tile_11_10_cnr);
770     }
771     RoundCornerCut(rcs);
772     Machine();
773   }
774   RoundCornerAdd(rcs);
775 }
776
777 module Tile00(){ ////toplevel
778   sz = [250,170];
779   c0 = tile01_tr + [0,-sz[1]];
780   c = Rectangle_corners(c0, sz);
781
782   // the edge c[1]..c[2] needs a diagonal chunk, from c1bis to c2bis
783   c2bis = [ -cutout_l_end_x + frontedge_len + frontcurve_strt_len, c[2][1] ];
784   c1bis = [ c[1][0],
785             c[2][1] -
786             (c[2][0] - c2bis[0]) * tan(90 - frontcurve_dualcurve_angle) ];
787
788   cnr_posts = Rectangle_corners2posts(c);
789   cty = cutout_tile01_y;
790   rcy = cty + frontcurve_avoid_y;
791   posts = [ cnr_posts[0],
792             cnr_posts[1],
793             cnr_posts[2] + [ 0,                             -rcy ],
794             cnr_posts[2] + [ -sz[0] + frontedge_len - cutout_l_end_x, -cty ],
795             cnr_posts[3] + [ 0,                             -cty ]
796             ];
797   rcs = R_CNR(c,1);
798   rc2 = [c1bis,c2bis,c[1]];
799   difference(){
800     union(){
801       difference(){
802         union(){
803           Rectangle_TileBase(c);
804           Posts(posts);
805           RoundEdge(R_EDGE(c,0));
806           RoundEdge(c[1], c1bis);
807           InterlockEdge(tile_01_00_cnr, c[0]);
808         }
809         RoundCornerCut(rcs);
810         translate([0,0,-20]) linear_extrude(height=40) {
811           polygon([ c1bis, c1bis + [50,0], c2bis + [50,0], c2bis ]);
812         }
813       }
814       RoundEdge(c1bis, c2bis);
815     }
816     Machine();
817     RoundCornerCut(rc2);
818   }
819   RoundCornerAdd(rcs);
820   RoundCornerAdd(rc2);
821 }
822
823 module FitTest_general(c0,sz, dobrace=false, bracexx=0){
824   c = Rectangle_corners(c0, sz);
825   brace = [7,7,9];
826   bsz = sz + [bracexx,0,0];
827   difference(){
828     union(){
829       Rectangle_TileBase(c);
830       if (dobrace) {
831         translate(concat(c0, [-brace[2] + 0.1])){
832           difference(){
833             cube(concat(bsz, [brace[2]]) - [5,0,0]);
834             translate(brace + [0,0, -25])
835               cube(concat(bsz, [50]) - brace*2 + [10,0,0]);
836           }
837         }
838       }
839       RoundEdge(R_EDGE(c,1));
840     }
841     Machine();
842   }
843 }
844
845 module FitTest_PairLink(cut=false){ ////toplevel
846   cy0=-55; cy1=85; cx=132;
847   bar = [10,10];
848   legrad = 12;
849   footrad_min = 1; footrad_max = 4; footrad_depth = 5;
850   strap = [3,5];
851   adj_neg_slop = 1.0;
852   bar_z_slop = 1.75;
853
854   // calculated
855   straphole_x_max = legrad/sqrt(2) + footrad_max;
856   dz = cut ? adj_neg_slop : 0;
857
858   translate([cx - bar[0]/2, cy0, dz + bar_z_slop])
859     cube([bar[0], cy1-cy0, bar[1] - bar_z_slop]);
860
861   for (endy=[cy0,cy1]) {
862     $fn=32;
863     translate([cx,endy,dz]){
864       // feet
865       for (rot=[45:90:315]) rotate(rot) {
866         translate([legrad,0,0]){
867           hull(){
868             cylinder(r= footrad_max, h=1);
869             translate([0,0,-footrad_depth])
870               cylinder(r= footrad_min, h=1);
871           }
872           if (cut)
873             translate([0,0,-10])
874             cylinder(r= footrad_min +
875                      adj_neg_slop * (footrad_max-footrad_min)/footrad_depth,
876                      h=20);
877         }
878       }
879       // legs
880       for (rot=[45,135]) rotate(rot) {
881         hull(){
882           for (s=[-1,+1]){
883             translate([s*legrad,0,0])
884               cylinder(r= footrad_max, h=bar[1]);
885           }
886         }
887       }
888       // strap holes
889       if (cut) {
890         for (rot=[0,180]) rotate(rot) {
891             translate([ straphole_x_max - strap[0]/2, 0,0 ])
892               cube(concat(strap,[20]), center=true);
893           }
894       }
895     }
896   }
897 }
898
899 module FitTest_RearCurve(){ ////toplevel
900   difference(){
901     FitTest_general([100,0], [180,100]);
902     FitTest_PairLink(true);
903     TestStrapSlots();
904   }
905 }
906
907 module FitTest_FrontCurve(){ ////toplevel
908   p0 = [100,-80];
909   sz = [180,80];
910   difference(){
911     intersection() {
912       Tile00();
913       translate([0,0,-8]) linear_extrude(height=18) {
914         translate(p0) square(sz);
915         translate(teststrapslots_at[3])
916           scale(2* [ teststrap_peg[0], teststrap[1] ])
917           circle(r=1, $fn=20);
918       }
919     }
920     FitTest_PairLink(true);
921     TestStrapSlots();
922   }
923 }
924
925 module FitTest_Entire(){ ////toplevel
926   p0 = [-33,-80];
927   szrear = [263,180];
928   szfront = [243,szrear[1]];
929   difference(){
930     FitTest_general(p0, szrear, dobrace=true, bracexx=0);
931     FitTest_PairLink(true);
932     translate(concat(p0,[0]) + [szfront[0],-10,-40])
933       cube([100, -p0[1], 80]);
934     TestStrapSlots();
935   }
936   intersection(){
937     FitTest_RearCurve();
938     translate(concat(p0,[-20])) cube(concat(szrear,[40]));
939   }
940   FitTest_FrontCurve();
941 }
942
943 module FitTest_EntireDemo(){ ////toplevel
944   FitTest_Entire();
945   //%Tile00();
946 }
947
948 module FitTest_EndEnd(){ ////toplevel
949   p0 = [-32,-32];
950   sz = [188,113];
951   sz2 = [168,100];
952   difference(){
953     FitTest_general(p0, sz);
954     translate([ p0[0] -1, p0[1]+sz2[1], -10])
955       cube([ sz2[0] +1, 50, 20 ]);
956   }
957 }
958
959 module FitTest_PairDemo(){ ////toplevel
960   sh=[-90,-15,0];
961   translate(sh){
962     FitTest_PairLink();
963     %FitTest_FrontCurve();
964     %FitTest_RearCurve();
965   }
966   rotate([0,0,180]){
967     translate(sh){
968       difference(){
969         union(){
970           FitTest_FrontCurve();
971           FitTest_RearCurve();
972         }
973         #FitTest_PairLink(true);
974       }
975     }
976   }
977 }
978
979 module RoundCornerDemo_plat(cnr){
980   mirror([0,0,1]) linear_extrude(height=1) polygon(cnr);
981 }
982
983 module RoundCornerDemo(){ ////toplevel
984   cnr = [ [-2,-3], [13,-3], [-12,9] ];
985   translate([0,25,0]) RoundCornerDemo_plat(cnr);
986   translate([25,0,0]) RoundCornerAdd(cnr);
987   translate([-25,0,0]) RoundCornerCut(cnr);
988   translate([0,-25,0]) RoundCorner_selector(cnr, 0);
989   difference(){
990     RoundCornerDemo_plat(cnr);
991     RoundCornerCut(cnr);
992   }
993   RoundCornerAdd(cnr);
994 }
995
996 module Demo(){ ////toplevel
997   translate(demo_slop*[-2,1]) color("blue") Tile12();
998   translate(demo_slop*[-2,0]) color("red")  Tile02();
999   translate(demo_slop*[-2,1]) color("orange") Tile11();
1000   translate(demo_slop*[-2,0]) color("purple") Tile01();
1001   translate(demo_slop*[-3,1]) color("blue")   Tile10();
1002   translate(demo_slop*[-3,0]) color("red")    Tile00();
1003   %Machine();
1004 }
1005   
1006 //TestPiece1();
1007 //TestPiece2();
1008 //Demo();
1009
1010 //Machine_NewRearProfile();
1011 //Machine_NewRearCurve();
1012 //Machine_NewFrontProfile();
1013 //Machine_NewEndProfile();
1014 //Machine_NewEndProfileDemo();
1015 //Machine_Curves();
1016 //Machine();
1017 //FitTest();
1018 //MachineEnvelope();