avocado/packages/math/test/quadtree.js
2021-03-20 02:24:51 -05:00

106 lines
3.8 KiB
JavaScript

require('source-map-support').install();
import {expect} from 'chai';
import QuadTree from '../src/quadtree';
describe('QuadTree', function() {
it('can specify a max depth', function() {
const maxData = 5;
const data = Array(maxData + 1).fill().map(() => ({}));
const tree = new QuadTree({maxData, size: [16, 16]});
data.forEach((datum, i) => {
expect(tree.quad.hasChildren()).to.equal(i > maxData);
tree.add(datum, [0, 0, 1, 1]);
expect(tree.quad.hasChildren()).to.equal(i >= maxData);
});
});
it('can add children to multiple quads', function() {
const maxData = 1;
const data = Array(maxData + 1).fill().map(() => ({}));
const tree = new QuadTree({maxData, maxDepth: 1, extent: [0, 0, 16, 16]});
tree.add(data[0], [4, 4, 4, 4]);
tree.add(data[1], [4, 4, 8, 8]);
expect(tree.quad.quads[0].data).to.have.lengthOf(2);
expect(tree.quad.quads[1].data).to.have.lengthOf(1);
expect(tree.quad.quads[2].data).to.have.lengthOf(1);
expect(tree.quad.quads[3].data).to.have.lengthOf(1);
});
it('can query without duplication', () => {
const maxData = 1;
const data = Array(maxData + 1).fill().map(() => ({}));
const tree = new QuadTree({maxData, size: [16, 16]});
tree.add(data[0], [4, 4, 4, 4]);
tree.add(data[1], [4, 4, 8, 8]);
expect(tree.query([0, 0, 16, 16]).size).to.equal(2);
});
it('can add and visit data without children', () => {
const maxData = 3;
const data = Array(maxData + 1).fill().map(() => ({}));
const tree = new QuadTree({maxData, size: [16, 16]});
tree.add(data[0], [0, 0, 2, 2]);
tree.add(data[1], [10, 0, 2, 2]);
tree.add(data[2], [0, 10, 2, 2]);
let map;
map = new Map();
tree.visit([0, 0, 8, 8], map);
expect(map.size).to.equal(1);
map = new Map();
tree.visit([0, 0, 4, 16], map);
expect(map.size).to.equal(2);
});
it('can add and visit data with children', () => {
const maxData = 3;
const data = Array(maxData + 1).fill().map(() => ({}));
const tree = new QuadTree({maxData, size: [16, 16]});
tree.add(data[0], [0, 0, 2, 2]);
tree.add(data[1], [10, 0, 2, 2]);
tree.add(data[2], [0, 10, 2, 2]);
tree.add(data[3], [10, 10, 2, 2]);
let map;
map = new Map();
tree.visit([0, 0, 8, 8], map);
expect(map.size).to.equal(1);
map = new Map();
tree.visit([0, 0, 4, 16], map);
expect(map.size).to.equal(2);
map = new Map();
tree.visit([0, 0, 16, 16], map);
expect(map.size).to.equal(4);
});
// it('can unsplit when data are removed', () => {
// const maxData = 2;
// const data = Array(maxData + 1).fill().map(() => ({}));
// const tree = new QuadTree({maxData, size: [16, 16]});
// tree.add(data[0], [2, 2, 4, 4]);
// tree.add(data[1], [3, 3, 4, 4]);
// tree.add(data[2], [4, 4, 4, 4]);
// expect(tree.quad.hasChildren()).to.be.true;
// tree.remove(data[1]);
// expect(tree.quad.hasChildren()).to.be.false;
// });
// it('can update data', () => {
// const maxData = 2;
// const data = Array(maxData + 1).fill().map(() => ({}));
// const tree = new QuadTree({maxData, maxDepth: 1, size: [16, 16]});
// tree.add(data[0], [2, 2, 4, 4]);
// tree.add(data[1], [3, 3, 4, 4]);
// tree.add(data[2], [4, 4, 8, 8]);
// expect(tree.quad.quads[0].data).to.have.lengthOf(3);
// expect(tree.quad.quads[1].data).to.have.lengthOf(1);
// tree.update(data[2], [4, 4, 4, 4]);
// expect(tree.quad.quads[0].data).to.have.lengthOf(3);
// expect(tree.quad.quads[1].data).to.have.lengthOf(0);
// });
it('can clear data', () => {
const tree = new QuadTree({size: [16, 16]});
tree.add({}, [2, 2, 4, 4]);
tree.add({}, [3, 3, 4, 4]);
tree.add({}, [4, 4, 8, 8]);
tree.clear();
const map = new Map();
tree.visit([0, 0, 16, 16], map);
expect(map.size).to.equal(0);
});
});