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,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ RSpec.describe Options do
8
+ subject(:options) { described_class.new(settings) }
9
+
10
+ let(:dimensions) { { width: 40, height: 30, depth: 20, thickness: 3 } }
11
+ let(:settings) { dimensions }
12
+
13
+ describe 'with the dimensions alone' do
14
+ its(:width) { is_expected.to eql(40.0) }
15
+ its(:thickness) { is_expected.to eql(3.0) }
16
+ its(:units) { is_expected.to eq('in') }
17
+ its(:lid) { is_expected.to eq('full') }
18
+ its(:page_layout) { is_expected.to eq('portrait') }
19
+ its(:metadata) { is_expected.to be(true) }
20
+ its(:inside_box) { is_expected.to be(false) }
21
+ its(:notch) { is_expected.to be_nil }
22
+ its(:kerf) { is_expected.to be_nil }
23
+ its(:format) { is_expected.to be_nil }
24
+ its(:file) { is_expected.to be_nil }
25
+ its(:page_size) { is_expected.to be_nil }
26
+
27
+ it { is_expected.not_to respond_to(:width=) }
28
+ end
29
+
30
+ describe 'with what a form sends: Strings, some of them blank' do
31
+ let(:settings) do
32
+ { 'width' => '70', 'height' => '20', 'depth' => '50', 'thickness' => '4.3', 'notch' => '', 'kerf' => '0.1',
33
+ 'units' => 'mm', 'lid' => 'plain', 'format' => 'SVG', 'page_size' => 'a4', 'page_layout' => '',
34
+ 'metadata' => '0', 'inside_box' => 'true', 'margin' => '', 'padding' => '2', 'stroke' => '0.2' }
35
+ end
36
+
37
+ its(:width) { is_expected.to eql(70.0) }
38
+ its(:thickness) { is_expected.to eql(4.3) }
39
+ its(:notch) { is_expected.to be_nil }
40
+ its(:kerf) { is_expected.to eql(0.1) }
41
+ its(:units) { is_expected.to eq('mm') }
42
+ its(:lid) { is_expected.to eq('plain') }
43
+ its(:format) { is_expected.to eq('svg') }
44
+ its(:page_size) { is_expected.to eq('A4') }
45
+ its(:page_layout) { is_expected.to eq('portrait') }
46
+ its(:metadata) { is_expected.to be(false) }
47
+ its(:inside_box) { is_expected.to be(true) }
48
+ its(:margin) { is_expected.to be_nil }
49
+ its(:padding) { is_expected.to eql(2.0) }
50
+ its(:stroke) { is_expected.to eql(0.2) }
51
+ end
52
+
53
+ describe 'with Symbols' do
54
+ let(:settings) { dimensions.merge(units: :mm, lid: :back, format: :pdf, page_layout: :landscape) }
55
+
56
+ its(:units) { is_expected.to eq('mm') }
57
+ its(:lid) { is_expected.to eq('back') }
58
+ its(:format) { is_expected.to eq('pdf') }
59
+ its(:page_layout) { is_expected.to eq('landscape') }
60
+ end
61
+
62
+ describe 'with a slope' do
63
+ let(:settings) { dimensions.merge('slope' => '45', 'front_height' => '10') }
64
+
65
+ its(:slope) { is_expected.to eql(45.0) }
66
+ its(:front_height) { is_expected.to eql(10.0) }
67
+
68
+ it 'carries both into the configuration' do
69
+ expect(options.to_configuration).to include(slope: 45.0, front_height: 10.0)
70
+ end
71
+
72
+ it 'takes a front height of zero' do
73
+ expect(described_class.new(dimensions.merge(slope: 60, front_height: 0)).front_height).to eql(0.0)
74
+ end
75
+
76
+ it 'takes a lid that lifts off' do
77
+ expect(described_class.new(settings.merge(lid: :plain)).lid).to eq('plain')
78
+ end
79
+ end
80
+
81
+ describe 'without a slope' do
82
+ its(:slope) { is_expected.to be_nil }
83
+ its(:front_height) { is_expected.to be_nil }
84
+
85
+ it 'reads blank Strings from a form as no slope' do
86
+ expect(described_class.new(dimensions.merge('slope' => '', 'front_height' => '')).slope).to be_nil
87
+ end
88
+ end
89
+
90
+ describe 'given an Options' do
91
+ it 'returns it' do
92
+ expect(described_class.new(options)).to be(options)
93
+ end
94
+ end
95
+
96
+ describe '#new' do
97
+ subject(:changed) { options.new(lid: :plain) }
98
+
99
+ its(:lid) { is_expected.to eq('plain') }
100
+ its(:width) { is_expected.to eql(40.0) }
101
+ end
102
+
103
+ describe 'what it refuses' do
104
+ {
105
+ { lid: 'sliding' } => 'lid cannot be "sliding", but must be one of: full, back, plain.',
106
+ { units: 'cm' } => 'units cannot be "cm", but must be one of: in, mm.',
107
+ { format: 'dxf' } => 'format cannot be "dxf", but must be one of: pdf, svg.',
108
+ { page_layout: 'sideways' } => 'page_layout cannot be "sideways", but must be one of: portrait, landscape.',
109
+ { page_size: 'A99' } => 'page_size cannot be "A99".',
110
+ { width: 'wide' } => 'width cannot be "wide".',
111
+ { depth: 0 } => 'depth cannot be 0.',
112
+ { kerf: -0.1 } => 'kerf cannot be -0.1.',
113
+ { metadata: 'maybe' } => 'metadata cannot be "maybe".',
114
+ { slope: 90, front_height: 10 } => 'slope cannot be 90.',
115
+ { slope: 0, front_height: 10 } => 'slope cannot be 0.',
116
+ { front_height: -1, slope: 45 } => 'front_height cannot be -1.',
117
+ { front_height: 10 } => 'front_height is the front of a sloped box, so it needs a slope.',
118
+ { lid: 'back', slope: 45, front_height: 10 } => 'lid cannot be "back" with a slope, but must be one of: full, plain.'
119
+ }.each do |bad, message|
120
+ it "raises InvalidOption when #{bad.keys.first} is #{bad.values.first.inspect}" do
121
+ expect { described_class.new(dimensions.merge(bad)) }.to raise_error(InvalidOption, message)
122
+ end
123
+ end
124
+
125
+ it 'raises InvalidOption for a key that is not an option' do
126
+ expect { described_class.new(dimensions.merge(lids: :plain)) }.to raise_error(InvalidOption, /lids/)
127
+ end
128
+
129
+ it 'raises MissingOption naming every dimension left out' do
130
+ expect { described_class.new(width: 40, depth: '') }
131
+ .to raise_error(MissingOption, 'height, depth, thickness are required, but missing.')
132
+ end
133
+
134
+ it 'raises MissingOption for a slope without a front height' do
135
+ expect { described_class.new(dimensions.merge(slope: 45)) }
136
+ .to raise_error(MissingOption, 'front_height is required with slope, but missing.')
137
+ end
138
+
139
+ it 'raises InvalidOption for a slope the depth cannot take' do
140
+ expect { described_class.new(dimensions.merge(slope: 10, front_height: 10)) }.to raise_error(InvalidOption, /needs a depth of/)
141
+ end
142
+
143
+ it 'raises MissingOption naming the one dimension left out' do
144
+ expect { described_class.new(dimensions.except(:width)) }.to raise_error(MissingOption, 'width is required, but missing.')
145
+ end
146
+ end
147
+
148
+ describe '#to_configuration' do
149
+ subject(:config) { described_class.new(dimensions.merge(units: :mm, lid: :back, inside_box: true, format: :svg)).to_configuration }
150
+
151
+ it { is_expected.to be_a(Configuration) }
152
+
153
+ its(:width) { is_expected.to eql(40.0) }
154
+ its(:lid) { is_expected.to eq('back') }
155
+ its(:debug) { is_expected.to be(true) }
156
+ its(:notch) { is_expected.to eql(9.0) }
157
+ its(:kerf) { is_expected.to be_within(1e-6).of(0.06096) }
158
+ its(:format) { is_expected.to be_nil }
159
+ end
160
+ end
161
+ end
162
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
+ require 'tmpdir'
4
5
 
