Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mourner/rbush
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: Eronana/rbush-3d
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.

Commits on Nov 29, 2017

  1. first commit

    Eronana committed Nov 29, 2017
    Copy the full SHA
    a690844 View commit details

Commits on Dec 1, 2017

  1. finish real 3D test

    Eronana committed Dec 1, 2017
    Copy the full SHA
    2d56d7b View commit details
  2. Copy the full SHA
    563b7f8 View commit details
  3. rename rbush to rbush3d

    Eronana committed Dec 1, 2017
    Copy the full SHA
    34ef570 View commit details

Commits on Dec 4, 2017

  1. finish benchmarks

    Eronana committed Dec 4, 2017
    Copy the full SHA
    2dadc70 View commit details
  2. Copy the full SHA
    1f5647c View commit details
  3. improved test coverage

    Eronana committed Dec 4, 2017
    Copy the full SHA
    8f913af View commit details
  4. add a rough 3d visualization

    mikolalysenko authored and Eronana committed Dec 4, 2017
    Copy the full SHA
    8aaf5b1 View commit details
  5. Copy the full SHA
    bd2013a View commit details
  6. Copy the full SHA
    d355504 View commit details
  7. add travis-ci and coveralls

    Eronana committed Dec 4, 2017
    Copy the full SHA
    cd742ad View commit details
  8. add badges

    Eronana authored Dec 4, 2017
    Copy the full SHA
    b58c15b View commit details

Commits on Dec 6, 2017

  1. update description field

    Eronana committed Dec 6, 2017
    Copy the full SHA
    6d6f5d0 View commit details

Commits on Dec 8, 2017

  1. add index.d.ts

    Eronana committed Dec 8, 2017
    Copy the full SHA
    bd488fc View commit details
  2. 0.0.2

    Eronana committed Dec 8, 2017
    Copy the full SHA
    2928e9f View commit details

Commits on Dec 18, 2017

  1. Update README.md

    Eronana authored Dec 18, 2017
    Copy the full SHA
    d047778 View commit details

Commits on Jun 29, 2018

  1. add raycast

    Eronana committed Jun 29, 2018
    Copy the full SHA
    586af71 View commit details
  2. Copy the full SHA
    c3106a8 View commit details

Commits on Jul 2, 2018

  1. use heap to optimize

    Eronana committed Jul 2, 2018
    Copy the full SHA
    4e70d21 View commit details
  2. Copy the full SHA
    1247ffb View commit details

Commits on Jul 3, 2018

  1. Merge pull request #2 from Eronana/feat_raycast

    add raycast
    Eronana authored Jul 3, 2018
    Copy the full SHA
    28256d7 View commit details

Commits on Jul 9, 2018

  1. port to typescript

    Eronana committed Jul 9, 2018
    Copy the full SHA
    8beb2e6 View commit details

Commits on Jul 10, 2018

  1. Copy the full SHA
    479f3ba View commit details
  2. update .npmignore

    Eronana committed Jul 10, 2018
    Copy the full SHA
    cff6aef View commit details

Commits on Dec 16, 2018

  1. Fix README.md typo

    pboyer authored Dec 16, 2018
    Copy the full SHA
    9f211a3 View commit details

Commits on Dec 17, 2018

  1. Merge pull request #3 from pboyer/patch-1

    Fix README.md typo
    Eronana authored Dec 17, 2018
    Copy the full SHA
    d76e351 View commit details
