-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtree.pl
210 lines (177 loc) · 5.94 KB
/
rtree.pl
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
:- module(rtree,
[ empty_rtree/2, % :Map, ?RTree
list_to_rtree/3, % :Map, +Objects, -RTree
rtree_insert/3, % +Object, +RTree0, -RTree
rtree_inside/3, % +Tree, +Rect, -Obj
rtree_intersects/3 % +Tree, +Object, -Obj
]).
:- use_module(kmeans).
:- use_module(library(apply)).
:- use_module(library(lists)).
:- meta_predicate
empty_rtree(2, ?),
list_to_rtree(2, +, -).
/** <module> A pure Prolog R-tree implementation
*/
rtree_setting(bulk_node_size, 2).
rtree_setting(split_node_size, 20).
:- meta_predicate
rect(2, +, -).
%% empty_rtree(:Map, ?Tree)
%
% True when Tree is an empty RTree.
empty_rtree(Map, r_tree(Map, rect(0,0,0,0), leaf, [])).
%% list_to_rtree(:Map, +Objects, -RTree) is det.
%
% Create an R-Tree from Objects.
list_to_rtree(Map, Objects, RTree) :-
length(Objects, Count),
rtree_setting(bulk_node_size, NodeSize),
Count =< NodeSize, !,
maplist(rect(Map), Objects, Rects),
rect_union_list(Rects, Rect),
RTree = r_tree(Map, Rect, leaf, Objects).
list_to_rtree(Map, Objects, RTree) :-
rtree_setting(bulk_node_size, Size),
k_means(Map, Size, Objects, Clusters),
maplist(list_to_rtree(Map), Clusters, SubTrees),
maplist(rect(rtree_rect), SubTrees, Rects),
rect_union_list(Rects, Rect),
RTree = r_tree(Map, Rect, internal, SubTrees).
rtree_map(Tree, Map) :- arg(1, Tree, Map).
rtree_rect(Tree, Rect) :- arg(2, Tree, Rect).
%% rtree_insert(+Object, +RTreeIn, -RTreeOut) is det.
%
% Insert Object into RTreeIn.
rtree_insert(Object, RTree0, RTree) :-
Map = _:_,
empty_rtree(Map, RTree0), !, % only the root can be empty
rect(Map, Object, Rect),
RTree = r_tree(Map, Rect, leaf, [Object]).
rtree_insert(Object, RTree0, RTree) :-
rtree_map(RTree0, Map),
rect(Map, Object, Rect),
rtree_insert(Rect, Object, RTree0, NewTrees),
( NewTrees = [One]
-> RTree = One
; maplist(rect(rtree_rect), NewTrees, NewRects),
rect_union_list(NewRects, Union),
RTree = r_tree(Map, Union, internal, NewTrees)
).
rtree_insert(Rect, Obj, RTree0, RTrees) :-
rtree_insert_(Rect, Obj, RTree0, RTrees), !.
rtree_insert(Rect, Obj, RTree0, RTrees) :-
gtrace,
rtree_insert_(Rect, Obj, RTree0, RTrees).
rtree_insert_(Rect, Object,
r_tree(Map, Rect0, leaf, Children0),
NewTrees) :- !,
Children1 = [Object|Children0],
length(Children0, Count),
rtree_setting(split_node_size, NodeSize),
( Count > NodeSize
-> k_means(Map, 2, Children1, Clusters),
maplist(new_node(leaf, Map), Clusters, NewTrees)
; rect_union(Rect0, Rect, Rect1),
NewTrees = [r_tree(Map, Rect1, leaf, Children1)]
).
rtree_insert_(Rect, Object,
r_tree(Map, Rect0, internal, Children0),
NewTrees) :-
primary_child(Rect, Child, Children0),
rtree_insert(Rect, Object, Child, New),
selectchk(Child, Children0, Children1),
rtree_insert_list(Children1, Rect, Object, Children2),
append(New, Children2, Children3),
length(Children3, Count),
rtree_setting(split_node_size, NodeSize),
( Count > NodeSize
-> k_means(rtree_rect, 2, Children3, Clusters),
maplist(new_node(internal, Map), Clusters, NewTrees)
; rect_union(Rect, Rect0, Union),
NewTrees = [r_tree(Map, Union, internal, Children3)]
).
new_node(internal, Map, SubTrees, r_tree(Map, Rect, internal, SubTrees)) :-
assertion(SubTrees \== []),
maplist(rect(rtree_rect), SubTrees, Rects),
rect_union_list(Rects, Rect).
new_node(leaf, Map, LeafNodes, r_tree(Map, Rect, leaf, LeafNodes)) :-
maplist(rect(Map), LeafNodes, Rects),
rect_union_list(Rects, Rect).
rtree_insert_list([], _, _, []).
rtree_insert_list([H|T0], Rect, Object, NewList) :-
rtree_rect(H, Rect0),
rect_intersects(Rect0, Rect), !,
rtree_insert(Rect, Object, H, HList),
append(HList, Rest, NewList),
rtree_insert_list(T0, Rect, Object, Rest).
rtree_insert_list([H|T0], Rect, Object, [H|T]) :-
rtree_insert_list(T0, Rect, Object, T).
primary_child(Rect, Child, [Child0|Children]) :-
k_dist(Rect, Child0, DBest),
primary_child(Children, Rect, Child0, Child, DBest).
primary_child([], _, Child, Child, _).
primary_child([H|T], Rect, Child0, Child, DBest) :-
k_dist(Rect, H, Dist),
( Dist < DBest
-> primary_child(T, Rect, H, Child, Dist)
; primary_child(T, Rect, Child0, Child, DBest)
).
k_dist(Rect, Tree, D) :-
rtree_rect(Tree, RectT),
rect_union(Rect, RectT, rect(Xs,Ys, Xe,Ye)),
D is sqrt((Xe-Xs)**2+(Ye-Ys)**2).
%% rtree_inside(+RTree, +Rect, -Object) is nondet.
%
% True when Object is an object that intersects with Rect
rtree_inside(r_tree(Map, Rect, Type, Children), Target, Object) :-
rect_intersects(Rect, Target),
( Type == internal
-> member(Child, Children),
rtree_inside(Child, Target, Object)
; member(Object, Children),
rect(Map, Object, ObjRect),
rect_intersects(Target, ObjRect)
).
%% rtree_intersects(+RTree, +Obj1, -Object) is nondet.
%
% True when Object is an object that intersects with Obj1
rtree_intersects(RTree, TargetObj, Object) :-
rtree_map(RTree, Map),
rect(Map, TargetObj, Rect),
rtree_inside(RTree, Rect, Object).
%% rect_union_list(+RectList, -Union)
rect_union_list([H|T], Union) :- !,
rect_union_list(T, H, Union).
rect_union_list([], rect(0,0,0,0)).
rect_union_list([], Union, Union).
rect_union_list([H|T], Union0, Union) :-
rect_union(H, Union0, Union1),
rect_union_list(T, Union1, Union).
rect_union(rect(Xas,Yas, Xae,Yae),
rect(Xbs,Ybs, Xbe,Ybe),
rect(Xs,Ys, Xe,Ye)) :-
Xs is min(Xas,Xbs),
Xe is max(Xae,Xbe),
Ys is min(Yas,Ybs),
Ye is max(Yae,Ybe).
%% rect_intersects(+Rect1, +Rect2) is semidet.
%
% True when Rect1 and Rect2 have a non-empty intersection.
rect_intersects(Rect1, Rect2) :-
rect_intersection(Rect1, Rect2, _).
rect_intersection(rect(SX1,SY1, EX1,EY1),
rect(SX2,SY2, EX2,EY2),
rect(SX,SY, EX,EY)) :-
range_intersect(SX1,EX1, SX2,EX2, SX,EX),
range_intersect(SY1,EY1, SY2,EY2, SY,EY).
range_intersect(S1,E1, S2,E2, S,E) :-
S is max(S1,S2),
E is min(E1,E2),
S =< E.
%% rect(:Map, +Object, -Rect)
%
% Rect is the bounding box of Object. Rect is a term
% rect(Xs,Ys, Xe,Ye).
rect(Map, O, Rect) :-
call(Map, O, Rect).