5
6
  module Laser
6
7
  module Cutter
@@ -8,32 +9,32 @@ module Laser
8
9
  RSpec.describe LayoutRenderer do
9
10
  describe '#render' do
10
11
  let(:renderer) { LayoutRenderer.new(config) }
11
- let(:file) { File.expand_path("../../../laser-cutter-pdf-test.#{$$}.pdf", __FILE__) }
12
+ let(:dir) { Dir.mktmpdir }
13
+ let(:file) { File.join(dir, 'box.pdf') }
14
+
15
+ after { FileUtils.remove_entry(dir) }
12
16
 
13
17
  def render_file(filename)
14
- real_file = ENV['RSPEC_SAVE_PDF'] ? true : false
15
18
  config.validate!
16
- expect(!File.exist?(filename)) if real_file
17
19
  renderer.render
18
- expect(File.exist?(filename)) if real_file
19
- expect(File.size(filename) > 0) if real_file
20
- rescue Exception => e
21
- warn e.backtrace.join("\n")
22
- fail e.message
23
- ensure
24
- if real_file
25
- begin
26
- File.delete(filename)
27
- rescue StandardError
28
- nil
29
- end
30
- expect(!File.exist?(filename))
31
- end
20
+ expect(File.binread(filename)).to start_with('%PDF')
32
21
  end