Showing with 1,927 additions and 1,352 deletions.
  1. +5 −2 .gitignore
  2. +9 −0 .npmignore
  3. +9 −0 .travis.yml
  4. +15 −0 .vscode/settings.json
  5. +1 −1 MIT-LICENSE
  6. +66 −155 README.md
  7. +0 −21 bench/bulk.bench.js
  8. +21 −0 bench/bulk.bench.ts
  9. +0 −38 bench/bulksearch.bench.js
  10. +38 −0 bench/bulksearch.bench.ts
  11. +0 −26 bench/gendata.js
  12. +26 −0 bench/gendata.ts
  13. +0 −33 bench/insert.bench.js
  14. +32 −0 bench/insert.bench.ts
  15. +0 −83 bench/perf.js
  16. +62 −0 bench/perf.ts
  17. +0 −40 bench/search.bench.js
  18. +40 −0 bench/search.bench.ts
  19. +0 −562 index.js
  20. +49 −13 package.json
  21. +739 −0 src/index.ts
  22. +0 −378 test/test.js
  23. +513 −0 test/test.ts
  24. +11 −0 tsconfig.build.json
  25. +36 −0 tsconfig.json
  26. +79 −0 tslint.json
  27. +176 −0 viz/viz3d.js
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
node_modules
npm-debug.log
coverage
rbush.js
rbush.min.js
rbush3d.js
rbush3d.min.js
package-lock.json
dist
.nyc_output
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
src
bench
viz
test
.travis.yml
.vscode
.gitignore
tsconfig.json
tsconfig.build.json
tslint.json
dist
.nyc_output
coverage
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -2,3 +2,12 @@ language: node_js
node_js:
- "4"
- "stable"
branches:
only:
- master
install:
- npm install
script:
- npm test
after_success:
- npm run coveralls
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"search.exclude": {
"node_modules": true,
"coverage": true
},
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"node_modules": true,
"package-lock.json": true,
"coverage": true,
".nyc_output": true
},
"editor.tabSize": 2,
}
2 changes: 1 addition & 1 deletion MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Vladimir Agafonkin
Copyright (c) 2017 Eronana

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
221 changes: 66 additions & 155 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,54 @@
RBush
RBush-3D
=====

