Class: Laser::Cutter::Notching::PathGenerator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/laser/cutter/notching/path_generator.rb

Overview

Draws the notched path of one Edge: a zigzag between its inside and outside lines, plus the corner boxes at the ends the edge names.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edge) ⇒ PathGenerator

Returns a new instance of PathGenerator.

Parameters:

  • edge (Edge) —

    the side to draw, with both its lines



20
21
22
# File 'lib/laser/cutter/notching/path_generator.rb', line 20

def initialize(edge)
  @edge = edge
end

Instance Attribute Details

#edge ⇒ Edge

Returns the side to draw.

Returns:

  • (Edge) —

    the side to draw



17
18
19
# File 'lib/laser/cutter/notching/path_generator.rb', line 17

def edge
  @edge
end

Instance Method Details

#adjust_for_kerf(vertices, direction) ⇒ void

This method returns an undefined value.

Moves the last vertex along the edge by the kerf.

Parameters:

  • vertices (Array<Geometry::Point>) —

    the path so far, changed in place

  • direction (Integer) —

    -1 backwards, 1 forwards



59
60
61
62
63
64
65
# File 'lib/laser/cutter/notching/path_generator.rb', line 59

def adjust_for_kerf(vertices, direction)
  return unless kerf?

  point = vertices.pop
  point = point.plus(2 * direction * shift_vector(1))
  vertices << point
end

#corner_box_sides ⇒ Array<Geometry::Line>

Returns the sides of the corner boxes, with their kerf strips when the corners are adjusted.

Returns:

  • (Array<Geometry::Line>) —

    the sides of the corner boxes, with their kerf strips when the corners are adjusted



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/laser/cutter/notching/path_generator.rb', line 69

def corner_box_sides
  ends = Edge::ENDS.select { |end_index| edge.corner_at?(end_index) }

  # These boxes occupy the corners of the 3D box. They do not match
  # in width to our notches because they are usually merged with them.
  # It's just an aesthetic choice I guess.
  boxes = ends.map { |end_index| Geometry::Rect[inside_point(end_index).clone, outside_point(end_index).clone] }
  sides = boxes.map(&:relocate!).map(&:sides)
  sides << ends.map { |end_index| kerf_strip(end_index) } if kerf? && adjust_corners
  sides.flatten
end

#d_index_across ⇒ Integer

Returns the axis across the edge: 0 for x, 1 for y.

Returns:

  • (Integer) —

    the axis across the edge: 0 for x, 1 for y



122
123
124
# File 'lib/laser/cutter/notching/path_generator.rb', line 122

def d_index_across
  (d_index_along + 1) % 2
end

#d_index_along ⇒ Integer

Returns the axis the edge runs along: 0 for x, 1 for y.

Returns:

  • (Integer) —

    the axis the edge runs along: 0 for x, 1 for y



117
118
119
# File 'lib/laser/cutter/notching/path_generator.rb', line 117

def d_index_along
  edge.inside.p1.x == edge.inside.p2.x ? 1 : 0
end

#direction_across ⇒ Integer

Returns 1 when the outside line lies at greater coordinates, -1 otherwise.

Returns:

  • (Integer) —

    1 when the outside line lies at greater coordinates, -1 otherwise



132
133
134
# File 'lib/laser/cutter/notching/path_generator.rb', line 132

def direction_across
  edge.inside.p1.coords.[](d_index_across) < edge.outside.p1.coords.[](d_index_across) ? 1 : -1
end

#direction_along ⇒ Integer

Returns 1 when the edge runs towards greater coordinates, -1 otherwise.

Returns:

  • (Integer) —

    1 when the edge runs towards greater coordinates, -1 otherwise



127
128
129
# File 'lib/laser/cutter/notching/path_generator.rb', line 127

def direction_along
  edge.inside.p1.coords.[](d_index_along) < edge.inside.p2.coords.[](d_index_along) ? 1 : -1
end

#generate ⇒ Array<Geometry::Line>

Calculates a notched path that flows between the outer edge of the box (outside_line) and inner (inside_line). Relative location of these lines also defines the direction and orientation of the box, and hence the notches.

We always want to create a symmetric path that has a notch in the middle (for center_out = true) or dip in the middle (center_out = false)

Returns:



32
33
34
35
36
37
# File 'lib/laser/cutter/notching/path_generator.rb', line 32

def generate
  lines = []
  lines << corner_box_sides if corners
  lines << notch_lines
  lines.flatten
end

#kerf_strip(end_index) ⇒ Array<Geometry::Line>

Widens the corner box at one end by the kerf, on the side where nothing is attached to it: along the edge when the first notch is a hole, across it when the first notch is a tab.

Parameters:

  • end_index (Integer) —

    1 for p1, 2 for p2

Returns:



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/laser/cutter/notching/path_generator.rb', line 87

def kerf_strip(end_index)
  inside = inside_point(end_index)
  outside = outside_point(end_index)
  along, across = first_notch_out? ? [outside, inside] : [inside, outside]
  moved = inside.plus(-2 * shift_vector(end_index, first_notch_out? ? 1 : 0))

  coords = []
  coords[d_index_along] = along[d_index_along]
  coords[d_index_across] = across[d_index_across]
  Geometry::Rect[moved, Geometry::Point[*coords]].sides << Geometry::Line[moved, inside.clone]
end

#notch_lines ⇒ Array<Geometry::Line>

The zigzag alone, without the corner boxes.

Returns:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/laser/cutter/notching/path_generator.rb', line 42

def notch_lines
  point = starting_point
  vertices = [point]
  adjust_for_kerf(vertices, -1) if adjust_end?(1)
  define_shifts.each do |shift|
    point = shift.next_point_after point
    vertices << point
  end
  adjust_for_kerf(vertices, 1) if adjust_end?(2)
  create_lines(vertices)
end

#shift_vector(index, dim_shift = 0) ⇒ Vector

Half the kerf, along one axis, outwards at one end of the edge.

Parameters:

  • index (Integer) —

    1 for p1, 2 for p2

  • dim_shift (Integer) (defaults to: 0) —

    0 for along the edge, 1 for across it

Returns:

  • (Vector)


104
105
106
107
108
109
# File 'lib/laser/cutter/notching/path_generator.rb', line 104

def shift_vector(index, dim_shift = 0)
  shift = []
  shift[(d_index_across + dim_shift) % 2] = 0
  shift[(d_index_along + dim_shift) % 2] = kerf / 2.0 * edge.send(:"v#{index}").[]((d_index_along + dim_shift) % 2)
  Vector.[](*shift)
end

#starting_point ⇒ Geometry::Point

Returns where the zigzag starts: p1 of the inside line.

Returns:



112
113
114
# File 'lib/laser/cutter/notching/path_generator.rb', line 112

def starting_point
  edge.inside.p1.clone # start
end