Giter VIP home page Giter VIP logo

quickhull3d's Introduction

quickhull3d

NPM version Codecov Status FOSSA Status

A robust quickhull implementation to find the convex hull of a set of 3d points in O(n log n) ported from John Lloyd implementation

Additional implementation material:

This library was incorporated into ThreeJS!. Thanks to https://github.com/Mugen87 for his work to move the primitives to ThreeJS primitives, the quickhull3d library will always be library agnostic and will operate with raw arrays.

Features

  • Key functions are well documented (including ascii graphics)
  • Faster than other JavaScript implementations of convex hull

Demo

Click on the image to see a demo!

demo

Minimal browser demo (using v3 or above)

<script type="module">
  import qh from 'https://cdn.jsdelivr.net/npm/quickhull3d@<version>/+esm'

  const points = [
    [0, 1, 0],
    [1, -1, 1],
    [-1, -1, 1],
    [0, -1, -1]
  ]

  const faces = qh(points)
  console.log(faces)
  // output:
  // [ [ 2, 1, 0 ], [ 3, 1, 2 ], [ 3, 0, 1 ], [ 3, 2, 0 ] ]
  // 1st face:
  //   points[2] = [-1, -1, 1]
  //   points[1] = [1, -1, 1]
  //   points[0] = [0, 1, 0]
  //   normal = (points[1] - points[2]) x (points[0] - points[2])
</script>

Installation

$ npm install --save quickhull3d

Usage

import qh from 'quickhull3d'

qh(points, options)

params

  • points {Array<Array>} an array of 3d points whose convex hull needs to be computed
  • options {Object} (optional)
  • options.skipTriangulation {Boolean} True to skip the triangulation of the faces (returning n-vertex faces)

returns An array of 3 element arrays, each subarray has the indices of 3 points which form a face whose normal points outside the polyhedra

isPointInsideHull(point, points, faces)

params

  • point {Array} The point that we want to check that it's a convex hull.
  • points {Array<Array>} The array of 3d points whose convex hull was computed
  • faces {Array<Array>} An array of 3 element arrays, each subarray has the indices of 3 points which form a face whose normal points outside the polyhedra

returns true if the point point is inside the convex hull

example

import qh, { isPointInsideHull } from 'quickhull3d'

const points = [
  [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1],
  [1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1]
]
const faces = qh(points)
expect(isPointInsideHull([0.5, 0.5, 0.5], points, faces)).toBe(true)
expect(isPointInsideHull([0, 0, -0.1], points, faces)).toBe(false)

Constructor

import QuickHull from 'quickhull3d/dist/QuickHull'

instance = new QuickHull(points)

params

  • points {Array} an array of 3d points whose convex hull needs to be computed

instance.build()

Computes the quickhull of all the points stored in the instance

time complexity O(n log n)

instance.collectFaces(skipTriangulation)

params

  • skipTriangulation {Boolean} (default: false) True to skip the triangulation and return n-vertices faces

returns

An array of 3-element arrays (or n-element arrays if skipTriangulation = true) which are the faces of the convex hull

Example

import qh from 'quickhull3d'
const points = [
  [0, 1, 0],
  [1, -1, 1],
  [-1, -1, 1],
  [0, -1, -1]
]

qh(points)
// output:
// [ [ 2, 0, 3 ], [ 0, 1, 3 ], [ 2, 1, 0 ], [ 2, 3, 1 ] ]
// 1st face:
//   points[2] = [-1, -1, 1]
//   points[0] = [0, 1, 0]
//   points[3] = [0, -1, -1]
//   normal = (points[0] - points[2]) x (points[3] - points[2])

Using the constructor:

import { QuickHull } from 'quickhull3d'
const points = [
  [0, 1, 0],
  [1, -1, 1],
  [-1, -1, 1],
  [0, -1, -1]
];
const instance = new QuickHull(points)
instance.build()
instance.collectFaces()   // returns an array of 3-element arrays

