quickhull3d: 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: https://github.com/jscad/OpenJSCAD.org/issues/1347#issuecomment-2157155171

Just using quickhull3d alone does show the bug as well (point 1 should appear in some faces, but does not): https://github.com/jscad/OpenJSCAD.org/issues/1347#issuecomment-2161074878

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: https://github.com/Hermann-SW/lattice_sphere_cmp/commit/7834f9a03ba058a7122af323d1e978cf289411c9#diff-93e00aa7bb721b3cc9d54a52895f396ada5a8dc0093c4c223ef9d7c39a1c145c https://github.com/Hermann-SW/lattice_sphere_cmp/commit/78225b5f94c8103bfdfdaf699b288428eb7497ca#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:
https://github.com/Hermann-SW/lattice_sphere_cmp/commit/6461507121b76790a2c54072ec9b162722099542#diff-93e00aa7bb721b3cc9d54a52895f396ada5a8dc0093c4c223ef9d7c39a1c145c

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Thanks for the graph, for the case and the expected output, I think this library can do a better job for this scenario so I have an algorithm that I haven’t implemented yet.

  • Before computing the initial tetrahedron check if the input is degenerate i.e. check if all the points are in the same plane
    • Pick 3 points and get a plane, project all the points towards the plane i.e. find the distance to the plane, if the projection distance is 0 for all the points then it means that all the points are in the same plane
  • Translate the origin so that one of the 3 points becomes the origin, rotate the frame so that the plane is parallel to a plane formed by any 2 of the 3 axis e.g. any of the xy, xz, yz planes
  • Discard the dimension where all the values are 0.
  • Do a 2d convex hull
  • Create a face where the indexes are the indexes of the convex hull points