Class: Contrek::Concurrent::Tile

Inherits:
Object
  • Object
show all
Defined in:
lib/contrek/finder/concurrent/tile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(finder:, start_x:, end_x:, name:, benchmarks: {}) ⇒ Tile

Returns a new instance of Tile.



7
8
9
10
11
12
13
14
15
# File 'lib/contrek/finder/concurrent/tile.rb', line 7

def initialize(finder:, start_x:, end_x:, name:, benchmarks: {})
  @finder = finder
  @start_x = start_x
  @end_x = end_x
  @name = name
  @prev = nil
  @next = nil
  @benchmarks = {outer: 0, inner: 0}.merge(benchmarks)
end

Instance Attribute Details

#benchmarksObject (readonly)

Returns the value of attribute benchmarks.



4
5
6
# File 'lib/contrek/finder/concurrent/tile.rb', line 4

def benchmarks
  @benchmarks
end

#circular_nextObject

Returns the value of attribute circular_next.



5
6
7
# File 'lib/contrek/finder/concurrent/tile.rb', line 5

def circular_next
  @circular_next
end

#clusterObject

Returns the value of attribute cluster.



5
6
7
# File 'lib/contrek/finder/concurrent/tile.rb', line 5

def cluster
  @cluster
end

#end_xObject (readonly)

Returns the value of attribute end_x.



4
5
6
# File 'lib/contrek/finder/concurrent/tile.rb', line 4

def end_x
  @end_x
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/contrek/finder/concurrent/tile.rb', line 4

def name
  @name
end

#nextObject

Returns the value of attribute next.



5
6
7
# File 'lib/contrek/finder/concurrent/tile.rb', line 5

def next
  @next
end

#prevObject

Returns the value of attribute prev.



5
6
7
# File 'lib/contrek/finder/concurrent/tile.rb', line 5

def prev
  @prev
end

#shapesObject (readonly)

Returns the value of attribute shapes.



4
5
6
# File 'lib/contrek/finder/concurrent/tile.rb', line 4

def shapes
  @shapes
end

#start_xObject (readonly)

Returns the value of attribute start_x.



4
5
6
# File 'lib/contrek/finder/concurrent/tile.rb', line 4

def start_x
  @start_x
end

Instance Method Details

#assign_raw_polygons!(raw_polylines, treemap = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/contrek/finder/concurrent/tile.rb', line 95

def assign_raw_polygons!(raw_polylines, treemap = nil)
  @shapes = []
  shapes_map = {}
  raw_polylines.each_with_index do |raw_polyline, polyline_index|
    next if raw_polyline[:bounds][:max_x] - raw_polyline[:bounds][:min_x] == 0
    shape = Shape.new.tap do |shape|
      shape.outer_polyline = Polyline.new(tile: self, polygon: raw_polyline[:outer], shape: shape, bounds: raw_polyline[:bounds])
      shape.inner_polylines = raw_polyline[:inner].map { |raw| InnerPolyline.new(shape: shape, raw_coordinates: raw) }
    end
    @shapes << shape

    if treemap && (treemap_entry = treemap[polyline_index])
      shapes_map[polyline_index] = shape
      if treemap_entry != [-1, -1]
        parent = shapes_map[treemap_entry.first]
        shape.set_parent_shape(parent)
        shape.parent_inner_polyline = parent.inner_polylines[treemap_entry.last]
      end
    end
  end
end

#assign_shapes!(shapes) ⇒ Object



51
52
53
54
55
56
# File 'lib/contrek/finder/concurrent/tile.rb', line 51

def assign_shapes!(shapes)
  shapes.each do |s|
    s.outer_polyline.tile = self
  end
  @shapes = shapes
end

#boundary_shapesObject



34
35
36
# File 'lib/contrek/finder/concurrent/tile.rb', line 34

def boundary_shapes
  @bbs ||= shapes.select { |s| s.outer_polyline.boundary? }
end

#compute_treemapObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/contrek/finder/concurrent/tile.rb', line 67

def compute_treemap
  @shapes_map = {}
  shape_index = 0

  @shapes.map do |shape|
    next if shape.outer_polyline.empty?
    @shapes_map[shape] = shape_index
    shape_index += 1
    if shape.parent_shape
      [@shapes_map[shape.parent_shape], shape.parent_shape.inner_polylines.index(shape.parent_inner_polyline)]
    else
      [-1, -1]
    end
  end
end

#infoObject



83
84
85
# File 'lib/contrek/finder/concurrent/tile.rb', line 83

def info
  {name: @name, start_x: @start_x, end_x: @end_x}
end

#initial_process!(finder) ⇒ Object



29
30
31
32
# File 'lib/contrek/finder/concurrent/tile.rb', line 29

def initial_process!(finder)
  result = finder.process_info
  assign_raw_polygons!(result[:polygons], result.[:treemap])
end

#inspectObject



91
92
93
# File 'lib/contrek/finder/concurrent/tile.rb', line 91

def inspect
  "#{self.class}[#{@name}]"
end

#iterateObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contrek/finder/concurrent/tile.rb', line 38

def iterate
  @shapes.each do |shape|
    shape.outer_polyline.raw.each do |position|
      yield(position, "O")
    end
    shape.inner_polylines.each do |inner|
      inner.each do |position|
        yield(position, "I")
      end
    end
  end
end

#left?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/contrek/finder/concurrent/tile.rb', line 21

def left?
  @start_x == 0
end

#right?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/contrek/finder/concurrent/tile.rb', line 25

def right?
  @end_x == @finder.maximum_width
end

#tg_border?(coord) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/contrek/finder/concurrent/tile.rb', line 87

def tg_border?(coord)
  coord[:x] == (@next.nil? ? @start_x : (@end_x - 1))
end

#to_raw_polygonsObject



58
59
60
61
62
63
64
65
# File 'lib/contrek/finder/concurrent/tile.rb', line 58

def to_raw_polygons
  @shapes.filter_map do |shape|
    unless shape.outer_polyline.empty?
      {outer: shape.outer_polyline.raw,
       inner: shape.inner_polylines.map(&:raw)}
    end
  end
end

#whole?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/contrek/finder/concurrent/tile.rb', line 17

def whole?
  @start_x == 0 && @end_x == @finder.maximum_width
end