33
22
 
34
- before do
35
- unless ENV['RSPEC_SAVE_PDF']
36
- expect_any_instance_of(Prawn::Document).to receive(:render_file).once
23
+ context 'with a slope' do
24
+ let(:config) do
25
+ Laser::Cutter::Configuration.new('box' => '4x3x5/0.25/0.6', 'slope' => '45', 'front_height' => '1', 'file' => file)
26
+ end
27
+
28
+ it 'is able to generate a PDF file' do
29
+ render_file file
30
+ end
31
+
32
+ it 'draws the seven panels of a sloped box' do
33
+ expect(renderer.total).to eq(Laser::Cutter::Sloped::Box.new(config).notches.size)
34
+ end
35
+
36
+ it 'draws them twice with the outline without kerf' do
37
+ expect(LayoutRenderer.new(Laser::Cutter::Configuration.new(config.to_hash.merge(debug: true))).total).to eq(2 * renderer.total)
37
38
  end
38
39
  end
39
40
 
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Renderer
8
+ RSpec.describe MetaRenderer do
9
+ subject(:renderer) { described_class.new(Configuration.new(settings)) }
10
+
11
+ let(:settings) { { box: '4x3x5/0.125', file: 'box.pdf' } }
12
+
13
+ def pages(settings) = LayoutRenderer.new(Configuration.new(settings)).document.scan(%r{/Type /Page\b}).size
14
+
15
+ it 'prints the settings of the box, in order' do
16
+ expect(renderer.fields.keys).to eq(%i[units width height depth thickness notch lid kerf stroke padding margin page_size])
17
+ end
18
+
19
+ it 'calls a page fitted to the box custom, and leaves its layout out' do
20
+ expect(renderer.fields.slice(:page_size, :page_layout)).to eq(page_size: 'custom')
21
+ end
22
+
23
+ it 'is as tall as its banner and one line for each setting' do
24
+ expect(renderer.enclosure.h).to be_within(1e-9).of(55 + (12 * 8.2) + 4)
25
+ end
26
+
27
+ it 'fits on the page of the box' do
28
+ expect(pages(settings)).to eq(1)
29
+ end
30
+
31
+ # Zero is a setting like any other: no kerf, or a slope that starts at the bottom.
32
+ context 'with a setting of zero' do
33
+ let(:settings) { super().merge(kerf: 0, slope: 50, front_height: 0) }
34
+
35
+ it 'prints the kerf and the front height' do
36
+ expect(renderer.fields).to include(kerf: 0, front_height: 0, slope: 50)
37
+ end
38
+
39
+ it 'keeps the order of the settings' do
40
+ expect(renderer.fields.keys.first(6)).to eq(%i[units width height depth front_height slope])
41
+ end
42
+
43
+ it 'is as tall as the lines it prints' do
44
+ expect(renderer.enclosure.h).to be_within(1e-9).of(55 + (14 * 8.2) + 4)
45
+ end
46
+
47
+ it 'fits on the page of the box' do
48
+ expect(pages(settings)).to eq(1)
49
+ end
50
+ end
51
+
52
+ context 'with a page size' do
53
+ let(:settings) { super().merge(page_size: 'A3', page_layout: 'landscape') }
54
+
55
+ it 'prints the size and the layout' do
56
+ expect(renderer.fields).to include(page_size: 'A3', page_layout: 'landscape')
57
+ end
58
+
59
+ it 'fits on the page' do
60
+ expect(pages(settings)).to eq(1)
61
+ end
62
+ end
63
+
64
+ context 'with a sloped box' do
65
+ let(:settings) { super().merge(slope: 45, front_height: 1) }
66
+
67
+ it 'prints the front height and the slope after the depth' do
68
+ expect(renderer.fields.keys[3, 3]).to eq(%i[depth front_height slope])
69
+ end
70
+
71
+ it 'grows by two lines' do
72
+ expect(renderer.enclosure.h).to be_within(1e-9).of(55 + (14 * 8.2) + 4)
73
+ end
74
+
75
+ it 'fits on the page of the box' do
76
+ expect(pages(settings)).to eq(1)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -48,4 +48,38 @@ RSpec.describe Laser::Cutter::Renderer::SvgRenderer do
48
48
  expect(svg).not_to include('<desc>')
