improvise octree

This commit is contained in:
RandomityGuy 2022-07-29 21:55:01 +05:30
parent b9ca536672
commit 2603538dba
4 changed files with 51 additions and 41 deletions

View file

@ -6,6 +6,10 @@ import h3d.col.Bounds;
class Octree { class Octree {
public var root:OctreeNode; public var root:OctreeNode;
static var DEFAULT_ROOT_NODE_SIZE = 1;
static var MIN_DEPTH = -52; // Huge
static var MAX_DEPTH = 8;
/** A map of each object in the octree to the node that it's in. This accelerates removal drastically, as the lookup step can be skipped. */ /** A map of each object in the octree to the node that it's in. This accelerates removal drastically, as the lookup step can be skipped. */
public var objectToNode:Map<IOctreeObject, OctreeNode>; public var objectToNode:Map<IOctreeObject, OctreeNode>;

View file

@ -34,11 +34,13 @@ class OctreeNode implements IOctreeElement {
public function insert(object:IOctreeObject) { public function insert(object:IOctreeObject) {
this.count++; this.count++;
if (this.octants != null) { if (this.octants != null) {
for (i in 0...8) { if (this.octants[0].largerThan(object)) {
var octant = this.octants[i]; for (i in 0...8) {
if (octant.largerThan(object) && octant.containsCenter(object)) { var octant = this.octants[i];
octant.insert(object); if (octant.containsCenter(object)) {
return; octant.insert(object);
return;
}
} }
} }
this.objects.push(object); this.objects.push(object);
@ -55,13 +57,15 @@ class OctreeNode implements IOctreeElement {
return; return;
this.createOctants(); this.createOctants();
// Put the objects into the correct octants. Note that all objects that couldn't fit into any octant will remain in the set. // Put the objects into the correct octants. Note that all objects that couldn't fit into any octant will remain in the set.
for (object in this.objects) { for (object in this.objects) {
for (j in 0...8) { if (this.octants[0].largerThan(object)) {
var octant = this.octants[j]; for (i in 0...8) {
if (octant.largerThan(object) && octant.containsCenter(object)) { var octant = this.octants[i];
octant.insert(object); if (octant.containsCenter(object)) {
this.objects.remove(object); octant.insert(object);
break; this.objects.remove(object);
}
} }
} }
} }
@ -104,6 +108,25 @@ class OctreeNode implements IOctreeElement {
} }
} }
public function update(object:IOctreeObject) {
this.objects.remove(object);
var node = this;
while (node != null) {
node.count--;
node.merge();
if (node.largerThan(object) && node.containsCenter(object)) {
node.insert(object);
return true;
}
node = node.parent;
}
return false;
}
public function merge() { public function merge() {
if (this.count > 8 || (this.octants == null)) if (this.count > 8 || (this.octants == null))
return; return;

View file

@ -1,45 +1,32 @@
package octree; package octree;
class PriorityQueue<T> { class PriorityQueue<T> {
var first:PriorityQueueNode<T>; var queue:Array<PriorityQueueNode<T>>;
public var count:Int; public var count:Int;
public function new() { public function new() {
count = 0; count = 0;
first = null; queue = [];
} }
public function enqueue(val:T, priority:Float) { public function enqueue(val:T, priority:Float) {
var node = new PriorityQueueNode<T>(val, priority); var node = new PriorityQueueNode<T>(val, priority);
if (this.first == null) { if (this.queue == null) {
this.first = node; this.queue = [node];
} else { } else {
if (this.first.priority >= priority) { if (this.queue[0].priority >= priority) {
node.next = this.first; this.queue.insert(0, node);
this.first.prev = node;
this.first = node;
} else { } else {
var n = this.first; var insertIndex = 0;
var end = false; var end = false;
while (n.priority < node.priority) { while (insertIndex < this.queue.length) {
if (n.next == null) { if (this.queue[insertIndex].priority > node.priority) {
end = true;
break; break;
} }
n = n.next; insertIndex++;
}
if (!end) {
if (n.prev != null) {
n.prev.next = node;
node.prev = n.prev;
}
n.prev = node;
node.next = n;
} else {
n.next = node;
node.prev = n;
} }
this.queue.insert(insertIndex, node);
} }
} }
@ -47,10 +34,8 @@ class PriorityQueue<T> {
} }
public function dequeue() { public function dequeue() {
var ret = this.first; var ret = this.queue[0];
this.first = this.first.next; this.queue.splice(0, 1);
if (this.first != null)
this.first.prev = null;
count--; count--;
return ret.value; return ret.value;
} }

View file

@ -3,8 +3,6 @@ package octree;
class PriorityQueueNode<T> { class PriorityQueueNode<T> {
public var value:T; public var value:T;
public var priority:Float; public var priority:Float;
public var next:PriorityQueueNode<T>;
public var prev:PriorityQueueNode<T>;
public function new(value:T, priority:Float) { public function new(value:T, priority:Float) {
this.value = value; this.value = value;