This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctree.cpp
193 lines (193 loc) · 6.65 KB
/
Octree.cpp
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
#include <iostream>
#include <vector>
#include <cmath>
class Point {
public:
double x, y, z;
Point(double x, double y, double z) : x(x), y(y), z(z) {}
};
class OctreeNode {
public:
Point center;
double size;
std::vector<Point> points;
OctreeNode* children[8];
OctreeNode(const Point& center, double size) : center(center), size(size) {
for (int i = 0; i < 8; ++i) {
children[i] = nullptr;
}
}
~OctreeNode() {
for (int i = 0; i < 8; ++i) {
delete children[i];
}
}
};
class Octree {
public:
Octree(const Point& center, double size) : root(new OctreeNode(center, size)) {}
~Octree() {
clear();
}
void insert(const Point& point) {
insert(root, point);
}
bool search(const Point& point) const {
return search(root, point);
}
std::vector<Point> queryRange(const Point& center, double size) const {
std::vector<Point> result;
queryRange(root, center, size, result);
return result;
}
void inOrderTraversal() const {
inOrderTraversal(root);
std::cout << std::endl;
}
void preOrderTraversal() const {
preOrderTraversal(root);
std::cout << std::endl;
}
void postOrderTraversal() const {
postOrderTraversal(root);
std::cout << std::endl;
}
void clear() {
delete root;
root = new OctreeNode(Point(0, 0, 0), 1.0);
}
private:
OctreeNode* root;
void insert(OctreeNode* node, const Point& point) {
if (node->size == 0.0) {
return;
}
int octant = getOctant(node->center, point);
if (node->children[octant] == nullptr) {
double childSize = node->size / 2.0;
Point childCenter = getChildCenter(node->center, octant, childSize);
node->children[octant] = new OctreeNode(childCenter, childSize);
}
insert(node->children[octant], point);
}
bool search(OctreeNode* node, const Point& point) const {
if (node->size == 0.0) {
return false;
}
int octant = getOctant(node->center, point);
if (node->children[octant] == nullptr) {
return false;
}
if (node->children[octant]->size == 0.0) {
for (const Point& p : node->children[octant]->points) {
if (p.x == point.x && p.y == point.y && p.z == point.z) {
return true;
}
}
return false;
}
return search(node->children[octant], point);
}
void queryRange(OctreeNode* node, const Point& center, double size, std::vector<Point>& result) const {
if (node->size == 0.0) {
return;
}
int octant = getOctant(node->center, center);
if (node->children[octant] == nullptr) {
return;
}
if (node->children[octant]->size == 0.0) {
for (const Point& p : node->children[octant]->points) {
if (isInsideRange(p, center, size)) {
result.push_back(p);
}
}
return;
}
queryRange(node->children[octant], center, size, result);
for (int i = 0; i < 8; ++i) {
if (i != octant) {
queryRange(node->children[i], center, size, result);
}
}
}
void inOrderTraversal(OctreeNode* node) const {
if (node != nullptr) {
for (int i = 0; i < 8; ++i) {
inOrderTraversal(node->children[i]);
}
for (const Point& point : node->points) {
std::cout << "(" << point.x << ", " << point.y << ", " << point.z << ") ";
}
}
}
void preOrderTraversal(OctreeNode* node) const {
if (node != nullptr) {
for (const Point& point : node->points) {
std::cout << "(" << point.x << ", " << point.y << ", " << point.z << ") ";
}
for (int i = 0; i < 8; ++i) {
preOrderTraversal(node->children[i]);
}
}
}
void postOrderTraversal(OctreeNode* node) const {
if (node != nullptr) {
for (int i = 0; i < 8; ++i) {
postOrderTraversal(node->children[i]);
}
for (const Point& point : node->points) {
std::cout << "(" << point.x << ", " << point.y << ", " << point.z << ") ";
}
}
}
int getOctant(const Point& center, const Point& point) const {
int octant = 0;
if (point.x > center.x) octant |= 1;
if (point.y > center.y) octant |= 2;
if (point.z > center.z) octant |= 4;
return octant;
}
Point getChildCenter(const Point& center, int octant, double childSize) const {
double xOffset = (octant & 1) ? childSize : -childSize;
double yOffset = (octant & 2) ? childSize : -childSize;
double zOffset = (octant & 4) ? childSize : -childSize;
return Point(center.x + xOffset, center.y + yOffset, center.z + zOffset);
}
bool isInsideRange(const Point& point, const Point& center, double size) const {
double halfSize = size / 2.0;
return point.x >= center.x - halfSize && point.x <= center.x + halfSize &&
point.y >= center.y - halfSize && point.y <= center.y + halfSize &&
point.z >= center.z - halfSize && point.z <= center.z + halfSize;
}
};
int main() {
Octree octree(Point(0, 0, 0), 10.0);
octree.insert(Point(2, 2, 2));
octree.insert(Point(5, 4, 3));
octree.insert(Point(9, 6, 7));
octree.insert(Point(4, 7, 1));
octree.insert(Point(8, 1, 5));
std::cout << "In-Order Traversal: ";
octree.inOrderTraversal();
std::cout << "Pre-Order Traversal: ";
octree.preOrderTraversal();
std::cout << "Post-Order Traversal: ";
octree.postOrderTraversal();
Point searchPoint(4, 7, 1);
std::cout << "Search for point (" << searchPoint.x << ", " << searchPoint.y << ", " << searchPoint.z
<< "): " << (octree.search(searchPoint) ? "Found" : "Not Found") << std::endl;
Point queryCenter(5, 5, 5);
double querySize = 4.0;
std::vector<Point> pointsInRange = octree.queryRange(queryCenter, querySize);
std::cout << "Points in Range (" << queryCenter.x << ", " << queryCenter.y << ", " << queryCenter.z
<< ") with Size " << querySize << ": ";
for (const Point& point : pointsInRange) {
std::cout << "(" << point.x << ", " << point.y << ", " << point.z << ") ";
}
std::cout << std::endl;
octree.clear();
std::cout << "In-Order Traversal (After Clear): ";
octree.inOrderTraversal();
return 0;
}