-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cpp
executable file
·71 lines (63 loc) · 1.88 KB
/
Node.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
#include "Node.hpp"
Node::Node() {
s = -1;
point = nullptr;
parent = nullptr;
is_aggregate = false;
xmin = -1; xmax = -1;
ymin = -1; ymax = -1;
zmin = -1; zmax = -1;
for (int i=0; i<8; i++)
children[i] = nullptr;
}
Node::Node(Node* parent0, double xmin0, double ymin0, double zmin0, double xmax0, double ymax0, double zmax0) {
point = nullptr;
parent = parent0;
is_aggregate = false;
xmin = xmin0; xmax = xmax0;
ymin = ymin0; ymax = ymax0;
zmin = zmin0; zmax = zmax0;
s = xmax-xmin;
if (ymax-ymin > s) s = ymax-ymin;
if (zmax-zmin > s) s = zmax-zmin;
for (int i=0; i<8; i++)
children[i] = nullptr;
}
Node::~Node() {
if (is_aggregate) {
delete point;
for (int i=0; i<8; i++) {
if (children[i] != nullptr)
delete children[i];
}
}
}
bool Node::contains(Point* p) {
return p->x >= xmin && p->x < xmax && p->y >= ymin && p->y < ymax && p->z >= zmin && p->z < zmax;
}
void Node::split(Point* new_point) {
is_aggregate = true;
int i, j, k, index;
double dx = (xmax-xmin)/2.0;
double dy = (ymax-ymin)/2.0;
double dz = (zmax-zmin)/2.0;
for (k=0; k<2; k++)
for (j=0; j<2; j++)
for (i=0; i<2; i++)
{
index = 4*k + 2*j + i;
children[index] = new Node(this, xmin+i*dx, ymin+j*dy, zmin+k*dz, xmin+(i+1)*dx, ymin+(j+1)*dy, zmin+(k+1)*dz );
children[index]->contains(point);
if ( children[index]->contains(point) )
children[index]->insert_point(point);
if ( children[index]->contains(new_point) )
children[index]->insert_point(new_point);
}
point = nullptr;
}
void Node::insert_point(Point* p) {
if (point == nullptr)
point = p;
else
split(p);
}