UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

37 lines (36 loc) 1.15 kB
"use strict"; import { Sphere, Vector3 } from "three"; import { OctreeNode } from "./Node"; import { arraySortBy } from "../../ArrayUtils"; const _position = new Vector3(); export class CoreOctree { constructor(bbox) { this._root = new OctreeNode(bbox); } setPoints(points) { this._root.setPoints(points); } traverse(callback) { this._root.traverse(callback); } // TODO: I am tempted to stop going through the leaves if // the ones currently seen already have the required number of points. // but that probably doesn't work as those points may end up being further // than the ones from the following leaf findPoints(position, distance, maxPointsCount, target) { const sphere = new Sphere(position, distance); if (this._root.intersectsSphere(sphere)) { this._root.pointsInSphere(sphere, target); } if (maxPointsCount == null) { return; } else { if (target.length > maxPointsCount) { target = arraySortBy(target, (point) => { return point.position(_position).distanceTo(position); }); target = target.slice(0, maxPointsCount); } } } }