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

ritter-bounding-sphere

v1.0.0

Published

compute bounding sphere for a set of points using Jack Ritter's algorithm

Downloads

3

Readme

ritter-bounding-sphere

CircleCI Coverage Status semantic-release Commitizen friendly npm version

Compute bounding sphere for a set of points using Jack Ritter's algorithm

https://en.wikipedia.org/wiki/Bounding_sphere#Ritter's_bounding_sphere

Ritter's algorithm is very simple and efficient, but gives only a coarse result which is usually 5% to 20% larger than the optimum.

Compatibility

ritter-bounding-sphere requires Symbol.iterator to be supported. It definitely works in Node 8+, but older environments may require a polyfill.

require('ritter-bounding-sphere')(points, [output])

Computes a bounding sphere for points, which must be an Iterable<[x, y, z]>. The iterator can mutate and return the same array instance if desired.

Returns the bounding sphere as an array of the form [x, y, z, radiusSquared] where x, y, z is the center. You can pass the output array to store the result in if you want to avoid the array allocation.

Adapting other point formats

ritter-bounding-sphere accepts point data in arrays so that you can optionally use Float32Array/Float64Arrays for speed. But if your points are in a different format you can easily adapt them to this library.

Let's say your points are in {x: number, y: number, z: number} format instead, and you want to output a bounding sphere of the form {center: {x: number, y: number, z: number}, radius: number}. Here's how your adapter could work:

const boundingSphere = require('ritter-bounding-sphere')

export function myBoundingSphere(points) {
  const point = [0, 0, 0]
  function* adapter() {
    for (let { x, y, z } of points) {
      point[0] = x
      point[1] = y
      point[2] = z
      yield point
    }
  }
  const [x, y, z, r2] = boundingSphere(adapter())
  return {
    center: { x, y, z },
    radius: Math.sqrt(r2),
  }
}

Example

On a tetrahedron:

> require('ritter-bounding-sphere')([[0, 0, 0], [1, 1, 0], [1, 0, 1], [0, 1, 1]])
[ 0.5540595619320077,  // center x
  0.44594043806799233, // center y
  0.2785205487644272,  // center z
  1.1344965948917598 ] // radius squared

Notice that this is about 22% larger than the optimum bounding sphere ([0.5, 0.5, 0.5, 0.75]). A perfect tetrahedron is one of the worst cases since all its points are equidistant.

Loose mode

require('ritter-bounding-sphere') corrects for floating-point error by doing a final pass to check if all points are in the resulting sphere, increasing the radius squared as necessary. If you want more performance and less accuracy, you can skip this final pass by using require('ritter-bounding-sphere').loose instead, which has the same signature.

Some points may lie just barely outside the loose output sphere (for instance, on 100 random points between [0, 0, 0] and [1, 1, 1]) the output sphere's radius squared is usually ~1e-16 less than the distance squared to some point.