mirror of
https://github.com/penpot/penpot.git
synced 2025-06-07 21:51:40 +02:00
Remove point abstraction and just use array for that on kdtree.
This commit is contained in:
parent
78e7f57655
commit
d64253dd1b
1 changed files with 14 additions and 51 deletions
65
vendor/kdtree.js
vendored
65
vendor/kdtree.js
vendored
|
@ -12,7 +12,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
goog.provide("kdtree");
|
goog.provide("kdtree");
|
||||||
goog.provide("kdtree.Point2d");
|
|
||||||
goog.provide("kdtree.KDTree");
|
goog.provide("kdtree.KDTree");
|
||||||
|
|
||||||
goog.require('goog.array');
|
goog.require('goog.array');
|
||||||
|
@ -25,36 +24,6 @@ goog.scope(function() {
|
||||||
const assertNumber = goog.asserts.assertNumber;
|
const assertNumber = goog.asserts.assertNumber;
|
||||||
const every = goog.array.every;
|
const every = goog.array.every;
|
||||||
|
|
||||||
class Point2d {
|
|
||||||
constructor(x, y, data) {
|
|
||||||
this.type = Point2d;
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
this.data = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static empty() {
|
|
||||||
return new Point2d(0, 0, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
get(index) {
|
|
||||||
if (index === 0) {
|
|
||||||
return this.x;
|
|
||||||
} else {
|
|
||||||
return this.y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
set(index, value) {
|
|
||||||
if (index === 0) {
|
|
||||||
this.x = value;
|
|
||||||
} else {
|
|
||||||
this.y = value;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Node {
|
class Node {
|
||||||
constructor(obj, dimension, parent) {
|
constructor(obj, dimension, parent) {
|
||||||
this.obj = obj;
|
this.obj = obj;
|
||||||
|
@ -77,7 +46,7 @@ goog.scope(function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
points.sort((a, b) => {
|
points.sort((a, b) => {
|
||||||
return a.get(dim) - b.get(dim);
|
return a[dim] - b[dim];
|
||||||
});
|
});
|
||||||
|
|
||||||
const median = Math.floor(points.length / 2);
|
const median = Math.floor(points.length / 2);
|
||||||
|
@ -102,15 +71,15 @@ goog.scope(function() {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
own = node.obj.get(dim);
|
own = node.obj[dim];
|
||||||
left = findMin(node.left, dim);
|
left = findMin(node.left, dim);
|
||||||
right = findMin(node.right, dim);
|
right = findMin(node.right, dim);
|
||||||
min = node;
|
min = node;
|
||||||
|
|
||||||
if (left !== null && left.obj.get(dim) < own) {
|
if (left !== null && left.obj[dim] < own) {
|
||||||
min = left;
|
min = left;
|
||||||
}
|
}
|
||||||
if (right !== null && right.obj.get(dim) < min.obj.get(dim)) {
|
if (right !== null && right.obj[dim] < min.obj[dim]) {
|
||||||
min = right;
|
min = right;
|
||||||
}
|
}
|
||||||
return min;
|
return min;
|
||||||
|
@ -121,7 +90,7 @@ goog.scope(function() {
|
||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (point.get(dim) < node.obj.get(dim)) {
|
if (point[dim] < node.obj[dim]) {
|
||||||
return innerSearch(point, node.left, node);
|
return innerSearch(point, node.left, node);
|
||||||
} else {
|
} else {
|
||||||
return innerSearch(point, node.right, node);
|
return innerSearch(point, node.right, node);
|
||||||
|
@ -137,7 +106,7 @@ goog.scope(function() {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (point.get(node.dimension) < node.obj.get(node.dimension)) {
|
if (point[node.dimension] < node.obj[node.dimension]) {
|
||||||
return nodeSearch(point, node.left);
|
return nodeSearch(point, node.left);
|
||||||
} else {
|
} else {
|
||||||
return nodeSearch(point, node.right);
|
return nodeSearch(point, node.right);
|
||||||
|
@ -167,7 +136,7 @@ goog.scope(function() {
|
||||||
insertPosition);
|
insertPosition);
|
||||||
|
|
||||||
const dimension = insertPosition.dimension;
|
const dimension = insertPosition.dimension;
|
||||||
if (point.get(dimension) < insertPosition.obj.get(dimension)) {
|
if (point[dimension] < insertPosition.obj[dimension]) {
|
||||||
insertPosition.left = newNode;
|
insertPosition.left = newNode;
|
||||||
} else {
|
} else {
|
||||||
insertPosition.right = newNode;
|
insertPosition.right = newNode;
|
||||||
|
@ -188,7 +157,7 @@ goog.scope(function() {
|
||||||
|
|
||||||
const pdim = node.parent.dimension;
|
const pdim = node.parent.dimension;
|
||||||
|
|
||||||
if (node.obj.get(pdim) < node.parent.obj.get(pdim)) {
|
if (node.obj[pdim] < node.parent.obj[pdim]) {
|
||||||
node.parent.left = null;
|
node.parent.left = null;
|
||||||
} else {
|
} else {
|
||||||
node.parent.right = null;
|
node.parent.right = null;
|
||||||
|
@ -227,10 +196,9 @@ goog.scope(function() {
|
||||||
bestNodes = new BinaryHeap(function (e) { return -e[1]; });
|
bestNodes = new BinaryHeap(function (e) { return -e[1]; });
|
||||||
|
|
||||||
const nearestSearch = (node) => {
|
const nearestSearch = (node) => {
|
||||||
const ownDistance = self.metric(point, node.obj);
|
const ownDistance = this.metric(point, node.obj);
|
||||||
const dimension = node.dimension;
|
const dimension = node.dimension;
|
||||||
const pointType = node.obj.type;
|
const linearPoint = [null, null];
|
||||||
const linearPoint = pointType.empty();
|
|
||||||
|
|
||||||
let otherChild, linearDistance, bestChild, i;
|
let otherChild, linearDistance, bestChild, i;
|
||||||
|
|
||||||
|
@ -243,9 +211,9 @@ goog.scope(function() {
|
||||||
|
|
||||||
for (i = 0; i < this.dimensions; i += 1) {
|
for (i = 0; i < this.dimensions; i += 1) {
|
||||||
if (i === node.dimension) {
|
if (i === node.dimension) {
|
||||||
linearPoint.set(i, point.get(i));
|
linearPoint[i] = point[i];
|
||||||
} else {
|
} else {
|
||||||
linearPoint.set(i, node.obj.get(i));
|
linearPoint[i] = node.obj[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,7 +231,7 @@ goog.scope(function() {
|
||||||
} else if (node.left === null) {
|
} else if (node.left === null) {
|
||||||
bestChild = node.right;
|
bestChild = node.right;
|
||||||
} else {
|
} else {
|
||||||
if (point.get(dimension) < node.obj.get(dimension)) {
|
if (point[dimension] < node.obj[dimension]) {
|
||||||
bestChild = node.left;
|
bestChild = node.left;
|
||||||
} else {
|
} else {
|
||||||
bestChild = node.right;
|
bestChild = node.right;
|
||||||
|
@ -454,11 +422,7 @@ goog.scope(function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
function distance2d(a, b){
|
function distance2d(a, b){
|
||||||
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
|
return Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2);
|
||||||
}
|
|
||||||
|
|
||||||
function point2d(x, y, data) {
|
|
||||||
return new Point2d(x, y, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function create2d(points) {
|
function create2d(points) {
|
||||||
|
@ -469,6 +433,5 @@ goog.scope(function() {
|
||||||
kdtree.KDTree = KDTree;
|
kdtree.KDTree = KDTree;
|
||||||
|
|
||||||
// Factory functions
|
// Factory functions
|
||||||
kdtree.point2d = point2d;
|
|
||||||
kdtree.create2d = create2d;
|
kdtree.create2d = create2d;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue