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,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
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,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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: laser-cutter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Gredeskoul
@@ -27,16 +27,16 @@ dependencies:
27
27
  name: dry-cli-autocomplete
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
- - - ">="
30
+ - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: '0'
32
+ version: '0.6'
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: '0.6'
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: dry-cli-help
42
42
  requirement: !ruby/object:Gem::Requirement
@@ -261,16 +261,23 @@ extra_rdoc_files: []
261
261
  files:
262
262
  - ".envrc"
263
263
  - ".github/workflows/lint.yml"
264
+ - ".github/workflows/pages.yml"
264
265
  - ".github/workflows/rspec.yml"
265
266
  - ".gitignore"
266
- - ".plans/001.00-dry-cli-migration/plan.md"
267
- - ".plans/002.00-lid-options/plan.md"
268
- - ".plans/003.00-ruby-api/plan.md"
267
+ - ".openlanes/config.yml"
268
+ - ".plans/001.00━━⚪━━❯dry-cli-migration/plan.md"
269
+ - ".plans/002.00━━⚪━━❯lid-options/plan.md"
270
+ - ".plans/003.00━━⚪━━❯ruby-api/plan.md"
271
+ - ".plans/004.00━━⚪━━❯can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/spec.md"
272
+ - ".plans/004.00━━⚪━━❯can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/state.json"
273
+ - ".plans/004.01━━⚪━━❯sloped-box-panels/plan.md"
274
+ - ".plans/decisions.md"
269
275
  - ".relaxed_rubocop.yml"
270
276
  - ".rspec"
271
277
  - ".rubocop.yml"
272
278
  - ".rubocop_todo.yml"
273
279
  - ".ruby-version"
280
+ - CHANGELOG.md
274
281
  - CLAUDE.md
275
282
  - CONTRIBUTING.md
276
283
  - Gemfile
@@ -278,10 +285,15 @@ files:
278
285
  - README.md
279
286
  - Rakefile
280
287
  - docs/badges/coverage_badge.svg
288
+ - docs/decisions/decisions-on-angled-box.md
289
+ - docs/decisions/stack.md
290
+ - docs/images/box-3d.avif
281
291
  - docs/images/box-lid-back.avif
282
292
  - docs/images/box-lid-full.avif
283
293
  - docs/images/box-lid-plain.avif
284
294
  - docs/images/box-metric.avif
295
+ - docs/images/box-sloped-3d.avif
296
+ - docs/images/box-sloped.avif
285
297
  - docs/images/box-tray.avif
286
298
  - docs/images/comparison.jpg
287
299
  - exe/laser-cutter
@@ -292,6 +304,7 @@ files:
292
304
  - lib/laser/cutter.rb
293
305
  - lib/laser/cutter/aggregator.rb
294
306
  - lib/laser/cutter/box.rb
307
+ - lib/laser/cutter/box_factory.rb
295
308
  - lib/laser/cutter/cli.rb
296
309
  - lib/laser/cutter/cli/command.rb
297
310
  - lib/laser/cutter/cli/completion.rb
@@ -304,13 +317,16 @@ files:
304
317
  - lib/laser/cutter/configuration.rb
305
318
  - lib/laser/cutter/geometry/dimensions.rb
306
319
  - lib/laser/cutter/geometry/line.rb
320
+ - lib/laser/cutter/geometry/loop.rb
307
321
  - lib/laser/cutter/geometry/point.rb
308
322
  - lib/laser/cutter/geometry/rect.rb
309
323
  - lib/laser/cutter/geometry/shape.rb
324
+ - lib/laser/cutter/geometry/transform.rb
310
325
  - lib/laser/cutter/geometry/tuple.rb
311
326
  - lib/laser/cutter/invalid_option.rb
312
327
  - lib/laser/cutter/launcher.rb
313
328
  - lib/laser/cutter/missing_option.rb
329
+ - lib/laser/cutter/notching/angled_edge.rb
314
330
  - lib/laser/cutter/notching/base.rb
315
331
  - lib/laser/cutter/notching/edge.rb
316
332
  - lib/laser/cutter/notching/infinite_iterator.rb
@@ -326,12 +342,24 @@ files:
326
342
  - lib/laser/cutter/renderer/meta_renderer.rb
