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 {
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. */
public var objectToNode:Map<IOctreeObject, OctreeNode>;

View file

@ -34,11 +34,13 @@ class OctreeNode implements IOctreeElement {
public function insert(object:IOctreeObject) {
this.count++;
if (this.octants != null) {
for (i in 0...8) {
var octant = this.octants[i];
if (octant.largerThan(object) && octant.containsCenter(object)) {
octant.insert(object);
return;
if (this.octants[0].largerThan(object)) {
for (i in 0...8) {
var octant = this.octants[i];
if (octant.containsCenter(object)) {
octant.insert(object);
return;
}
}
}
this.objects.push(object);
@ -55,13 +57,15 @@ class OctreeNode implements IOctreeElement {
return;
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.
for (object in this.objects) {
for (j in 0...8) {
var octant = this.octants[j];
if (octant.largerThan(object) && octant.containsCenter(object)) {
octant.insert(object);
this.objects.remove(object);
break;
if (this.octants[0].largerThan(object)) {
for (i in 0...8) {
var octant = this.octants[i];
if (octant.containsCenter(object)) {
octant.insert(object);
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() {
if (this.count > 8 || (this.octants == null))
return;

View file

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

View file

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