Benchmarks

Specs:

MacBook Pro (Retina, Mid 2012)
2.3 GHz Intel Core i7
8 GB 1600 MHz DDR3
NVIDIA GeForce GT 650M 1024 MB

Versus convex-hull

// LEGEND: program:numberOfPoints
quickhull3d:100 x 6,212 ops/sec 1.24% (92 runs sampled)
convexhull:100 x 2,507 ops/sec 1.20% (89 runs sampled)
quickhull3d:1000 x 1,171 ops/sec 0.93% (97 runs sampled)
convexhull:1000 x 361 ops/sec 1.38% (88 runs sampled)
quickhull3d:10000 x 190 ops/sec 1.33% (87 runs sampled)
convexhull:10000 x 32.04 ops/sec 2.37% (56 runs sampled)
quickhull3d:100000 x 11.90 ops/sec 6.34% (34 runs sampled)
convexhull:100000 x 2.81 ops/sec 2.17% (11 runs sampled)
quickhull3d:200000 x 5.11 ops/sec 10.05% (18 runs sampled)
convexhull:200000 x 1.23 ops/sec 3.33% (8 runs sampled)

quickhull3d vs convexhull

License

Mauricio Poppe. Licensed under the MIT license.

FOSSA Status

quickhull3d's People

Contributors

dependabot[bot] avatar fossabot avatar mauriciopoppe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

quickhull3d's Issues

Incorrect results for objects with duplicate vertices

After upgrading from version 3.0.0 to version 3.1.0, I've started getting incorrect results for hulls generated from triangulated meshes. These meshes are in triangle-soup format, so they have duplicate vertices. I'm able to repro the failure in my application by translating a cube. In some translations, it works correctly, but in other translations, it gives garbage faces.

The cube in question
image

Expected result (3.0.0) - 6 faces
image

Actual result (3.1.0) - 1 face
image

Minimal repro: https://jsfiddle.net/tqoazr9k/1/

quickhull3d does not always work if all points are on a same plane

I worked on JSCAd application lattice_sphere_cmp and made use of new geom3.fromPoints() which is based on quickhull3d. I realized that if all 3d points are on a same plane, the result might be wrong as in this bug.jscad simple JSCAD example:
image

I created JSCAD issue #1347, but was asked today to get quickhull3d fixed instead:
jscad/OpenJSCAD.org#1347 (comment)

Just using quickhull3d alone does show the bug as well (point 1 should appear in some faces, but does not):
jscad/OpenJSCAD.org#1347 (comment)

pi@raspberrypi5:~/quickhull3d $ node bug.js
[ [ 3, 0, 2 ], [ 4, 3, 2 ], [ 4, 2, 0 ], [ 4, 0, 3 ] ]
pi@raspberrypi5:~/quickhull3d $ cat bug.js 
import qh from 'quickhull3d'

const runner = qh.default

const pts = [[-8,1,0],[-7,4,0],[-6,5,-2],[-7,0,-4],[-8,0,-1]]

const faces = runner(pts)

console.log(faces)
pi@raspberrypi5:~/quickhull3d $ 

I already implemented fixes in the mentioned github issue — they are part of my lattice_sphere_cmp already. Basically a test is done whether all points are on a same plane. If not, normal quickhull3d is used. Otherwise a point known to be not on plane is added, then quickhull3d works nicely on the not all points on plane input, and finally all faces containing the added point are removed. Finally the result is the single face left, together with its inverse (to guarantee that face is visible from both sides). First animation shows what happens if only the remaining face is used in geom3, the second animation if both faces are used:
Peek_2024-06-09_01-05
Peek_2024-06-09_01-06

A good argument for having two faces in the result is looking at a tetrahedron where one edge is shortened until length 0:
image
The top and bottom face will remain.

The fix I have as discussed has to deal with non-integer distance of plane from origin, so comparison does not always work. I introduced a tolerance value for the functions that made it work, but I do not really like that.

