Class: Graphomaton::Exporters::Svg::SpatialIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/graphomaton/exporters/svg.rb

Constant Summary collapse

MAX_CELLS_PER_ITEM =
256

Instance Method Summary collapse

Constructor Details

#initialize(cell_size:) ⇒ SpatialIndex

Returns a new instance of SpatialIndex.



15
16
17
18
19
20
# File 'lib/graphomaton/exporters/svg.rb', line 15

def initialize(cell_size:)
  @cell_size = [cell_size.to_f, 1.0].max
  @cells = Hash.new { |hash, key| hash[key] = [] }
  @values = []
  @oversized_values = []
end

Instance Method Details

#insert(bounds, value = bounds) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/graphomaton/exporters/svg.rb', line 22

def insert(bounds, value = bounds)
  @values << value
  keys = cell_keys(bounds)
  if keys
    keys.each { |key| @cells[key] << value }
  else
    @oversized_values << value
  end
  value
end

#query(bounds) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/graphomaton/exporters/svg.rb', line 33

def query(bounds)
  keys = cell_keys(bounds)
  return @values.dup unless keys

  seen = {}
  @oversized_values.each { |value| seen[value.object_id] = true }
  keys.each_with_object(@oversized_values.dup) do |key, values|
    @cells[key].each do |value|
      identity = value.object_id
      next if seen[identity]

      seen[identity] = true
      values << value
    end
  end
end