ubilabs / kd-tree-javascript

JavaScript k-d Tree Implementation
MIT License
642 stars 109 forks source link

when building kd-tree, sort break the binding between indces and points #16

Open exialym opened 7 years ago

sunshichen commented 6 years ago

How do you solve this problem? Is there anyway to get the nodes' indices of original buffer?

benmaier commented 2 years ago

I solved this by replacing line 29 (https://github.com/ubilabs/kd-tree-javascript/blob/master/kdTree.js#L29)

  function kdTree(points, metric, dimensions) {

with

  function kdTree(points, metric, dimensions) {

    points = points.slice();
    points.forEach(function(p, i){
      p.i = i;
    });

then, each result obiect in the nearest-array has its original position in the points-array saved in the .i attribute. Also, the slice prevents mutation of the point positions, which happened in my use case.