mourner / rbush

RBush — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles
MIT License
2.46k stars 237 forks source link

How to search when usiong the extended class for points #119

Closed rwillett closed 4 months ago

rwillett commented 3 years ago

Hi,

We have your rtree code working well and wanted to use the simple extension for points in the docs.

We can create the point tree (or we think we can), but no matter what we search on, we never get a result back.

export class RpointService extends RBush {
    toBBox([x, y]) { return {minX: x, minY: y, maxX: x, maxY: y}; }
    compareMinX(a, b) { return a.x - b.x; }
    compareMinY(a, b) { return a.y - b.y; }

    constructor() {
        super();
        var debug = true;

    if (debug)
            console.log("RpointService constructor called");
    }

    insert(v) {
        console.log("Insert called");
        return super.insert(v);
    }

    all() {
        return super.all();
    }

    search(v) {
        return super.search(v);
    }
}

We can add points using

        var pTree = new RpointService();

        for (var i = 0 ; i < cutLines.length ; i++)
        {
            console.log("CalculateJoinLines: Adding " + JSON.stringify(cutLines[i] , null , 2));

            pTree.insert([ cutLines[i].p1.X , cutLines[i].p1.Y]);
        }

and can see something using

        console.log("ptree = " + JSON.stringify(pTree.all() , null , 2));

ptree = [
  [
    2500,
    9999
  ],
  [
    2500,
    14999
  ]
]

but we cannot find the right format to pass to search to get something returned (anything!),

We've tried

        result = rTree.search({
            minX: 40,
            minY: 20,
            maxX: 80,
            maxY: 70
    });
``
and
    result = rTree.search([20,20]);


and a few other formats, but nothing seems to work. 

Any help or advice welcomed.

Thanks

Rob
mourner commented 4 months ago

Sorry for not responding here. This seems to be a user error — compareMinX and compareMinY here expect {x, y} format while the items are actually [x, y], hence the broken index:

    toBBox([x, y]) { return {minX: x, minY: y, maxX: x, maxY: y}; }
    compareMinX(a, b) { return a.x - b.x; } // should be return a[0] - b[0]
    compareMinY(a, b) { return a.y - b.y; } // should be return a[1] - b[1]