npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bsp-tree

v1.0.0

Published

constructs a binary space partitioning tree

Downloads

9

Readme

bsp-tree

constructs a binary space partitioning tree.

A BSP tree is a binary tree used to sort and search for polytopes in n-dimensional space.

example

For example, given a square somewhere in the XY plane, we select the first split, and thus the root of the BSP tree, to cut the square in half in the X direction. At each slice, we will choose a line of the opposite orientation from the last one, so the second slice will divide each of the new pieces in the Y direction.

The result is shown in the following figure along with the BSP tree which describes it:

+-----------+      +-----+-----+      +-----+-----+
|           |      |     |     |      |     |     |
|           |      |     |     |      |  d  |     |
|           |      |     |     |      |     |     |
|     a     |  ->  |  b  X  c  |  ->  +--Y--+  c  |  -> ...
|           |      |     |     |      |     |     |
|           |      |     |     |      |  e  |     |
|           |      |     |     |      |     |     |
+-----------+      +-----+-----+      +-----+-----+
      a                  X                  X           ...
                       -/ \+              -/ \+
                       /   \              /   \
                      b     c            Y     c
                                       -/ \+
                                       /   \
                                      d     e

to build such a tree that recursively divides its first half:

var bsp = require('bsp-tree');

var node = bsp(8),
    vertical = true;

while (node.data.shape[0] > 1) {
  vertical = !(node.split(vertical).dir);
  node = node.leafs[0];
}

methods

var tree = bsp(size [, repr])

Returns a data structure tree that represents the root of a BSP tree of finite size.

Following methods are both applicable to a tree and its leafs.

leaf.split(vertical=false)

Divides a tree (or a leaf) into leafs. Each leaf represents a convex region which is partitioned in two convex sub-regions at each side of a cut hyperplane. The root leaf contains the complete space.

leaf.merge()

Merges the split parts.

leaf.flip()

Flips the orientation.

var repr = leaf.serialize()

Returns an array representation of a tree that you can use to create a copy.

var tree = bsp(4);
tree.split().leafs[0].split(true);
var copy = bsp(4, tree.serialize());

Following methods are only applicable to root node.

Given an offset of a leaf (that can be accessed through leaf.data.offset) these return the nearest plane's offset in x or y direction.

var offset = tree.north(offset)

var offset = tree.south(offset)

var offset = tree.west(offset)

var offset = tree.east(offset)

properties

leaf.leafs

Nodes of a subdivision can be accessed through leafs array. If not divided this property is null by default.

leaf.data

Underlying ndarray instance which is shared among all split parts.

leaf.dir

A boolean that indicates the split direction.

license

mit