49
49
  end
50
50
  end
51
+
52
+ context 'with a slope' do
53
+ let(:settings) { { box: '4x3x5/0.25/0.6', slope: 45, front_height: 1, file: file } }
54
+
55
+ before { renderer.render }
56
+
57
+ it 'draws the seven panels of a sloped box' do
58
+ expect(svg.scan('<line').size).to eq(renderer.total).and eq(Laser::Cutter::Sloped::Box.new(config).notches.size)
59
+ end
60
+
61
+ it 'draws lines along the slope' do
62
+ skewed = svg.scan(/x1="([\d.-]+)" y1="([\d.-]+)" x2="([\d.-]+)" y2="([\d.-]+)"/).map { |found| found.map(&:to_f) }
63
+ .reject { |x1, y1, x2, y2| (x1 - x2).abs < 1e-4 || (y1 - y2).abs < 1e-4 }
64
+ expect(skewed.map { |x1, y1, x2, y2| ((y2 - y1) / (x2 - x1)).abs.round(3) }.uniq).to eq([1.0])
65
+ end
66
+
67
+ it 'keeps every line on the page' do
68
+ width, height = svg.match(/viewBox="0 0 ([\d.]+) ([\d.]+)"/).captures.map(&:to_f)
69
+ xs, ys = svg.scan(/[xy][12]="([\d.-]+)"/).flatten.map(&:to_f).each_slice(2).to_a.transpose
70
+ expect([xs.min, ys.min, width - xs.max, height - ys.max]).to all(be_within(0.002).of(0.125))
71
+ end
72
+
73
+ context 'when it starts at the bottom' do
74
+ let(:settings) { super().merge(slope: 50, front_height: 0, kerf: 0) }
75
+
76
+ it 'describes a front height of zero, and no kerf' do
77
+ expect(svg).to match(/<desc>.*front_height: 0, slope: 50,.*kerf: 0,/m)
78
+ end
79
+ end
80
+
81
+ it 'describes the slope' do
82
+ expect(svg).to match(/<desc>.*depth: 5\.0, front_height: 1, slope: 45, thickness/m)
83
+ end
84
+ end
51
85
  end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Sloped