For my lattice_sphere_cmp application all points are from ℤ³, and I used cross product instead of normalized normal vector to determine whether all points are on a same plane. That is clean, but all points in ℤ³ is not the normal use case for quickhull3d.

This is just issue report, no complete quickhull3d solution yet.
The code implemented can be found in these two diffs as well:
Hermann-SW/lattice_sphere_cmp@7834f9a#diff-93e00aa7bb721b3cc9d54a52895f396ada5a8dc0093c4c223ef9d7c39a1c145c
Hermann-SW/lattice_sphere_cmp@78225b5#diff-93e00aa7bb721b3cc9d54a52895f396ada5a8dc0093c4c223ef9d7c39a1c145c
The whole discussion on the algorithm is in this discord thread:
https://discord.com/channels/775984095250612234/1248251413629370389

Initial fromPointsConvexPlaneℤ3(pts) code is here:
Hermann-SW/lattice_sphere_cmp@6461507#diff-93e00aa7bb721b3cc9d54a52895f396ada5a8dc0093c4c223ef9d7c39a1c145c

display="centroid!=normal +face" demonstrates the code discussed to display selected faces:
image

hangs with simple 7 point hull

[ [ 104, 216, 53 ],
  [ 104, 217, 52 ],
  [ 105, 216, 52 ],
  [ 88, 187, 43 ],
  [ 89, 187, 44 ],
  [ 89, 188, 43 ],
  [ 90, 187, 43 ] ]

node -v

v0.10.38

convex-hull module calcs ok

[ [ 0, 1, 3 ],
  [ 1, 0, 2 ],
  [ 4, 0, 3 ],
  [ 0, 4, 2 ],
  [ 1, 5, 3 ],
  [ 5, 1, 2 ],
  [ 6, 4, 3 ],
  [ 5, 6, 3 ],
  [ 2, 4, 6 ],
  [ 5, 2, 6 ] ]

version 1.1.1

bug in doAdjacentMerge

If mergeType === MERGE_NON_CONVEX, then doAdjacentMerge doesn't really do anything, it just sets 'merge' variable to true and that's all. That's because, "if (merge) {...}" code block is in the wrong scope, it should be one level up (in while-loop, not in the else part of "if (mergeType === MERGE_NON_CONVEX)"). Original QuickHull3D.java code looks OK, the problem is javascript-version specific:

do {
   if (mergeType === MERGE_NON_CONVEX) {
       ....
      merge = true
   } else {
      .....
      if (merge) {
            debug('face merge')
       ....
       }
    }
} while(...)

Gives different set of faces to Matlab convhulln

Hello!

First of all, thank you for making this code accessible.

I've been transcribing some MATlab code into Javascript and need to find the triangle faces making up the convex hull of a set of vertices (in 3D coordinates). The MATlab code uses the convhulln function. This convhulln function also makes use of the qhull as implemented in http://www.qhull.org/. http://www.mathworks.com/help/matlab/ref/convhulln.html

This library doesn't seem to be outputting the same triangle faces to that of convulln and I'm not quite sure why. There are some faces that are similar but in general, most are not. The only similarity between convulln and quickhull-3d is they output the same number of faces that make up the convex hull (but these faces are not the same in general)

Any light you could shed on this would be greatly appreciated.

Here are the vertices I'm using and the results of both MATlab's convhulln and quickhull3d. Please note I've used a sorting algorithm to sort them in the from first vertices to last. This should not effect the actual triangle faces since I sorted them in exactly the same way in both the MATlab and JS code.
EDIT: The sorting function I implemented initially was incorrect. Have changed it to the correct one but still get different output (likely to do with the triangulation process implemented after convex hull is found)

