laser-cutter 2.0.1 → 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 (104) 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/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
  6. 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
  7. 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
  8. data/.plans/decisions.md +11 -0
  9. data/CHANGELOG.md +134 -0
  10. data/CLAUDE.md +18 -6
  11. data/Gemfile +2 -1
  12. data/README.md +223 -50
  13. data/Rakefile +11 -7
  14. data/docs/badges/coverage_badge.svg +2 -2
  15. data/docs/decisions/decisions-on-angled-box.md +93 -0
  16. data/docs/decisions/stack.md +6 -0
  17. data/docs/images/box-3d.avif +0 -0
  18. data/docs/images/box-sloped-3d.avif +0 -0
  19. data/docs/images/box-sloped.avif +0 -0
  20. data/justfile +24 -8
  21. data/laser-cutter.gemspec +1 -1
  22. data/lib/laser/cutter/aggregator.rb +28 -0
  23. data/lib/laser/cutter/box.rb +95 -5
  24. data/lib/laser/cutter/box_factory.rb +15 -0
  25. data/lib/laser/cutter/cli/command.rb +27 -1
  26. data/lib/laser/cutter/cli/config_file.rb +2 -0
  27. data/lib/laser/cutter/cli/examples.rb +3 -0
  28. data/lib/laser/cutter/cli/generate.rb +37 -6
  29. data/lib/laser/cutter/cli/help.rb +4 -0
  30. data/lib/laser/cutter/cli/page_sizes.rb +3 -0
  31. data/lib/laser/cutter/cli/version.rb +2 -0
  32. data/lib/laser/cutter/cli.rb +23 -4
  33. data/lib/laser/cutter/configuration.rb +38 -4
  34. data/lib/laser/cutter/geometry/dimensions.rb +7 -0
  35. data/lib/laser/cutter/geometry/line.rb +42 -0
  36. data/lib/laser/cutter/geometry/loop.rb +138 -0
  37. data/lib/laser/cutter/geometry/point.rb +3 -0
  38. data/lib/laser/cutter/geometry/rect.rb +25 -0
  39. data/lib/laser/cutter/geometry/shape.rb +19 -1
  40. data/lib/laser/cutter/geometry/transform.rb +50 -0
  41. data/lib/laser/cutter/geometry/tuple.rb +47 -11
  42. data/lib/laser/cutter/launcher.rb +14 -1
  43. data/lib/laser/cutter/missing_option.rb +2 -0
  44. data/lib/laser/cutter/notching/angled_edge.rb +67 -0
  45. data/lib/laser/cutter/notching/base.rb +8 -0
  46. data/lib/laser/cutter/notching/edge.rb +44 -4
  47. data/lib/laser/cutter/notching/infinite_iterator.rb +11 -1
  48. data/lib/laser/cutter/notching/path_generator.rb +40 -6
  49. data/lib/laser/cutter/notching/shift.rb +4 -0
  50. data/lib/laser/cutter/options.rb +25 -4
  51. data/lib/laser/cutter/page_manager.rb +12 -2
  52. data/lib/laser/cutter/renderer/base.rb +19 -3
  53. data/lib/laser/cutter/renderer/box_renderer.rb +14 -1
  54. data/lib/laser/cutter/renderer/layout_renderer.rb +10 -0
  55. data/lib/laser/cutter/renderer/line_renderer.rb +6 -0
  56. data/lib/laser/cutter/renderer/meta_renderer.rb +33 -9
  57. data/lib/laser/cutter/renderer/rect_renderer.rb +5 -0
  58. data/lib/laser/cutter/renderer/svg_renderer.rb +9 -1
  59. data/lib/laser/cutter/renderer.rb +2 -0
  60. data/lib/laser/cutter/sloped/box.rb +90 -0
  61. data/lib/laser/cutter/sloped/corners.rb +58 -0
  62. data/lib/laser/cutter/sloped/layout.rb +97 -0
  63. data/lib/laser/cutter/sloped/notcher.rb +53 -0
  64. data/lib/laser/cutter/sloped/panels.rb +121 -0
  65. data/lib/laser/cutter/sloped/placement.rb +36 -0
  66. data/lib/laser/cutter/sloped/profile.rb +176 -0
  67. data/lib/laser/cutter/sloped/request.rb +31 -0
  68. data/lib/laser/cutter/sloped/ring_panel.rb +120 -0
  69. data/lib/laser/cutter/sloped/side_panel.rb +89 -0
  70. data/lib/laser/cutter/types.rb +3 -0
  71. data/lib/laser/cutter/units_converter.rb +5 -0
  72. data/lib/laser/cutter/version.rb +2 -1
  73. data/lib/laser/cutter/zero_value_not_allowed.rb +2 -0
  74. data/lib/laser/cutter.rb +2 -0
  75. data/spec/laser/cutter/angled_edge_spec.rb +103 -0
  76. data/spec/laser/cutter/box_factory_spec.rb +19 -0
  77. data/spec/laser/cutter/cli_spec.rb +87 -7
  78. data/spec/laser/cutter/configuration_spec.rb +37 -0
  79. data/spec/laser/cutter/loop_spec.rb +107 -0
  80. data/spec/laser/cutter/options_spec.rb +51 -9
  81. data/spec/laser/cutter/renderer/layout_renderer_spec.rb +18 -0
  82. data/spec/laser/cutter/renderer/meta_renderer_spec.rb +82 -0
  83. data/spec/laser/cutter/renderer/svg_renderer_spec.rb +34 -0
  84. data/spec/laser/cutter/sloped/assembly_spec.rb +78 -0
  85. data/spec/laser/cutter/sloped/box_spec.rb +151 -0
  86. data/spec/laser/cutter/sloped/corners_spec.rb +45 -0
  87. data/spec/laser/cutter/sloped/layout_spec.rb +77 -0
  88. data/spec/laser/cutter/sloped/notcher_spec.rb +57 -0
  89. data/spec/laser/cutter/sloped/panels_spec.rb +175 -0
  90. data/spec/laser/cutter/sloped/placement_spec.rb +50 -0
  91. data/spec/laser/cutter/sloped/request_spec.rb +48 -0
  92. data/spec/laser/cutter/sloped/ring_panel_spec.rb +102 -0
  93. data/spec/laser/cutter/sloped/side_panel_spec.rb +98 -0
  94. data/spec/laser/cutter/sloped_profile_spec.rb +156 -0
  95. data/spec/laser/cutter/transform_spec.rb +53 -0
  96. data/spec/spec_helper.rb +4 -0
  97. data/spec/support/loops.rb +106 -0
  98. data/spec/support/sloped.rb +26 -0
  99. data/spec/support/solid.rb +95 -0
  100. data/spec/wishes/wish-tilted-box.png +0 -0
  101. metadata +55 -9
  102. /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
  103. /data/.plans/{002.00-lid-options/plan.md → 002.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257lid-options/plan.md"} +0 -0
  104. /data/.plans/{003.00-ruby-api/plan.md → 003.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257ruby-api/plan.md"} +0 -0
@@ -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
@@ -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