mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2025-10-30 08:11:25 +00:00
removed narrowphase
This commit is contained in:
parent
c97b36c432
commit
bafb7d353d
4 changed files with 0 additions and 280 deletions
|
|
@ -1,7 +0,0 @@
|
|||
package octreenarrowphase;
|
||||
|
||||
import polygonal.ds.Prioritizable;
|
||||
|
||||
interface IOctreeNode<T> extends Prioritizable {
|
||||
function getNodeType():Int;
|
||||
}
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
package octreenarrowphase;
|
||||
|
||||
import polygonal.ds.PriorityQueue;
|
||||
import dif.math.Box3F;
|
||||
import dif.math.Point3F;
|
||||
|
||||
class Octree<T> {
|
||||
var root:OctreeNode<T>;
|
||||
|
||||
public function new(pts:Array<OctreePoint<T>>, binPoints:Int = 8) {
|
||||
var pos = pts;
|
||||
|
||||
var min = new Point3F();
|
||||
var max = new Point3F();
|
||||
|
||||
// Generate the bounding box
|
||||
for (index => op in pos) {
|
||||
var p = op.point;
|
||||
if (p.x < min.x)
|
||||
min.x = p.x;
|
||||
if (p.y < min.y)
|
||||
min.y = p.y;
|
||||
if (p.z < min.z)
|
||||
min.z = p.z;
|
||||
|
||||
if (p.x > max.x)
|
||||
max.x = p.x;
|
||||
if (p.y > max.y)
|
||||
max.y = p.y;
|
||||
if (p.z > max.z)
|
||||
max.z = p.z;
|
||||
}
|
||||
|
||||
root = new OctreeNode();
|
||||
root.box = new Box3F(min.x, min.y, min.z, max.x, max.y, max.z);
|
||||
|
||||
// We use the insert method because its much faster doing this way
|
||||
for (index => pt in pts)
|
||||
root.insert(pt.point, pt.value);
|
||||
}
|
||||
|
||||
public function find(pt:Point3F)
|
||||
return root.find(pt);
|
||||
|
||||
public function remove(pt:Point3F)
|
||||
return root.remove(pt);
|
||||
|
||||
public function insert(pt:Point3F, value:T)
|
||||
return root.insert(pt, value);
|
||||
|
||||
public function knn(point:Point3F, number:Int) {
|
||||
var queue = new PriorityQueue<IOctreeNode<T>>();
|
||||
root.priority = cast(-root.box.getClosestPoint(point).sub(point).lengthSq());
|
||||
queue.enqueue(root);
|
||||
|
||||
var l = new Array<OctreePoint<T>>();
|
||||
|
||||
while (l.length < number && queue.size > 0) {
|
||||
var node = queue.dequeue();
|
||||
|
||||
switch (node.getNodeType()) {
|
||||
case 1:
|
||||
var leaf:OctreeNode<T> = cast node;
|
||||
for (index => pt in leaf.points) {
|
||||
pt.priority = cast(-pt.point.sub(point).lengthSq());
|
||||
queue.enqueue(pt);
|
||||
}
|
||||
|
||||
case 0:
|
||||
var pt:OctreePoint<T> = cast node;
|
||||
l.push(pt);
|
||||
|
||||
case 2:
|
||||
var n:OctreeNode<T> = cast node;
|
||||
for (subnode in n.nodes) {
|
||||
subnode.priority = cast(-subnode.box.getClosestPoint(point).sub(point).lengthSq());
|
||||
queue.enqueue(subnode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return l;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,168 +0,0 @@
|
|||
package octreenarrowphase;
|
||||
|
||||
import dif.math.Box3F;
|
||||
import dif.math.Point3F;
|
||||
|
||||
class OctreeNode<T> implements IOctreeNode<T> {
|
||||
public var nodes:Array<OctreeNode<T>>;
|
||||
|
||||
public var priority:Int;
|
||||
public var position:Int;
|
||||
|
||||
var isLeaf:Bool;
|
||||
|
||||
public var points:Array<OctreePoint<T>>;
|
||||
|
||||
var center:Point3F;
|
||||
|
||||
public var box:Box3F;
|
||||
|
||||
public function new() {
|
||||
this.isLeaf = true;
|
||||
this.points = new Array<OctreePoint<T>>();
|
||||
}
|
||||
|
||||
public function getCount() {
|
||||
if (this.isLeaf) {
|
||||
return this.points.length;
|
||||
} else {
|
||||
var res = 0;
|
||||
for (index => value in nodes) {
|
||||
res += value.getCount();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
function getIsEmpty() {
|
||||
if (this.isLeaf)
|
||||
return this.getCount() == 0;
|
||||
else {
|
||||
for (index => value in nodes) {
|
||||
if (!value.getIsEmpty())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function find(pt:Point3F) {
|
||||
if (this.isLeaf) {
|
||||
for (index => value in this.points) {
|
||||
if (value.point.equal(pt))
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
var msk = 0;
|
||||
msk |= (pt.x - center.x) < 0 ? 1 : 0;
|
||||
msk |= (pt.y - center.y) < 0 ? 2 : 0;
|
||||
msk |= (pt.z - center.z) < 0 ? 4 : 0;
|
||||
|
||||
return nodes[msk].find(pt);
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(pt:Point3F) {
|
||||
if (this.isLeaf) {
|
||||
var found = false;
|
||||
var idx = -1;
|
||||
for (index => value in this.points) {
|
||||
if (value.point.equal(pt)) {
|
||||
found = true;
|
||||
idx = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
return this.points.remove(this.points[idx]);
|
||||
} else
|
||||
return false;
|
||||
} else {
|
||||
var msk = 0;
|
||||
msk |= (pt.x - center.x) < 0 ? 1 : 0;
|
||||
msk |= (pt.y - center.y) < 0 ? 2 : 0;
|
||||
msk |= (pt.z - center.z) < 0 ? 4 : 0;
|
||||
|
||||
var ret = nodes[msk].remove(pt);
|
||||
this.merge();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public function insert(pt:Point3F, value:T) {
|
||||
if (this.isLeaf) {
|
||||
this.points.push(new OctreePoint(pt, value));
|
||||
subdivide();
|
||||
} else {
|
||||
var msk = 0;
|
||||
msk |= (pt.x - center.x) < 0 ? 1 : 0;
|
||||
msk |= (pt.y - center.y) < 0 ? 2 : 0;
|
||||
msk |= (pt.z - center.z) < 0 ? 4 : 0;
|
||||
nodes[msk].insert(pt, value);
|
||||
}
|
||||
}
|
||||
|
||||
function subdivide(binPoints:Int = 8) {
|
||||
var min = new Point3F(box.minX, box.minY, box.minZ);
|
||||
var max = new Point3F(box.maxX, box.maxY, box.maxZ);
|
||||
center = min.add(max).scalarDiv(2);
|
||||
|
||||
if (points.length > binPoints) {
|
||||
isLeaf = false;
|
||||
|
||||
var size = max.sub(min);
|
||||
|
||||
nodes = new Array<OctreeNode<T>>();
|
||||
for (i in 0...8) {
|
||||
nodes.push(new OctreeNode<T>());
|
||||
}
|
||||
|
||||
nodes[0].box = new Box3F(center.x, center.y, center.z, max.x, max.y, max.z);
|
||||
nodes[1].box = new Box3F(center.x - (size.x / 2), center.y, center.z, max.x - (size.x / 2), max.y, max.z);
|
||||
nodes[2].box = new Box3F(center.x, center.y - (size.y / 2), center.z, max.x, max.y - (size.y / 2), max.z);
|
||||
nodes[3].box = new Box3F(center.x - (size.x / 2), center.y - (size.y / 2), center.z, max.x - (size.x / 2), max.y - (size.y / 2), max.z);
|
||||
nodes[4].box = new Box3F(center.x, center.y, center.z - (size.z / 2), max.x, max.y, max.z - (size.z / 2));
|
||||
nodes[5].box = new Box3F(center.x - (size.x / 2), center.y, center.z - (size.z / 2), max.x - (size.x / 2), max.y, max.z - (size.z / 2));
|
||||
nodes[6].box = new Box3F(center.x, center.y - (size.y / 2), center.z - (size.z / 2), max.x, max.y - (size.y / 2), max.z - (size.z / 2));
|
||||
nodes[7].box = new Box3F(min.x, min.y, min.z, max.x, max.y, max.z);
|
||||
|
||||
for (index => pt in points) {
|
||||
var msk = 0;
|
||||
msk |= (pt.point.x - center.x) < 0 ? 1 : 0;
|
||||
msk |= (pt.point.y - center.y) < 0 ? 2 : 0;
|
||||
msk |= (pt.point.z - center.z) < 0 ? 4 : 0;
|
||||
|
||||
if (!nodes[msk].find(pt.point))
|
||||
nodes[msk].points.push(new OctreePoint(pt.point, pt.value));
|
||||
}
|
||||
|
||||
points = null;
|
||||
|
||||
for (index => value in nodes) {
|
||||
value.subdivide(binPoints);
|
||||
}
|
||||
} else {
|
||||
isLeaf = true;
|
||||
}
|
||||
}
|
||||
|
||||
function merge() {
|
||||
if (this.isLeaf) {
|
||||
return;
|
||||
} else {
|
||||
if (this.getIsEmpty()) {
|
||||
this.isLeaf = true;
|
||||
this.nodes = null;
|
||||
this.points = new Array<OctreePoint<T>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getNodeType() {
|
||||
if (this.isLeaf)
|
||||
return 1;
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
package octreenarrowphase;
|
||||
|
||||
import dif.math.Point3F;
|
||||
|
||||
class OctreePoint<T> implements IOctreeNode<T> {
|
||||
public var point:Point3F;
|
||||
|
||||
public var priority:Int;
|
||||
public var position:Int;
|
||||
|
||||
public var value:T;
|
||||
|
||||
public function new(point:Point3F, value:T) {
|
||||
this.point = point;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public function getNodeType() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue