morphology.scad 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2013 Oskar Linde. All rights reserved.
  2. // License: BSD
  3. //
  4. // This library contains basic 2D morphology operations
  5. //
  6. // outset(d=1) - creates a polygon at an offset d outside a 2D shape
  7. // inset(d=1) - creates a polygon at an offset d inside a 2D shape
  8. // fillet(r=1) - adds fillets of radius r to all concave corners of a 2D shape
  9. // rounding(r=1) - adds rounding to all convex corners of a 2D shape
  10. // shell(d,center=false) - makes a shell of width d along the edge of a 2D shape
  11. // - positive values of d places the shell on the outside
  12. // - negative values of d places the shell on the inside
  13. // - center=true and positive d places the shell centered on the edge
  14. module outset(d=1) {
  15. // Bug workaround for older OpenSCAD versions
  16. if (version_num() < 20130424) render() outset_extruded(d) child();
  17. else minkowski() {
  18. circle(r=d);
  19. child();
  20. }
  21. }
  22. module outset_extruded(d=1) {
  23. projection(cut=true) minkowski() {
  24. cylinder(r=d);
  25. linear_extrude(center=true) child();
  26. }
  27. }
  28. module inset(d=1) {
  29. render() inverse() outset(d=d) inverse() child();
  30. }
  31. module fillet(r=1) {
  32. inset(d=r) render() outset(d=r) child();
  33. }
  34. module rounding(r=1) {
  35. outset(d=r) inset(d=r) child();
  36. }
  37. module shell(d,center=false) {
  38. if (center && d > 0) {
  39. difference() {
  40. outset(d=d/2) child();
  41. inset(d=d/2) child();
  42. }
  43. }
  44. if (!center && d > 0) {
  45. difference() {
  46. outset(d=d) child();
  47. child();
  48. }
  49. }
  50. if (!center && d < 0) {
  51. difference() {
  52. child();
  53. inset(d=-d) child();
  54. }
  55. }
  56. if (d == 0) child();
  57. }
  58. // Below are for internal use only
  59. module inverse() {
  60. difference() {
  61. square(1e5,center=true);
  62. child();
  63. }
  64. }
  65. // TEST CODE
  66. use <mirror.scad>
  67. module arrow(l=1,w=.6,t=0.15) {
  68. mirror_y() polygon([[0,0],[l,0],[l-w/2,w/2],[l-w/2-sqrt(2)*t,w/2],[l-t/2-sqrt(2)*t,t/2],[0,t/2]]);
  69. }
  70. module shape() {
  71. polygon([[0,0],[1,0],[1.5,1],[2.5,1],[2,-1],[0,-1]]);
  72. }
  73. if(0) assign($fn=32) {
  74. for (p = [0:10*3-1]) assign(o=floor(p/3)) {
  75. translate([(p%3)*2.5,-o*3]) {
  76. //%if (p % 3 == 1) translate([0,0,1]) shape();
  77. if (p % 3 == 0) shape();
  78. if (p % 3 == 1) translate([0.6,0]) arrow();
  79. if (p % 3 == 2) {
  80. if (o == 0) inset(d=0.3) shape();
  81. if (o == 1) outset(d=0.3) shape();
  82. if (o == 2) rounding(r=0.3) shape();
  83. if (o == 3) fillet(r=0.3) shape();
  84. if (o == 4) shell(d=0.3) shape();
  85. if (o == 5) shell(d=-0.3) shape();
  86. if (o == 6) shell(d=0.3,center=true) shape();
  87. if (o == 7) rounding(r=0.3) fillet(r=0.3) shape();
  88. if (o == 8) shell(d=0.3,center=true) fillet(r=0.3) rounding(r=0.3) shape();
  89. if (o == 9) shell(d=-0.3) fillet(r=0.3) rounding(r=0.3) shape();
  90. }
  91. }
  92. }
  93. }