Class: RTree

Inherits:
Object
  • Object
show all
Defined in:
lib/nswtopo/geometry/r_tree.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes, bounds, object = nil) ⇒ RTree

Returns a new instance of RTree.



2
3
4
# File 'lib/nswtopo/geometry/r_tree.rb', line 2

def initialize(nodes, bounds, object = nil)
  @nodes, @bounds, @object = nodes, bounds, object
end

Class Method Details

.load(bounds_objects, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nswtopo/geometry/r_tree.rb', line 16

def self.load(bounds_objects, &block)
  case
  when block_given? then load bounds_objects.map(&block).zip(bounds_objects)
  when bounds_objects.one? then RTree.new [], *bounds_objects.first
  else
    nodes = bounds_objects.sort_by do |bounds, object|
      bounds[0].inject(&:+)
    end.in_two.map do |bounds_objects|
      bounds_objects.sort_by do |bounds, object|
        bounds[1].inject(&:+)
      end.in_two.map do |bounds_objects|
        load bounds_objects
      end
    end.flatten
    RTree.new nodes, bounds_objects.map(&:first).transpose.map(&:flatten).map(&:minmax)
  end
end

Instance Method Details

#each {|@bounds, @object| ... } ⇒ Object

Yields:

  • (@bounds, @object)


47
48
49
50
51
52
# File 'lib/nswtopo/geometry/r_tree.rb', line 47

def each(&block)
  @nodes.each do |node|
    node.each(&block)
  end
  yield @bounds, @object if @object
end

#overlaps?(bounds, buffer) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
# File 'lib/nswtopo/geometry/r_tree.rb', line 6

def overlaps?(bounds, buffer)
  return false if @bounds.empty?
  return true unless bounds
  bounds.zip(@bounds).all? do |bound1, bound2|
    bound1.zip(bound2.rotate).each.with_index.all? do |limits, index|
      limits.rotate(index).inject(&:-) <= buffer
    end
  end
end

#search(bounds, buffer: 0, searched: Set.new) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nswtopo/geometry/r_tree.rb', line 34

def search(bounds, buffer: 0, searched: Set.new)
  Enumerator.new do |yielder|
    next if searched.include? self
    if overlaps? bounds, buffer
      @nodes.each do |node|
        node.search(bounds, buffer: buffer, searched: searched).inject(yielder, &:<<)
      end
      yielder << @object if @nodes.empty?
    end
    searched << self
  end
end