RBush is a high-performance JavaScript library for 2D **spatial indexing** of points and rectangles.
It's based on an optimized **R-tree** data structure with **bulk insertion** support.
RBush-3D is 3D version of [RBush](https://github.com/mourner/rbush).

*Spatial index* is a special data structure for points and rectangles
that allows you to perform queries like "all items within this bounding box" very efficiently
(e.g. hundreds of times faster than looping over all items).
It's most commonly used in maps and data visualizations.
[![Build Status](https://travis-ci.org/Eronana/rbush-3d.svg?branch=master)](https://travis-ci.org/Eronana/rbush-3d)
[![Coverage Status](https://coveralls.io/repos/github/Eronana/rbush-3d/badge.svg?branch=master)](https://coveralls.io/github/Eronana/rbush-3d?branch=master)

[![Build Status](https://travis-ci.org/mourner/rbush.svg?branch=master)](https://travis-ci.org/mourner/rbush)
[![](https://img.shields.io/badge/simply-awesome-brightgreen.svg)](https://github.com/mourner/projects)
## TODO
- [x] real 3D test
- [ ] Demos
- [x] Benchmarks

## Demos

The demos contain visualization of trees generated from 50k bulk-loaded random points.
Open web console to see benchmarks;
click on buttons to insert or remove items;
click to perform search under the cursor.

* [randomly clustered data](http://mourner.github.io/rbush/viz/viz-cluster.html)
* [uniformly distributed random data](http://mourner.github.io/rbush/viz/viz-uniform.html)

## Install

Install with NPM (`npm install rbush`), or use CDN links for browsers:
[rbush.js](https://unpkg.com/rbush@2.0.1/rbush.js),
[rbush.min.js](https://unpkg.com/rbush@2.0.1/rbush.min.js)
Install with NPM (`npm install rbush-3d`), and Chinese user could use CNPM(`cnpm install rbush-3d`).

Or use CDN links for browsers:
[rbush3d.js](https://unpkg.com/rbush-3d@0.0.4/dist/rbush3d.js),
[rbush3d.min.js](https://unpkg.com/rbush-3d@0.0.4/dist/rbush3d.min.js)

## Usage

### Creating a Tree

```js
var tree = rbush();
```typescript
import { RBush3D } from 'rbush-3d';
const tree = new RBush3D();
```

An optional argument to `rbush` defines the maximum number of entries in a tree node.
`9` (used by default) is a reasonable choice for most applications.
An optional argument to `RBush3D` defines the maximum number of entries in a tree node.
`16` (used by default) is a reasonable choice for most applications.
Higher value means faster insertion and slower search, and vice versa.

```js
var tree = rbush(16);
```typescript
const tree = new RBush3D(16);
```

### Adding Data

Insert an item:

```js
var item = {
```typescript
const item = {
minX: 20,
minY: 40,
minZ: 60,
maxX: 30,
maxY: 50,
maxZ: 70,
foo: 'bar'
};
tree.insert(item);
@@ -63,45 +58,43 @@ tree.insert(item);

Remove a previously inserted item:

```js
```typescript
tree.remove(item);
```

By default, RBush removes objects by reference.
By default, RBush-3D removes objects by reference.
However, you can pass a custom `equals` function to compare by value for removal,
which is useful when you only have a copy of the object you need removed (e.g. loaded from server):

```js
```typescript
tree.remove(itemCopy, function (a, b) {
return a.id === b.id;
});
```

Remove all items:

```js
```typescript
tree.clear();
```

### Data Format

By default, RBush assumes the format of data points to be an object
with `minX`, `minY`, `maxX` and `maxY` properties.
By default, RBush-3D assumes the format of data points to be an object
with `minX`, `minY`, `minZ`, `maxX`, `maxY` and `maxZ` properties.
You can customize this by providing an array with corresponding accessor strings
as a second argument to `rbush` like this:
as a second argument to `rbush3d` like this:

```js
var tree = rbush(9, ['[0]', '[1]', '[0]', '[1]']); // accept [x, y] points
tree.insert([20, 50]);
```typescript
const tree = rbush3d(16, ['[0]', '[1]', '[2]', '[0]', '[1]', '[2]']); // accept [x, y, z] points
tree.insert([20, 50, 80]);
```

If you're indexing a static list of points (you don't need to add/remove points after indexing), you should use [kdbush](https://github.com/mourner/kdbush) which performs point indexing 5-8x faster than RBush.

### Bulk-Inserting Data

Bulk-insert the given data into the tree:

```js
```typescript
tree.load([item1, item2, ...]);
```

@@ -118,70 +111,68 @@ but makes query performance worse if the data is scattered.

### Search

```js
var result = tree.search({
```typescript
const result = tree.search({
minX: 40,
minY: 20,
minZ: 50,
maxX: 80,
maxY: 70
maxY: 70,
maxZ: 90
});
```

Returns an array of data items (points or rectangles) that the given bounding box intersects.

Note that the `search` method accepts a bounding box in `{minX, minY, maxX, maxY}` format
Note that the `search` method accepts a bounding box in `{minX, minY, minZ, maxX, maxY, maxZ}` format
regardless of the format specified in the constructor (which only affects inserted objects).

```js
var allItems = tree.all();
```typescript
const allItems = tree.all();
```

Returns all items of the tree.

### Collisions

```js
var result = tree.collides({minX: 40, minY: 20, maxX: 80, maxY: 70});
```typescript
const result = tree.collides({minX: 40, minY: 20, minZ: 50, maxX: 80, maxY: 70, maxZ: 90});
```

Returns `true` if there are any items intersecting the given bounding box, otherwise `false`.


### Export and Import

```js
```typescript
// export data as JSON object
var treeData = tree.toJSON();
const treeData = tree.toJSON();

// import previously exported data
var tree = rbush(9).fromJSON(treeData);
const tree = rbush3d(16).fromJSON(treeData);
```

Importing and exporting as JSON allows you to use RBush on both the server (using Node.js) and the browser combined,
Importing and exporting as JSON allows you to use RBush-3D on both the server (using Node.js) and the browser combined,
e.g. first indexing the data on the server and and then importing the resulting tree data on the client for searching.

Note that the `nodeSize` option passed to the constructor must be the same in both trees for export/import to work properly.

### K-Nearest Neighbors

For "_k_ nearest neighbors around a point" type of queries for RBush,
check out [rbush-knn](https://github.com/mourner/rbush-knn).

## Performance

The following sample performance test was done by generating
random uniformly distributed rectangles of ~0.01% area and setting `maxEntries` to `16`
(see `debug/perf.js` script).
Performed with Node.js v6.2.2 on a Retina Macbook Pro 15 (mid-2012).

Test | RBush | [old RTree](https://github.com/imbcmdth/RTree) | Improvement
---------------------------- | ------ | ------ | ----
insert 1M items one by one | 3.18s | 7.83s | 2.5x
1000 searches of 0.01% area | 0.03s | 0.93s | 30x
1000 searches of 1% area | 0.35s | 2.27s | 6.5x
1000 searches of 10% area | 2.18s | 9.53s | 4.4x
remove 1000 items one by one | 0.02s | 1.18s | 50x
bulk-insert 1M items | 1.25s | n/a | 6.7x
(see `debug/perf.ts` script).
Performed with Node.js v8.9.1 on a MacBook Pro (15-inch, 2017).

Test | RBush-3D | [RBush](https://github.com/mourner/rbush) (2D version)
---------------------------- | -------- | ------
insert 1M items one by one | 4.30s | 2.94s
1000 searches of 0.01% area | 0.02s | 0.03s
1000 searches of 1% area | 0.09s | 0.31s
1000 searches of 10% area | 0.73s | 1.80s
remove 1000 items one by one | 0.02s | 0.02s
bulk-insert 1M items | 1.40s | 1.17s


## Algorithms Used

@@ -202,90 +193,10 @@ bulk-insert 1M items | 1.25s | n/a | 6.7x
## Development

```bash
npm install # install dependencies
npm install # install dependencies

npm test # check the code with JSHint and run tests
npm run perf # run performance benchmarks
npm run cov # report test coverage (with more detailed report in coverage/lcov-report/index.html)
npm test # check the code with JSHint and run tests
npm run perf # run performance benchmarks
npm run cover # report test coverage (with more detailed report in coverage/lcov-report/index.html)
npm run viz # show 3d visualization in browser
```

## Compatibility

RBush should run on Node and all major browsers. The only caveat: IE 8 needs an [Array#indexOf polyfill](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill) for `remove` method to work.

## Changelog

#### 2.0.1 — June 29, 2016

- Fixed browser builds in NPM.

#### 2.0.0 — June 29, 2016

- **Breaking:** changed the default format of inserted items from `[20, 40, 30, 50]` to `{minX: 20, minY: 40, maxX: 30, maxY: 50}`.
- **Breaking:** changed the `search` method argument format from `[20, 40, 30, 50]` to `{minX: 20, minY: 40, maxX: 30, maxY: 50}`.
- Improved performance by up to 30%.
- Added `equalsFn` optional argument to `remove` to be able to remove by value rather than by reference.
- Changed the source code to use CommonJS module format. Browser builds are automatically built and published to NPM.
- Quickselect algorithm (used internally) is now a [separate module](https://github.com/mourner/quickselect).

#### 1.4.3 — May 17, 2016

- Fixed an error when inserting many empty bounding boxes.

#### 1.4.2 — Dec 16, 2015

- 50% faster insertion.

#### 1.4.1 — Sep 16, 2015

- Fixed insertion in IE8.

#### 1.4.0 — Apr 22, 2015

- Added `collides` method for fast collision detection.

#### 1.3.4 — Aug 31, 2014

- Improved bulk insertion performance for a large number of items (e.g. up to 100% for inserting a million items).
- Fixed performance regression for high node sizes.

#### 1.3.3 — Aug 30, 2014

- Improved bulk insertion performance by ~60-70%.
- Improved insertion performance by ~40%.
- Improved search performance by ~30%.

#### 1.3.2 — Nov 25, 2013

- Improved removal performance by ~50%. [#18](https://github.com/mourner/rbush/pull/18)

#### 1.3.1 — Nov 24, 2013

- Fixed minor error in the choose split axis algorithm. [#17](https://github.com/mourner/rbush/pull/17)
- Much better test coverage (near 100%). [#6](https://github.com/mourner/rbush/issues/6)

#### 1.3.0 — Nov 21, 2013

- Significantly improved search performance (especially on large-bbox queries — up to 3x faster). [#11](https://github.com/mourner/rbush/pull/11)
- Added `all` method for getting all of the tree items. [#11](https://github.com/mourner/rbush/pull/11)
- Made `toBBox`, `compareMinX`, `compareMinY` methods public, made it possible to avoid Content Security Policy issues by overriding them for custom format. [#14](https://github.com/mourner/rbush/pull/14) [#12](https://github.com/mourner/rbush/pull/12)

#### 1.2.5 — Nov 5, 2013

- Fixed a bug where insertion failed on a tree that had all items removed previously. [#10](https://github.com/mourner/rbush/issues/10)

#### 1.2.4 — Oct 25, 2013

- Added Web Workers support. [#9](https://github.com/mourner/rbush/pull/9)

#### 1.2.3 — Aug 30, 2013

- Added AMD support. [#8](https://github.com/mourner/rbush/pull/8)

#### 1.2.2 — Aug 27, 2013

- Eliminated recursion when recalculating node bboxes (on insert, remove, load).

#### 1.2.0 — Jul 19, 2013

First fully functional RBush release.
Loading