Class: Laser::Cutter::Notching::PathGenerator
- Inherits:
-
Object
- Object
- Laser::Cutter::Notching::PathGenerator
- 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
-
#edge ⇒ Object
Returns the value of attribute edge.
Instance Method Summary collapse
- #adjust_for_kerf(vertices, direction) ⇒ Object
-
#corner_box_sides ⇒ Object
These two boxes occupy the corners of the 3D box.
- #d_index_across ⇒ Object
-
#d_index_along ⇒ Object
0 = X, 1 = Y.
- #direction_across ⇒ Object
- #direction_along ⇒ Object
-
#generate ⇒ Object
Calculates a notched path that flows between the outer edge of the box (outside_line) and inner (inside_line).
-
#initialize(edge) ⇒ PathGenerator
constructor
This class generates lines that zigzag between two lines: the outside line, and the inside line of a single edge.
- #shift_vector(index, dim_shift = 0) ⇒ Object
- #starting_point ⇒ Object
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
#edge ⇒ Object
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_sides ⇒ Object
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_across ⇒ Object
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_along ⇒ Object
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_across ⇒ Object
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_along ⇒ Object
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 |
#generate ⇒ Object
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_point ⇒ Object
123 124 125 |
# File 'lib/laser-cutter/notching/path_generator.rb', line 123 def starting_point edge.inside.p1.clone # start end |