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

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

Overview

One of the key “tricks” that this algorithm applies, is that it converts everything into pure set of lines in the end. It then tries to find all intersections of the lines so that we can remove duplicates. So any segment of any line that is covered by 2 lines or more is removed, cleared completely for an empty space. This turns out to be very useful indeed, because we can paint with wide brush strokes to get the carcass, and then fine tune it by adding or removing line segments. Some of the lines below are added to actually remove the lines that might have otherwise been there.

This comes especially handy when drawing corner boxes, which are deliberately made not to match the notch width, but to match thickness of the material. The corner notces for these sides will therefore have length equal to the thickness + regular notch length.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edge) ⇒ PathGenerator

This class generates lines that zigzag between two lines: the outside line, and the inside line of a single edge. Edge class encapsulates both of them with additional properties.



58
59
60
# File 'lib/laser-cutter/notching/path_generator.rb', line 58

def initialize(edge)
  @edge = edge
end

Instance Attribute Details

#edgeObject

Returns the value of attribute edge.



53
54
55
# File 'lib/laser-cutter/notching/path_generator.rb', line 53

def edge
  @edge
end

Instance Method Details

#adjust_for_kerf(vertices, direction) ⇒ Object



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

def adjust_for_kerf(vertices, direction)
  if kerf?
    point = vertices.pop
    point = corners ? point.plus(2 * direction * shift_vector(1)) : point
    vertices << point
  end
end

#corner_box_sidesObject

These two boxes occupy the corners of the 3D box. They do not match in width to our notches because they are usually merged with them. Their size is equal to the thickness of the material (adjusted for kerf) It’s just an aesthetic choice I guess.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/laser-cutter/notching/path_generator.rb', line 102

def corner_box_sides
  boxes = []
  extra_lines = []

  boxes << Geometry::Rect[edge.inside.p1.clone, edge.outside.p1.clone]
  boxes << Geometry::Rect[edge.inside.p2.clone, edge.outside.p2.clone]

  extra_lines << add_corners if adjust_corners && kerf?
  sides = boxes.flatten.map(&:relocate!).map(&:sides)
  sides << extra_lines if !extra_lines.empty?
  sides.flatten
end

#d_index_acrossObject



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

def d_index_across
  (d_index_along + 1) % 2
end

#d_index_alongObject

0 = X, 1 = Y



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

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

#direction_acrossObject



140
141
142
# File 'lib/laser-cutter/notching/path_generator.rb', line 140

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

#direction_alongObject



136
137
138
# File 'lib/laser-cutter/notching/path_generator.rb', line 136

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

#generateObject

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)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/laser-cutter/notching/path_generator.rb', line 68

def generate
  shifts = define_shifts
  vertices = []
  lines = []

  if corners
    lines << corner_box_sides
  end

  point = starting_point

  vertices << point
  adjust_for_kerf(vertices,-1) if adjust_corners && !first_notch_out?
  shifts.each do |shift|
    point = shift.next_point_after point
    vertices << point
  end
  adjust_for_kerf(vertices, 1) if adjust_corners && !first_notch_out?
  lines << create_lines(vertices)
  lines.flatten
end

#shift_vector(index, dim_shift = 0) ⇒ Object



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

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}".to_sym).[]((d_index_along + dim_shift) % 2)
  Vector.[](*shift)
end

#starting_pointObject



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

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