|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hexogen\KDTree; |
|
4
|
|
|
|
|
5
|
|
|
class FSKDTree implements KDTreeInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var NodeInterface |
|
9
|
|
|
*/ |
|
10
|
|
|
private $root; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private $maxBoundary; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $minBoundary; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var int number of items in the tree |
|
24
|
|
|
*/ |
|
25
|
|
|
private $length; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $dimensions; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var |
|
34
|
|
|
*/ |
|
35
|
|
|
private $handler; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ItemFactoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $factory; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* FSKDTree constructor. |
|
44
|
|
|
* @param $path |
|
45
|
|
|
* @param ItemFactoryInterface $factory |
|
46
|
|
|
*/ |
|
47
|
48 |
|
public function __construct($path, ItemFactoryInterface $factory) |
|
48
|
|
|
{ |
|
49
|
48 |
|
$this->factory = $factory; |
|
50
|
48 |
|
$this->handler = fopen($path, 'rb'); |
|
51
|
48 |
|
$this->readInitData(); |
|
52
|
48 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* FSKDTree destructor |
|
56
|
|
|
*/ |
|
57
|
48 |
|
public function __destruct() |
|
58
|
|
|
{ |
|
59
|
48 |
|
fclose($this->handler); |
|
60
|
48 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return int |
|
64
|
|
|
*/ |
|
65
|
12 |
|
public function getItemCount(): int |
|
66
|
|
|
{ |
|
67
|
12 |
|
return $this->length; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return NodeInterface |
|
72
|
|
|
*/ |
|
73
|
12 |
|
public function getRoot() |
|
74
|
|
|
{ |
|
75
|
12 |
|
return $this->root; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
12 |
|
public function getMinBoundary(): array |
|
82
|
|
|
{ |
|
83
|
12 |
|
return $this->minBoundary; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
12 |
|
public function getMaxBoundary(): array |
|
90
|
|
|
{ |
|
91
|
12 |
|
return $this->maxBoundary; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
12 |
|
public function getDimensionCount(): int |
|
98
|
|
|
{ |
|
99
|
12 |
|
return $this->dimensions; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Read binary data and convert it to an object |
|
104
|
|
|
*/ |
|
105
|
48 |
|
private function readInitData() |
|
106
|
|
|
{ |
|
107
|
48 |
|
$this->readDimensionsCount(); |
|
108
|
48 |
|
$this->readItemsCount(); |
|
109
|
48 |
|
$this->readUpperBound(); |
|
110
|
48 |
|
$this->readLowerBound(); |
|
111
|
48 |
|
$this->setRoot(); |
|
112
|
48 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* read num of dimensions in array |
|
116
|
|
|
*/ |
|
117
|
48 |
|
private function readDimensionsCount() |
|
118
|
|
|
{ |
|
119
|
48 |
|
$binData = fread($this->handler, 4); |
|
120
|
48 |
|
$this->dimensions = unpack('V', $binData)[1]; |
|
121
|
48 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* read number of items in the tree |
|
125
|
|
|
*/ |
|
126
|
48 |
|
private function readItemsCount() |
|
127
|
|
|
{ |
|
128
|
48 |
|
$binData = fread($this->handler, 4); |
|
129
|
48 |
|
$this->length = unpack('V', $binData)[1]; |
|
130
|
48 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* read upper boundary point |
|
134
|
|
|
*/ |
|
135
|
48 |
|
private function readUpperBound() |
|
136
|
|
|
{ |
|
137
|
48 |
|
$this->maxBoundary = $this->readPoint(); |
|
138
|
48 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* read lower boundary point |
|
142
|
|
|
*/ |
|
143
|
48 |
|
private function readLowerBound() |
|
144
|
|
|
{ |
|
145
|
48 |
|
$this->minBoundary = $this->readPoint(); |
|
146
|
48 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* set tree root |
|
150
|
|
|
*/ |
|
151
|
48 |
|
private function setRoot() |
|
152
|
|
|
{ |
|
153
|
48 |
|
if ($this->length == 0) { |
|
154
|
12 |
|
return; |
|
155
|
|
|
} |
|
156
|
36 |
|
$position = ftell($this->handler); |
|
157
|
36 |
|
$this->root = new FSNode($this->factory, $this->handler, $position, $this->dimensions); |
|
158
|
36 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Read point |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
48 |
|
private function readPoint() |
|
165
|
|
|
{ |
|
166
|
48 |
|
$dataLength = 8 * $this->dimensions; |
|
167
|
48 |
|
$binData = fread($this->handler, $dataLength); |
|
168
|
48 |
|
$dValues = unpack('d' . $this->dimensions, $binData); |
|
169
|
48 |
|
return array_values($dValues); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|