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,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ RSpec.describe BoxFactory do
8
+ let(:settings) { { width: 4, height: 3, depth: 5, thickness: 0.25, units: 'in', file: 'box.pdf' } }
9
+
10
+ it 'builds the box with six panels for settings without a slope' do
11
+ expect(described_class.for(Configuration.new(settings))).to be_a(Box)
12
+ end
13
+
14
+ it 'builds a sloped box for settings with a slope' do
15
+ expect(described_class.for(Configuration.new(settings.merge(slope: 45, front_height: 1)))).to be_a(Sloped::Box)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'digest'
5
+
6
+ module Laser
7
+ module Cutter
8
+ # Every outline here is checked as a region, point by point, against a
9
+ # description of the panel that owes nothing to the code that drew it.
10
+ RSpec.describe Box, 'lid' do
11
+ # Wide enough for a sample to land in every strip the kerf adds.
12
+ let(:kerf) { 0.5 }
13
+ let(:boxes) { Hash.new { |cache, (lid, cut)| cache[[lid, cut]] = build(lid, cut) } }
14
+
15
+ def build(lid, cut)
16
+ settings = dimensions.merge(units: 'mm', file: 'box.pdf', lid: lid, kerf: cut)
17
+ Box.new(Configuration.new(settings)).tap(&:generate_notches)
18
+ end
19
+
20
+ # @return [Array<Array(Float, Float)>] points over a face and the margin around it
21
+ def samples(face)
22
+ Outline.samples(face, margin: 1.5 * dimensions[:thickness])
23
+ end
24
+
25
+ # @return [Boolean] whether a point is inside the named panel of a box cut without kerf
26
+ def material?(lid, name, x, y)
27
+ Outline.inside?(boxes[[lid, 0.0]].outlines[name], x, y)
28
+ end
29
+
30
+ def between?(value, from, to)
31
+ from < value && value < to
32
+ end
33
+
34
+ # The region each panel should cover without kerf, given the box with a full lid.
35
+ def expected?(lid, face, x, y)
36
+ t = dimensions[:thickness]
37
+ full = material?(:full, face.name, x, y)
38
+ across = between?(x, face.p1.x, face.p2.x)
39
+ case face.name
40
+ when 'bottom' then full
41
+ when 'front', 'left', 'right' then full && y > face.p1.y
42
+ when 'back' then lid == :plain ? full && y < face.p2.y : full && (across || y < face.p2.y)
43
+ when 'top'
44
+ covered = between?(x, face.p1.x - t, face.p2.x + t) && between?(y, face.p1.y - t, face.p2.y + t)
45
+ lid == :plain || y > face.p1.y ? covered : covered && (full || !across)
46
+ end
47
+ end
48
+
49
+ shared_examples 'a box with a lid that lifts off' do |lid|
50
+ let(:box) { boxes[[lid, 0.0]] }
51
+ let(:cut) { boxes[[lid, kerf]] }
52
+
53
+ it 'closes every outline' do
54
+ expect([box, cut].flat_map { |b| b.outlines.values }).to all(satisfy { |lines| Outline.closed?(lines) })
55
+ end
56
+
57
+ it 'keeps every panel where the full lid has it' do
58
+ expect(box.faces.map(&:to_a)).to eq(boxes[[:full, 0.0]].faces.map(&:to_a))
59
+ end
60
+
61
+ %w[top front back left right bottom].each do |name|
62
+ it "cuts the #{name} panel to shape" do
63
+ face = box.public_send(name)
64
+ wrong = samples(face).reject { |x, y| material?(lid, name, x, y) == expected?(lid, face, x, y) }
65
+ expect(wrong).to be_empty
66
+ end
67
+
68
+ it "grows the #{name} panel by half the kerf on every side" do
69
+ wrong = samples(box.public_send(name)).reject do |x, y|
70
+ Outline.inside?(cut.outlines[name], x, y) == Outline.within?(box.outlines[name], x, y, kerf / 2)
71
+ end
72
+ expect(wrong).to be_empty
73
+ end
74
+ end
75
+ end
76
+
77
+ shared_examples 'a box of any size' do
78
+ describe 'with a plain lid' do
79
+ it_behaves_like 'a box with a lid that lifts off', :plain
80
+
81
+ it 'draws the lid as four lines' do
82
+ expect(boxes[[:plain, kerf]].outlines['top'].size).to eq(4)
83
+ end
84
+
85
+ it 'sizes the lid to the outside of the walls, plus the kerf' do
86
+ w, d, t = dimensions.values_at(:width, :depth, :thickness)
87
+ lengths = boxes[[:plain, kerf]].outlines['top'].map(&:length).sort
88
+ expect(lengths).to match_array([d, d, w, w].map { |side| be_within(1e-6).of(side + (2 * t) + kerf) })
89
+ end
90
+ end
91
+
92
+ describe 'with a lid notched into the back wall' do
93
+ it_behaves_like 'a box with a lid that lifts off', :back
94
+
95
+ it 'leaves the back wall its notches under the lid' do
96
+ back = boxes[[:back, 0.0]].back
97
+ tabs = samples(back).count { |x, y| y > back.p2.y && material?(:back, 'back', x, y) }
98
+ expect(tabs).to be_positive
99
+ end
100
+ end
101
+ end
102
+
103
+ context 'when the lid would own the corners anyway, and its notches start with a tab' do
104
+ let(:dimensions) { { width: 50, height: 60, depth: 70, thickness: 6, notch: 10 } }
105
+
106
+ it_behaves_like 'a box of any size'
107
+ end
108
+
109
+ context 'when the front would own the corners, and the notches of the lid start with a hole' do
110
+ let(:dimensions) { { width: 40, height: 30, depth: 20, thickness: 3, notch: 5 } }
111
+
112
+ it_behaves_like 'a box of any size'
113
+ end
114
+
115
+ context 'when the box is wide and its material thin' do
116
+ let(:dimensions) { { width: 100, height: 80, depth: 60, thickness: 3, notch: 9 } }
117
+
118
+ it_behaves_like 'a box of any size'
119
+ end
120
+
121
+ describe 'a full lid' do
122
+ # Digests of the lines 2.0.0 drew, before the lid became an option.
123
+ {
124
+ [{ width: 50, height: 60, depth: 70, thickness: 6, notch: 10 }, 0.0] => '35d6a17c6f524b00',
125
+ [{ width: 50, height: 60, depth: 70, thickness: 6, notch: 10 }, 0.1] => 'af287e64ac6a249f',
126
+ [{ width: 40, height: 30, depth: 20, thickness: 3, notch: 6 }, 0.15] => '1aed155401688002',
127
+ [{ width: 100, height: 80, depth: 60, thickness: 3, notch: 9 }, 0.1] => '06b3cbc4e66bd18b'
128
+ }.each do |(dimensions, cut), digest|
129
+ it "draws what 2.0.0 drew for #{dimensions.values.join('x')} with a #{cut} kerf" do
130
+ lines = Box.new(Configuration.new(dimensions.merge(units: 'mm', file: 'box.pdf', kerf: cut))).generate_notches
131
+ coordinates = lines.map { |l| [l.p1.x, l.p1.y, l.p2.x, l.p2.y].map { |v| v.round(6) + 0.0 } }.sort
132
+ expect(Digest::SHA256.hexdigest(coordinates.inspect)[0, 16]).to eq(digest)
133
+ end
134
+ end
135
+
136
+ it 'is the default' do
137
+ expect(Box.new(Configuration.new(box: '4x3x2/0.125', file: 'box.pdf')).lid).to eq(:full)
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -10,13 +10,13 @@ RSpec.describe 'laser-cutter', type: :aruba do
10
10
  expect(last_command_started).to have_output(/generate.*page-sizes.*examples.*help/m)