vertices = [ 
  [ 0.9510565162951535, -0.3090169943749474, 0 ],
  [ 0.5877852522924731, -0.8090169943749475, 0 ],
  [ 6.123233995736766e-17, -1, 0 ],
  [ -0.5591929034707466, 0.8290375725550418, 0 ],
  [ -0.9510565162951535, -0.3090169943749475, 0 ],
  [ -0.9510565162951536, 0.3090169943749473, 0 ],
  [ -0.5877852522924732, 0.8090169943749473, 0 ],
  [ -1.8369701987210297e-16, 1, 0 ],
  [ 0.5877852522924729, 0.8090169943749476, 0 ],
  [ 0.9510565162951535, 0.3090169943749476, 0 ],
  [ 0.984807753012208, 0, -0.17364817766693033 ],
  [ 0.30432233187297814, -0.9366078308002486, -0.17364817766693033 ],
  [ -0.796726208379082, -0.5788554735638644, -0.17364817766693033 ],
  [ -0.7967262083790821, 0.5788554735638641, -0.17364817766693033 ],
  [ 0.3043223318729779, 0.9366078308002487, -0.17364817766693033 ],
  [ 0.5000000000000001, -0.5, 0.7071067811865475 ],
  [ -0.5, -0.5000000000000001, 0.7071067811865475 ],
  [ -0.5000000000000001, 0.5, 0.7071067811865475 ],
  [ 0.4999999999999999, 0.5000000000000001, 0.7071067811865475 ],
  [ 6.123233995736766e-17, 0, 1 ] 
]

triangles from quickhull3d:
dim = 36x3

trianglesqh = [ 
  [ 0, 10, 11 ],
  [ 1, 0, 11 ],
  [ 1, 15, 0 ],
  [ 2, 1, 11 ],
  [ 2, 11, 12 ],
  [ 2, 15, 1 ],
  [ 5, 13, 6 ],
  [ 5, 17, 4 ],
  [ 6, 13, 3 ],
  [ 7, 14, 8 ],
  [ 7, 17, 3 ],
  [ 7, 18, 17 ],
  [ 9, 10, 0 ],
  [ 9, 18, 8 ],
  [ 10, 9, 8 ],
  [ 10, 14, 12 ],
  [ 11, 10, 12 ],
  [ 12, 13, 4 ],
  [ 13, 5, 4 ],
  [ 13, 14, 3 ],
  [ 14, 7, 3 ],
  [ 14, 10, 8 ],
  [ 14, 13, 12 ],
  [ 15, 2, 16 ],
  [ 15, 18, 0 ],
  [ 15, 19, 18 ],
  [ 16, 2, 12 ],
  [ 16, 12, 4 ],
  [ 17, 5, 6 ],
  [ 17, 6, 3 ],
  [ 17, 16, 4 ],
  [ 17, 19, 16 ],
  [ 18, 7, 8 ],
  [ 18, 9, 0 ],
  [ 18, 19, 17 ],
  [ 19, 15, 16 ] 
]

triangles from MATlab:
dim = 36x3

trianglesm = [ 
  [ 0, 1, 11 ],
  [ 0, 9, 18 ],
  [ 0, 10, 9 ],
  [ 0, 11, 10 ],
  [ 0, 15, 1 ],
  [ 0, 18, 15 ],
  [ 1, 2, 11 ],
  [ 1, 18, 2 ],
  [ 2, 3, 11 ],
  [ 2, 15, 16 ],
  [ 2, 16, 3 ],
  [ 3, 4, 12 ],
  [ 3, 12, 11 ],
  [ 3, 16, 4 ],
  [ 4, 5, 12 ],
  [ 4, 17, 5 ],
  [ 5, 8, 13 ],
  [ 5, 13, 12 ],
  [ 5, 16, 17 ],
  [ 5, 17, 6 ],
  [ 6, 7, 14 ],
  [ 6, 14, 13 ],
  [ 6, 17, 7 ],
  [ 7, 8, 14 ],
  [ 7, 17, 18 ],
  [ 7, 18, 8 ],
  [ 8, 9, 10 ],
  [ 8, 10, 14 ],
  [ 8, 18, 9 ],
  [ 10, 11, 14 ],
  [ 11, 12, 13 ],
  [ 11, 13, 14 ],
  [ 15, 18, 19 ],
  [ 15, 19, 16 ],
  [ 16, 19, 17 ],
  [ 17, 19, 18 ]
 ]

