Giter VIP home page Giter VIP logo

ritter-bounding-sphere's Introduction

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 yield the same array instance more than once 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.

ritter-bounding-sphere's People

Contributors

greenkeeper[bot] avatar jedwards1211 avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.