0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 08:50:57 -05:00

Expose a more cleaner api for kdtree impl.

This commit is contained in:
Andrey Antukh 2016-06-11 16:00:54 +03:00
parent 53a9b4e8ba
commit 447da07789
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95

61
vendor/kdtree/core.js vendored
View file

@ -14,7 +14,6 @@
"use strict"; "use strict";
goog.provide("kdtree.core"); goog.provide("kdtree.core");
goog.provide("kdtree.core.KDTree");
goog.require("kdtree.heap"); goog.require("kdtree.heap");
goog.require("goog.asserts"); goog.require("goog.asserts");
@ -39,26 +38,6 @@ goog.scope(function() {
constructor() { constructor() {
this.root = null; this.root = null;
} }
initialize(points) {
assert(goog.isArray(points));
this.root = buildTree(null, points, 0);
}
isInitialized() {
return this.root !== null;
}
clear() {
this.root = null
}
nearest(point, maxNodes) {
assert(goog.isArray(point));
assert(maxNodes >= 1);
assert(this.isInitialized())
return searchNearest(this.root, point, maxNodes);
}
} }
// --- Private Api (implementation) // --- Private Api (implementation)
@ -170,13 +149,21 @@ goog.scope(function() {
function create(points) { function create(points) {
const tree = new KDTree(); const tree = new KDTree();
if (goog.isArray(points)) { if (goog.isArray(points)) {
tree.initialize(points); return initialize(tree, points);
} else {
return tree;
} }
return tree;
}; };
function initialize(tree, width, height, widthStep, heightStep) { function initialize(tree, points) {
assert(goog.isArray(points));
assert(tree instanceof KDTree);
tree.root = buildTree(null, points, 0);
return tree;
}
function setup(tree, width, height, widthStep, heightStep) {
const totalSize = Math.floor((width/widthStep) * (height/heightStep)); const totalSize = Math.floor((width/widthStep) * (height/heightStep));
const points = new Array(totalSize); const points = new Array(totalSize);
let pos = 0; let pos = 0;
@ -187,14 +174,32 @@ goog.scope(function() {
} }
} }
tree.initialize(points); return initialize(tree, points);
}
function isInitialized(tree) {
assert(tree instanceof KDTree);
return tree.root !== null;
}
function clear(tree) {
assert(tree instanceof KDTree);
tree.root = null;
return tree; return tree;
} }
// Types function nearest(tree, point, maxNodes) {
kdtree.core.KDTree = KDTree; assert(goog.isArray(point));
assert(maxNodes >= 1);
assert(isInitialized(tree));
return searchNearest(tree.root, point, maxNodes);
}
// Factory functions // Factory functions
kdtree.core.create = create; kdtree.core.create = create;
kdtree.core.initialize = initialize; kdtree.core.initialize = initialize;
kdtree.core.setup = setup;
kdtree.core.isInitialized = isInitialized;
kdtree.core.clear = clear;
kdtree.core.nearest = nearest;
}); });