11
11
  end
12
12
 
13
- it 'wraps help at 90 columns or fewer' do
14
- expect(last_command_started.output.lines.map { |line| line.chomp.length }.max).to be <= 90
13
+ it 'wraps help at 100 columns or fewer' do
14
+ expect(last_command_started.output.lines.map { |line| line.chomp.length }.max).to be <= 100
15
15
  end
16
16
  end
17
17
 
18
18
  describe 'the width of help' do
19
- { 200 => 90, 80 => 74, 0 => 74, nil => 74 }.each do |columns, width|
19
+ { 200 => 100, 80 => 74, 0 => 84, nil => 84 }.each do |columns, width|
20
20
  it "is #{width} on a terminal #{columns.inspect} columns wide" do
21
21
  expect(Laser::Cutter::CLI.help_width(columns)).to eq(width)
22
22
  end
@@ -50,7 +50,7 @@ RSpec.describe 'laser-cutter', type: :aruba do
50
50
  %w[version -V --version].each do |argument|
51
51
  it "answers to #{argument}" do
52
52
  run_command_and_stop("laser-cutter #{argument}")
53
- expect(last_command_started).to have_output(Laser::Cutter::VERSION)
53
+ expect(last_command_started.stdout).to eq("#{Laser::Cutter::VERSION}\n")
54
54
  end
