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
@@ -3,14 +3,42 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Renderer
6
+ # Draws the block of settings at the bottom left of a PDF, under a
7
+ # banner that names the gem.
6
8
  class MetaRenderer < Laser::Cutter::Renderer::Base
7
- META_KEYS = %i(units width height depth thickness notch kerf stroke padding margin page_size page_layout).freeze
9
+ # The settings printed, in order, when they are set.
10
+ META_KEYS = %i(units width height depth front_height slope thickness notch lid kerf stroke padding margin page_size page_layout).freeze
8
11
 
12
+ # The block, in points: its width, the height of the banner above the
13
+ # settings, the height of one line of them, and the space under the last.
14
+ WIDTH = 140
15
+ # @see WIDTH
16
+ BANNER_HEIGHT = 55
17
+ # @see WIDTH
18
+ LINE_HEIGHT = 8.2
19
+ # @see WIDTH
20
+ FOOTER_HEIGHT = 4
21
+
22
+ # @param config [Configuration]
9
23
  def initialize(config = {})
10
24
  self.config = config
11
- self.enclosure = Laser::Cutter::Geometry::Rect.create(Laser::Cutter::Geometry::Point[1, 1], 140, 150)
25
+ height = BANNER_HEIGHT + (fields.size * LINE_HEIGHT) + FOOTER_HEIGHT
26
+ self.enclosure = Laser::Cutter::Geometry::Rect.create(Laser::Cutter::Geometry::Point[1, 1], WIDTH, height)
27
+ end
28
+
29
+ # The settings the block prints, in order. The block is as tall as
30
+ # they need: one line too many would start a second page.
31
+ #
32
+ # @return [Hash{Symbol => Object}]
33
+ def fields
34
+ metadata = config.to_hash
35
+ metadata[:page_size] ||= 'custom'
36
+ metadata.delete(:page_layout) if metadata[:page_size].eql?('custom')
37
+ META_KEYS.select { |key| metadata[key] }.to_h { |key| [key, metadata[key]] }
12
38
  end
13
39
 
40
+ # @param pdf [Prawn::Document]
41
+ # @return [void]
14
42
  def render(pdf = nil)
15
43
  banner = <<-EOF
16
44
 
@@ -23,14 +51,10 @@ module Laser
23
51
  EOF
24
52
 
25
53
  meta_color = BLUE
26
- meta_top_height = 55
27
-
28
- metadata = config.to_hash
29
- metadata[:page_size] ||= 'custom'
30
- metadata.delete(:page_layout) if metadata[:page_size].eql?('custom')
54
+ meta_top_height = BANNER_HEIGHT
31
55
 
32
- meta_fields = META_KEYS.find_all{ |k| metadata[k] }.join(": \n") + ": \n"
33
- meta_values = META_KEYS.find_all{ |k| metadata[k] }.map{ |k| metadata[k].to_s }.join("\n")
56
+ meta_fields = fields.keys.join(": \n") + ": \n"
57
+ meta_values = fields.values.join("\n")
34
58
 
35
59
  rect = enclosure
36
60
 
@@ -3,8 +3,13 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Renderer
6
+ # Strokes the four sides of a rectangle onto a Prawn document.
6
7
  class RectRenderer < Base
8
+ # @return [Geometry::Rect] the rectangle to draw
7
9
  alias_method :rect, :subject
10
+
11
+ # @param pdf [Prawn::Document]
12
+ # @return [Array<Geometry::Line>] the sides
8
13
  def render(pdf)
9
14
  rect.sides.each do |side|
10
15
  LineRenderer.new(config, side).render(pdf)
@@ -9,9 +9,10 @@ module Laser
9
9
  # fits the page to the box, and carries the metadata as a <desc>
10
10
  # instead of drawing it.
11
11
  class SvgRenderer < Base
12
+ # @param config [Configuration] the box is built from it by {BoxFactory}
12
13
  def initialize(config)
13
14
  super
14
- self.subject = Laser::Cutter::Box.new(config)
15
+ self.subject = Laser::Cutter::BoxFactory.for(config)
15
16
  end
16
17
 