And here are images of the convex hull (done in matlab)

Matlab Hull
hull

Quickhull3d Hull
hullwrong

Holes in the convex hull.

Crashes on these points

> var qh = require('quickhull3d');
> var pts =
[ [ 38, 89, 0 ],
  [ 85, 91, 0 ],
  [ 94, 89, 0 ],
  [ 70, 40, 0 ],
  [ 63, 90, 0 ],
  [ 60, 52, 0 ],
  [ 20, 16, 0 ],
  [ 38, 13, 0 ],
  [ 25, 82, 0 ],
  [ 7, 80, 0 ],
  [ 28, 80, 0 ],
  [ 43, 64, 0 ],
  [ 48, 68, 0 ],
  [ 86, 64, 0 ],
  [ 52, 60, 0 ],
  [ 56, 75, 0 ],
  [ 47, 3, 0 ],
  [ 68, 13, 0 ],
  [ 50, 44, 0 ],
  [ 82, 54, 0 ],
  [ 96, 45, 0 ],
  [ 85, 1, 0 ],
  [ 30, 24, 0 ],
  [ 54, 3, 0 ],
  [ 65, 95, 0 ],
  [ 95, 7, 0 ],
  [ 97, 42, 0 ],
  [ 44, 73, 0 ],
  [ 79, 55, 0 ],
  [ 72, 34, 0 ],
  [ 8, 18, 0 ],
  [ 79, 46, 0 ],
  [ 92, 32, 0 ],
  [ 61, 29, 0 ],
  [ 21, 7, 0 ],
  [ 24, 25, 0 ],
  [ 41, 32, 0 ],
  [ 72, 91, 0 ],
  [ 1, 63, 0 ],
  [ 18, 8, 0 ],
  [ 44, 99, 0 ],
  [ 59, 90, 0 ],
  [ 12, 15, 0 ],
  [ 81, 60, 0 ],
  [ 21, 10, 0 ],
  [ 9, 71, 0 ],
  [ 12, 86, 0 ],
  [ 99, 41, 0 ],
  [ 32, 48, 0 ],
  [ 40, 20, 0 ],
  [ 47, 89, 0 ],
  [ 41, 9, 0 ],
  [ 29, 80, 0 ],
  [ 56, 63, 0 ],
  [ 36, 11, 0 ],
  [ 25, 14, 0 ],
  [ 44, 40, 0 ],
  [ 60, 41, 0 ],
  [ 32, 81, 0 ],
  [ 64, 56, 0 ],
  [ 41, 90, 0 ],
  [ 29, 73, 0 ],
  [ 68, 97, 0 ],
  [ 91, 90, 0 ],
  [ 57, 0, 0 ],
  [ 67, 34, 0 ],
  [ 64, 8, 0 ],
  [ 66, 65, 0 ],
  [ 39, 90, 0 ],
  [ 72, 62, 0 ],
  [ 63, 13, 0 ],
  [ 45, 94, 0 ],
  [ 2, 34, 0 ] ];
> qh(pts);
TypeError: Cannot read property 'point' of undefined
    at QuickHull.createInitialSimplex (/tmp/node-v6.11.1-linux-x64/qhull3/node_modules/quickhull3d/dist/QuickHull.js:398:32)
    at QuickHull.build (/tmp/node-v6.11.1-linux-x64/qhull3/node_modules/quickhull3d/dist/QuickHull.js:835:12)
    at runner (/tmp/node-v6.11.1-linux-x64/qhull3/node_modules/quickhull3d/dist/index.js:18:12)
    at repl:1:1
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)

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.