55
55
  end
56
56
  end
@@ -74,11 +74,10 @@ RSpec.describe 'laser-cutter', type: :aruba do
74
74
  end
75
75
  end
76
76
 
77
- # dry-cli-autocomplete writes to $stdout rather than the stream Aruba hands
78
- # the command, so this reads the process's stdout instead.
79
77
  describe 'completion' do
80
78
  it 'prints a zsh completion script' do
81
- expect { run_command_and_stop('laser-cutter completion zsh') }.to output(/laser-cutter/).to_stdout
79
+ run_command_and_stop('laser-cutter completion zsh')
80
+ expect(last_command_started).to have_output(/laser-cutter/)
82
81
  end
83
82
  end
84
83
 
@@ -99,7 +98,16 @@ RSpec.describe 'laser-cutter', type: :aruba do
99
98
  expect(info.lines.map { |line| line.chomp.length }.max).to eq(Laser::Cutter::CLI.help_width)
100
99
  end
101
100
 
101
+ it 'draws the info box 100 wide in a terminal 200 columns wide' do
102
+ set_environment_variable('COLUMNS', '200')
103
+ run_command_and_stop("laser-cutter generate #{box} -o box.pdf")
104
+ info = last_command_started.stdout.split('┌─ Success').first
105
+ expect(info.lines.map { |line| line.chomp.length }.max).to eq(100)
106
+ end
107
+
108
+ # Wide enough for any working directory Aruba picks, which 80 columns are not.
102
109
  it 'keeps the path of the file on one line' do
110
+ set_environment_variable('COLUMNS', '200')
103
111
  run_command_and_stop("laser-cutter generate #{box} -o box.pdf")
104
112
  expect(last_command_started.stdout).to include(expand_path('box.pdf'))
105
113
  end
@@ -111,6 +119,7 @@ RSpec.describe 'laser-cutter', type: :aruba do
111
119
  end
112
120
 
113
121
  it 'closes with a success box that gives the path of the file' do
122
+ set_environment_variable('COLUMNS', '200')
114
123
  run_command_and_stop("laser-cutter generate #{box} -f svg -o box.svg")
115
124
  expect(last_command_started.stdout).to match(/─ Info ─.*─ Success ─.*Generated SVG file:.*box\.svg/m)
116
125
  end
@@ -120,6 +129,55 @@ RSpec.describe 'laser-cutter', type: :aruba do
120
129
  expect(read('box.svg').join).to include('<svg', '<line').and include('viewBox="0 0 9.2012 11.5512"')
121
130
  end
122
131
 