17
18
  # @return [Array<Geometry::Line>] every line to cut, computed once
@@ -24,37 +25,44 @@ module Laser
24
25
  lines.size
25
26
  end
26
27
 
27
- # Writes the file, yielding after each line drawn.
28
- def render
28
+ # @return [String] the SVG, yielding after each line drawn
29
+ def document
29
30
  svg = Victor::SVG.new(width: "#{width}#{units}", height: "#{height}#{units}", viewBox: "0 0 #{width} #{height}")
30
31
  svg.element(:desc, description) if config.metadata
31
32
  lines.each do |line|
32
33
  svg.line(**coordinates(line), stroke: "##{config[:color] || BLACK}", stroke_width: config.stroke)
33
34
  yield line if block_given?
34
35
  end
35
- File.write(config.file, svg.render)
36
+ svg.render
36
37
  end
37
38
 
38
39
  private
39
40
 
41
+ # @return [Float] the margin, in the units of the settings
40
42
  def margin
41
43
  config.margin.to_f
42
44
  end
43
45
 
46
+ # @return [Float] the width of the page: the box and both margins
44
47
  def width
45
48
  (subject.enclosure.p2.x + (2 * margin)).round(5)
46
49
  end
47
50
 
51
+ # @return [Float] the height of the page: the box and both margins
48
52
  def height
49
53
  (subject.enclosure.p2.y + (2 * margin)).round(5)
50
54
  end
51
55
 
52
56
  # SVG counts y down from the top, the box geometry up from the bottom.
57
+ #
58
+ # @param line [Geometry::Line]
59
+ # @return [Hash{Symbol => Float}] x1, y1, x2 and y2 on the page
53
60
  def coordinates(line)
54
61
  { x1: (line.p1.x + margin).round(5), y1: (height - margin - line.p1.y).round(5),
55
62
  x2: (line.p2.x + margin).round(5), y2: (height - margin - line.p2.y).round(5) }
56
63
  end
57
64
 
65
+ # @return [String] the gem's version and the settings, for the <desc>
58
66
  def description
59
67
  fields = MetaRenderer::META_KEYS.filter_map { |key| "#{key}: #{config[key]}" if config[key] }
60
68
  "Made with laser-cutter v#{VERSION}. #{fields.join(', ')}"
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
+ # Draws a box into a document: a PDF through Prawn, or an SVG through
6
+ # Victor. {Renderer.for} picks the renderer for a format.
5
7
  module Renderer
6
8
  # The renderer that writes each output format.