327
343
  - lib/laser/cutter/renderer/rect_renderer.rb
328
344
  - lib/laser/cutter/renderer/svg_renderer.rb
345
+ - lib/laser/cutter/sloped/box.rb
346
+ - lib/laser/cutter/sloped/corners.rb
347
+ - lib/laser/cutter/sloped/layout.rb
348
+ - lib/laser/cutter/sloped/notcher.rb
349
+ - lib/laser/cutter/sloped/panels.rb
350
+ - lib/laser/cutter/sloped/placement.rb
351
+ - lib/laser/cutter/sloped/profile.rb
352
+ - lib/laser/cutter/sloped/request.rb
353
+ - lib/laser/cutter/sloped/ring_panel.rb
354
+ - lib/laser/cutter/sloped/side_panel.rb
329
355
  - lib/laser/cutter/types.rb
330
356
  - lib/laser/cutter/units_converter.rb
331
357
  - lib/laser/cutter/version.rb
332
358
  - lib/laser/cutter/zero_value_not_allowed.rb
333
359
  - lib/laser_cutter.rb
334
360
  - spec/laser/cutter/aggregator_spec.rb
361
+ - spec/laser/cutter/angled_edge_spec.rb
362
+ - spec/laser/cutter/box_factory_spec.rb
335
363
  - spec/laser/cutter/box_lid_spec.rb
336
364
  - spec/laser/cutter/box_spec.rb
337
365
  - spec/laser/cutter/cli/config_file_spec.rb
@@ -340,18 +368,36 @@ files:
340
368
  - spec/laser/cutter/dimensions_spec.rb
341
369
  - spec/laser/cutter/edge_spec.rb
342
370
  - spec/laser/cutter/line_spec.rb
371
+ - spec/laser/cutter/loop_spec.rb
343
372
  - spec/laser/cutter/options_spec.rb
344
373
  - spec/laser/cutter/page_manager_spec.rb
345
374
  - spec/laser/cutter/path_generator_spec.rb
346
375
  - spec/laser/cutter/point_spec.rb
347
376
  - spec/laser/cutter/rect_spec.rb
348
377
  - spec/laser/cutter/renderer/layout_renderer_spec.rb
378
+ - spec/laser/cutter/renderer/meta_renderer_spec.rb
349
379
  - spec/laser/cutter/renderer/svg_renderer_spec.rb
350
380
  - spec/laser/cutter/renderer_spec.rb
381
+ - spec/laser/cutter/sloped/assembly_spec.rb
382
+ - spec/laser/cutter/sloped/box_spec.rb
383
+ - spec/laser/cutter/sloped/corners_spec.rb
384
+ - spec/laser/cutter/sloped/layout_spec.rb
385
+ - spec/laser/cutter/sloped/notcher_spec.rb
386
+ - spec/laser/cutter/sloped/panels_spec.rb
387
+ - spec/laser/cutter/sloped/placement_spec.rb
388
+ - spec/laser/cutter/sloped/request_spec.rb
389
+ - spec/laser/cutter/sloped/ring_panel_spec.rb
390
+ - spec/laser/cutter/sloped/side_panel_spec.rb
391
+ - spec/laser/cutter/sloped_profile_spec.rb
392
+ - spec/laser/cutter/transform_spec.rb
351
393
  - spec/laser/cutter_spec.rb
352
394
  - spec/spec_helper.rb
353
395
  - spec/support/aruba.rb
396
+ - spec/support/loops.rb
354
397
  - spec/support/outline.rb
398
+ - spec/support/sloped.rb
399
+ - spec/support/solid.rb
400
+ - spec/wishes/wish-tilted-box.png
355
401
  homepage: https://github.com/kigster/laser-cutter
356
402
  licenses:
357
403
  - MIT
@@ -371,7 +417,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
371
417
  - !ruby/object:Gem::Version
372
418
  version: '0'
373
419
  requirements: []
374
- rubygems_version: 4.0.21
420
+ rubygems_version: 4.0.23
375
421
  specification_version: 4
376
422
  summary: Creates notched box outlines for laser-cut boxes which are geometrically
377
423
  symmetric and pleasing to the eye.