132
+ describe 'a sloped box' do
133
+ let(:sloped) { '-b 4x3x5/0.25/0.6 --slope 45 --front-height 1' }
134
+
135
+ it 'writes a PDF' do
136
+ run_command_and_stop("laser-cutter generate #{sloped} -o box.pdf")
137
+ expect(read('box.pdf').first).to start_with('%PDF')
138
+ end
139
+
140
+ it 'writes an SVG with lines along the slope' do
141
+ run_command_and_stop("laser-cutter generate #{sloped} -f svg -o box.svg")
142
+ expect(read('box.svg').join).to match(/<line x1="(?<x1>[\d.]+)" y1="(?<y1>[\d.]+)" x2="(?!\k<x1>")[\d.]+" y2="(?!\k<y1>")[\d.]+"/)
143
+ end
144
+
145
+ it 'names the front height and the slope among the dimensions' do
146
+ run_command_and_stop("laser-cutter generate #{sloped} -o box.pdf")
147
+ expect(last_command_started.stdout).to match(/Depth:\s+5\.0 in.*Front:\s+1\.0 in.*Slope:\s+45\.0°.*Lid:\s+full/m)
148
+ end
149
+
150
+ it 'lays a plain lid on the slope' do
151
+ run_command_and_stop("laser-cutter generate #{sloped} --lid plain -o box.pdf")
152
+ expect(last_command_started.stdout).to match(/Slope:\s+45\.0°.*Lid:\s+plain.*─ Success ─/m)
153
+ end
154
+
155
+ {
156
+ '--slope 45' => /front_height is required with slope, but missing/,
157
+ '--front-height 1' => /front_height is the front of a sloped box, so it needs a slope/,
158
+ '--slope 45 --front-height 1 --lid back' => /lid cannot be "back" with a slope, but must be one of: full, plain/,
159
+ '--slope 10 --front-height 1' => /10\.0° slope from 1\.0 up to 3\.0 needs a depth of 11\.343/,
160
+ '--slope 95 --front-height 1' => /slope must be between 0 and 90 degrees, got 95\.0/
161
+ }.each do |arguments, message|
162
+ it "fails on #{arguments}" do
163
+ set_environment_variable('COLUMNS', '200')
164
+ run_command_and_stop("laser-cutter generate -b 4x3x5/0.25/0.6 #{arguments} -o box.pdf", fail_on_error: false)
165
+ expect(last_command_started).to have_exit_status(1)
166
+ expect(last_command_started.stderr).to match(/─ Error ─/).and match(message)
167
+ end
168
+ end
169
+
170
+ it 'says in the help which lid a slope cannot take' do
171
+ run_command_and_stop('laser-cutter generate --help')
172
+ expect(last_command_started.stdout.gsub(/\s+/, ' ')).to include('into the back wall only (not with --slope)')
173
+ end
174
+
175
+ it 'explains --slope and --front-height in the help' do
176
+ run_command_and_stop('laser-cutter generate --help')
177
+ expect(last_command_started).to have_output(/--slope.*--front-height/m)
178
+ end
179
+ end
180
+
123
181
  it 'draws the box without kerf as well with --inside-box' do
124
182
  run_command_and_stop("laser-cutter generate #{box} --inside-box -o box.pdf")
125
183
  expect(last_command_started).to have_output(%r{Drawing box\.pdf 656/656})
@@ -148,6 +206,34 @@ RSpec.describe 'laser-cutter', type: :aruba do
148
206
  expect(last_command_started).to have_output(/"depth": 2\.0/)
149
207
  end
150
208
 