7
9
  FORMATS = {
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ # The box whose front slopes back, with seven panels: its side view,
6
+ # its panels, their notches and their layout on the page.
7
+ module Sloped
8
+ # A box whose front slopes back, drawn as seven panels. It answers what
9
+ # the renderers ask of a Box: the lines to cut, the rectangle that
10
+ # encloses them, and room for the metadata.
11
+ #
12
+ # The panels come from Panels, each in its own frame and without kerf.
13
+ # Here each one is grown by half the kerf, so it is cut to size, and
14
+ # moved to its place on the sheet.
15
+ class Box
16
+ # How the tilted panel joins the side walls: notched into both, or lying on them.
17
+ LIDS = %i[full plain].freeze
18
+
19
+ # @return [Profile] the side view of the box
20
+ attr_reader :profile
21
+
22
+ # @return [Geometry::Point] the size of the metadata block the layout keeps clear
23
+ attr_reader :metadata
24
+
25
+ # @param config [Configuration, Hash] width, height, depth, front_height, slope, thickness, and
26
+ # optionally notch, kerf, padding and lid
27
+ # @raise [InvalidOption] when the numbers do not make this shape
28
+ def initialize(config)
29
+ @config = config
30
+ @profile = Profile.new(depth: config[:depth], height: config[:height], front_height: config[:front_height],
31
+ slope: config[:slope], thickness: config[:thickness])
32
+ @metadata = Geometry::Point[0, 0]
33
+ end
34
+
35
+ # Moves every panel clear of a metadata block of the given size.
36
+ #
37
+ # @param size [Geometry::Point]
38
+ def metadata=(size)
39
+ @metadata = size
40
+ @outlines = @enclosure = nil
41
+ end
42
+
43
+ # @return [Hash{String => Array<Geometry::Line>}] the lines to cut for each panel, by its name
44
+ def outlines
45
+ @outlines ||= begin
46
+ placements = layout.placements
47
+ panels.to_h.to_h do |name, loop|
48
+ [name, loop.map { |point| placements[name].apply(point) }.grow(kerf / 2.0).lines]
49
+ end
50
+ end
51
+ end
52
+
53
+ # @return [Array<Geometry::Line>] every line to cut
54
+ def generate_notches
55
+ outlines.values.flatten
56
+ end
57
+ alias notches generate_notches
58
+
59
+ # @return [Geometry::Rect] the smallest rectangle around every line
60
+ def enclosure
61
+ @enclosure ||= begin
62
+ points = generate_notches.flat_map { |line| [line.p1, line.p2] }
63
+ xs = points.map(&:x)
64
+ ys = points.map(&:y)
65
+ Geometry::Rect[Geometry::Point[xs.min, ys.min], Geometry::Point[xs.max, ys.max]]
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ # @return [Float] the kerf, 0 when none is set
72
+ def kerf = (@config[:kerf] || 0.0).to_f
73
+
74
+ # @return [Symbol] :full or :plain
75
+ def lid = (@config[:lid] || LIDS.first).to_sym
76
+
77
+ # @return [Panels] the seven panels, notched, each in its own frame
78
+ def panels
79
+ notcher = Notcher.new(notch_width: @config[:notch] || (3 * profile.thickness), thickness: profile.thickness)
80
+ Panels.new(profile: profile, width: @config[:width], notcher: notcher, lid: lid)
81
+ end
82
+
83
+ # @return [Layout] where each panel goes on the page
84
+ def layout
85
+ Layout.new(sizes: panels.sizes, thickness: profile.thickness, padding: @config[:padding] || 0.0, metadata: metadata)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # Which panel fills the corner cube at each square corner of the box.
7
+ #
8
+ # Three panels meet at such a corner and one of them carries the cube,
9
+ # as a square on its own corner. The side wall never does. Of the other
10
+ # two, the back and the front strip come first, as the front and back
11
+ # do in the box with six panels. A panel whose two notches next to the
12
+ # corner are both holes cannot carry it, since the square would hang
13
+ # off the panel by a single point; the other panel then begins with a
14
+ # tab there, and takes it.
15
+ class Corners
16
+ # @param profile [Profile]
17
+ # @param width [Numeric] the inside width of the box
18
+ # @param notcher [Notcher]
19
+ def initialize(profile:, width:, notcher:)
20
+ @profile = profile
21
+ @width = width
22
+ @notcher = notcher
23
+ freeze
24
+ end
25
+
26
+ # @return [Symbol, nil] :front or :bottom; nil without a front strip, where the corner is not square
27
+ def front_bottom
28
+ return unless @profile.front_strip?
29
+
30
+ carries?(@profile.front_height) ? :front : :bottom
31
+ end
32
+
33
+ # @return [Symbol] :back or :bottom
34
+ def bottom_back
35
+ carries?(@profile.height) ? :back : :bottom
36
+ end
37
+
38
+ # @return [Symbol, nil] :back or :top; nil without a top strip, where the corner is not square
39
+ def back_top
40
+ return unless @profile.top_strip?
41
+
42
+ carries?(@profile.height) ? :back : :top
43
+ end
44
+
45
+ private
46
+
47
+ # Whether a panel with its middle notches out, as wide as the box and
48
+ # with ends of the given length, begins with a tab on either edge.
49
+ #
50
+ # @param end_length [Float] the length of the panel's ends
51
+ # @return [Boolean]
52
+ def carries?(end_length)
53
+ [@width, end_length].any? { |length| @notcher.tab_first?(length, center_out: true) }
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # Lays the panels of a sloped box on the sheet, unfolded: the panels
7
+ # that wrap around the box in one column, in the order they wrap, and a
8
+ # side wall either side of the back.
9
+ #
10
+ # +---------+
11
+ # | front |
12
+ # +---------+
13
+ # | tilted |
14
+ # +---------+
15
+ # | top |
16
+ # +-------+ +---------+ +-------+
17
+ # | left | | back | | right |
18
+ # +-------+ X---------+ +-------+
19
+ # +---------+
20
+ # | bottom |
21
+ # +---------+
22
+ # 0,0
23
+ #
24
+ # Edges that face each other on the sheet meet on the box. The front
25
+ # strip is turned half way round, so the edge it shares with the tilted
26
+ # panel faces that panel, and the right wall is the mirror image of the
27
+ # left. The corner at the bottom left is kept free for the metadata.
28
+ class Layout
29
+ # @param sizes [Hash{String => Array(Float, Float)}] the inside width and length of each panel, see Panels#sizes
30
+ # @param thickness [Numeric] of the material, which a notched edge reaches past its panel by
31
+ # @param padding [Numeric] the space between two panels
32
+ # @param metadata [Geometry::Point] the size of the block to keep free at the bottom left
33
+ def initialize(sizes:, thickness:, padding:, metadata:)
34
+ @sizes = sizes
35
+ @thickness = thickness.to_f
36
+ @padding = padding.to_f
37
+ @metadata = metadata
38
+ freeze
39
+ end
40
+
41
+ # @return [Hash{String => Placement}] where each panel goes, by name
42
+ def placements
43
+ wall = 2 * @thickness
44
+ x, y = origin
45
+ depth = @sizes['left'][0]
46
+ placed = {
47
+ 'left' => place('left', x - depth - wall - @padding, y),
48
+ 'right' => place('right', x + @sizes['back'][0] + wall + @padding, y, flip_x: true),
49
+ 'bottom' => place('bottom', x, y - @sizes['bottom'][1] - wall - @padding),
50
+ 'back' => place('back', x, y)
51
+ }
52
+ placed.merge(column(x, y + @sizes['back'][1]))
53
+ end
54
+
55
+ private
56
+
57
+ # The bottom left corner of the back, the X in the picture: clear of
58
+ # the left wall and the bottom, and of the metadata block.
59
+ #
60
+ # @return [Array(Float, Float)] its x and y
61
+ def origin
62
+ clear = @padding + @sizes['left'][0] + (3 * @thickness)
63
+ [[clear, @metadata.x + (2 * @thickness) + @padding].max, [clear, @metadata.y + (2 * @thickness) + @padding].max]
64
+ end
65
+
66
+ # The panels above the back. A strip reaches a thickness towards the
67
+ # panel it is notched into; the plain edges in between reach nowhere.
68
+ #
69
+ # @param x [Float] the left of the column
70
+ # @param y [Float] the top of the back
71
+ # @return [Hash{String => Placement}] the top strip, when there is one,
72
+ # the tilted panel, and the front strip, when there is one
73
+ def column(x, y)
74
+ placed = {}
75
+ if @sizes['top']
76
+ y += (2 * @thickness) + @padding
77
+ placed['top'] = place('top', x, y)
78
+ y += @sizes['top'][1]
79
+ end
80
+ y += @padding
81
+ placed['tilted'] = place('tilted', x, y)
82
+ placed['front'] = place('front', x, y + @sizes['tilted'][1] + @padding, flip_x: true, flip_y: true) if @sizes['front']
83
+ placed
84
+ end
85
+
86
+ # @param name [String] the panel
87
+ # @param x [Float]
88
+ # @param y [Float]
89
+ # @param flips [Hash] flip_x and flip_y, for {Placement}
90
+ # @return [Placement] the panel with its bottom left corner at x, y
91
+ def place(name, x, y, **flips)
92
+ Placement.new(Geometry::Point[x, y], size: @sizes[name], **flips)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # Draws the notched path between two points of a panel, and says how it
7
+ # begins. Every panel of the sloped box is walked counter-clockwise, so
8
+ # the material a path notches into always lies to its right.
9
+ #
10
+ # Paths are drawn without kerf: a panel is grown by half the kerf once
11
+ # its outline is whole.
12
+ class Notcher
13
+ # @return [Float] the thickness of the material
14
+ attr_reader :thickness
15
+
16
+ # @param notch_width [Numeric] the notch length asked for, a guide only
17
+ # @param thickness [Numeric] of the material
18
+ def initialize(notch_width:, thickness:)
19
+ @notch_width = notch_width.to_f
20
+ @thickness = thickness.to_f
21
+ freeze
22
+ end
23
+
24
+ # @param from [Geometry::Point] a corner of the inside outline
25
+ # @param to [Geometry::Point] the next corner
26
+ # @param center_out [Boolean] whether the middle notch is a tab
27
+ # @return [Array<Geometry::Point>] the path, from the one corner to the other
28
+ def path(from, to, center_out:)
29
+ lines = edge(from, to, center_out).lines
30
+ lines.map(&:p1) << lines.last.p2
31
+ end
32
+
33
+ # @param length [Numeric] of a side
34
+ # @param center_out [Boolean] whether its middle notch is a tab
35
+ # @return [Boolean] whether the side begins, and so ends, with a tab
36
+ def tab_first?(length, center_out:)
37
+ edge(Geometry::Point[0, 0], Geometry::Point[length, 0], center_out).first_notch_out?
38
+ end
39
+
40
+ private
41
+
42
+ # @param from [Geometry::Point]
43
+ # @param to [Geometry::Point]
44
+ # @param center_out [Boolean] whether the center notch is a tab
45
+ # @return [Notching::AngledEdge] the run, notched outwards to its right, without kerf
46
+ def edge(from, to, center_out)
47
+ Notching::AngledEdge.new(from, to, outward: :right, notch_width: @notch_width, thickness: thickness,
48
+ kerf: 0.0, center_out: center_out)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # The panels of a sloped box, each drawn in its own frame and without
7
+ # kerf: two side walls, the bottom, the back, the tilted panel, and the
8
+ # top and front strips when the box has them.
9
+ #
10
+ # Along every joint the two panels that meet take opposite middle
11
+ # notches, so they interlock. A side wall takes a hole everywhere; the
12
+ # back and the front strip take a tab on every edge; the bottom and the
13
+ # top strip take a tab into the side walls and a hole into the back and
14
+ # the front strip.
15
+ class Panels
16
+ # The names of the panels, in the order they are drawn.
17
+ NAMES = %w[left right bottom back top tilted front].freeze
18
+
19
+ # @param profile [Profile]
20
+ # @param width [Numeric] the inside width of the box
21
+ # @param notcher [Notcher]
22
+ # @param lid [Symbol] :full or :plain, how the tilted panel joins the side walls
23
+ def initialize(profile:, width:, notcher:, lid:)
24
+ @profile = profile
25
+ @width = width.to_f
26
+ @notcher = notcher
27
+ @lid = lid
28
+ @corners = Corners.new(profile: profile, width: width, notcher: notcher)
29
+ freeze
30
+ end
31
+
32
+ # @return [Hash{String => Geometry::Loop}] the outline of each panel the box has, by name
33
+ def to_h
34
+ side = SidePanel.new(profile: @profile, notcher: @notcher, lid: @lid).loop
35
+ { 'left' => side, 'right' => side, 'bottom' => bottom, 'back' => back,
36
+ 'top' => top, 'tilted' => tilted, 'front' => front }.compact
37
+ end
38
+
39
+ # The inside size of each panel, which is what a Placement flips it within.
40
+ #
41
+ # @return [Hash{String => Array(Float, Float)}] width and length of each panel the box has, by name
42
+ def sizes
43
+ side = [@profile.depth, @profile.height]
44
+ { 'left' => side, 'right' => side, 'bottom' => [@width, @profile.depth], 'back' => [@width, @profile.height],
45
+ 'top' => strip(@profile.top_strip_length), 'tilted' => [@width, @profile.tilted_length],
46
+ 'front' => strip(@profile.front_strip_length) }.compact
47
+ end
48
+
49
+ private
50
+
51
+ # @param length [Float] the length of a strip, 0 when it is dropped
52
+ # @return [Array(Float, Float), nil] its width and length; nil when it is dropped
53
+ def strip(length)
54
+ [@width, length] if length.positive?
55
+ end
56
+
57
+ # @param length [Float] from the near edge to the far edge
58
+ # @param joint [Float] how far along each end the notches into the side wall run
59
+ # The other keywords, near, far and corners, go to {RingPanel}.
60
+ #
61
+ # @return [Geometry::Loop] the outline of the panel
62
+ def panel(length:, joint: length, **)
63
+ RingPanel.new(notcher: @notcher, width: @width, length: length, joint: joint, **).loop
64
+ end
65
+
66
+ # Near the front, far at the back.
67
+ #
68
+ # @return [Geometry::Loop]
69
+ def bottom
70
+ panel(length: @profile.depth, near: (false if @profile.front_strip?), far: false,
71
+ corners: carried(near: @corners.front_bottom, far: @corners.bottom_back, by: :bottom))
72
+ end
73
+
74
+ # Near the bottom, far at the top.
75
+ #
76
+ # @return [Geometry::Loop]
77
+ def back
78
+ panel(length: @profile.height, near: true, far: (true if @profile.top_strip?),
79
+ corners: carried(near: @corners.bottom_back, far: @corners.back_top, by: :back))
80
+ end
81
+
82
+ # Near the back, far over the end of the tilted panel.
83
+ #
84
+ # @return [Geometry::Loop, nil] nil without a top strip
85
+ def top
86
+ return unless @profile.top_strip?
87
+
88
+ panel(length: @profile.top_strip_length, joint: @profile.top_depth, near: false,
89
+ corners: carried(near: @corners.back_top, by: :top))
90
+ end
91
+
92
+ # Near the bottom, far over the end of the tilted panel.
93
+ #
94
+ # @return [Geometry::Loop, nil] nil without a front strip
95
+ def front
96
+ return unless @profile.front_strip?
97
+
98
+ panel(length: @profile.front_strip_length, joint: @profile.front_height, near: true,
99
+ corners: carried(near: @corners.front_bottom, by: :front))
100
+ end
101
+
102
+ # Notched into the side walls end to end, or lying on them.
103
+ #
104
+ # @return [Geometry::Loop]
105
+ def tilted
106
+ panel(length: @profile.tilted_length, joint: @lid == :plain ? 0.0 : @profile.tilted_length)
107
+ end
108
+
109
+ # The edges whose corners a panel carries.
110
+ #
111
+ # @param by [Symbol] the panel
112
+ # @param near [Symbol, nil] the panel that carries the corners of the near edge
113
+ # @param far [Symbol, nil] the panel that carries the corners of the far edge
114
+ # @return [Array<Symbol>] :near, :far, both or neither
115
+ def carried(by:, near: nil, far: nil)
116
+ { near: near, far: far }.select { |_edge, owner| owner == by }.keys
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # Where a panel goes on the sheet: its own origin moved to a point, and
7
+ # the panel turned over within its own size when asked. Flipping x draws
8
+ # the mirror image, which the second side wall is; flipping both turns
9
+ # the panel half way round.
10
+ class Placement
11
+ # @return [Geometry::Point] where the origin of the panel lands
12
+ attr_reader :origin
13
+
14
+ # @param origin [Geometry::Point] where the origin of the panel lands
15
+ # @param size [Array(Numeric, Numeric)] the width and length a flip turns the panel within
16
+ # @param flip_x [Boolean] whether x runs from the far side back
17
+ # @param flip_y [Boolean] whether y runs from the far side back
18
+ def initialize(origin, size:, flip_x: false, flip_y: false)
19
+ @origin = origin.clone
20
+ @size = size.map(&:to_f).freeze
21
+ @flip_x = flip_x
22
+ @flip_y = flip_y
23
+ freeze
24
+ end
25
+
26
+ # @param point [Geometry::Point] in the frame of the panel
27
+ # @return [Geometry::Point] on the sheet
28
+ def apply(point)
29
+ x = @flip_x ? @size[0] - point.x : point.x
30
+ y = @flip_y ? @size[1] - point.y : point.y
31
+ Geometry::Point[@origin.x + x, @origin.y + y]
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end