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,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Sloped
8
+ RSpec.describe Profile do
9
+ let(:depth) { 6.0 }
10
+ let(:height) { 4.0 }
11
+ let(:front_height) { 1.0 }
12
+ let(:slope) { 45 }
13
+ let(:thickness) { 0.25 }
14
+
15
+ subject(:profile) do
16
+ described_class.new(depth: depth, height: height, front_height: front_height, slope: slope, thickness: thickness)
17
+ end
18
+
19
+ def xy(point) = point.to_a.map { |c| c.round(9) }
20
+
21
+ # The outside face of the tilted panel lies one thickness up and towards
22
+ # the front from its inside face.
23
+ def outside(point)
24
+ a = profile.angle
25
+ point.plus(-thickness * Math.sin(a), thickness * Math.cos(a))
26
+ end
27
+
28
+ context 'with both strips' do
29
+ its(:top_depth) { is_expected.to be_within(1e-9).of(3.0) }
30
+ its(:slope_length) { is_expected.to be_within(1e-9).of(3 * Math.sqrt(2)) }
31
+
32
+ it 'outlines five corners, counter-clockwise from the front of the bottom' do
33
+ expect(profile.vertices.map { |v| xy(v) }).to eq([[0, 0], [6, 0], [6, 4], [3, 4], [0, 1]])
34
+ end
35
+
36
+ it 'meets the top strip with no gap and no overlap' do
37
+ strip_front_corner = [depth - profile.top_strip_length, height]
38
+ expect(xy(outside(profile.tilted_span.p2))).to eq(strip_front_corner.map { |c| c.round(9) })
39
+ end
40
+
41
+ it 'meets the front strip with no gap and no overlap' do
42
+ strip_top_corner = [0.0, profile.front_strip_length]
43
+ expect(xy(outside(profile.tilted_span.p1))).to eq(strip_top_corner.map { |c| c.round(9) })
44
+ end
45
+
46
+ it 'makes the tilted panel as long as the span it covers' do
47
+ expect(profile.tilted_length).to be_within(1e-9).of(profile.tilted_span.length)
48
+ end
49
+
50
+ it 'draws the outside face a thickness out from the span, and parallel to it' do
51
+ expect([profile.tilted_outside.p1, profile.tilted_outside.p2].map { |point| xy(point) })
52
+ .to eq([profile.tilted_span.p1, profile.tilted_span.p2].map { |point| xy(outside(point)) })
53
+ end
54
+
55
+ it 'keeps the span on the slope' do
56
+ low, high = [profile.tilted_span.p1, profile.tilted_span.p2]
57
+ expect((high.y - low.y) / (high.x - low.x)).to be_within(1e-9).of(1.0)
58
+ end
59
+ end
60
+
61
+ context 'when the slope reaches the back' do
62
+ let(:front_height) { 4.0 - (6.0 * Math.tan(Math::PI / 6)) }
63
+ let(:slope) { 30 }
64
+
65
+ it { is_expected.not_to be_top_strip }
66
+ its(:top_strip_length) { is_expected.to eq(0.0) }
67
+
68
+ it 'outlines four corners' do
69
+ expect(profile.vertices.size).to eq(4)
70
+ end
71
+
72
+ it 'runs the tilted panel to the back corner' do
73
+ expect(xy(profile.tilted_span.p2)).to eq([6.0, 4.0])
74
+ end
75
+ end
76
+
77
+ context 'when the slope starts at the bottom' do
78
+ let(:front_height) { 0 }
79
+
80
+ it { is_expected.not_to be_front_strip }
81
+ its(:front_strip_length) { is_expected.to eq(0.0) }
82
+
83
+ it 'outlines four corners' do
84
+ expect(profile.vertices.map { |v| xy(v) }).to eq([[0, 0], [6, 0], [6, 4], [4, 4]])
85
+ end
86
+
87
+ it 'runs the tilted panel from the front corner' do
88
+ expect(xy(profile.tilted_span.p1)).to eq([0.0, 0.0])
89
+ end
90
+ end
91
+
92
+ context 'when the slope runs from the bottom to the back' do
93
+ let(:front_height) { 0 }
94
+ let(:slope) { Math.atan(4.0 / 6.0) * 180 / Math::PI }
95
+
96
+ it { is_expected.not_to be_top_strip }
97
+ it { is_expected.not_to be_front_strip }
98
+
99
+ it 'outlines a wedge of three corners' do
100
+ expect(profile.vertices.map { |v| xy(v) }).to eq([[0, 0], [6, 0], [6, 4]])
101
+ end
102
+
103
+ it 'runs the tilted panel the whole slope' do
104
+ expect(profile.tilted_length).to be_within(1e-9).of(Math.hypot(6, 4))
105
+ end
106
+ end
107
+
108
+ it 'is frozen' do
109
+ expect(profile).to be_frozen
110
+ end
111
+
112
+ describe 'refusing what it cannot draw' do
113
+ def build(**overrides)
114
+ described_class.new(depth: depth, height: height, front_height: front_height,
115
+ slope: slope, thickness: thickness, **overrides)
116
+ end
117
+
118
+ [0, 90, -5, 120].each do |angle|
119
+ it "refuses a #{angle}° slope" do
120
+ expect { build(slope: angle) }.to raise_error(InvalidOption, /slope must be between/)
121
+ end
122
+ end
123
+
124
+ it 'refuses a slope too shallow for the depth' do
125
+ expect { build(slope: 20) }.to raise_error(InvalidOption, /needs a depth/)
126
+ end
127
+
128
+ it 'refuses a slope too steep to leave a tilted panel' do
129
+ expect { build(slope: 89) }.to raise_error(InvalidOption, /89\.0° slope.*leaves no tilted panel.*lower the front height/)
130
+ end
131
+
132
+ it 'refuses a slope too shallow to leave a tilted panel' do
133
+ expect { build(front_height: 3.9, slope: 5) }
134
+ .to raise_error(InvalidOption, /5\.0° slope.*leaves no tilted panel.*lower the front height/)
135
+ end
136
+
137
+ it 'refuses a front too close to the back to leave a tilted panel' do
138
+ expect { build(front_height: 3.9) }.to raise_error(InvalidOption, /front height of 3\.9 leaves no tilted panel/)
139
+ end
140
+
141
+ it 'refuses a front as tall as the back' do
142
+ expect { build(front_height: 4) }.to raise_error(InvalidOption, /must be below/)
143
+ end
144
+
145
+ it 'refuses a negative front height' do
146
+ expect { build(front_height: -1) }.to raise_error(InvalidOption, /at least zero/)
147
+ end
148
+
149
+ it 'refuses a zero thickness' do
150
+ expect { build(thickness: 0) }.to raise_error(InvalidOption, /thickness must be above zero/)
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Geometry
8
+ RSpec.describe Transform do
9
+ def point(x, y) = Point[x, y]
10
+
11
+ context 'with no turn and no move' do
12
+ subject(:transform) { described_class.new(point(0, 0), 0) }
13
+
14
+ it 'leaves a point where it is' do
15
+ expect(transform.apply(point(3, 4))).to eql(point(3, 4))
16
+ end
17
+ end
18
+
19
+ context 'along an upward run from {1,1}' do
20
+ subject(:transform) { described_class.along(point(1, 1), point(1, 5)) }
21
+
22
+ its(:angle) { is_expected.to be_within(1e-9).of(Math::PI / 2) }
23
+
24
+ it 'maps the local x axis onto the run' do
25
+ expect(transform.apply(point(4, 0))).to eql(point(1, 5))
26
+ end
27
+
28
+ it 'maps the local y axis to the left of the run' do
29
+ expect(transform.apply(point(0, 1))).to eql(point(0, 1))
30
+ end
31
+
32
+ it 'maps both ends of a line' do
33
+ line = transform.apply(Line.new(point(0, 0), point(2, 1)))
34
+ expect([line.p1, line.p2]).to eql([point(1, 1), point(0, 3)])
35
+ end
36
+ end
37
+
38
+ it 'keeps lengths' do
39
+ transform = described_class.along(point(2, 3), point(5, 7))
40
+ expect(transform.apply(Line.new(point(0, 0), point(5, 0))).length).to be_within(1e-9).of(5)
41
+ end
42
+
43
+ it 'refuses what it cannot move' do
44
+ expect { described_class.new(point(0, 0), 0).apply(:rect) }.to raise_error(ArgumentError)
45
+ end
46
+
47
+ it 'is frozen' do
48
+ expect(described_class.new(point(0, 0), 1)).to be_frozen
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'tmpdir'
5
+
6
+ # What a Ruby program, such as the MakeABox.io website, calls instead of the command line.
7
+ RSpec.describe Laser::Cutter do
8
+ let(:settings) { { width: 40, height: 30, depth: 20, thickness: 3, units: 'mm' } }
9
+ let(:options) { Laser::Cutter::Options.new(settings) }
10
+ let(:dir) { Dir.mktmpdir }
11
+
12
+ after { FileUtils.remove_entry(dir) }
13
+
14
+ describe '.render' do
15
+ subject(:document) { described_class.render(options) }
16
+
17
+ it { is_expected.to start_with('%PDF') }
18
+
19
+ it 'writes no file' do
20
+ expect { Dir.chdir(dir) { document } }.not_to(change { Dir.children(dir) })
21
+ end
22
+
23
+ it 'draws an SVG when the options ask for one' do
24
+ expect(described_class.render(options.new(format: :svg))).to include('<svg', 'width="109.46048mm"')
25
+ end
26
+
27
+ it 'takes a Hash, the way a form sends it' do
28
+ expect(described_class.render(settings.transform_keys(&:to_s).transform_values(&:to_s))).to start_with('%PDF')
29
+ end
30
+
31
+ it 'yields each line it draws' do
32
+ lines = []
33
+ described_class.render(options.new(format: 'svg')) { |line| lines << line }
34
+ expect(lines.size).to eq(280)
35
+ end
36
+
37
+ { nil => 280, '' => 280, 'full' => 280, :back => 208, 'back' => 208, :plain => 184, 'plain' => 184 }.each do |lid, lines|
38
+ it "draws #{lines} lines for the lid #{lid.inspect}" do
39
+ svg = described_class.render(settings.merge(lid: lid, format: :svg))
40
+ expect(svg.scan('<line').size).to eq(lines)
41
+ end
42
+ end
43
+
44
+ it 'names the lid in the description of an SVG' do
45
+ expect(described_class.render(settings.merge(lid: :plain, format: :svg))).to match(/<desc>.*lid: plain/m)
46
+ end
47
+
48
+ it 'leaves the description out of an SVG without metadata' do
49
+ expect(described_class.render(settings.merge(format: :svg, metadata: false))).not_to include('<desc>')
50
+ end
51
+
52
+ it 'draws the box without kerf as well in a PDF, when asked' do
53
+ lines = 0
54
+ described_class.render(options.new(inside_box: true)) { lines += 1 }
55
+ expect(lines).to eq(560)
56
+ end
57
+
58
+ it 'raises InvalidOption for a lid it does not know' do
59
+ expect { described_class.render(settings.merge(lid: 'sliding')) }.to raise_error(Laser::Cutter::InvalidOption, /lid/)
60
+ end
61
+
62
+ it 'raises MissingOption for a dimension left out' do
63
+ expect { described_class.render(settings.except(:depth)) }.to raise_error(Laser::Cutter::MissingOption, /depth is required/)
64
+ end
65
+ end
66
+
67
+ describe '.write' do
68
+ let(:path) { File.join(dir, 'box.svg') }
69
+
70
+ it 'takes the format from the extension, and returns the path' do
71
+ expect(described_class.write(options.new(file: path, lid: :back))).to eq(path)
72
+ expect(File.read(path).scan('<line').size).to eq(208)
73
+ end
74
+
75
+ it 'writes the format it is given, whatever the extension' do
76
+ described_class.write(settings.merge(file: path, format: :pdf))
77
+ expect(File.binread(path)).to start_with('%PDF')
78
+ end
79
+
80
+ it 'writes a PDF when the extension says nothing' do
81
+ described_class.write(settings.merge(file: File.join(dir, 'box')))
82
+ expect(File.binread(File.join(dir, 'box'))).to start_with('%PDF')
83
+ end
84
+
85
+ it 'raises MissingOption without a file' do
86
+ expect { described_class.write(options) }.to raise_error(Laser::Cutter::MissingOption, /file is required/)
87
+ end
88
+ end
89
+
90
+ # app/helpers/home_helper.rb and app/controllers/home_controller.rb, written against 1.0.3.
91
+ describe 'the calls MakeABox.io makes today' do
92
+ let(:form) { { 'width' => '40', 'height' => '30', 'depth' => '20', 'thickness' => '3', 'units' => 'mm', 'lid' => '' } }
93
+ let(:config) { Laser::Cutter::Configuration.new(form.merge(metadata: true)) }
94
+ let(:path) { File.join(dir, 'box.pdf') }
95
+
96
+ before { config['file'] = path }
97
+
98
+ it 'still draw a box' do
99
+ config.validate!
100
+ Laser::Cutter::Renderer::LayoutRenderer.new(config).render
101
+ expect(File.binread(path)).to start_with('%PDF')
102
+ end
103
+
104
+ it 'still list the page sizes' do
105
+ expect(Laser::Cutter::PageManager.new(config.units).page_size_values)
106
+ .to include(['A4', be_within(0.1).of(210), be_within(0.1).of(297)])
107
+ end
108
+
109
+ it 'still fail on a missing dimension with an error the website rescues' do
110
+ config.delete(:width)
111
+ expect { config.validate! }.to raise_error(Laser::Cutter::MissingOption)
112
+ end
113
+ end
114
+ end
data/spec/spec_helper.rb CHANGED
@@ -25,6 +25,10 @@ SimpleCov.at_exit do
25
25
  end