209
+ { 'plain' => 224, 'back' => 260, 'full' => 328 }.each do |lid, lines|
210
+ it "draws #{lines} lines for a #{lid} lid" do
211
+ run_command_and_stop("laser-cutter generate #{box} --lid #{lid} -o box.pdf")
212
+ expect(last_command_started).to have_output(%r{Lid:\s+#{lid}.*Drawing box\.pdf #{lines}/#{lines}}m)
213
+ end
214
+ end
215
+
216
+ { '3x3x3' => [3.0, 3.0, 3.0], '9x4x9' => [9.0, 4.0, 9.0] }.each do |size, dimensions|
217
+ it "generates a #{size} inch storage box with a removable lid" do
218
+ run_command_and_stop("laser-cutter generate -u in -b #{size}/0.125 --lid plain -o storage.pdf -W settings.json")
219
+
220
+ expect(last_command_started).to have_exit_status(0)
221
+ expect(read('storage.pdf').first).to start_with('%PDF')
222
+ expect(JSON.parse(read('settings.json').join)).to include(
223
+ 'width' => dimensions[0], 'height' => dimensions[1], 'depth' => dimensions[2],
224
+ 'thickness' => 0.125, 'units' => 'in', 'lid' => 'plain'
225
+ )
226
+ end
227
+ end
228
+
229
+ it 'keeps the lid a saved configuration asks for' do
230
+ run_command_and_stop("laser-cutter generate #{box} -L plain -o box.pdf -W settings.json")
231
+ expect(JSON.parse(read('settings.json').join)).to include('lid' => 'plain')
232
+
233
+ run_command_and_stop('laser-cutter generate -R settings.json -o again.pdf')
234
+ expect(last_command_started).to have_output(%r{Drawing again\.pdf 224/224})
235
+ end
236
+
151
237
  it 'opens the file with --open' do
152
238
  allow_any_instance_of(Laser::Cutter::CLI::Generate).to receive(:system)
153
239
  run_command_and_stop("laser-cutter generate #{box} -o box.pdf --open")
@@ -167,11 +253,32 @@ RSpec.describe 'laser-cutter', type: :aruba do
167
253
  expect(last_command_started.stderr.lines.map { |line| line.chomp.length }.max).to eq(Laser::Cutter::CLI.help_width)
168
254
  end
169
255
 
256
+ # Aruba's streams are not a tty, like a pipe: boxes must still follow the terminal.
257
+ { '200' => 100, '60' => 54 }.each do |columns, width|
258
+ it "draws the error box #{width} wide in a terminal #{columns} columns wide" do
259
+ set_environment_variable('COLUMNS', columns)
260
+ run_command_and_stop("laser-cutter generate #{box}", fail_on_error: false)
261
+ expect(last_command_started.stderr.lines.map { |line| line.chomp.length }.max).to eq(width)
262
+ end
263
+ end
264
+
170
265
  it 'fails on an unknown format' do
171
266
  run_command_and_stop("laser-cutter generate #{box} -f dxf -o box.dxf", fail_on_error: false)
172
267
  expect(last_command_started).to have_output(/unknown format "dxf", expected one of: pdf, svg/)
173
268
  end
174
269
 
270
+ it 'fails on an unknown lid' do
271
+ run_command_and_stop("laser-cutter generate #{box} --lid sliding -o box.pdf", fail_on_error: false)
272
+ expect(last_command_started).to have_exit_status(1)
273
+ expect(last_command_started.stderr).to match(/was called with arguments.*--lid sliding/)
274
+ end
275
+
276
+ it 'fails on an unknown lid in a saved configuration' do
277
+ write_file('settings.json', '{"box": "4x3x2/0.125/0.5", "lid": "sliding"}')
278
+ run_command_and_stop('laser-cutter generate -R settings.json -o box.pdf', fail_on_error: false)
279
+ expect(last_command_started.stderr).to match(/─ Error ─.*lid is "sliding", but must be one of: full, back, plain/m)
280
+ end
281
+
175
282
  it 'fails on a missing configuration file' do
176
283
  run_command_and_stop('laser-cutter generate -R nothing.json -o box.pdf', fail_on_error: false)
177
284
  expect(last_command_started).to have_output(/cannot read the configuration from nothing\.json/)
@@ -40,6 +40,60 @@ module Laser
40
40
  end
41
41
  end
42
42
 
43
+ describe 'lid' do
44
+ let(:opts) { { 'box' => '2x3x2/0.125/0.5', 'file' => '/tmp/a' } }
45
+
46
+ its(:lid) { is_expected.to eql('full') }
47
+
48
+ %w[full back plain].each do |lid|
49
+ it "accepts #{lid}" do
50
+ expect { described_class.new(opts.merge('lid' => lid)).validate! }.not_to raise_error
51
+ end
52
+ end
53
+
54
+ it 'rejects anything else' do
55
+ expect { described_class.new(opts.merge('lid' => 'sliding')).validate! }
56
+ .to raise_error(Laser::Cutter::InvalidOption, /lid is "sliding"/)
57
+ end
58
+ end
59
+
60
+ describe 'slope' do
61
+ let(:opts) { { 'box' => '4x3x5/0.125', 'file' => '/tmp/a', 'slope' => '45', 'front_height' => '1' } }
62
+
63
+ its(:slope) { is_expected.to eql(45.0) }
64
+ its(:front_height) { is_expected.to eql(1.0) }
65
+
66
+ it 'passes with a front height' do
67
+ expect { config.validate! }.not_to raise_error
68
+ end
69
+
70
+ it 'needs a front height' do
71
+ expect { described_class.new(opts.except('front_height')).validate! }
72
+ .to raise_error(Laser::Cutter::MissingOption, /front_height is required with slope/)
73
+ end
74
+
75
+ it 'rejects a front height without a slope' do
76
+ expect { described_class.new(opts.except('slope')).validate! }.to raise_error(Laser::Cutter::InvalidOption, /needs a slope/)
77
+ end
78
+
79
+ it 'rejects a lid notched into the back wall' do
80
+ expect { described_class.new(opts.merge('lid' => 'back')).validate! }
81
+ .to raise_error(Laser::Cutter::InvalidOption, /lid cannot be "back" with a slope/)
82
+ end
83
+
84
+ it 'rejects numbers that do not make the shape' do
85
+ expect { described_class.new(opts.merge('slope' => '10')).validate! }
86
+ .to raise_error(Laser::Cutter::InvalidOption, /needs a depth/)
87
+ end
88
+
89
+ context 'after change_units(:mm)' do
90
+ subject(:config) { described_class.new(opts).tap { |c| c.change_units(:mm) } }
91
+
92
+ its(:front_height) { is_expected.to eql(25.4) }
93
+ its(:slope) { is_expected.to eql(45.0) }
94
+ end
95
+ end
96
+
43
97
  context 'when notch is omitted' do
44
98
  let(:opts) { { 'box' => '2.0x1.0x2/0.125', 'file' => '/tmp/a' } }
45
99
 
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Geometry
8
+ RSpec.describe Loop do
9
+ def loop_of(*pairs) = described_class.new(pairs.map { |x, y| Point[x, y] })
10
+ def corners(outline) = outline.points.map { |point| point.to_a.map { |c| c.round(9) + 0.0 } }
11
+
12
+ subject(:square) { loop_of([0, 0], [4, 0], [4, 4], [0, 4]) }
13
+
14
+ its(:area) { is_expected.to eq(16.0) }
15
+ it { is_expected.to be_frozen }
16
+
17
+ it 'joins its corners end to end, back to the first' do
18
+ expect(square.lines.map { |line| [line.p1.to_a, line.p2.to_a] })
19
+ .to eq([[[0, 0], [4, 0]], [[4, 0], [4, 4]], [[4, 4], [0, 4]], [[0, 4], [0, 0]]])
20
+ end
21
+
22
+ it 'hands out copies of its corners' do
23
+ square.points.first.x = 9
24
+ expect(corners(square).first).to eq([0, 0])
25
+ end
26
+
27
+ describe 'what a walk leaves behind' do
28
+ it 'drops a point given twice' do
29
+ expect(corners(loop_of([0, 0], [4, 0], [4, 0], [4, 4], [0, 4], [0, 0]))).to eq(corners(square))
30
+ end
31
+
32
+ it 'drops a point in the middle of a straight run' do
33
+ expect(corners(loop_of([0, 0], [1, 0], [3, 0], [4, 0], [4, 4], [0, 4]))).to eq(corners(square))
34
+ end
35
+
36
+ it 'drops a spike, out and straight back' do
37
+ expect(corners(loop_of([0, 0], [4, 0], [4, 4], [4, 6], [4, 4], [0, 4]))).to eq(corners(square))
38
+ end
39
+
40
+ it 'drops a spike that ends on the first point' do
41
+ expect(corners(loop_of([0, 0], [4, 0], [4, 4], [0, 4], [0, 0], [-2, 0]))).to eq(corners(square))
42
+ end
43
+
44
+ it 'drops a spike that turns back only part of the way' do
45
+ expect(corners(loop_of([0, 0], [6, 0], [4, 0], [4, 4], [0, 4]))).to eq(corners(square))
46
+ end
47
+
48
+ it 'keeps a corner a point away from the noise of a float' do
49
+ expect(corners(loop_of([0, 0], [4, 0], [4 + 1e-13, 1e-13], [4, 4], [0, 4])).size).to eq(4)
50
+ end
51
+
52
+ it 'refuses a walk that encloses nothing' do
53
+ expect { loop_of([0, 0], [1, 0], [2, 0]) }.to raise_error(ArgumentError, /on one line/)
54
+ end
55
+
56
+ it 'refuses fewer than three corners' do
57
+ expect { loop_of([0, 0], [1, 0]) }.to raise_error(ArgumentError, /three corners/)
58
+ end
59
+ end
60
+
61
+ describe '#grow' do
62
+ it 'moves every side of a square out' do
63
+ expect(corners(square.grow(0.5))).to eq([[-0.5, -0.5], [4.5, -0.5], [4.5, 4.5], [-0.5, 4.5]])
64
+ end
65
+
66
+ it 'grows a clockwise outline outwards too' do
67
+ expect(described_class.new(square.points.reverse).grow(0.5).area).to eq(25.0)
68
+ end
69
+
70
+ it 'moves an inside corner out with the sides that meet there' do
71
+ ell = loop_of([0, 0], [4, 0], [4, 2], [2, 2], [2, 4], [0, 4])
72
+ expect(corners(ell.grow(0.25))).to eq([[-0.25, -0.25], [4.25, -0.25], [4.25, 2.25], [2.25, 2.25], [2.25, 4.25], [-0.25, 4.25]])
73
+ end
74
+
75
+ it 'keeps a corner that is not square sharp, each side the distance out' do
76
+ grown = loop_of([0, 0], [4, 0], [4, 3]).grow(0.1)
77
+ expect(corners(grown).map { |x, y| [x.round(6), y.round(6)] }).to eq([[-0.3, -0.1], [4.1, -0.1], [4.1, 3.2]])
78
+ end
79
+
80
+ it 'shrinks by a negative distance' do
81
+ expect(square.grow(-1).area).to eq(4.0)
82
+ end
83
+
84
+ it 'returns itself for no distance' do
85
+ expect(square.grow(0)).to be(square)
86
+ end
87
+
88
+ # The box with six panels widens its tabs and narrows its holes by
89
+ # the kerf, edge by edge. Growing the outline it cuts without kerf
90
+ # by half the kerf must come to the same lines.
91
+ it 'grows a panel by half the kerf to what Box draws with that kerf' do
92
+ settings = { width: 70, height: 20, depth: 50, thickness: 4.3, notch: 10, units: 'mm', file: 'box.pdf' }
93
+ plain, cut = [0.0, 0.3].map { |kerf| Box.new(Configuration.new(settings.merge(kerf: kerf))).tap(&:generate_notches) }
94
+ grown = plain.outlines.transform_values { |lines| Loops.rounded(Loops.chain(lines).grow(0.15).lines) }
95
+ expect(grown).to eq(cut.outlines.transform_values { |lines| Loops.rounded(lines) })
96
+ end
97
+ end
98
+
99
+ describe '#map' do
100
+ it 'moves every corner' do
101
+ expect(corners(square.map { |point| point.plus(1, 2) })).to eq([[1, 2], [5, 2], [5, 6], [1, 6]])
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end