Class: Laser::Cutter::Geometry::Loop

Inherits:
Object
  • Object
show all
Defined in:
lib/laser/cutter/geometry/loop.rb

Overview

A closed outline: points in order, the last joined back to the first.

A panel is drawn by walking its edges in turn, which leaves the walk with points it does not need: the same point twice where two edges meet, a spike where one edge steps out and the next steps straight back, and points in the middle of a straight run. A Loop drops all three, so what is left has one point for each corner.

It cannot be changed: #grow and #map return a new Loop.

Constant Summary collapse

EPSILON =

Lengths this close to zero, relative to the lines measured, are zero.

1e-9

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ Loop

Returns a new instance of Loop.

Parameters:

  • points (Array<Point>) —

    the walk around the outline

Raises:

  • (ArgumentError) —

    when fewer than three corners are left, or they enclose nothing



21
22
23
24
25
26
27
# File 'lib/laser/cutter/geometry/loop.rb', line 21

def initialize(points)
  @corners = tidy(points.map(&:clone)).freeze
  raise ArgumentError, "an outline needs three corners, got #{@corners.size}" if @corners.size < 3
  raise ArgumentError, 'an outline needs an area, but its corners lie on one line' if area <= EPSILON

  freeze
end

Instance Method Details

#area ⇒ Float

Returns the area enclosed, positive whichever way the points run.

Returns:

  • (Float) —

    the area enclosed, positive whichever way the points run



40
41
42
# File 'lib/laser/cutter/geometry/loop.rb', line 40

def area
  signed_area.abs
end

#grow(distance) ⇒ Loop

The outline moved outwards by a distance on every side, the corners kept sharp. Half the kerf makes a panel cut to size.

Parameters:

  • distance (Numeric) —

    outwards when positive

Returns:



49
50
51
52
53
54
55
# File 'lib/laser/cutter/geometry/loop.rb', line 49

def grow(distance)
  return self if distance.zero?

  side = signed_area.positive? ? 1 : -1
  normals = lines.map { |line| outward(line, side) }
  Loop.new(@corners.each_with_index.map { |point, index| mitre(point, normals[index - 1], normals[index], distance) })
end

#lines ⇒ Array<Line>

Returns one line for each side, end to end.

Returns:

  • (Array<Line>) —

    one line for each side, end to end



35
36
37
# File 'lib/laser/cutter/geometry/loop.rb', line 35

def lines
  @corners.each_with_index.map { |point, index| Line.new(point, @corners[(index + 1) % @corners.size]) }
end

#map {|point| ... } ⇒ Loop

Returns the outline with every corner mapped.

Yield Parameters:

Yield Returns:

Returns:

  • (Loop) —

    the outline with every corner mapped



60
61
62
# File 'lib/laser/cutter/geometry/loop.rb', line 60

def map(&)
  Loop.new(@corners.map(&))
end

#points ⇒ Array<Point>

Returns a copy of the corners, in the order given.

Returns:

  • (Array<Point>) —

    a copy of the corners, in the order given



30
31
32
# File 'lib/laser/cutter/geometry/loop.rb', line 30

def points
  @corners.map(&:clone)
end