Skylicht Engine
Loading...
Searching...
No Matches
CKDTree3f.h
1/*
2!@
3MIT License
4
5Copyright (c) 2025 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include <vector>
28
29namespace Skylicht
30{
39 {
40 public:
44 struct SKDNode
45 {
47 float Pos[3];
49 void* Data;
51 int Dir;
52
57 };
58
59 protected:
60 SKDNode* m_root;
61
62 private:
63 // Simple block allocator to reduce per-node new/delete cost
64 std::vector<SKDNode*> m_blocks;
65 SKDNode* m_currentBlock;
66 size_t m_blockIndex;
67 static constexpr size_t NODE_BLOCK_SIZE = 1024;
68
69 core::array<SKDNode*> m_stack;
70 public:
75
79 virtual ~CKDTree3f();
80
86 inline void insert(const core::vector3df& pos, void* data)
87 {
88 insert(pos.X, pos.Y, pos.Z, data);
89 }
90
98 void insert(float x, float y, float z, void* data);
99
105 void clear();
106
112 inline SKDNode* nearest(const core::vector3df& pos)
113 {
114 return nearest(pos.X, pos.Y, pos.Z);
115 }
116
124 SKDNode* nearest(float x, float y, float z);
125
131 SKDNode* nearest(const float* pos);
132
140 inline int nearestRange(const core::vector3df& pos, float range, core::array<SKDNode*>& outResults)
141 {
142 return nearestRange(&pos.X, range, outResults);
143 }
144
152 int nearestRange(const float* pos, float range, core::array<SKDNode*>& outResults);
153
154 private:
155
156 SKDNode* allocNode(float x, float y, float z, void* data, int dir);
157 };
158}
int nearestRange(const float *pos, float range, core::array< SKDNode * > &outResults)
Find all nodes within a radius of a point.
void clear()
Remove all nodes from the tree.
int nearestRange(const core::vector3df &pos, float range, core::array< SKDNode * > &outResults)
Find all nodes within a radius of a point.
Definition CKDTree3f.h:140
SKDNode * nearest(float x, float y, float z)
Find the node nearest to a point.
SKDNode * nearest(const core::vector3df &pos)
Find the node nearest to a point.
Definition CKDTree3f.h:112
void insert(const core::vector3df &pos, void *data)
Insert a point and payload into the tree.
Definition CKDTree3f.h:86
virtual ~CKDTree3f()
Destroy the KD-tree and release all node blocks.
void insert(float x, float y, float z, void *data)
Insert a point and payload into the tree.
CKDTree3f()
Construct an empty KD-tree.
SKDNode * nearest(const float *pos)
Find the node nearest to a point.
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
Node stored in the KD-tree.
Definition CKDTree3f.h:45
void * Data
User payload associated with the node.
Definition CKDTree3f.h:49
int Dir
Split axis used by this node: 0 = X, 1 = Y, 2 = Z.
Definition CKDTree3f.h:51
float Pos[3]
Position of the node in 3D space.
Definition CKDTree3f.h:47
SKDNode * Left
Child with smaller coordinate on Dir.
Definition CKDTree3f.h:54
SKDNode * Right
Child with greater or equal coordinate on Dir.
Definition CKDTree3f.h:56