8
+ # The panels, stood up in space the way they are glued. Two panels that
9
+ # both claim a point of material cannot be put together, and that is
10
+ # what catches a corner cube given to two panels, or a strip that runs
11
+ # into the tilted panel.
12
+ RSpec.describe 'the sloped box, put together' do
13
+ include_context 'with a sloped box'
14
+
15
+ let(:panels) { Panels.new(profile: profile, width: width, notcher: notcher, lid: lid) }
16
+ let(:solid) { Solid.new(profile: profile, width: width, outlines: panels.to_h) }
17
+
18
+ shared_examples 'panels that fit' do
19
+ it 'has no material that two panels both claim' do
20
+ expect(solid.collisions).to be_empty
21
+ end
22
+ end
23
+
24
+ it_behaves_like 'panels that fit'
25
+
26
+ it 'finds the panel a point of material belongs to' do
27
+ owners = [[-0.1, 2.5, 1.0], [2.0, 2.5, -0.1], [2.0, 5.1, 1.5], [2.0, 4.0, 3.1], [2.0, 1.0, 2.1], [2.0, -0.1, 0.5]]
28
+ expect(owners.map { |point| solid.owners_of(*point) }).to eq([['left'], ['bottom'], ['back'], ['top'], ['tilted'], ['front']])
29
+ end
30
+
31
+ # The back is taller than the front strip, so in its place it runs into the side walls.
32
+ it 'would catch a panel standing where another already is' do
33
+ doubled = Solid.new(profile: profile, width: width, outlines: panels.to_h.merge('front' => panels.to_h['back']))
34
+ expect(doubled.collisions.keys).to include('front+left', 'front+right')
35
+ end
36
+
37
+ context 'when the bottom and the top strip carry the corners' do
38
+ let(:notch) { 0.7 }
39
+
40
+ it_behaves_like 'panels that fit'
41
+ end
42
+
43
+ context 'with a tilted panel that lifts off' do
44
+ let(:lid) { :plain }
45
+
46
+ it_behaves_like 'panels that fit'
47
+ end
48
+
49
+ context 'at 60 degrees from a front half as high as the back' do
50
+ let(:slope) { 60 }
51
+ let(:front_height) { 1.5 }
52
+
53
+ it_behaves_like 'panels that fit'
54
+ end
55
+
56
+ context 'without a front strip' do
57
+ let(:front_height) { 0 }
58
+ let(:slope) { 50 }
59
+
60
+ it_behaves_like 'panels that fit'
61
+ end
62
+
63
+ context 'without a top strip' do
64
+ let(:slope) { Math.atan(2.0 / 5.0) * 180 / Math::PI }
65
+
66
+ it_behaves_like 'panels that fit'
67
+ end
68
+
69
+ context 'with neither strip' do
70
+ let(:front_height) { 0 }
71
+ let(:slope) { Math.atan(3.0 / 5.0) * 180 / Math::PI }
72
+
73
+ it_behaves_like 'panels that fit'
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Sloped
8
+ RSpec.describe Box do
9
+ subject(:box) { described_class.new(Configuration.new(settings)) }
10
+
11
+ let(:padding) { 0.2 }
12
+ let(:kerf) { 0.0 }
13
+ let(:overrides) { {} }
14
+ let(:settings) do
15
+ { width: 4, height: 3, depth: 5, front_height: 1, slope: 45, thickness: 0.25, notch: 0.6,
16
+ kerf: kerf, padding: padding, units: 'in', file: 'box.pdf' }.merge(overrides)
17
+ end
18
+
19
+ # @return [Array(Float, Float, Float, Float)] the least and most x, then y, of a panel
20
+ def bounds(name)
21
+ points = box.outlines.fetch(name).flat_map { |line| [line.p1, line.p2] }
22
+ [*points.map(&:x).minmax, *points.map(&:y).minmax].map { |c| c.round(6) }
23
+ end
24
+
25
+ it 'answers what the renderers ask of a box' do
26
+ expect(box).to respond_to(:generate_notches, :notches, :outlines, :enclosure, :metadata=)
27
+ end
28
+
29
+ it 'draws seven panels' do
30
+ expect(box.outlines.keys).to eq(%w[left right bottom back top tilted front])
31
+ end
32
+
33
+ it 'returns every line of every panel' do
34
+ expect(box.generate_notches.size).to eq(box.outlines.values.sum(&:size)).and eq(box.notches.size)
35
+ end
36
+
37
+ it 'closes every outline' do
38
+ expect(box.outlines.values.map { |lines| Outline.closed?(lines) }).to all(be(true))
39
+ end
40
+
41
+ # Across: a side wall 5.5, the column 4.5, a side wall 5.5, and two paddings.
42
+ # Up: the bottom 5.5, the back 3.5, the strips and the tilted panel, and four paddings.
43
+ it 'encloses every line, from the corner of the sheet' do
44
+ expect(box.enclosure.to_a.flatten.map { |c| c.round(6) }).to eq([0.0, 0.0, 15.9, 17.335534])
45
+ end
46
+
47
+ describe 'the sheet' do
48
+ it 'unfolds the panels around the back, a padding apart' do
49
+ gaps = [bounds('back')[2] - bounds('bottom')[3], bounds('top')[2] - bounds('back')[3],
50
+ bounds('tilted')[2] - bounds('top')[3], bounds('front')[2] - bounds('tilted')[3],
51
+ bounds('back')[0] - bounds('left')[1], bounds('right')[0] - bounds('back')[1]]
52
+ expect(gaps.map { |gap| gap.round(6) }).to all(eq(padding))
53
+ end
54
+
55
+ it 'lines the column up between the side walls' do
56
+ expect(%w[bottom back top tilted front].map { |name| bounds(name).first(2) }.uniq).to eq([[5.7, 10.2]])
57
+ end
58
+
59
+ it 'draws the right wall as the mirror image of the left' do
60
+ axis = (bounds('left')[0] + bounds('right')[1]) / 2
61
+ mirrored = box.outlines['left'].map do |line|
62
+ Geometry::Line.new(Geometry::Point[(2 * axis) - line.p1.x, line.p1.y], Geometry::Point[(2 * axis) - line.p2.x, line.p2.y])
63
+ end
64
+ expect(Loops.rounded(mirrored)).to eq(Loops.rounded(box.outlines['right']))
65
+ end
66
+
67
+ it 'turns the front strip so its plain edge faces the tilted panel' do
68
+ lowest = box.outlines['front'].select { |line| [line.p1.y, line.p2.y].all? { |y| (y - bounds('front')[2]).abs < 1e-6 } }
69
+ expect(lowest.map(&:length).map { |length| length.round(6) }).to eq([4.5])
70
+ end
71
+ end
72
+
73
+ describe 'room for the metadata' do
74
+ before { box.metadata = Geometry::Point[9, 8] }
75
+
76
+ it 'moves the bottom to the right of the block and the left wall above it' do
77
+ expect([bounds('bottom')[0], bounds('left')[2]]).to eq([9.45, 8.45])
78
+ end
79
+
80
+ it 'draws afresh once the block is known' do
81
+ before = box.enclosure
82
+ box.metadata = Geometry::Point[20, 20]
83
+ expect(box.enclosure.p2.x).to be > before.p2.x
84
+ end
85
+ end
86
+
87
+ describe 'kerf' do
88
+ let(:kerf) { 0.02 }
89
+ let(:plain) { described_class.new(Configuration.new(settings.merge(kerf: 0.0))) }
90
+
91
+ it 'grows the sheet by half the kerf on every side' do
92
+ expect(box.enclosure.to_a.flatten.zip(plain.enclosure.to_a.flatten).map { |cut, drawn| (cut - drawn).round(6) })
93
+ .to eq([-0.01, -0.01, 0.01, 0.01])
94
+ end
95
+
96
+ it 'keeps every line, each one moved out and none turned' do
97
+ turned = box.outlines['left'].zip(plain.outlines['left']).reject do |cut, drawn|
98
+ ((cut.p2.x - cut.p1.x) * (drawn.p2.y - drawn.p1.y)) - ((cut.p2.y - cut.p1.y) * (drawn.p2.x - drawn.p1.x)) < 1e-9
99
+ end
100
+ expect([box.outlines['left'].size, turned]).to eq([plain.outlines['left'].size, []])
101
+ end
102
+
103
+ it 'moves the slope of a side wall out by half the kerf, square to the slope' do
104
+ longest = ->(lines) { lines.reject { |l| (l.p1.x - l.p2.x).abs < 1e-6 || (l.p1.y - l.p2.y).abs < 1e-6 }.max_by(&:length) }
105
+ cut = longest.call(box.outlines['left'])
106
+ drawn = longest.call(plain.outlines['left'])
107
+ offset = ((cut.p1.x - drawn.p1.x) * -Math.sin(Math::PI / 4)) + ((cut.p1.y - drawn.p1.y) * Math.cos(Math::PI / 4))
108
+ expect(offset.abs).to be_within(1e-9).of(0.01)
109
+ end
110
+ end
111
+
112
+ describe 'the lid' do
113
+ let(:overrides) { { lid: 'plain' } }
114
+
115
+ it 'draws the tilted panel as a rectangle that lies on the side walls' do
116
+ expect([box.outlines['tilted'].size, bounds('tilted').first(2)]).to eq([4, [5.7, 10.2]])
117
+ end
118
+ end
119
+
120
+ describe 'the defaults' do
121
+ subject(:box) { described_class.new({ width: 4, height: 3, depth: 5, front_height: 1, slope: 45, thickness: 0.25 }) }
122
+
123
+ it 'draws without kerf, padding, notch or lid, from a plain Hash' do
124
+ expect(box.outlines.keys.size).to eq(7)
125
+ end
126
+
127
+ it 'takes a notch three times the thickness' do
128
+ expect(box.outlines['tilted'].size).to eq(described_class.new({ width: 4, height: 3, depth: 5, front_height: 1, slope: 45,
129
+ thickness: 0.25, notch: 0.75 }).outlines['tilted'].size)
130
+ end
131
+ end
132
+
133
+ context 'without strips' do
134
+ let(:overrides) { { front_height: 0, slope: Math.atan(3.0 / 5.0) * 180 / Math::PI } }
135
+
136
+ it 'draws five panels' do
137
+ expect(box.outlines.keys).to eq(%w[left right bottom back tilted])
138
+ end
139
+ end
140
+
141
+ it 'refuses numbers that do not make the shape' do
142
+ expect { described_class.new(Configuration.new(settings.merge(slope: 10))) }.to raise_error(InvalidOption, /needs a depth/)
143
+ end
144
+
145
+ it 'exposes the profile it draws' do
146
+ expect(box.profile.top_depth).to be_within(1e-9).of(3.0)
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Sloped
8
+ RSpec.describe Corners do
9
+ include_context 'with a sloped box'
10
+
11
+ subject(:corners) { described_class.new(profile: profile, width: width, notcher: notcher) }
12
+
13
+ # A notch of 0.6 gives the width 9 notches, which begin with a tab.
14
+ context 'when the back and the front strip begin with a tab along the width' do
15
+ its(:front_bottom) { is_expected.to eq(:front) }
16
+ its(:bottom_back) { is_expected.to eq(:back) }
17
+ its(:back_top) { is_expected.to eq(:back) }
18
+ end
19
+
20
+ # A notch of 0.7 gives the width and the height 7 notches and the
21
+ # front 3, and a tab in the middle of those begins with a hole.
22
+ context 'when they begin with a hole on both edges at the corner' do
23
+ let(:notch) { 0.7 }
24
+
25
+ its(:front_bottom) { is_expected.to eq(:bottom) }
26
+ its(:bottom_back) { is_expected.to eq(:bottom) }
27
+ its(:back_top) { is_expected.to eq(:top) }
28
+ end
29
+
30
+ context 'without a front strip' do
31
+ let(:front_height) { 0 }
32
+
33
+ its(:front_bottom) { is_expected.to be_nil }
34
+ end
35
+
36
+ context 'without a top strip' do
37
+ let(:slope) { Math.atan(2.0 / 5.0) * 180 / Math::PI }
38
+
39
+ its(:back_top) { is_expected.to be_nil }
40
+ its(:bottom_back) { is_expected.to eq(:back) }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end