26
26
 
27
27
  require "rspec/its"
28
+
29
+ # Help and boxes size themselves to the terminal, and help fixes its width when
30
+ # the gem loads. Pin it, or the CLI specs pass in CI and fail in a wide terminal.
31
+ ENV["COLUMNS"] = "80"
28
32
  require "laser/cutter"
29
33
 
30
34
  Dir[File.expand_path("support/**/*.rb", __dir__)].each { |file| require file }
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Reads an outline as a walk around a panel, without using any of the code
4
+ # that drew it: where its tabs are along a joint, whether it crosses itself,
5
+ # and the loop a pile of lines makes.
6
+ module Loops
7
+ module_function
8
+
9
+ # How far apart two numbers may be and still be the same.
10
+ CLOSE = 1e-6
11
+
12
+ # The stretches of a joint where a panel has a tab: where its outline runs
13
+ # one thickness out, to the right of the way the joint is walked.
14
+ #
15
+ # @param lines [Array<Laser::Cutter::Geometry::Line>] the outline of the panel
16
+ # @param from [Laser::Cutter::Geometry::Point] where the joint begins, on the inside outline
17
+ # @param to [Laser::Cutter::Geometry::Point] where it ends
18
+ # @param thickness [Float] of the material
19
+ # @return [Array<Array(Float, Float)>] each tab as the distances from +from+ it starts and ends at, in order
20
+ def tabs(lines, from:, to:, thickness:)
21
+ length = Math.hypot(to.x - from.x, to.y - from.y)
22
+ along = [(to.x - from.x) / length, (to.y - from.y) / length]
23
+ place = lambda do |point|
24
+ delta = [point.x - from.x, point.y - from.y]
25
+ [(delta[0] * along[0]) + (delta[1] * along[1]), (delta[0] * along[1]) - (delta[1] * along[0])]
26
+ end
27
+ runs = lines.map { |line| [place.call(line.p1), place.call(line.p2)] }
28
+ .select { |ends| ends.all? { |_distance, out| (out - thickness).abs < CLOSE } }
29
+ .map { |ends| ends.map(&:first).sort.map { |distance| distance.clamp(0.0, length) } }
30
+ runs.reject { |start, finish| finish - start < CLOSE }.sort
31
+ end
32
+
33
+ # Whether the tabs of two panels fill a joint between them, each stretch
34
+ # of it a tab of one panel or the other and never of both. The two panels
35
+ # walk the joint in opposite directions.
36
+ #
37
+ # @param ours [Array<Array(Float, Float)>] the tabs of one panel, see {tabs}
38
+ # @param theirs [Array<Array(Float, Float)>] the tabs of the other
39
+ # @param length [Float] of the joint
40
+ # @return [Boolean]
41
+ def interlock?(ours, theirs, length)
42
+ turned = theirs.map { |start, finish| [length - finish, length - start] }
43
+ edges = (ours + turned).sort.flatten
44
+ return false unless ours.any? && theirs.any?
45
+
46
+ edges.first.abs < CLOSE && (edges.last - length).abs < CLOSE &&
47
+ edges[1..-2].each_slice(2).all? { |finish, start| (finish - start).abs < CLOSE }
48
+ end
49
+
50
+ # Whether no two sides of an outline cross, or touch anywhere but at the
51
+ # corner two neighbours share.
52
+ #
53
+ # @param lines [Array<Laser::Cutter::Geometry::Line>] the sides, end to end
54
+ # @return [Boolean]
55
+ def simple?(lines)
56
+ lines.each_with_index.none? do |line, index|
57
+ ((index + 2)...lines.size).any? do |other|
58
+ next false if index.zero? && other == lines.size - 1
59
+
60
+ touch?(line, lines[other])
61
+ end
62
+ end
63
+ end
64
+
65
+ def touch?(first, second)
66
+ turns = [[first, second.p1], [first, second.p2], [second, first.p1], [second, first.p2]].map do |line, point|
67
+ ((line.p2.x - line.p1.x) * (point.y - line.p1.y)) - ((line.p2.y - line.p1.y) * (point.x - line.p1.x))
68
+ end
69
+ return false if turns.all? { |turn| turn.abs < CLOSE } && !overlap?(first, second)
70
+
71
+ (turns[0] * turns[1]) <= CLOSE && (turns[2] * turns[3]) <= CLOSE
72
+ end
73
+
74
+ # Whether two lines along the same straight line share a stretch of it.
75
+ def overlap?(first, second)
76
+ axis = (first.p2.x - first.p1.x).abs > (first.p2.y - first.p1.y).abs ? 0 : 1
77
+ a = [first.p1[axis], first.p2[axis]].sort
78
+ b = [second.p1[axis], second.p2[axis]].sort
79
+ a[0] <= b[1] + CLOSE && b[0] <= a[1] + CLOSE
80
+ end
81
+
82
+ # The loop that lines make when each ends where another begins.
83
+ #
84
+ # @param lines [Array<Laser::Cutter::Geometry::Line>] in any order
85
+ # @return [Laser::Cutter::Geometry::Loop]
86
+ def chain(lines)
87
+ key = ->(point) { point.to_a.map { |c| c.round(6) + 0.0 } }
88
+ meeting = lines.flat_map { |line| [[key.call(line.p1), line], [key.call(line.p2), line]] }
89
+ .group_by(&:first).transform_values { |pairs| pairs.map(&:last) }
90
+ line = lines.first
91
+ at = line.p2
92
+ points = lines.drop(1).each_with_object([line.p1]) do |_, walked|
93
+ walked << at
94
+ line = meeting.fetch(key.call(at)).find { |candidate| !candidate.equal?(line) }
95
+ at = key.call(line.p1) == key.call(at) ? line.p2 : line.p1
96
+ end
97
+ Laser::Cutter::Geometry::Loop.new(points)
98
+ end
99
+
100
+ # Lines as sorted, rounded pairs of points, so two outlines compare whatever their order.
101
+ #
102
+ # @return [Array<Array<Array(Float, Float)>>]
103
+ def rounded(lines)
104
+ lines.map { |line| [line.p1, line.p2].map { |point| point.to_a.map { |c| c.round(5) + 0.0 } }.sort }.sort
105
+ end
106
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Reads the lines of one panel as the region they enclose, without using any
4
+ # of the code that drew them.
5
+ module Outline
6
+ module_function
7
+
8
+ # Whether a point is inside the outline, by the even-odd rule: a ray from
9
+ # the point crosses the outline an odd number of times.
10
+ #
11
+ # @param lines [Array<Laser::Cutter::Geometry::Line>] an outline of horizontal and vertical lines
12
+ # @return [Boolean]
13
+ def inside?(lines, x, y)
14
+ lines.count do |line|
15
+ next false unless line.p1.x == line.p2.x && line.p1.x > x
16
+
17
+ low, high = [line.p1.y, line.p2.y].sort
18
+ low < y && y < high
19
+ end.odd?
20
+ end
21
+
22
+ # Whether the outline grown by a margin on every side covers the point.
23
+ #
24
+ # @return [Boolean]
25
+ def within?(lines, x, y, margin)
26
+ offsets = [-margin, 0, margin]
27
+ offsets.product(offsets).any? { |dx, dy| inside?(lines, x + dx, y + dy) }
28
+ end
29
+
30
+ # Whether every line ends where another begins, so the outline has no gap.
31
+ #
32
+ # @return [Boolean]
33
+ def closed?(lines)
34
+ ends = lines.flat_map { |line| [line.p1, line.p2] }.map { |point| point.to_a.map { |c| c.round(6) + 0.0 } }
35
+ ends.tally.values.all?(&:even?)
36
+ end
37
+
38
+ # Points spread over a rectangle, off any round coordinate a line could sit on.
39
+ #
40
+ # @param rect [Laser::Cutter::Geometry::Rect]
41
+ # @param margin [Float] how far past the rectangle to reach
42
+ # @param steps [Integer] points per axis
43
+ # @return [Array<Array(Float, Float)>]
44
+ def samples(rect, margin:, steps: 61)
45
+ xs = spread(rect.p1.x - margin, rect.p2.x + margin, steps)
46
+ ys = spread(rect.p1.y - margin, rect.p2.y + margin, steps)
47
+ xs.product(ys)
48
+ end
49
+
50
+ def spread(from, to, steps)
51
+ Array.new(steps) { |i| from + ((to - from) * (i + 0.4142) / steps) }
52
+ end
53
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The sloped box most of the specs draw: 4 wide, 5 deep, 3 high at the back
4
+ # and 1 at the front, rising at 45 degrees, in quarter-inch material. Its
5
+ # slope runs 2 back, so the top strip is 3 deep.
6
+ RSpec.shared_context 'with a sloped box' do
7
+ let(:width) { 4.0 }
8
+ let(:depth) { 5.0 }
9
+ let(:height) { 3.0 }
10
+ let(:front_height) { 1.0 }
11
+ let(:slope) { 45 }
12
+ let(:thickness) { 0.25 }
13
+ let(:notch) { 0.6 }
14
+ let(:lid) { :full }
15
+
16
+ let(:profile) do
17
+ Laser::Cutter::Sloped::Profile.new(depth: depth, height: height, front_height: front_height, slope: slope,
18
+ thickness: thickness)
19
+ end
20
+ let(:notcher) { Laser::Cutter::Sloped::Notcher.new(notch_width: notch, thickness: thickness) }
21
+
22
+ def point(x, y) = Laser::Cutter::Geometry::Point[x, y]
23
+
24
+ # @return [Array<Array(Float, Float)>] the corners of an outline, rounded
25
+ def corners_of(outline) = outline.points.map { |corner| corner.to_a.map { |c| c.round(6) + 0.0 } }
26
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Stands the panels of a sloped box up in space, the way they are glued, and
4
+ # samples the material: a point that two panels both claim means they cannot
5
+ # be put together. It knows only where each panel goes, not how it was drawn.
6
+ #
7
+ # Space is x across the width, y back from the front, and z up, all from the
8
+ # inside corner at the front of the bottom on the left.
9
+ class Solid
10
+ # Each panel's own frame: where a point in space falls on its outline, and
11
+ # how deep it is in the material, counted from the inside face.
12
+ FRAMES = {
13
+ 'left' => ->(x, y, z, _box) { [y, z, -x] },
14
+ 'right' => ->(x, y, z, box) { [y, z, x - box.width] },
15
+ 'bottom' => ->(x, y, z, _box) { [x, y, -z] },
16
+ 'back' => ->(x, y, z, box) { [x, z, y - box.profile.depth] },
17
+ 'top' => ->(x, y, z, box) { [x, box.profile.depth - y, z - box.profile.height] },
18
+ 'front' => ->(x, y, z, _box) { [x, z, -y] },
19
+ 'tilted' => ->(x, y, z, box) { box.on_slope(x, y, z) }
20
+ }.freeze
21
+
22
+ # Sampling starts off the grid by a different amount on each axis, so no
23
+ # sample lands on a face two panels share.
24
+ OFFSETS = [0.31, 0.47, 0.59].freeze
25
+
26
+ attr_reader :profile, :width
27
+
28
+ # @param profile [Laser::Cutter::Sloped::Profile]
29
+ # @param width [Float] the inside width of the box
30
+ # @param outlines [Hash{String => Laser::Cutter::Geometry::Loop}] each panel in its own frame, by name
31
+ def initialize(profile:, width:, outlines:)
32
+ @profile = profile
33
+ @width = width
34
+ @polygons = outlines.transform_values { |outline| outline.points.map(&:to_a) }
35
+ @low = profile.tilted_span.p1.to_a
36
+ @cos = Math.cos(profile.angle)
37
+ @sin = Math.sin(profile.angle)
38
+ end
39
+
40
+ # @param step [Float] the distance between samples
41
+ # @return [Hash{String => Integer}] how many samples each pair of panels both claim, by "name+name"
42
+ def collisions(step: profile.thickness / 2.0)
43
+ xs, ys, zs = ranges(step)
44
+ found = Hash.new(0)
45
+ xs.each do |x|
46
+ ys.each do |y|
47
+ zs.each do |z|
48
+ next if hollow?(x, y, z)
49
+
50
+ owners = owners_of(x, y, z)
51
+ found[owners.sort.join('+')] += 1 if owners.size > 1
52
+ end
53
+ end
54
+ end
55
+ found
56
+ end
57
+
58
+ # @return [Array<String>] the panels whose material holds a point
59
+ def owners_of(x, y, z)
60
+ @polygons.select do |name, polygon|
61
+ u, v, depth = FRAMES.fetch(name).call(x, y, z, self)
62
+ depth.positive? && depth < profile.thickness && inside?(polygon, u, v)
63
+ end.keys
64
+ end
65
+
66
+ # A point in the frame of the tilted panel: along the slope from its low end, and out from its inside face.
67
+ def on_slope(x, y, z)
68
+ along = y - @low[0]
69
+ up = z - @low[1]
70
+ [x, (along * @cos) + (up * @sin), (up * @cos) - (along * @sin)]
71
+ end
72
+
73
+ private
74
+
75
+ # Whether a point is in the space the box encloses, clear of every panel.
76
+ # Most samples are, and need no closer look.
77
+ def hollow?(x, y, z)
78
+ x.between?(0, width) && y.between?(0, profile.depth) && z.between?(0, profile.height) && on_slope(x, y, z).last.negative?
79
+ end
80
+
81
+ # The box and a thickness all round it, which is as far as a tab stands out.
82
+ def ranges(step)
83
+ reach = profile.thickness
84
+ [[-reach, width + reach], [-reach, profile.depth + reach], [-reach, profile.height + reach]]
85
+ .zip(OFFSETS).map { |(from, to), offset| (from / step).floor.upto((to / step).ceil).map { |index| (index + offset) * step } }
86
+ end
87
+
88
+ # Whether a point is inside a polygon: a ray from it crosses the sides an odd number of times.
89
+ def inside?(polygon, x, y)
90
+ polygon.each_with_index.count do |(xi, yi), index|
91
+ xj, yj = polygon[index - 1]
92
+ ((yi > y) != (yj > y)) && (x < (((xj - xi) * (y - yi)) / (yj - yi)) + xi)
93
+ end.odd?
94
+ end
95
+ end
Binary file