laser-cutter 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/pages.yml +44 -0
  3. data/.gitignore +2 -0
  4. data/.openlanes/config.yml +106 -0
  5. data/.plans/002.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257lid-options/plan.md +45 -0
  6. data/.plans/003.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257ruby-api/plan.md +37 -0
  7. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/spec.md +40 -0
  8. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/state.json +337 -0
  9. data/.plans/004.01/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257sloped-box-panels/plan.md +91 -0
  10. data/.plans/decisions.md +11 -0
  11. data/CHANGELOG.md +134 -0
  12. data/CLAUDE.md +30 -5
  13. data/Gemfile +2 -1
  14. data/README.md +309 -31
  15. data/Rakefile +11 -7
  16. data/docs/badges/coverage_badge.svg +2 -2
  17. data/docs/decisions/decisions-on-angled-box.md +93 -0
  18. data/docs/decisions/stack.md +6 -0
  19. data/docs/images/box-3d.avif +0 -0
  20. data/docs/images/box-lid-back.avif +0 -0
  21. data/docs/images/box-lid-full.avif +0 -0
  22. data/docs/images/box-lid-plain.avif +0 -0
  23. data/docs/images/box-metric.avif +0 -0
  24. data/docs/images/box-sloped-3d.avif +0 -0
  25. data/docs/images/box-sloped.avif +0 -0
  26. data/docs/images/box-tray.avif +0 -0
  27. data/justfile +24 -8
  28. data/laser-cutter.gemspec +2 -1
  29. data/lib/laser/cutter/aggregator.rb +28 -0
  30. data/lib/laser/cutter/box.rb +192 -24
  31. data/lib/laser/cutter/box_factory.rb +15 -0
  32. data/lib/laser/cutter/cli/command.rb +27 -1
  33. data/lib/laser/cutter/cli/config_file.rb +2 -0
  34. data/lib/laser/cutter/cli/examples.rb +8 -0
  35. data/lib/laser/cutter/cli/generate.rb +41 -5
  36. data/lib/laser/cutter/cli/help.rb +4 -0
  37. data/lib/laser/cutter/cli/page_sizes.rb +3 -0
  38. data/lib/laser/cutter/cli/version.rb +2 -0
  39. data/lib/laser/cutter/cli.rb +23 -4
  40. data/lib/laser/cutter/configuration.rb +42 -2
  41. data/lib/laser/cutter/geometry/dimensions.rb +7 -0
  42. data/lib/laser/cutter/geometry/line.rb +42 -0
  43. data/lib/laser/cutter/geometry/loop.rb +138 -0
  44. data/lib/laser/cutter/geometry/point.rb +3 -0
  45. data/lib/laser/cutter/geometry/rect.rb +25 -0
  46. data/lib/laser/cutter/geometry/shape.rb +19 -1
  47. data/lib/laser/cutter/geometry/transform.rb +50 -0
  48. data/lib/laser/cutter/geometry/tuple.rb +47 -11
  49. data/lib/laser/cutter/invalid_option.rb +8 -0
  50. data/lib/laser/cutter/launcher.rb +14 -1
  51. data/lib/laser/cutter/missing_option.rb +2 -0
  52. data/lib/laser/cutter/notching/angled_edge.rb +67 -0
  53. data/lib/laser/cutter/notching/base.rb +8 -0
  54. data/lib/laser/cutter/notching/edge.rb +59 -4
  55. data/lib/laser/cutter/notching/infinite_iterator.rb +11 -1
  56. data/lib/laser/cutter/notching/path_generator.rb +86 -81
  57. data/lib/laser/cutter/notching/shift.rb +4 -0
  58. data/lib/laser/cutter/options.rb +175 -0
  59. data/lib/laser/cutter/page_manager.rb +12 -2
  60. data/lib/laser/cutter/renderer/base.rb +26 -4
  61. data/lib/laser/cutter/renderer/box_renderer.rb +14 -1
  62. data/lib/laser/cutter/renderer/layout_renderer.rb +13 -3
  63. data/lib/laser/cutter/renderer/line_renderer.rb +6 -0
  64. data/lib/laser/cutter/renderer/meta_renderer.rb +33 -9
  65. data/lib/laser/cutter/renderer/rect_renderer.rb +5 -0
  66. data/lib/laser/cutter/renderer/svg_renderer.rb +12 -4
  67. data/lib/laser/cutter/renderer.rb +2 -0
  68. data/lib/laser/cutter/sloped/box.rb +90 -0
  69. data/lib/laser/cutter/sloped/corners.rb +58 -0
  70. data/lib/laser/cutter/sloped/layout.rb +97 -0
  71. data/lib/laser/cutter/sloped/notcher.rb +53 -0
  72. data/lib/laser/cutter/sloped/panels.rb +121 -0
  73. data/lib/laser/cutter/sloped/placement.rb +36 -0
  74. data/lib/laser/cutter/sloped/profile.rb +176 -0
  75. data/lib/laser/cutter/sloped/request.rb +31 -0
  76. data/lib/laser/cutter/sloped/ring_panel.rb +120 -0
  77. data/lib/laser/cutter/sloped/side_panel.rb +89 -0
  78. data/lib/laser/cutter/types.rb +60 -0
  79. data/lib/laser/cutter/units_converter.rb +5 -0
  80. data/lib/laser/cutter/version.rb +2 -1
  81. data/lib/laser/cutter/zero_value_not_allowed.rb +2 -0
  82. data/lib/laser/cutter.rb +40 -0
  83. data/spec/laser/cutter/angled_edge_spec.rb +103 -0
  84. data/spec/laser/cutter/box_factory_spec.rb +19 -0
  85. data/spec/laser/cutter/box_lid_spec.rb +142 -0
  86. data/spec/laser/cutter/cli_spec.rb +114 -7
  87. data/spec/laser/cutter/configuration_spec.rb +54 -0
  88. data/spec/laser/cutter/loop_spec.rb +107 -0
  89. data/spec/laser/cutter/options_spec.rb +162 -0
  90. data/spec/laser/cutter/renderer/layout_renderer_spec.rb +21 -20
  91. data/spec/laser/cutter/renderer/meta_renderer_spec.rb +82 -0
  92. data/spec/laser/cutter/renderer/svg_renderer_spec.rb +34 -0
  93. data/spec/laser/cutter/sloped/assembly_spec.rb +78 -0
  94. data/spec/laser/cutter/sloped/box_spec.rb +151 -0
  95. data/spec/laser/cutter/sloped/corners_spec.rb +45 -0
  96. data/spec/laser/cutter/sloped/layout_spec.rb +77 -0
  97. data/spec/laser/cutter/sloped/notcher_spec.rb +57 -0
  98. data/spec/laser/cutter/sloped/panels_spec.rb +175 -0
  99. data/spec/laser/cutter/sloped/placement_spec.rb +50 -0
  100. data/spec/laser/cutter/sloped/request_spec.rb +48 -0
  101. data/spec/laser/cutter/sloped/ring_panel_spec.rb +102 -0
  102. data/spec/laser/cutter/sloped/side_panel_spec.rb +98 -0
  103. data/spec/laser/cutter/sloped_profile_spec.rb +156 -0
  104. data/spec/laser/cutter/transform_spec.rb +53 -0
  105. data/spec/laser/cutter_spec.rb +114 -0
  106. data/spec/spec_helper.rb +4 -0
  107. data/spec/support/loops.rb +106 -0
  108. data/spec/support/outline.rb +53 -0
  109. data/spec/support/sloped.rb +26 -0
  110. data/spec/support/solid.rb +95 -0
  111. data/spec/wishes/wish-tilted-box.png +0 -0
  112. metadata +81 -7
  113. /data/.plans/{001.00-dry-cli-migration/plan.md → 001.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257dry-cli-migration/plan.md"} +0 -0
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # The side view of a box whose front slopes back: the inside outline of
7
+ # a side wall, and the lengths of the panels that close it.
8
+ #
9
+ # Seen from the left, front to the left, the outline runs
10
+ #
11
+ # top (Dt)
12
+ # +--------+
13
+ # / |
14
+ # tilted / |
15
+ # / | back (H)
16
+ # + |
17
+ # front | |
18
+ # (Hf) +------------+
19
+ # bottom (D)
20
+ #
21
+ # Every length is of the inside outline, in the box's units. The tilted
22
+ # panel rises at +slope+ degrees, so the top strip is whatever depth is
23
+ # left: Dt = D - (H - Hf) / tan(slope). A strip of zero size is dropped.
24
+ #
25
+ # Where the tilted panel meets a strip, the corner is obtuse and the
26
+ # joint is a plain butt joint: the strip runs past the corner over the
27
+ # end of the tilted panel, and the tilted panel stops short of it, so
28
+ # their outside faces meet with no gap and no overlap. Where it meets
29
+ # the back or the bottom directly, the corner is acute, and both panels
30
+ # simply end at the corner.
31
+ class Profile
32
+ # @return [Float] depth, height, front height, and the thickness of the material
33
+ attr_reader :depth, :height, :front_height, :thickness
34
+
35
+ # @return [Float] the angle of the tilted panel above horizontal, in radians
36
+ attr_reader :angle
37
+
38
+ # @param depth [Numeric] D, the inside depth of the box
39
+ # @param height [Numeric] H, the inside height at the back
40
+ # @param front_height [Numeric] Hf, the inside height at the front
41
+ # @param slope [Numeric] the angle of the tilted panel, in degrees
42
+ # @param thickness [Numeric] of the material
43
+ # @raise [InvalidOption] when the numbers do not make this shape
44
+ def initialize(depth:, height:, front_height:, slope:, thickness:)
45
+ @depth = depth.to_f
46
+ @height = height.to_f
47
+ @front_height = front_height.to_f
48
+ @thickness = thickness.to_f
49
+ @slope = slope.to_f
50
+ validate!
51
+ @angle = @slope * Math::PI / 180
52
+ compute_lengths!
53
+ freeze
54
+ end
55
+
56
+ # @return [Float] Dt, the inside depth of the top strip
57
+ attr_reader :top_depth
58
+
59
+ # @return [Float] S, the inside length of the slope, corner to corner
60
+ attr_reader :slope_length
61
+
62
+ # Whether the box has a top strip, and a front strip.
63
+ def top_strip? = top_depth.positive?
64
+ def front_strip? = front_height.positive?
65
+
66
+ # The inside outline, counter-clockwise from the front of the bottom.
67
+ #
68
+ # @return [Array<Geometry::Point>] five corners; four when one strip is dropped,
69
+ # and three, a wedge, when both are
70
+ def vertices
71
+ points = [[0, 0], [depth, 0], [depth, height]]
72
+ points << [depth - top_depth, height] if top_strip?
73
+ points << [0, front_height] if front_strip?
74
+ points.map { |x, y| Geometry::Point[x, y] }
75
+ end
76
+
77
+ # The part of the slope the tilted panel covers, and so the part of a
78
+ # side wall's sloped edge that is notched.
79
+ #
80
+ # @return [Geometry::Line] from its low end to its high end
81
+ def tilted_span
82
+ low = slope_low.plus(direction * front_cut)
83
+ high = slope_high.plus(direction * -top_cut)
84
+ Geometry::Line.new(low, high)
85
+ end
86
+
87
+ # The outside face of the tilted panel, one thickness out from its span.
88
+ #
89
+ # @return [Geometry::Line] from its low end to its high end
90
+ def tilted_outside
91
+ out = Vector[-Math.sin(angle), Math.cos(angle)] * thickness
92
+ span = tilted_span
93
+ Geometry::Line.new(span.p1.plus(out), span.p2.plus(out))
94
+ end
95
+
96
+ # @return [Float] the length of the tilted panel, end to end
97
+ def tilted_length = slope_length - front_cut - top_cut
98
+
99
+ # @return [Float] the length of the top strip, front to back; 0 when it is dropped
100
+ def top_strip_length = top_strip? ? top_depth + (thickness / Math.sin(angle)) : 0.0
101
+
102
+ # @return [Float] the height of the front strip; 0 when it is dropped
103
+ def front_strip_length = front_strip? ? front_height + (thickness / Math.cos(angle)) : 0.0
104
+
105
+ private
106
+
107
+ attr_reader :slope
108
+
109
+ # @return [void]
110
+ # @raise [InvalidOption] when a length or the slope is out of range, or
111
+ # the front is not below the back
112
+ def validate!
113
+ { depth: depth, height: height, thickness: thickness }.each do |name, value|
114
+ raise InvalidOption, "#{name} must be above zero, got #{value}" unless value.positive?
115
+ end
116
+ raise InvalidOption, "slope must be between 0 and 90 degrees, got #{slope}" unless slope.positive? && slope < 90
117
+ raise InvalidOption, "front height must be at least zero, got #{front_height}" if front_height.negative?
118
+ return if front_height < height
119
+
120
+ raise InvalidOption, "front height (#{front_height}) must be below the height (#{height})"
121
+ end
122
+
123
+ # Sets the depth of the top strip and the length of the slope.
124
+ #
125
+ # @return [void]
126
+ # @raise [InvalidOption] when the slope needs more depth than the box
127
+ # has, or leaves no tilted panel
128
+ def compute_lengths!
129
+ run = (height - front_height) / Math.tan(angle)
130
+ if run > depth + Geometry::Tuple::PRECISION
131
+ raise InvalidOption,
132
+ "a #{slope}° slope from #{front_height} up to #{height} needs a depth of #{run.round(3)}, " \
133
+ "but the depth is #{depth}; raise the slope or the front height"
134
+ end
135
+
136
+ @top_depth = [depth - run, 0.0].max
137
+ @top_depth = 0.0 if @top_depth < Geometry::Tuple::PRECISION
138
+ @slope_length = Math.hypot(run, height - front_height)
139
+ refuse_no_tilted_panel!
140
+ end
141
+
142
+ # Under a strip the tilted panel stops short of the corner: by more at
143
+ # the front the steeper the slope, and by more at the top the shallower.
144
+ # A slope too short for both cuts leaves none of the panel. Between two
145
+ # strips that is when the front strip, Hf + t / cos(slope), reaches the
146
+ # height, so a lower front always helps and a different slope may not.
147
+ #
148
+ # @return [void]
149
+ # @raise [InvalidOption] when the tilted panel would have no length
150
+ def refuse_no_tilted_panel!
151
+ return if tilted_length > Geometry::Tuple::PRECISION
152
+
153
+ raise InvalidOption,
154
+ "a #{slope}° slope from a front height of #{front_height} leaves no tilted panel in material " \
155
+ "#{thickness} thick: the strips take #{(front_cut + top_cut).round(3)} of a slope " \
156
+ "#{slope_length.round(3)} long; lower the front height"
157
+ end
158
+
159
+ # @return [Geometry::Point] the low end of the slope, at the top of the front
160
+ def slope_low = Geometry::Point[0, front_height]
161
+ # @return [Geometry::Point] the high end of the slope, at the front of the top
162
+ def slope_high = Geometry::Point[depth - top_depth, height]
163
+ # @return [Vector] the unit vector up the slope
164
+ def direction = Vector[Math.cos(angle), Math.sin(angle)]
165
+
166
+ # How far the tilted panel stops short of each corner, under a strip.
167
+ #
168
+ # @return [Float] at the top; 0 without a top strip
169
+ def top_cut = top_strip? ? thickness / Math.tan(angle) : 0.0
170
+ # @return [Float] how far the tilted panel stops short of the front
171
+ # corner; 0 without a front strip
172
+ def front_cut = front_strip? ? thickness * Math.tan(angle) : 0.0
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # Checks that settings which ask for a sloped box can be drawn, before
7
+ # anything is. Settings with neither a slope nor a front height ask for
8
+ # the box with six panels, and pass.
9
+ module Request
10
+ # @param settings [Hash, Configuration] slope, front_height, lid, depth, height and thickness, by Symbol
11
+ # @return [Profile, nil] the side view of the sloped box; nil when the settings ask for none
12
+ # @raise [MissingOption] for a slope without a front height
13
+ # @raise [InvalidOption] for a front height without a slope, a lid a sloped box
14
+ # cannot have, or numbers that do not make the shape
15
+ def self.validate!(settings)
16
+ slope, front_height = settings.values_at(:slope, :front_height)
17
+ return if slope.nil? && front_height.nil?
18
+ raise InvalidOption, 'front_height is the front of a sloped box, so it needs a slope.' if slope.nil?
19
+ raise MissingOption, 'front_height is required with slope, but missing.' if front_height.nil?
20
+
21
+ lid = (settings[:lid] || Box::LIDS.first).to_sym
22
+ unless Box::LIDS.include?(lid)
23
+ raise InvalidOption, "lid cannot be #{lid.to_s.inspect} with a slope, but must be one of: #{Box::LIDS.join(', ')}."
24
+ end
25
+
26
+ Profile.new(**settings.to_h.slice(:depth, :height, :thickness), front_height: front_height, slope: slope)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # One of the rectangular panels that wrap around the sloped box between
7
+ # its two side walls: the bottom, the back, a strip or the tilted panel.
8
+ #
9
+ # In its own frame the panel is +width+ along x, from one side wall to
10
+ # the other, and +length+ along y, from its near edge to its far edge.
11
+ #
12
+ # far edge
13
+ # 3 +----------+ 2
14
+ # | |
15
+ # end | | end
16
+ # | |
17
+ # 0 +----------+ 1
18
+ # near edge
19
+ #
20
+ # - A long edge is notched into the panel beyond it, or plain.
21
+ # - An end is notched into the side wall from the near edge as far as
22
+ # +joint+, and from there on covers the edge of the side wall, one
23
+ # thickness out. A strip ends that way, past the corner of the wall,
24
+ # and so does a tilted panel that lifts off, along its whole length.
25
+ # - A corner next to two notched edges may carry the corner cube.
26
+ class RingPanel
27
+ # Which way is out of the panel across its left end, near edge, right
28
+ # end and far edge. Corner n lies between entry n and the one after.
29
+ OUTWARDS = [[-1, 0], [0, -1], [1, 0], [0, 1]].freeze
30
+
31
+ # @param notcher [Notcher]
32
+ # @param width [Numeric] between the side walls
33
+ # @param length [Numeric] from the near edge to the far edge
34
+ # @param near [Boolean, nil] the middle notch of the near edge: true for a tab, false for a hole, nil for a plain edge
35
+ # @param far [Boolean, nil] the same, for the far edge
36
+ # @param joint [Numeric] how far along each end, from the near edge, the notches into the side wall run
37
+ # @param corners [Array<Symbol>] the edges whose two corners this panel carries: :near, :far
38
+ def initialize(notcher:, width:, length:, joint:, near: nil, far: nil, corners: [])
39
+ @notcher = notcher
40
+ @width = width.to_f
41
+ @length = length.to_f
42
+ @joint = joint.to_f
43
+ @near = near
44
+ @far = far
45
+ @corners = corners
46
+ freeze
47
+ end
48
+
49
+ # @return [Geometry::Loop] the outline, counter-clockwise from the near left corner
50
+ def loop
51
+ Geometry::Loop.new(near_edge + corner(1) + right_end + corner(2) + far_edge + corner(3) + left_end + corner(0))
52
+ end
53
+
54
+ private
55
+
56
+ # @return [Float] the thickness of the material
57
+ def thickness = @notcher.thickness
58
+
59
+ # @param x [Float]
60
+ # @param y [Float]
61
+ # @return [Geometry::Point]
62
+ def point(x, y) = Geometry::Point[x, y]
63
+
64
+ # @return [Array<Geometry::Point>] the near edge, left to right; none when it is plain
65
+ def near_edge
66
+ @near.nil? ? [] : @notcher.path(point(0, 0), point(@width, 0), center_out: @near)
67
+ end
68
+
69
+ # @return [Array<Geometry::Point>] the far edge, right to left; none when it is plain
70
+ def far_edge
71
+ @far.nil? ? [] : @notcher.path(point(@width, @length), point(0, @length), center_out: @far)
72
+ end
73
+
74
+ # Up the right end: the notches, then the stretch that covers the wall.
75
+ #
76
+ # @return [Array<Geometry::Point>]
77
+ def right_end
78
+ notched(point(@width, 0), point(@width, @joint)) + covering(@width + thickness, @joint, @length)
79
+ end
80
+
81
+ # Down the left end, the same two stretches the other way round.
82
+ #
83
+ # @return [Array<Geometry::Point>]
84
+ def left_end
85
+ covering(-thickness, @length, @joint) + notched(point(0, @joint), point(0, 0))
86
+ end
87
+
88
+ # @param from [Geometry::Point]
89
+ # @param to [Geometry::Point]
90
+ # @return [Array<Geometry::Point>] the end notched into the side wall; none when the joint has no length
91
+ def notched(from, to)
92
+ @joint.positive? ? @notcher.path(from, to, center_out: true) : []
93
+ end
94
+
95
+ # @param x [Float] one thickness out from the end
96
+ # @param from [Float] where the stretch starts along y
97
+ # @param to [Float] where it ends
98
+ # @return [Array<Geometry::Point>] the stretch that covers the edge of the side wall; none when the joint runs the whole length
99
+ def covering(x, from, to)
100
+ @joint < @length ? [point(x, from), point(x, to)] : []
101
+ end
102
+
103
+ # The square on a corner this panel carries: out along the edge that
104
+ # arrives, round the outer corner, and back along the edge that leaves.
105
+ # The outline then passes the inner corner twice, and Loop drops the
106
+ # spikes that leaves wherever a tab runs into the square.
107
+ #
108
+ # @param index [Integer] 0 to 3, counter-clockwise from the near left corner
109
+ # @return [Array<Geometry::Point>] none when the panel does not carry that corner
110
+ def corner(index)
111
+ return [] unless @corners.include?(index < 2 ? :near : :far)
112
+
113
+ inner = point(index.between?(1, 2) ? @width : 0.0, index < 2 ? 0.0 : @length)
114
+ arriving, leaving = [OUTWARDS[index], OUTWARDS[(index + 1) % 4]].map { |x, y| Vector[x, y] * thickness }
115
+ [inner, inner.plus(arriving), inner.plus(arriving + leaving), inner.plus(leaving), inner]
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # A side wall of the sloped box: the profile, notched into every panel
7
+ # that closes it.
8
+ #
9
+ # In its own frame x is the depth from the front and y the height, as
10
+ # in Profile. Its square edges are notched into the bottom, the back and
11
+ # the strips. Along the slope it is notched into the tilted panel, over
12
+ # the span that panel covers, or left plain under a tilted panel that
13
+ # lifts off.
14
+ #
15
+ # Where the tilted panel stops short under a strip, a wedge stays open
16
+ # between its square end and the strip. The wall closes the end of that
17
+ # wedge with a triangle, so nobody sees into the box from the side:
18
+ #
19
+ # strip
20
+ # ----+=========
21
+ # |\ wedge
22
+ # end | \
23
+ # of \ \ slope
24
+ # tilted \
25
+ class SidePanel
26
+ # @param profile [Profile]
27
+ # @param notcher [Notcher]
28
+ # @param lid [Symbol] :full for a tilted panel notched into the wall, :plain for one lying on it
29
+ def initialize(profile:, notcher:, lid:)
30
+ @profile = profile
31
+ @notcher = notcher
32
+ @lid = lid
33
+ freeze
34
+ end
35
+
36
+ # @return [Geometry::Loop] the outline, counter-clockwise from the front of the bottom
37
+ def loop
38
+ Geometry::Loop.new(bottom + back + top + slope + front)
39
+ end
40
+
41
+ private
42
+
43
+ # The corners of the inside outline, see Profile#vertices.
44
+ #
45
+ # @return [Array<Geometry::Point>]
46
+ def corners = @profile.vertices
47
+
48
+ # @param from [Geometry::Point]
49
+ # @param to [Geometry::Point]
50
+ # @return [Array<Geometry::Point>] the run, notched with a hole in the middle
51
+ def notched(from, to)
52
+ @notcher.path(from, to, center_out: false)
53
+ end
54
+
55
+ # @return [Array<Geometry::Point>] the bottom, front to back
56
+ def bottom = notched(corners[0], corners[1])
57
+
58
+ # @return [Array<Geometry::Point>] the back, bottom to top
59
+ def back = notched(corners[1], corners[2])
60
+
61
+ # Along the top strip, then on under its end to the top of the wedge.
62
+ #
63
+ # @return [Array<Geometry::Point>] none without a top strip
64
+ def top
65
+ return [] unless @profile.top_strip?
66
+
67
+ notched(corners[2], corners[3]) << @profile.tilted_outside.p2
68
+ end
69
+
70
+ # Down the slope, over the span the tilted panel covers.
71
+ #
72
+ # @return [Array<Geometry::Point>]
73
+ def slope
74
+ span = @profile.tilted_span
75
+ @lid == :plain ? [span.p2, span.p1] : notched(span.p2, span.p1)
76
+ end
77
+
78
+ # From the top of the wedge under the front strip, down that strip.
79
+ #
80
+ # @return [Array<Geometry::Point>] none without a front strip
81
+ def front
82
+ return [] unless @profile.front_strip?
83
+
84
+ [@profile.tilted_outside.p1] + notched(corners.last, corners.first)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ # The types of the attributes of {Options}. Each one coerces what a form
6
+ # or a command line sends, Strings mostly, and rejects what it cannot use.
7
+ module Types
8
+ include Dry.Types()
9
+
10
+ # Whether a value was left out: nil, or the empty String of a blank form field.
11
+ #
12
+ # @param value [Object]
13
+ # @return [Boolean]
14
+ def self.blank?(value)
15
+ value.nil? || Dry::Types::Undefined.equal?(value) || (value.respond_to?(:empty?) && value.empty?)
16
+ end
17
+
18
+ # One of a few words, given as a String or a Symbol. Blank means the default.
19
+ #
20
+ # @param values [Array<String>] the words allowed
21
+ # @param default [String] the word a blank value stands for
22
+ # @return [Dry::Types::Type]
23
+ def self.choice(values, default:)
24
+ String.default(default).enum(*values).constructor { |value| blank?(value) ? Dry::Types::Undefined : value.to_s }
25
+ end
26
+
27
+ # A type that also takes nil, and reads a blank value as nil.
28
+ #
29
+ # @param type [Dry::Types::Type]
30
+ # @return [Dry::Types::Type]
31
+ def self.optional(type)
32
+ type.optional.constructor { |value| blank?(value) ? nil : value }
33
+ end
34
+
35
+ # A size of the box or of its material: a number above zero.
36
+ Length = Coercible::Float.constrained(gt: 0)
37
+
38
+ # A distance that may be zero, such as the kerf or a margin.
39
+ Allowance = Coercible::Float.constrained(gteq: 0)
40
+
41
+ # The angle of a sloped front above horizontal, in degrees: flat and upright are both out.
42
+ Slope = Coercible::Float.constrained(gt: 0, lt: 90)
43
+
44
+ # The units every dimension is in.
45
+ Units = choice(%w[in mm], default: 'in')
46
+
47
+ # How the lid joins the walls, see Box::LIDS.
48
+ Lid = choice(Box::LIDS.map(&:to_s), default: 'full')
49
+
50
+ # The way the page is turned, for a PDF.
51
+ PageLayout = choice(%w[portrait landscape], default: 'portrait')
52
+
53
+ # The kind of file to draw.
54
+ Format = Coercible::String.constructor(&:downcase).enum(*Renderer::FORMATS.keys)
55
+
56
+ # The name of a page size a PDF can take, such as A4 or LETTER.
57
+ PageSize = Coercible::String.constructor(&:upcase).enum(*PageManager::SIZES.keys)
58
+ end
59
+ end
60
+ end
@@ -2,11 +2,16 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
+ # Converts lengths between millimetres and inches, rounded to five digits.
5
6
  class UnitsConverter
7
+ # @param value [Numeric] a length in millimetres
8
+ # @return [Float] the same length in inches
6
9
  def self.mm2in(value)
7
10
  (0.039370079 * value).round(5)
8
11
  end
9
12
 
13
+ # @param value [Numeric] a length in inches
14
+ # @return [Float] the same length in millimetres
10
15
  def self.in2mm(value)
11
16
  (25.4 * value).round(5)
12
17
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
- VERSION = "2.0.0"
5
+ # The version of the gem.
6
+ VERSION = "3.0.0"
6
7
  end
7
8
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
+ # Raised when an option that must be above zero, such as a dimension or
6
+ # the thickness, is zero.
5
7
  class ZeroValueNotAllowed < MissingOption; end
6
8
  end
7
9
  end
data/lib/laser/cutter.rb CHANGED
@@ -14,12 +14,52 @@ require "dry/cli"
14
14
  require "dry/cli/help"
15
15
  require "dry/cli/ui"
16
16
  require "dry/cli/autocomplete/command"
17
+ require "dry/struct"
17
18
  require "tty-screen"
18
19
  require "zeitwerk"
19
20
 
21
+ # The namespace of the laser-cutter gem.
20
22
  module Laser
23
+ # Draws the notched panels of a box. A Ruby program needs {Options},
24
+ # {Cutter.render} and {Cutter.write}; the command line is one caller of many.
21
25
  module Cutter
26
+ # The base class of every error the gem raises.
22
27
  class Error < StandardError; end
28
+
29
+ class << self
30
+ # Draws a box, and returns the document without writing a file.
31
+ #
32
+ # @example In a Rails controller
33
+ # box = params.require(:box).permit(*Laser::Cutter::Options.attribute_names)
34
+ # options = Laser::Cutter::Options.new(box.to_h)
35
+ # send_data Laser::Cutter.render(options), type: "application/pdf", filename: "box.pdf"
36
+ #
37
+ # @param options [Options, Hash] a Hash becomes an {Options}
38
+ # @yieldparam line [Geometry::Line] each line, once it is drawn
39
+ # @return [String] the PDF, or the SVG when the options ask for one
40
+ # @raise [MissingOption, InvalidOption]
41
+ def render(options, &)
42
+ options = Options.new(options)
43
+ Renderer.for(options.format || Renderer::FORMATS.keys.first, options.to_configuration).document(&)
44
+ end
45
+
46
+ # Draws a box into the file the options name. Without a format, the
47
+ # extension of the file decides, and anything but .svg is a PDF.
48
+ #
49
+ # @param options [Options, Hash] a Hash becomes an {Options}
50
+ # @yieldparam line [Geometry::Line] each line, once it is drawn
51
+ # @return [String] the path of the file
52
+ # @raise [MissingOption, InvalidOption]
53
+ def write(options, &)
54
+ options = Options.new(options)
55
+ raise MissingOption, 'file is required, but missing.' unless options.file
56
+
57
+ extension = File.extname(options.file).delete('.').downcase
58
+ format = options.format || (Renderer::FORMATS.key?(extension) ? extension : nil)
59
+ File.binwrite(options.file, render(options.new(format: format), &))
60
+ options.file
61
+ end
62
+ end
23
63
  end
24
64
  end
25
65
 
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Notching
8
+ RSpec.describe AngledEdge do
9
+ let(:thickness) { 0.25 }
10
+ let(:notch) { 0.75 }
11
+ let(:kerf) { 0.0 }
12
+ let(:center_out) { false }
13
+ let(:outward) { :right }
14
+ let(:from) { Geometry::Point[2, 1] }
15
+ # A 5-long run at atan(3/4), about 36.87 degrees.
16
+ let(:to) { Geometry::Point[6, 4] }
17
+ let(:options) { { notch_width: notch, thickness: thickness, kerf: kerf, center_out: center_out } }
18
+
19
+ subject(:edge) { described_class.new(from, to, outward: outward, **options) }
20
+
21
+ let(:vertices) { edge.lines.flat_map { |line| [line.p1, line.p2] } }
22
+ let(:direction) { [(to.x - from.x) / 5.0, (to.y - from.y) / 5.0] }
23
+ # Unit normal to the left of the run.
24
+ let(:left) { [-direction[1], direction[0]] }
25
+
26
+ def along(point) = ((point.x - from.x) * direction[0]) + ((point.y - from.y) * direction[1])
27
+ def across(point) = ((point.x - from.x) * left[0]) + ((point.y - from.y) * left[1])
28
+
29
+ it 'joins its lines end to end' do
30
+ expect(edge.lines.each_cons(2).all? { |a, b| a.p2.eql?(b.p1) }).to be(true)
31
+ end
32
+
33
+ it 'starts and ends on the inside line' do
34
+ expect([edge.lines.first.p1, edge.lines.last.p2]).to eql([from, to])
35
+ end
36
+
37
+ it 'stays between the ends of the run' do
38
+ expect(vertices.map { |v| along(v) }).to all(be_between(-1e-9, 5 + 1e-9))
39
+ end
40
+
41
+ context 'with the material to the right of the run' do
42
+ it 'puts every vertex on the inside line or one thickness to its right' do
43
+ expect(vertices.map { |v| across(v).round(9) }.uniq).to contain_exactly(0.0, -thickness)
44
+ end
45
+ end
46
+
47
+ context 'with the material to the left of the run' do
48
+ let(:outward) { :left }
49
+
50
+ it 'puts every vertex on the inside line or one thickness to its left' do
51
+ expect(vertices.map { |v| across(v).round(9) }.uniq).to contain_exactly(0.0, thickness)
52
+ end
53
+ end
54
+
55
+ it 'draws one notch per stretch along the run' do
56
+ runs = edge.lines.count { |line| across(line.p1).round(9) == across(line.p2).round(9) }
57
+ expect(runs).to eq(edge.notch_count)
58
+ end
59
+
60
+ it 'matches the turned copy of the same side laid flat' do
61
+ flat = described_class.new(Geometry::Point[0, 0], Geometry::Point[5, 0], outward: outward, **options)
62
+ turned = flat.lines.map { |line| edge.transform.apply(line) }
63
+ expect(turned.zip(edge.lines).all? { |a, b| a.p1.eql?(b.p1) && a.p2.eql?(b.p2) }).to be(true)
64
+ end
65
+
66
+ # The tilted panel is a plain rectangle; its end must mate with the
67
+ # sloped wall, so both must agree on the notches.
68
+ describe 'mating with a rectangular panel of the same length' do
69
+ let(:panel) do
70
+ inside = Geometry::Line.new(Geometry::Point[0, 0], Geometry::Point[5, 0])
71
+ outside = Geometry::Line.new(Geometry::Point[-thickness, -thickness], Geometry::Point[5 + thickness, -thickness])
72
+ Edge.new(outside, inside, options.merge(center_out: !center_out))
73
+ end
74
+
75
+ it 'has the same notch count and width' do
76
+ expect([edge.notch_count, edge.notch_width]).to eq([panel.notch_count, panel.notch_width])
77
+ end
78
+
79
+ it 'starts with a hole where the panel starts with a tab' do
80
+ expect(edge.first_notch_out?).to eq(!panel.first_notch_out?)
81
+ end
82
+ end
83
+
84
+ context 'with a kerf' do
85
+ let(:kerf) { 0.01 }
86
+
87
+ it 'moves each end out along the run by half the kerf' do
88
+ ends = [edge.lines.first.p1, edge.lines.last.p2].map { |p| along(p).round(9) }
89
+ expect(ends).to eq([-kerf / 2, 5 + (kerf / 2)])
90
+ end
91
+ end
92
+
93
+ it 'refuses a run with no length' do
94
+ expect { described_class.new(from, from, outward: outward, **options) }.to raise_error(ArgumentError, /needs a length/)
95
+ end
96
+
97
+ it 'refuses an unknown side' do
98
+ expect { described_class.new(from, to, outward: :up, **options) }.to raise_error(ArgumentError, /outward/)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end