Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions src/builder/hybridBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { BVHNode, FloatArray, FloatArrayType } from '../core/BVHNode.js';
import { areaBox, areaFromTwoBoxes, expandBoxByMargin, getLongestAxis, isBoxInsideBox, isExpanded, unionBox, unionBoxChanged } from '../utils/boxUtils.js';
import { SortedListPriority } from '../utils/sortedListPriority.js';
import { HeapItem, MinHeap } from '../utils/heap.js';
import { IBVHBuilder, onLeafCreationCallback } from './IBVHBuilder.js';

export class HybridBuilder<N = {}, L = {}> implements IBVHBuilder<N, L> {
public root: BVHNode<N, L> = null;
public readonly highPrecision: boolean;
protected _sortedList = new SortedListPriority();
protected _typeArray: FloatArrayType;
protected count = 0;

Expand Down Expand Up @@ -253,58 +252,64 @@ export class HybridBuilder<N = {}, L = {}> implements IBVHBuilder<N, L> {
return { parent, left: sibling, right: leaf, box: new this._typeArray(6) } as BVHNode<N, L>;
}

// TODO precalcola area nodo
protected findBestSibling(leafBox: FloatArray): BVHNode<N, L> {
const root = this.root;
if (root.object !== undefined) return root;

let bestNode = root;
let bestCost = areaFromTwoBoxes(leafBox, root.box);
const leafArea = areaBox(leafBox);
_findBestSibling({ node: root, value: bestCost - areaBox(root.box) });

if (root.object !== undefined) return root;

const sortedList = this._sortedList;
sortedList.clear();
let nodeObj = { node: root, inheritedCost: bestCost - areaBox(root.box) };
function _findBestSibling(nodeObj: HeapItem): void {
const minHeap = new MinHeap();

do {
const { node, inheritedCost } = nodeObj;
do {
if (minHeap.isFull()) {
_findBestSibling(nodeObj);
} else {
const { node, value: inheritedCost } = nodeObj;

if (leafArea + inheritedCost >= bestCost) break;
if (leafArea + inheritedCost >= bestCost) break;

const nodeL = node.left;
const nodeR = node.right;
const nodeL = node.left;
const nodeR = node.right;

const directCostL = areaFromTwoBoxes(leafBox, nodeL.box);
const currentCostL = directCostL + inheritedCost;
const inheritedCostL = currentCostL - areaBox(nodeL.box);
const directCostL = areaFromTwoBoxes(leafBox, nodeL.box);
const currentCostL = directCostL + inheritedCost;
const inheritedCostL = currentCostL - areaBox(nodeL.box);

const directCostR = areaFromTwoBoxes(leafBox, nodeR.box);
const currentCostR = directCostR + inheritedCost;
const inheritedCostR = currentCostR - areaBox(nodeR.box);
const directCostR = areaFromTwoBoxes(leafBox, nodeR.box);
const currentCostR = directCostR + inheritedCost;
const inheritedCostR = currentCostR - areaBox(nodeR.box);

if (currentCostL > currentCostR) {
if (bestCost > currentCostR) {
bestNode = nodeR;
bestCost = currentCostR;
}
} else if (bestCost > currentCostL) {
bestNode = nodeL;
bestCost = currentCostL;
}
if (currentCostL > currentCostR) {
if (bestCost > currentCostR) {
bestNode = nodeR as BVHNode<N, L>;
bestCost = currentCostR;
}
} else if (bestCost > currentCostL) {
bestNode = nodeL as BVHNode<N, L>;
bestCost = currentCostL;
}

if (inheritedCostR > inheritedCostL) {
if (leafArea + inheritedCostL >= bestCost) continue;
if (nodeL.object === undefined) sortedList.push({ node: nodeL, inheritedCost: inheritedCostL });
if (inheritedCostR > inheritedCostL) {
if (leafArea + inheritedCostL >= bestCost) continue;
if (nodeL.object === undefined) minHeap.add({ node: nodeL, value: inheritedCostL });

if (leafArea + inheritedCostR >= bestCost) continue;
if (nodeR.object === undefined) sortedList.push({ node: nodeR, inheritedCost: inheritedCostR });
} else {
if (leafArea + inheritedCostR >= bestCost) continue;
if (nodeR.object === undefined) sortedList.push({ node: nodeR, inheritedCost: inheritedCostR });
if (leafArea + inheritedCostR >= bestCost) continue;
if (nodeR.object === undefined) minHeap.add({ node: nodeR, value: inheritedCostR });
} else {
if (leafArea + inheritedCostR >= bestCost) continue;
if (nodeR.object === undefined) minHeap.add({ node: nodeR, value: inheritedCostR });

if (leafArea + inheritedCostL >= bestCost) continue;
if (nodeL.object === undefined) sortedList.push({ node: nodeL, inheritedCost: inheritedCostL });
}
} while ((nodeObj = sortedList.pop()));
if (leafArea + inheritedCostL >= bestCost) continue;
if (nodeL.object === undefined) minHeap.add({ node: nodeL, value: inheritedCostL });
}
}
} while ((nodeObj = minHeap.poll()));
}

return bestNode;
}
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export * from './core/BVHNode.js';
export * from './utils/boxUtils.js';
export * from './utils/conversionUtils.js';
export * from './utils/frustum.js';
export * from './utils/heap.js';
export * from './utils/inspector.js';
export * from './utils/intersectUtils.js';
export * from './utils/sortedListDesc.js';
export * from './utils/sortedListPriority.js';
69 changes: 69 additions & 0 deletions src/utils/heap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { BVHNode } from '../core/BVHNode.js';

export type HeapItem = { value: number; node: BVHNode<unknown, unknown> };

/**
* @reference https://github.com/zrwusa/data-structure-typed/blob/main/src/data-structures/heap/heap.ts
*/
export class MinHeap {
public maxSize = 16;
protected _elements: HeapItem[] = [];

public add(element: HeapItem): boolean {
this._elements.push(element);
return this._bubbleUp(this._elements.length - 1);
}

public poll(): HeapItem | undefined {
const elements = this._elements;
if (elements.length === 0) return;
const value = elements[0];
const last = elements.pop();
if (elements.length) {
elements[0] = last;
this._sinkDown(0, elements.length >> 1);
}
return value;
}

public isFull(): boolean {
return this._elements.length >= this.maxSize;
}

public clear(): void {
this._elements.length = 0;
}

protected _bubbleUp(index: number): boolean {
const elements = this._elements;
const element = elements[index];
while (index > 0) {
const parent = (index - 1) >> 1;
const parentItem = elements[parent];
if (parentItem.value <= element.value) break;
elements[index] = parentItem;
index = parent;
}
elements[index] = element;
return true;
}

protected _sinkDown(index: number, halfLength: number): boolean {
const elements = this._elements;
const element = elements[index];
while (index < halfLength) {
let left = (index << 1) | 1;
const right = left + 1;
let minItem = elements[left];
if (right < elements.length && minItem.value > elements[right].value) {
left = right;
minItem = elements[right];
}
if (minItem.value >= element.value) break;
elements[index] = minItem;
index = left;
}
elements[index] = element;
return true;
}
}
29 changes: 0 additions & 29 deletions src/utils/sortedListDesc.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/utils/sortedListPriority.ts

This file was deleted.