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.
- checksums.yaml +4 -4
- data/.github/workflows/pages.yml +44 -0
- data/.gitignore +2 -0
- data/.openlanes/config.yml +106 -0
- 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
- 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
- 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
- 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
- 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
- data/.plans/decisions.md +11 -0
- data/CHANGELOG.md +134 -0
- data/CLAUDE.md +30 -5
- data/Gemfile +2 -1
- data/README.md +309 -31
- data/Rakefile +11 -7
- data/docs/badges/coverage_badge.svg +2 -2
- data/docs/decisions/decisions-on-angled-box.md +93 -0
- data/docs/decisions/stack.md +6 -0
- data/docs/images/box-3d.avif +0 -0
- data/docs/images/box-lid-back.avif +0 -0
- data/docs/images/box-lid-full.avif +0 -0
- data/docs/images/box-lid-plain.avif +0 -0
- data/docs/images/box-metric.avif +0 -0
- data/docs/images/box-sloped-3d.avif +0 -0
- data/docs/images/box-sloped.avif +0 -0
- data/docs/images/box-tray.avif +0 -0
- data/justfile +24 -8
- data/laser-cutter.gemspec +2 -1
- data/lib/laser/cutter/aggregator.rb +28 -0
- data/lib/laser/cutter/box.rb +192 -24
- data/lib/laser/cutter/box_factory.rb +15 -0
- data/lib/laser/cutter/cli/command.rb +27 -1
- data/lib/laser/cutter/cli/config_file.rb +2 -0
- data/lib/laser/cutter/cli/examples.rb +8 -0
- data/lib/laser/cutter/cli/generate.rb +41 -5
- data/lib/laser/cutter/cli/help.rb +4 -0
- data/lib/laser/cutter/cli/page_sizes.rb +3 -0
- data/lib/laser/cutter/cli/version.rb +2 -0
- data/lib/laser/cutter/cli.rb +23 -4
- data/lib/laser/cutter/configuration.rb +42 -2
- data/lib/laser/cutter/geometry/dimensions.rb +7 -0
- data/lib/laser/cutter/geometry/line.rb +42 -0
- data/lib/laser/cutter/geometry/loop.rb +138 -0
- data/lib/laser/cutter/geometry/point.rb +3 -0
- data/lib/laser/cutter/geometry/rect.rb +25 -0
- data/lib/laser/cutter/geometry/shape.rb +19 -1
- data/lib/laser/cutter/geometry/transform.rb +50 -0
- data/lib/laser/cutter/geometry/tuple.rb +47 -11
- data/lib/laser/cutter/invalid_option.rb +8 -0
- data/lib/laser/cutter/launcher.rb +14 -1
- data/lib/laser/cutter/missing_option.rb +2 -0
- data/lib/laser/cutter/notching/angled_edge.rb +67 -0
- data/lib/laser/cutter/notching/base.rb +8 -0
- data/lib/laser/cutter/notching/edge.rb +59 -4
- data/lib/laser/cutter/notching/infinite_iterator.rb +11 -1
- data/lib/laser/cutter/notching/path_generator.rb +86 -81
- data/lib/laser/cutter/notching/shift.rb +4 -0
- data/lib/laser/cutter/options.rb +175 -0
- data/lib/laser/cutter/page_manager.rb +12 -2
- data/lib/laser/cutter/renderer/base.rb +26 -4
- data/lib/laser/cutter/renderer/box_renderer.rb +14 -1
- data/lib/laser/cutter/renderer/layout_renderer.rb +13 -3
- data/lib/laser/cutter/renderer/line_renderer.rb +6 -0
- data/lib/laser/cutter/renderer/meta_renderer.rb +33 -9
- data/lib/laser/cutter/renderer/rect_renderer.rb +5 -0
- data/lib/laser/cutter/renderer/svg_renderer.rb +12 -4
- data/lib/laser/cutter/renderer.rb +2 -0
- data/lib/laser/cutter/sloped/box.rb +90 -0
- data/lib/laser/cutter/sloped/corners.rb +58 -0
- data/lib/laser/cutter/sloped/layout.rb +97 -0
- data/lib/laser/cutter/sloped/notcher.rb +53 -0
- data/lib/laser/cutter/sloped/panels.rb +121 -0
- data/lib/laser/cutter/sloped/placement.rb +36 -0
- data/lib/laser/cutter/sloped/profile.rb +176 -0
- data/lib/laser/cutter/sloped/request.rb +31 -0
- data/lib/laser/cutter/sloped/ring_panel.rb +120 -0
- data/lib/laser/cutter/sloped/side_panel.rb +89 -0
- data/lib/laser/cutter/types.rb +60 -0
- data/lib/laser/cutter/units_converter.rb +5 -0
- data/lib/laser/cutter/version.rb +2 -1
- data/lib/laser/cutter/zero_value_not_allowed.rb +2 -0
- data/lib/laser/cutter.rb +40 -0
- data/spec/laser/cutter/angled_edge_spec.rb +103 -0
- data/spec/laser/cutter/box_factory_spec.rb +19 -0
- data/spec/laser/cutter/box_lid_spec.rb +142 -0
- data/spec/laser/cutter/cli_spec.rb +114 -7
- data/spec/laser/cutter/configuration_spec.rb +54 -0
- data/spec/laser/cutter/loop_spec.rb +107 -0
- data/spec/laser/cutter/options_spec.rb +162 -0
- data/spec/laser/cutter/renderer/layout_renderer_spec.rb +21 -20
- data/spec/laser/cutter/renderer/meta_renderer_spec.rb +82 -0
- data/spec/laser/cutter/renderer/svg_renderer_spec.rb +34 -0
- data/spec/laser/cutter/sloped/assembly_spec.rb +78 -0
- data/spec/laser/cutter/sloped/box_spec.rb +151 -0
- data/spec/laser/cutter/sloped/corners_spec.rb +45 -0
- data/spec/laser/cutter/sloped/layout_spec.rb +77 -0
- data/spec/laser/cutter/sloped/notcher_spec.rb +57 -0
- data/spec/laser/cutter/sloped/panels_spec.rb +175 -0
- data/spec/laser/cutter/sloped/placement_spec.rb +50 -0
- data/spec/laser/cutter/sloped/request_spec.rb +48 -0
- data/spec/laser/cutter/sloped/ring_panel_spec.rb +102 -0
- data/spec/laser/cutter/sloped/side_panel_spec.rb +98 -0
- data/spec/laser/cutter/sloped_profile_spec.rb +156 -0
- data/spec/laser/cutter/transform_spec.rb +53 -0
- data/spec/laser/cutter_spec.rb +114 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/loops.rb +106 -0
- data/spec/support/outline.rb +53 -0
- data/spec/support/sloped.rb +26 -0
- data/spec/support/solid.rb +95 -0
- data/spec/wishes/wish-tilted-box.png +0 -0
- metadata +81 -7
- /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
data/lib/laser/cutter/cli.rb
CHANGED
|
@@ -6,25 +6,43 @@ module Laser
|
|
|
6
6
|
module CLI
|
|
7
7
|
extend Dry::CLI::Registry
|
|
8
8
|
|
|
9
|
+
# The name of the executable, as help and completion print it.
|
|
9
10
|
PROGRAM = 'laser-cutter'
|
|
10
11
|
|
|
11
12
|
# Help and boxes are never wider than this, however wide the terminal is.
|
|
12
|
-
HELP_WIDTH =
|
|
13
|
+
HELP_WIDTH = 100
|
|
13
14
|
|
|
14
15
|
# Columns assumed when there is no terminal, or it reports none.
|
|
15
|
-
DEFAULT_COLUMNS =
|
|
16
|
+
DEFAULT_COLUMNS = 90
|
|
16
17
|
|
|
18
|
+
# `COLUMNS` wins over the console, as it does for dry-cli-help, so a test can pin the width.
|
|
19
|
+
#
|
|
20
|
+
# @param columns [Integer, nil] the width of the terminal; nil or 0 for {DEFAULT_COLUMNS}
|
|
17
21
|
# @return [Integer] columns help wraps at, and a box takes: the terminal's less 6,
|
|
18
22
|
# capped at HELP_WIDTH
|
|
19
|
-
def self.help_width(columns =
|
|
23
|
+
def self.help_width(columns = Dry::CLI::Help::Terminal.width)
|
|
20
24
|
columns = DEFAULT_COLUMNS unless columns&.positive?
|
|
21
25
|
[columns - 6, HELP_WIDTH].min
|
|
22
26
|
end
|
|
23
27
|
|
|
28
|
+
# Boxes as wide as help. dry-cli-ui assumes 80 columns on a stream that is
|
|
29
|
+
# not a tty, such as a pipe, so it is told the terminal's width.
|
|
30
|
+
#
|
|
31
|
+
# @param out [IO] where info and success boxes go
|
|
32
|
+
# @param err [IO] where error boxes go
|
|
33
|
+
# @return [Dry::CLI::UI::Console]
|
|
34
|
+
def self.console(out:, err:)
|
|
35
|
+
Dry::CLI::UI::Console.new(out: out, err: err, width: Dry::CLI::Help::Terminal.width, box_width: help_width)
|
|
36
|
+
end
|
|
37
|
+
|
|
24
38
|
Dry::CLI::Help.configure do
|
|
39
|
+
pastel = Pastel.new
|
|
40
|
+
|
|
25
41
|
title "laser-cutter #{Laser::Cutter::VERSION}"
|
|
26
42
|
description 'Draws the notched panels of a box, ready to cut on a laser cutter, as a PDF or an SVG.'
|
|
27
|
-
epilogue '
|
|
43
|
+
epilogue "Source Code ━━━━━━━━▶ #{pastel.cyan.underline('https://github.com/kigster/laser-cutter')}\n\n" \
|
|
44
|
+
"API Documentation ━━▶ #{pastel.cyan.underline('https://kigster.github.io/laser-cutter/')}\n\n" \
|
|
45
|
+
"Make An Enclosure ━━▶ #{pastel.cyan.underline('https://makeabox.io/')}"
|
|
28
46
|
|
|
29
47
|
width CLI.help_width
|
|
30
48
|
exit_code_without_arguments 0
|
|
@@ -34,6 +52,7 @@ module Laser
|
|
|
34
52
|
usage :yellow
|
|
35
53
|
example :yellow
|
|
36
54
|
option :green
|
|
55
|
+
title :bold, :yellow, :underline
|
|
37
56
|
example_comment :bright_black
|
|
38
57
|
end
|
|
39
58
|
end
|
|
@@ -2,20 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
|
+
# The settings of one box, as the renderers take them: a Hash with
|
|
6
|
+
# Symbol keys, read as methods (config.width). It expands the
|
|
7
|
+
# --box WxHxD/T[/N] shorthand, casts numeric Strings to Floats, and fills
|
|
8
|
+
# in the kerf, margin, padding and stroke for the units, and a notch of
|
|
9
|
+
# three times the thickness.
|
|
5
10
|
class Configuration < Hashie::Mash
|
|
6
11
|
include ::Hashie::Extensions::Mash::SymbolizeKeys
|
|
7
12
|
|
|
13
|
+
# The --box shorthand: WxHxD/T, or WxHxD/T/N with a notch.
|
|
8
14
|
SIZE_REGEXP = %r{[\d.]+x[\d.]+x[\d.]+/[\d.]+(/[\d.]+)?}
|
|
9
15
|
|
|
10
|
-
|
|
16
|
+
# Distances, in the units of the box: cast to floats, and converted with the units.
|
|
17
|
+
FLOATS = %i(width height depth front_height thickness notch margin padding stroke kerf).freeze
|
|
18
|
+
# Angles, in degrees: cast to floats, and left alone when the units change.
|
|
19
|
+
ANGLES = %i(slope).freeze
|
|
20
|
+
# Settings that must not be zero.
|
|
11
21
|
NON_ZERO = %i(width height depth thickness stroke).freeze
|
|
22
|
+
# Settings that must be given.
|
|
12
23
|
REQUIRED = %i(width height depth thickness notch file).freeze
|
|
13
24
|
|
|
14
25
|
@mutex = Mutex.new
|
|
15
26
|
|
|
16
27
|
class << self
|
|
28
|
+
# @!attribute [rw] mutex
|
|
29
|
+
# @return [Mutex] guards the computing of {.defaults}
|
|
30
|
+
# @!attribute [rw] defaults
|
|
31
|
+
# @return [Hashie::Mash] the defaults, with the distances for :in and :mm
|
|
17
32
|
attr_accessor :mutex, :defaults
|
|
18
33
|
|
|
34
|
+
# Computes the defaults once.
|
|
35
|
+
#
|
|
36
|
+
# @return [Hashie::Mash] a copy of the defaults
|
|
19
37
|
def compute_defaults!
|
|
20
38
|
mutex.synchronize do
|
|
21
39
|
if defined?(@defaults)
|
|
@@ -26,10 +44,13 @@ module Laser
|
|
|
26
44
|
end.dup
|
|
27
45
|
end
|
|
28
46
|
|
|
47
|
+
# @return [Hashie::Mash] the defaults, with the distances for inches
|
|
48
|
+
# and the same converted to millimetres
|
|
29
49
|
def compute_metric_units!
|
|
30
50
|
defaults = Hashie::Mash.new({
|
|
31
51
|
units: :in,
|
|
32
52
|
page_layout: 'portrait',
|
|
53
|
+
lid: 'full',
|
|
33
54
|
metadata: true,
|
|
34
55
|
in: {
|
|
35
56
|
kerf: 0.0024, # smallest kerf for thin material, usually it's more than that.
|
|
@@ -46,10 +67,13 @@ module Laser
|
|
|
46
67
|
|
|
47
68
|
compute_defaults!
|
|
48
69
|
|
|
70
|
+
# @param options [Hash] the settings, by String or Symbol key; nil
|
|
71
|
+
# values, a blank lid and units it does not know are dropped
|
|
49
72
|
def initialize(options = {})
|
|
50
73
|
::Hashie::Extensions::SymbolizeKeys.symbolize_keys!(options)
|
|
51
74
|
|
|
52
75
|
options.delete_if { |_k, v| v.nil? }
|
|
76
|
+
options.delete(:lid) if options[:lid].to_s.empty? # a blank form field
|
|
53
77
|
if options[:units]
|
|
54
78
|
unit = options[:units].to_sym
|
|
55
79
|
unless self.class.defaults.key?(unit) || self.class.defaults.key?(unit.to_s)
|
|
@@ -64,13 +88,19 @@ module Laser
|
|
|
64
88
|
self[:width], self[:height], self[:depth] = dim.split('x')
|
|
65
89
|
delete(:box)
|
|
66
90
|
end
|
|
67
|
-
FLOATS.each do |k|
|
|
91
|
+
(FLOATS + ANGLES).each do |k|
|
|
68
92
|
self[k] = self[k].to_f if self[k].is_a?(String)
|
|
69
93
|
end
|
|
70
94
|
merge!(self.class.defaults[self[:units].to_sym].merge(self))
|
|
71
95
|
self[:notch] = (self[:thickness] * 3.0).round(5) if self[:thickness] && self[:notch].nil?
|
|
72
96
|
end
|
|
73
97
|
|
|
98
|
+
# Checks that the settings make a box.
|
|
99
|
+
#
|
|
100
|
+
# @return [void]
|
|
101
|
+
# @raise [MissingOption] when a required setting is missing
|
|
102
|
+
# @raise [ZeroValueNotAllowed] when a setting that must not be zero is
|
|
103
|
+
# @raise [InvalidOption] for a lid it does not know, or a slope that does not make a box
|
|
74
104
|
def validate!
|
|
75
105
|
missing = []
|
|
76
106
|
REQUIRED.each { |k| missing << k if self[k].nil? }
|
|
@@ -79,8 +109,18 @@ module Laser
|
|
|
79
109
|
zeros = []
|
|
80
110
|
NON_ZERO.each { |k| zeros << k if self[k] == 0 }
|
|
81
111
|
raise ZeroValueNotAllowed, "#{zeros.join(', ')} #{zeros.size > 1 ? 'are' : 'is'} required, but is zero." unless zeros.empty?
|
|
112
|
+
|
|
113
|
+
unless Box::LIDS.include?(lid.to_s.to_sym)
|
|
114
|
+
raise InvalidOption, "lid is #{lid.to_s.inspect}, but must be one of: #{Box::LIDS.join(', ')}."
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
Sloped::Request.validate!(self)
|
|
82
118
|
end
|
|
83
119
|
|
|
120
|
+
# Converts every distance to other units, and leaves angles alone.
|
|
121
|
+
#
|
|
122
|
+
# @param new_units [String, Symbol] "in" or "mm"
|
|
123
|
+
# @return [Symbol, nil] the new units, or nil when nothing changed
|
|
84
124
|
def change_units(new_units)
|
|
85
125
|
new_units = new_units.to_sym
|
|
86
126
|
return if new_units == units.to_sym
|
|
@@ -2,24 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
|
+
# Unitless planar geometry: points, lines, rectangles and closed loops.
|
|
5
6
|
module Geometry
|
|
7
|
+
# The width, height and depth of a box, written as "WxHxD".
|
|
6
8
|
class Dimensions < Tuple
|
|
9
|
+
# @return [Float] the width
|
|
7
10
|
def w
|
|
8
11
|
coords.[](0)
|
|
9
12
|
end
|
|
10
13
|
|
|
14
|
+
# @return [Float] the height
|
|
11
15
|
def h
|
|
12
16
|
coords.[](1)
|
|
13
17
|
end
|
|
14
18
|
|
|
19
|
+
# @return [Float] the depth
|
|
15
20
|
def d
|
|
16
21
|
coords.[](2)
|
|
17
22
|
end
|
|
18
23
|
|
|
24
|
+
# @return [String] the separator between the values in a String
|
|
19
25
|
def separator
|
|
20
26
|
'x'
|
|
21
27
|
end
|
|
22
28
|
|
|
29
|
+
# @return [Array<Symbol>] the keys a Hash of dimensions uses
|
|
23
30
|
def hash_keys
|
|
24
31
|
[:w, :h, :d]
|
|
25
32
|
end
|
|
@@ -3,13 +3,25 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Geometry
|
|
6
|
+
# A straight line between two points. Two lines are equal whichever
|
|
7
|
+
# way they run.
|
|
6
8
|
class Line < Shape
|
|
9
|
+
# @param array [Array] whatever {#initialize} accepts
|
|
10
|
+
# @return [Line]
|
|
7
11
|
def self.[](*array)
|
|
8
12
|
new(*array)
|
|
9
13
|
end
|
|
10
14
|
|
|
15
|
+
# @!attribute [rw] p1
|
|
16
|
+
# @return [Point] where the line starts
|
|
17
|
+
# @!attribute [rw] p2
|
|
18
|
+
# @return [Point] where the line ends
|
|
11
19
|
attr_accessor :p1, :p2
|
|
12
20
|
|
|
21
|
+
# @param point1 [Point, Hash] where the line starts, or a Hash with
|
|
22
|
+
# :from and :to, each anything {Point} accepts
|
|
23
|
+
# @param point2 [Point, nil] where the line ends, unless point1 is a Hash
|
|
24
|
+
# @raise [RuntimeError] when a point is missing
|
|
13
25
|
def initialize(point1, point2 = nil)
|
|
14
26
|
if point1.is_a?(Hash)
|
|
15
27
|
options = point1
|
|
@@ -23,6 +35,9 @@ module Laser
|
|
|
23
35
|
raise 'Both points are required for line definition' unless p1 && p2
|
|
24
36
|
end
|
|
25
37
|
|
|
38
|
+
# @param another [Line]
|
|
39
|
+
# @return [Boolean] whether both lines lie on one horizontal or vertical
|
|
40
|
+
# line and share at least a point
|
|
26
41
|
def overlaps?(another)
|
|
27
42
|
xs, ys = sorted_coords(another)
|
|
28
43
|
return false unless xs.all?{ |x| x == xs[0] } || ys.all?{ |y| y == ys[0] }
|
|
@@ -32,12 +47,19 @@ module Laser
|
|
|
32
47
|
true
|
|
33
48
|
end
|
|
34
49
|
|
|
50
|
+
# @param another [Line]
|
|
51
|
+
# @return [Array(Array, Array)] the x and then the y ranges of both lines,
|
|
52
|
+
# each range sorted
|
|
35
53
|
def sorted_coords(another)
|
|
36
54
|
xs = [[p1.x, p2.x].sort, [another.p1.x, another.p2.x].sort]
|
|
37
55
|
ys = [[p1.y, p2.y].sort, [another.p1.y, another.p2.y].sort]
|
|
38
56
|
[xs, ys]
|
|
39
57
|
end
|
|
40
58
|
|
|
59
|
+
# The parts of two overlapping lines that only one of them covers.
|
|
60
|
+
#
|
|
61
|
+
# @param another [Line]
|
|
62
|
+
# @return [Array(Line, Line), nil] nil when the lines do not overlap
|
|
41
63
|
def xor(another)
|
|
42
64
|
return nil unless overlaps?(another)
|
|
43
65
|
|
|
@@ -49,6 +71,10 @@ module Laser
|
|
|
49
71
|
Line.new(Point[xs[2], ys[2]], Point[xs[3], ys[3]])]
|
|
50
72
|
end
|
|
51
73
|
|
|
74
|
+
# Moves the line so that it starts at its position, keeping its
|
|
75
|
+
# length and direction.
|
|
76
|
+
#
|
|
77
|
+
# @return [Line] self
|
|
52
78
|
def relocate!
|
|
53
79
|
dx = p2.x - p1.x
|
|
54
80
|
dy = p2.y - p1.y
|
|
@@ -58,45 +84,61 @@ module Laser
|
|
|
58
84
|
self
|
|
59
85
|
end
|
|
60
86
|
|
|
87
|
+
# @return [Point] the middle of the line
|
|
61
88
|
def center
|
|
62
89
|
Point.new((p2.x + p1.x) / 2, (p2.y + p1.y) / 2)
|
|
63
90
|
end
|
|
64
91
|
|
|
92
|
+
# @return [Float]
|
|
65
93
|
def length
|
|
66
94
|
Math.sqrt(((p2.x - p1.x)**2) + ((p2.y - p1.y)**2))
|
|
67
95
|
end
|
|
68
96
|
|
|
97
|
+
# @return [String] the class and both points, such as "line {0,0}=>{1,0}"
|
|
69
98
|
def to_s
|
|
70
99
|
"#{self.class.name.gsub(/.*::/, '').downcase} #{p1}=>#{p2}"
|
|
71
100
|
end
|
|
72
101
|
|
|
102
|
+
# @param other [Line]
|
|
103
|
+
# @return [Boolean] whether the lines join the same two points, either way round
|
|
73
104
|
def eql?(other)
|
|
74
105
|
(other.p1.eql?(p1) && other.p2.eql?(p2)) ||
|
|
75
106
|
(other.p2.eql?(p1) && other.p1.eql?(p2))
|
|
76
107
|
end
|
|
77
108
|
|
|
109
|
+
# @return [Line] the same line, running from the lesser point to the greater
|
|
78
110
|
def normalized
|
|
79
111
|
p1 < p2 ? Line.new(p1, p2) : Line.new(p2, p1)
|
|
80
112
|
end
|
|
81
113
|
|
|
114
|
+
# Orders lines by their normalized points.
|
|
115
|
+
#
|
|
116
|
+
# @param other [Line]
|
|
117
|
+
# @return [Integer, nil]
|
|
82
118
|
def <=>(other)
|
|
83
119
|
n1 = normalized
|
|
84
120
|
n2 = other.normalized
|
|
85
121
|
n1.p1.eql?(n2.p1) ? n1.p2 <=> n2.p2 : n1.p1 <=> n2.p1
|
|
86
122
|
end
|
|
87
123
|
|
|
124
|
+
# @param other [Line]
|
|
125
|
+
# @return [Boolean] whether this line comes first, by p1 and then by p2
|
|
88
126
|
def <(other)
|
|
89
127
|
p1 == other.p1 ? p2 < other.p2 : p1 < other.p1
|
|
90
128
|
end
|
|
91
129
|
|
|
130
|
+
# @param other [Line]
|
|
131
|
+
# @return [Boolean] whether this line comes last, by p1 and then by p2
|
|
92
132
|
def >(other)
|
|
93
133
|
p1 == other.p1 ? p2 > other.p2 : p1 > other.p1
|
|
94
134
|
end
|
|
95
135
|
|
|
136
|
+
# @return [Integer] the same for a line and its reverse
|
|
96
137
|
def hash
|
|
97
138
|
[p1.to_a, p2.to_a].sort.hash
|
|
98
139
|
end
|
|
99
140
|
|
|
141
|
+
# @return [Line] a copy with copies of both points
|
|
100
142
|
def clone
|
|
101
143
|
self.class.new(p1, p2)
|
|
102
144
|
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Laser
|
|
4
|
+
module Cutter
|
|
5
|
+
module Geometry
|
|
6
|
+
# A closed outline: points in order, the last joined back to the first.
|
|
7
|
+
#
|
|
8
|
+
# A panel is drawn by walking its edges in turn, which leaves the walk
|
|
9
|
+
# with points it does not need: the same point twice where two edges
|
|
10
|
+
# meet, a spike where one edge steps out and the next steps straight
|
|
11
|
+
# back, and points in the middle of a straight run. A Loop drops all
|
|
12
|
+
# three, so what is left has one point for each corner.
|
|
13
|
+
#
|
|
14
|
+
# It cannot be changed: #grow and #map return a new Loop.
|
|
15
|
+
class Loop
|
|
16
|
+
# Lengths this close to zero, relative to the lines measured, are zero.
|
|
17
|
+
EPSILON = 1e-9
|
|
18
|
+
|
|
19
|
+
# @param points [Array<Point>] the walk around the outline
|
|
20
|
+
# @raise [ArgumentError] when fewer than three corners are left, or they enclose nothing
|
|
21
|
+
def initialize(points)
|
|
22
|
+
@corners = tidy(points.map(&:clone)).freeze
|
|
23
|
+
raise ArgumentError, "an outline needs three corners, got #{@corners.size}" if @corners.size < 3
|
|
24
|
+
raise ArgumentError, 'an outline needs an area, but its corners lie on one line' if area <= EPSILON
|
|
25
|
+
|
|
26
|
+
freeze
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @return [Array<Point>] a copy of the corners, in the order given
|
|
30
|
+
def points
|
|
31
|
+
@corners.map(&:clone)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [Array<Line>] one line for each side, end to end
|
|
35
|
+
def lines
|
|
36
|
+
@corners.each_with_index.map { |point, index| Line.new(point, @corners[(index + 1) % @corners.size]) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [Float] the area enclosed, positive whichever way the points run
|
|
40
|
+
def area
|
|
41
|
+
signed_area.abs
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The outline moved outwards by a distance on every side, the corners
|
|
45
|
+
# kept sharp. Half the kerf makes a panel cut to size.
|
|
46
|
+
#
|
|
47
|
+
# @param distance [Numeric] outwards when positive
|
|
48
|
+
# @return [Loop]
|
|
49
|
+
def grow(distance)
|
|
50
|
+
return self if distance.zero?
|
|
51
|
+
|
|
52
|
+
side = signed_area.positive? ? 1 : -1
|
|
53
|
+
normals = lines.map { |line| outward(line, side) }
|
|
54
|
+
Loop.new(@corners.each_with_index.map { |point, index| mitre(point, normals[index - 1], normals[index], distance) })
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @yieldparam point [Point]
|
|
58
|
+
# @yieldreturn [Point]
|
|
59
|
+
# @return [Loop] the outline with every corner mapped
|
|
60
|
+
def map(&)
|
|
61
|
+
Loop.new(@corners.map(&))
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
# The shoelace formula.
|
|
67
|
+
#
|
|
68
|
+
# @return [Float] positive when the points run counter-clockwise
|
|
69
|
+
def signed_area
|
|
70
|
+
lines.sum { |line| (line.p1.x * line.p2.y) - (line.p2.x * line.p1.y) } / 2.0
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The unit normal of a line that points out of the outline.
|
|
74
|
+
#
|
|
75
|
+
# @param line [Line] a side of the outline
|
|
76
|
+
# @param side [Integer] 1 when the points run counter-clockwise, -1 otherwise
|
|
77
|
+
# @return [Vector]
|
|
78
|
+
def outward(line, side)
|
|
79
|
+
length = line.length
|
|
80
|
+
Vector[side * (line.p2.y - line.p1.y) / length, side * (line.p1.x - line.p2.x) / length]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Where the two sides that meet at a corner cross once each has moved out.
|
|
84
|
+
#
|
|
85
|
+
# @param point [Point] the corner
|
|
86
|
+
# @param before [Vector] the outward normal of the side that ends at the corner
|
|
87
|
+
# @param after [Vector] the outward normal of the side that starts there
|
|
88
|
+
# @param distance [Numeric] how far each side moves
|
|
89
|
+
# @return [Point]
|
|
90
|
+
def mitre(point, before, after, distance)
|
|
91
|
+
point.plus((before + after) * (distance / (1 + before.inner_product(after))))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Drops what a walk leaves behind until nothing more can go.
|
|
95
|
+
#
|
|
96
|
+
# @param points [Array<Point>] the walk
|
|
97
|
+
# @return [Array<Point>] the corners, or fewer than three when the walk
|
|
98
|
+
# encloses nothing
|
|
99
|
+
def tidy(points)
|
|
100
|
+
loop do
|
|
101
|
+
kept = points.each_with_index.reject { |_point, index| needless?(points, index) }.map(&:first)
|
|
102
|
+
return kept if kept.size == points.size || kept.size < 3
|
|
103
|
+
|
|
104
|
+
points = kept
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Only the first of two such points in a row goes in one pass, since
|
|
109
|
+
# dropping one changes the neighbours of the other.
|
|
110
|
+
#
|
|
111
|
+
# @param points [Array<Point>]
|
|
112
|
+
# @param index [Integer]
|
|
113
|
+
# @return [Boolean] whether to drop the point at index in this pass
|
|
114
|
+
def needless?(points, index)
|
|
115
|
+
removable?(points, index) && !removable?(points, index - 1)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Whether a point repeats the one before it, or lies on the straight
|
|
119
|
+
# line through its neighbours: in the middle of a run, or at the tip
|
|
120
|
+
# of a spike.
|
|
121
|
+
#
|
|
122
|
+
# @param points [Array<Point>]
|
|
123
|
+
# @param index [Integer]
|
|
124
|
+
# @return [Boolean]
|
|
125
|
+
def removable?(points, index)
|
|
126
|
+
before = points[index - 1]
|
|
127
|
+
point = points[index]
|
|
128
|
+
after = points[(index + 1) % points.size]
|
|
129
|
+
incoming = Math.hypot(point.x - before.x, point.y - before.y)
|
|
130
|
+
return true if incoming <= EPSILON
|
|
131
|
+
|
|
132
|
+
cross = ((point.x - before.x) * (after.y - point.y)) - ((point.y - before.y) * (after.x - point.x))
|
|
133
|
+
cross.abs <= EPSILON * incoming * Math.hypot(after.x - point.x, after.y - point.y)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -3,28 +3,49 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Geometry
|
|
6
|
+
# An axis-aligned rectangle, given by two opposite corners: p1, the
|
|
7
|
+
# lesser, and p2. It keeps its four vertices and sides up to date.
|
|
6
8
|
class Rect < Line
|
|
9
|
+
# @!attribute [rw] sides
|
|
10
|
+
# @return [Array<Line>] the four sides, counter-clockwise from p1
|
|
11
|
+
# @!attribute [rw] vertices
|
|
12
|
+
# @return [Array<Point>] the four corners, counter-clockwise from p1
|
|
7
13
|
attr_accessor :sides, :vertices
|
|
8
14
|
|
|
15
|
+
# @param p1 [Point] one corner
|
|
16
|
+
# @param p2 [Point] the opposite corner
|
|
17
|
+
# @return [Rect]
|
|
9
18
|
def self.[](p1, p2)
|
|
10
19
|
Rect.new(p1, p2)
|
|
11
20
|
end
|
|
12
21
|
|
|
22
|
+
# @param point [Point] the corner with the least x and y
|
|
23
|
+
# @param w [Float] the width
|
|
24
|
+
# @param h [Float] the height
|
|
25
|
+
# @param name [String, Symbol, nil]
|
|
26
|
+
# @return [Rect]
|
|
13
27
|
def self.create(point, w, h, name = nil)
|
|
14
28
|
r = Rect.new(point, Point[point.x + w, point.y + h])
|
|
15
29
|
r.name = name
|
|
16
30
|
r
|
|
17
31
|
end
|
|
18
32
|
|
|
33
|
+
# @param line [Line] its ends become opposite corners
|
|
34
|
+
# @return [Rect]
|
|
19
35
|
def self.from_line(line)
|
|
20
36
|
Rect.new(line.p1, line.p2)
|
|
21
37
|
end
|
|
22
38
|
|
|
39
|
+
# Takes whatever {Line#initialize} takes.
|
|
23
40
|
def initialize(*)
|
|
24
41
|
super
|
|
25
42
|
relocate!
|
|
26
43
|
end
|
|
27
44
|
|
|
45
|
+
# Moves the rectangle to its position and rebuilds its vertices and
|
|
46
|
+
# sides.
|
|
47
|
+
#
|
|
48
|
+
# @return [Rect] self
|
|
28
49
|
def relocate!
|
|
29
50
|
super
|
|
30
51
|
self.vertices = []
|
|
@@ -39,18 +60,22 @@ module Laser
|
|
|
39
60
|
self
|
|
40
61
|
end
|
|
41
62
|
|
|
63
|
+
# @return [Float] the width
|
|
42
64
|
def w
|
|
43
65
|
p2.x - p1.x
|
|
44
66
|
end
|
|
45
67
|
|
|
68
|
+
# @return [Float] the height
|
|
46
69
|
def h
|
|
47
70
|
p2.y - p1.y
|
|
48
71
|
end
|
|
49
72
|
|
|
73
|
+
# @return [String] the size and the position
|
|
50
74
|
def to_s
|
|
51
75
|
"#{sprintf '%3d', w}(w)x#{sprintf '%3d', h}(h) @ #{position}"
|
|
52
76
|
end
|
|
53
77
|
|
|
78
|
+
# @return [Array<Array<Float>>] both corners, as [[x1, y1], [x2, y2]]
|
|
54
79
|
def to_a
|
|
55
80
|
[[p1.x, p1.y], [p2.x, p2.y]]
|
|
56
81
|
end
|
|
@@ -3,36 +3,54 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Geometry
|
|
6
|
+
# The base of a shape that sits at a position and can be moved there.
|
|
6
7
|
class Shape
|
|
8
|
+
# @!attribute [rw] name
|
|
9
|
+
# @return [String, Symbol, nil] the name of the shape, such as the face of a box
|
|
7
10
|
attr_accessor :position, :name
|
|
8
11
|
|
|
12
|
+
# @return [Point] where the shape sits, the origin until set
|
|
9
13
|
def position
|
|
10
14
|
@position ||= Point.new(0, 0)
|
|
11
15
|
end
|
|
12
16
|
|
|
17
|
+
# @param value [Float] the new x of the position
|
|
18
|
+
# @return [Float] the value
|
|
13
19
|
def x=(value)
|
|
14
20
|
position.x = value
|
|
15
21
|
end
|
|
16
22
|
|
|
23
|
+
# @return [Float] the x of the position
|
|
17
24
|
def x
|
|
18
25
|
position.x
|
|
19
26
|
end
|
|
20
27
|
|
|
28
|
+
# @param value [Float] the new y of the position
|
|
29
|
+
# @return [Float] the value
|
|
21
30
|
def y=(value)
|
|
22
31
|
position.y = value
|
|
23
32
|
end
|
|
24
33
|
|
|
34
|
+
# @return [Float] the y of the position
|
|
25
35
|
def y
|
|
26
36
|
position.y
|
|
27
37
|
end
|
|
28
38
|
|
|
39
|
+
# Moves the shape so that its position is the point.
|
|
40
|
+
#
|
|
41
|
+
# @param new_point [Point]
|
|
42
|
+
# @return [Shape] self
|
|
29
43
|
def move_to(new_point)
|
|
30
44
|
self.position = new_point
|
|
31
45
|
relocate!
|
|
32
46
|
self
|
|
33
47
|
end
|
|
34
48
|
|
|
35
|
-
#
|
|
49
|
+
# Moves the shape's own coordinates to its position. Each shape
|
|
50
|
+
# implements it.
|
|
51
|
+
#
|
|
52
|
+
# @return [void]
|
|
53
|
+
# @raise [RuntimeError] when the shape does not implement it
|
|
36
54
|
def relocate!
|
|
37
55
|
raise 'Abstract method'
|
|
38
56
|
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Laser
|
|
4
|
+
module Cutter
|
|
5
|
+
module Geometry
|
|
6
|
+
# A rotation followed by a move, from a local frame into the page.
|
|
7
|
+
#
|
|
8
|
+
# Notches are drawn along the x axis of a local frame, where every side
|
|
9
|
+
# is horizontal and the axis-aligned notching code applies unchanged.
|
|
10
|
+
# A Transform then turns and moves the result onto a side at any angle.
|
|
11
|
+
class Transform
|
|
12
|
+
# @return [Point] where the local origin lands
|
|
13
|
+
attr_reader :origin
|
|
14
|
+
|
|
15
|
+
# @return [Float] the turn, in radians, counter-clockwise
|
|
16
|
+
attr_reader :angle
|
|
17
|
+
|
|
18
|
+
# The frame whose x axis runs from one point towards another, with
|
|
19
|
+
# its origin on the first. Its y axis points to the left of that run.
|
|
20
|
+
#
|
|
21
|
+
# @param from [Point]
|
|
22
|
+
# @param to [Point]
|
|
23
|
+
# @return [Transform]
|
|
24
|
+
def self.along(from, to)
|
|
25
|
+
new(from, Math.atan2(to.y - from.y, to.x - from.x))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @param origin [Point]
|
|
29
|
+
# @param angle [Numeric] radians, counter-clockwise
|
|
30
|
+
def initialize(origin, angle)
|
|
31
|
+
@origin = origin.clone.freeze
|
|
32
|
+
@angle = angle.to_f
|
|
33
|
+
@cos = Math.cos(@angle)
|
|
34
|
+
@sin = Math.sin(@angle)
|
|
35
|
+
freeze
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @param shape [Point, Line] in the local frame
|
|
39
|
+
# @return [Point, Line] a new shape, on the page
|
|
40
|
+
def apply(shape)
|
|
41
|
+
case shape
|
|
42
|
+
when Line then Line.new(apply(shape.p1), apply(shape.p2))
|
|
43
|
+
when Point then Point[origin.x + (shape.x * @cos) - (shape.y * @sin), origin.y + (shape.x * @sin) + (shape.y * @cos)]
|
|
44
|
+
else raise ArgumentError, "cannot transform #{shape.class}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|