Skip to content

Commit

Permalink
Organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sopiro committed Mar 24, 2024
1 parent 75e4c22 commit 5db506c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 81 deletions.
66 changes: 33 additions & 33 deletions out/aabbtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,39 @@ export class AABBTree {
ancestor = ancestor.parent;
}
}
removeLeaf(leaf) {
let parent = leaf.parent;
// node is root
if (parent == undefined) {
assert(this.root == leaf);
this.root = undefined;
return;
}
let grandParent = parent.parent;
let sibling = parent.child1 == leaf ? parent.child2 : parent.child1;
// node has grandparent
if (grandParent != undefined) {
sibling.parent = grandParent;
if (grandParent.child1 == parent) {
grandParent.child1 = sibling;
}
else {
grandParent.child2 = sibling;
}
let ancestor = grandParent;
while (ancestor != undefined) {
let child1 = ancestor.child1;
let child2 = ancestor.child2;
ancestor.aabb = union(child1.aabb, child2.aabb);
this.rotate(ancestor);
ancestor = ancestor.parent;
}
}
else {
this.root = sibling;
sibling.parent = undefined;
}
}
rotate(node) {
if (node.isLeaf) {
return;
Expand Down Expand Up @@ -214,39 +247,6 @@ export class AABBTree {
}
node1.parent = parent2;
}
removeLeaf(leaf) {
let parent = leaf.parent;
// node is root
if (parent == undefined) {
assert(this.root == leaf);
this.root = undefined;
return;
}
let grandParent = parent.parent;
let sibling = parent.child1 == leaf ? parent.child2 : parent.child1;
// node has grandparent
if (grandParent != undefined) {
sibling.parent = grandParent;
if (grandParent.child1 == parent) {
grandParent.child1 = sibling;
}
else {
grandParent.child2 = sibling;
}
let ancestor = grandParent;
while (ancestor != undefined) {
let child1 = ancestor.child1;
let child2 = ancestor.child2;
ancestor.aabb = union(child1.aabb, child2.aabb);
this.rotate(ancestor);
ancestor = ancestor.parent;
}
}
else {
this.root = sibling;
sibling.parent = undefined;
}
}
queryPoint(point) {
let res = [];
if (this.root == undefined) {
Expand Down
96 changes: 48 additions & 48 deletions src/aabbtree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,54 @@ export class AABBTree
}
}

private removeLeaf(leaf: Node): void
{
let parent = leaf.parent;

// node is root
if (parent == undefined)
{
assert(this.root == leaf);
this.root = undefined;
return;
}

let grandParent = parent.parent;
let sibling = parent.child1 == leaf ? parent.child2! : parent.child1!;

// node has grandparent
if (grandParent != undefined)
{
sibling.parent = grandParent;
if (grandParent.child1 == parent)
{
grandParent.child1 = sibling;
}
else
{
grandParent.child2 = sibling;
}

let ancestor: Node | undefined = grandParent;
while (ancestor != undefined)
{
let child1 = ancestor.child1!;
let child2 = ancestor.child2!;

ancestor.aabb = union(child1.aabb, child2.aabb);

this.rotate(ancestor);

ancestor = ancestor.parent;
}
}
else
{
this.root = sibling;
sibling.parent = undefined;
}
}

private rotate(node: Node): void
{
if (node.isLeaf)
Expand Down Expand Up @@ -315,54 +363,6 @@ export class AABBTree
node1.parent = parent2;
}

private removeLeaf(leaf: Node): void
{
let parent = leaf.parent;

// node is root
if (parent == undefined)
{
assert(this.root == leaf);
this.root = undefined;
return;
}

let grandParent = parent.parent;
let sibling = parent.child1 == leaf ? parent.child2! : parent.child1!;

// node has grandparent
if (grandParent != undefined)
{
sibling.parent = grandParent;
if (grandParent.child1 == parent)
{
grandParent.child1 = sibling;
}
else
{
grandParent.child2 = sibling;
}

let ancestor: Node | undefined = grandParent;
while (ancestor != undefined)
{
let child1 = ancestor.child1!;
let child2 = ancestor.child2!;

ancestor.aabb = union(child1.aabb, child2.aabb);

this.rotate(ancestor);

ancestor = ancestor.parent;
}
}
else
{
this.root = sibling;
sibling.parent = undefined;
}
}

queryPoint(point: Vector2): Node[]
{
let res: Node[] = [];
Expand Down

0 comments on commit 5db506c

Please sign in to comment.