SpiecsEngine
 
Loading...
Searching...
No Matches

◆ insert_recursive()

template<uint32_t K>
void scl::kd_tree< K >::insert_recursive ( Node *& node,
std::shared_ptr< std::vector< item > > points,
int depth )
inlineprivate

Recursive function to insert a point into the kd_tree.

Parameters
[in]noderecursive node.
[in]pointsInserted points in k d.
[in]depthrecursive depth.

Calculate current dimension (cd).

Sort points in cd.

Get Center iterator.

Base case: If node is null, create a new node.

Split points to left and right.

Insert in left and right.

Definition at line 241 of file KDTree.h.

246 {
247 if (points->size() == 0) return;
248
252 int cd = depth % K;
253
257 std::multimap<float, uint32_t> sorted;
258 if (points->size() <= 512)
259 {
260 for (int i = 0; i < points->size(); i++)
261 {
262 sorted.emplace((*points)[i][cd], i);
263 }
264 }
265 else
266 {
267 for (int i = 0; i < 512; i++)
268 {
269 uint32_t index = static_cast<uint32_t>((points->size() - 1) * std::rand() / static_cast<float>(RAND_MAX));
270 sorted.emplace((*points)[index][cd], index);
271 }
272 }
273
277 auto centerit = sorted.begin();
278 for (int i = 0; i < std::floor(sorted.size() * 0.5); i++) ++centerit;
279
283 if (node == nullptr)
284 {
285 node = new Node((*points)[centerit->second]);
286 ++m_Size;
287 }
288 else
289 {
290 SPICES_CORE_ERROR("Cannot insert a KDTree Node which is not empty.");
291 }
292
296 std::shared_ptr<std::vector<item>> leftPoints = std::make_shared<std::vector<item>>();
297 std::shared_ptr<std::vector<item>> rightPoints = std::make_shared<std::vector<item>>();
298
299 for (int i = 0; i < points->size(); i++)
300 {
301 if (i == centerit->second) continue;
302 else if ((*points)[i][cd] <= centerit->first)
303 {
304 leftPoints->push_back((*points)[i]);
305 }
306 else
307 {
308 rightPoints->push_back((*points)[i]);
309 }
310 }
311
315 insert_recursive(node->m_Left, leftPoints, depth + 1);
316 insert_recursive(node->m_Right, rightPoints, depth + 1);
317 }
void insert_recursive(Node *&node, std::shared_ptr< std::vector< item > > points, int depth)
Recursive function to insert a point into the kd_tree.
Definition KDTree.h:241
std::atomic_size_t m_Size
Sizes of this kd tree.
Definition KDTree.h:81