From 5db506cacca4e4c3636835b0827369b38f401bd6 Mon Sep 17 00:00:00 2001 From: sopiro Date: Mon, 25 Mar 2024 07:41:07 +0900 Subject: [PATCH] Organize code --- out/aabbtree.js | 66 +++++++++++++++++----------------- src/aabbtree.ts | 96 ++++++++++++++++++++++++------------------------- 2 files changed, 81 insertions(+), 81 deletions(-) diff --git a/out/aabbtree.js b/out/aabbtree.js index 3796282..912f9d0 100644 --- a/out/aabbtree.js +++ b/out/aabbtree.js @@ -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; @@ -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) { diff --git a/src/aabbtree.ts b/src/aabbtree.ts index 9042958..c4084e5 100644 --- a/src/aabbtree.ts +++ b/src/aabbtree.ts @@ -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) @@ -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[] = [];