-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.rb
More file actions
366 lines (321 loc) · 9.13 KB
/
node.rb
File metadata and controls
366 lines (321 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
################################################################################
require 'rectangle'
class Node
attr_accessor :children,:data,:parent,:bounding_box,:id # DEBUG
# node holds refernce of tree it belongs to, that way every node doesn't
# have to hold infos about min and max data (children) number and dimension
# of data stored in tree
def initialize (tree, parent = nil, id = "") # id - DEBUG
@tree = tree
@parent = parent
@children = []
@data = []
@bounding_box = Rectangle.new(tree.dimension)
@id = id # DEBUG
end
def find_data (rect)
if leaf?
if @data.member?(rect)
return self
end
else
ret = nil
if @children.any? {|child| ret = child.find_data(rect)}
return ret
end
end
end
def insert_data (rect)
# DEBUG "\ninsert_data(#{rect}) called on #{self}" # DEBUG
just_insert_data (rect)
check_overflow()
self
end
def delete_data (rect)
@data.delete(rect)
update_boxes()
check_underfill()
end
def export (file,name = "1")
# DEBUG name
file.write("\"#{name}\"[shape=record,label=\"")
if leaf?
file.write(([@bounding_box] + @data).map(&:export).join("|"))
else
file.write(@bounding_box.export)
end
file.puts("\"];")
counter = 1
@children.each do |child|
child_name = "#{name}.#{counter.to_s}"
child.export(file,child_name)
file.puts("\"#{name}\" -> \"#{child_name}\";")
counter += 1
end
end
def export_rects(file)
@bounding_box.export_rect(file,true)
if leaf?
@data.each {|data| data.export_rect(file)}
else
@children.each {|child| child.export_rects(file)}
end
end
def to_s
"#<#{self.class.name}:#{object_id} @box=#{@bounding_box}>"
end
def inspect
to_s
end
def leaf?
@children.empty?
end
def root?
@parent.nil?
end
# returns nil if the node itself is leaf
def children_are_leaves?
unless leaf?
@children[0].leaf?
end
end
def box_area
@bounding_box.area
end
# how much do i need to enlarge bounding_box to insert rect in the node
def box_area_enlargement (rect)
return rect.area if (@data + @children).empty?
if leaf?
new_area = (@data + [rect]).reduce(:enlarge).area
else
new_area = (@children.map(&:bounding_box) +
[rect]).reduce(:enlarge).area
end
new_area - box_area
end
# child_changed parameter is needed for Tree#better_node method
# where child_overlap after rectangle addition is computed
# method is called on parent
def child_overlap (child,child_changed = child)
# DEBUG "child_overlap(#{child},\n #{child_changed}) called"
# DEBUG (@children - [child]).inspect
child_changed.bounding_box.
overlap_area_sum((@children - [child]).map(&:bounding_box))
# DEBUG "child_overlap finished"
end
# computes enlargement of child node overlap with other children
# after inserting data rectangle
# method is called on parent
# POKUD JE NEKTERE Z DETI PLNE, POSERE SE - SNAZI SE SPLITOVAT KLON, PRICEMZ
# PRIDA UZEL DO RODICE
def child_overlap_enlargement (child,data)
# DEBUG "child_overlap_enlargement called on #{child},#{data}" # DEBUG
# DEBUG "child_overlap(#{child},\n #{child.clone.insert_data(data)}) = " +
# "#{child_overlap(child,child.clone.insert_data(data))}"
# DEBUG "child_overlap(#{child} = #{child_overlap(child)}"
# remove second parameter from insert_data after proper debug
# DEBUG_OFF()
overlap = child_overlap(child,child.clone.just_insert_data(data)) - child_overlap(child)
# DEBUG_ON()
overlap
end
def add_child(node)
just_add_child(node)
check_overflow()
end
def clone
# DEBUG "clone called on #{self}" # DEBUG
new_node = Node.new(@tree, @parent ? @parent.clone : nil)
new_node.children = @children.clone
new_node.data = @data.clone
new_node.update_box
new_node
end
def check_overflow
# DEBUG "check_overflow called on #{self}"
if leaf?
split_data if @data.count > @tree.max
else
split_children if @children.count > @tree.max
end
end
def check_underfill
unless root?
delete if (leaf? ? @data.count : @children.count) < @tree.min
else
if @children.count == 1
@tree.root = @children[0]
@tree.root.parent = nil
end
end
end
def delete
@parent.delete_child(self)
@data.each {|data| @tree.insert(data)}
end
def delete_child(child)
@children.delete(child)
check_underfill()
end
def just_insert_data (rect)
return self if @data.member?(rect)
@data << rect
update_boxes
self
end
################################################################################
# protected
# doesn't check for overflow, used by split
def just_add_child(node)
return if @children.member?(node)
@children << node
node.parent = self
update_boxes
end
def update_box
# DEBUG "update_box(#{@id}) called" # DEBUG
return if (@data + @children) == []
if leaf?
@bounding_box = @data.reduce(:enlarge)
else
@bounding_box = @children.map(&:bounding_box).reduce(:enlarge)
end
end
def update_parent_box
return unless @parent
@parent.update_box
@parent.update_parent_box
end
def update_children_boxes
@children.each do |child|
child.update_children_boxes
child.update_box
end
end
################################################################################
# private
def update_boxes
# DEBUG "update_boxes(#{@id}) called" # DEBUG
update_children_boxes
update_box
update_parent_box
end
def area_value(group1,group2)
Rectangle.bounding_box(group1).area +
Rectangle.bounding_box(group2).area
end
def margin_value(group1,group2)
Rectangle.bounding_box(group1).margin +
Rectangle.bounding_box(group2).margin
end
def overlap_value(group1,group2)
Rectangle.bounding_box(group1).overlap_area(Rectangle.bounding_box(group2))
end
def get_distributions(data)
distributions = []
1.upto(@tree.max - 2*@tree.min + 2) do |i|
distributions << [data.values_at(0..((@tree.min-1)*i)),
data.values_at(((@tree.min-1)*i + 1)..data.count - 1)]
end
distributions
end
def sort_by_axis(data,axis)
data.sort do |rect1,rect2|
low_diff = rect1.extents[axis][0] - rect2.extents[axis][0]
unless low_diff == 0
low_diff
else
rect1.extents[axis][1] - rect2.extents[axis][1]
end
end
end
def better_distribution(dist1,dist2)
overlap1 = overlap_value(*dist1)
overlap2 = overlap_value(*dist2)
(overlap1 < overlap2 || (overlap1 == overlap2 &&
area_value(*dist1) < area_value(*dist2))) ? dist1 : dist2
end
# for each axis sorts the data, gets the distributions, counts sum
# of margin_values of each distribution and returns the axis number
# with the smallest sum
def choose_split_axis(data)
sums = []
@tree.dimension.times do |i|
distributions = get_distributions(sort_by_axis(data,i))
sums << distributions.map { |(group1,group2)|
margin_value(group1,group2) }.reduce(&:+)
end
sums.find_index(sums.min)
end
def choose_split_index(data,axis)
distributions = get_distributions(sort_by_axis(data,axis))
distributions.reduce { |dist1,dist2| better_distribution(dist1,dist2) }
end
def get_parent
if @parent
@parent
else
new_root = Node.new(@tree)
DEBUG_OFF()
new_root.add_child(self)
DEBUG_ON()
@tree.root = new_root
new_root
end
end
def split_data
# DEBUG "split_data called on #{self}"
axis = choose_split_axis(@data)
distribution = choose_split_index(@data,axis)
new_node = Node.new(@tree)
# distribution[0].each { |data| new_node.insert_data(data) }
new_node.data = distribution[0]
new_node.update_box
@data = distribution[1]
get_parent.add_child(new_node)
update_boxes
# DEBUG "split_data finihed on #{self}"
end
def find_children(rect_list)
rect_list.map { |rect|
@children.find { |child| child.bounding_box == rect }}
end
def split_children
# DEBUG "examining split_children call"
# DEBUG "children:"
# DEBUG @children
data = @children.map(&:bounding_box)
# DEBUG "data:"
# DEBUGPP data
axis = choose_split_axis(data)
# DEBUG "axis: #{axis}"
distribution = choose_split_index(data,axis)
# DEBUG "distribution:"
# DEBUGPP distribution
children_dist = distribution.map { |group| find_children(group) }
# DEBUG "children_dist:"
# DEBUGPP children_dist
new_node = Node.new(@tree)
# children_dist[0].each { |child| new_node.just_add_child(child) }
new_node.children = children_dist[0]
@children = children_dist[1]
get_parent.add_child(new_node)
update_boxes
new_node.update_boxes
end
def split
if leaf?
split_data
else
split_children
end
end
def join
end
end
if __FILE__ == $0
$node = Node.new(Tree.new(2,5,3))
$rect = Rectangle.new([[10, 20], [40, 30]])
$rect2 = Rectangle.new([[10, 20], [30, 40]])
$node.insert_data($rect)
end