-
Notifications
You must be signed in to change notification settings - Fork 0
/
octree.js
161 lines (143 loc) · 4.94 KB
/
octree.js
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
function myOctree(position, size, accuracy) {
this.maxDistance = Math.max(size[0], Math.max(size[1], size[1]));
this.accuracy = 0;
this.root = new myOctree.Cell(this, position, size, 0);
}
myOctree.MaxLevel = 2;
myOctree.prototype.add = function (p, data) {
this.root.add(p, data);
};
myOctree.prototype.is_part_of = function (x) {
return this.root.is_part_of(x);
};
myOctree.prototype.point_search = function (p, r, options) {
options = options || { };
var result = { points: [], data: [] };
this.root.point_search(p, r, result, options);
return result;
};
myOctree.prototype.level = function (cell, level, result) {
if (typeof level == 'undefined') {
level = cell;
cell = this.root;
}
result = result || [];
if (cell.level == level) {
if (cell.points.length > 0) {
result.push(cell);
}
return result;
} else {
cell.children.forEach(function (child) {
this.level(child, level, result);
}.bind(this));
return result;
}
};
myOctree.Cell = function (tree, position, size, level) {
this.tree = tree;
this.position = position;
this.size = size;
this.level = level;
this.points = [];
this.data = [];
// this.temp = new Vec3(); //temp vector for distance calculation
this.children = [];
};
myOctree.Cell.prototype.is_part_of = function (p) {
if (!this.contains(p))
return null;
if (this.children.length > 0) {
for (var i = 0; i < this.children.length; i++) {
var duplicate = this.children[i].is_part_of(p);
if (duplicate) {
return duplicate;
}
}
return null;
} else {
var minDistSqrt = this.tree.accuracy * this.tree.accuracy;
for (var i = 0; i < this.points.length; i++) {
var o = this.points[i];
var distSq = p.squareDistance(o);
if (distSq <= minDistSqrt) {
return o;
}
}
return null;
}
};
myOctree.Cell.prototype.add = function (p, data) {
this.points.push(p);
this.data.push(data);
if (this.children.length > 0) {
this.addToChildren(p, data);
} else {
if (this.points.length > 1 && this.level < myOctree.MaxLevel) {
this.split();
}
}
};
myOctree.Cell.prototype.addToChildren = function (p, data) {
for (var i = 0; i < this.children.length; i++) {
if (this.children[i].contains(p)) {
this.children[i].add(p, data);
break;
}
}
};
myOctree.Cell.prototype.contains = function (p) {
return p[0] >= this.position[0] - this.tree.accuracy
&& p[1] >= this.position[1] - this.tree.accuracy
&& p[2] >= this.position[2] - this.tree.accuracy
&& p[0] < this.position[0] + this.size[0] + this.tree.accuracy
&& p[1] < this.position[1] + this.size[1] + this.tree.accuracy
&& p[2] < this.position[2] + this.size[2] + this.tree.accuracy;
};
myOctree.Cell.prototype.split = function () {
var x = this.position[0];
var y = this.position[1];
var z = this.position[2];
var w2 = this.size[0] / 2;
var h2 = this.size[1] / 2;
var d2 = this.size[2] / 2;
this.children.push(new myOctree.Cell(this.tree, Vec.of(x, y, z), Vec.of(w2, h2, d2), this.level + 1));
this.children.push(new myOctree.Cell(this.tree, Vec.of(x + w2, y, z), Vec.of(w2, h2, d2), this.level + 1));
this.children.push(new myOctree.Cell(this.tree, Vec.of(x, y, z + d2), Vec.of(w2, h2, d2), this.level + 1));
this.children.push(new myOctree.Cell(this.tree, Vec.of(x + w2, y, z + d2), Vec.of(w2, h2, d2), this.level + 1));
this.children.push(new myOctree.Cell(this.tree, Vec.of(x, y + h2, z), Vec.of(w2, h2, d2), this.level + 1));
this.children.push(new myOctree.Cell(this.tree, Vec.of(x + w2, y + h2, z), Vec.of(w2, h2, d2), this.level + 1));
this.children.push(new myOctree.Cell(this.tree, Vec.of(x, y + h2, z + d2), Vec.of(w2, h2, d2), this.level + 1));
this.children.push(new myOctree.Cell(this.tree, Vec.of(x + w2, y + h2, z + d2), Vec.of(w2, h2, d2), this.level + 1));
for (var i = 0; i < this.points.length; i++) {
this.addToChildren(this.points[i], this.data[i]);
}
};
myOctree.Cell.prototype.point_search = function (p, r, result, options) {
if (this.points.length > 0 && this.children.length == 0) {
for (var i = 0; i < this.points.length; i++) {
var dist = this.points[i].minus(p).norm();
if (dist <= r) {
if (dist == 0 && options.notSelf)
continue;
result.points.push(this.points[i]);
if (options.includeData) result.data.push(this.data[i]);
}
}
}
var children = this.children;
if (children.length > 0) {
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.points.length > 0) {
if (p[0] < child.position[0] - r || p[0] > child.position[0] + child.size[0] + r ||
p[1] < child.position[1] - r || p[1] > child.position[1] + child.size[1] + r ||
p[2] < child.position[2] - r || p[2] > child.position[2] + child.size[2] + r
) {
continue;
}
child.point_search(p, r, result, options);
}
}
}
};