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

#overlaps?(bounds) ⇒ Boolean

Returns:

  • (Boolean)


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

def overlaps?(bounds)
  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(&:<=)
    end
  end
end

#search(bounds, searched = Set.new) ⇒ Object



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

def search(bounds, searched = Set.new)
  Enumerator.new do |yielder|
    unless searched.include? self
      if overlaps? bounds
        @nodes.each do |node|
          node.search(bounds, searched).each { |object| yielder << object }
        end
        yielder << @object if @nodes.empty?
      end
      searched << self
    end
  end
end