Skip to content

Instantly share code, notes, and snippets.

@marcusps
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusps/1a5e0cd001984b064892 to your computer and use it in GitHub Desktop.
Save marcusps/1a5e0cd001984b064892 to your computer and use it in GitHub Desktop.
ConvexHullMesh bug in Mathematica

ConvexHullMesh sometimes excludes valid points from convex hull

I have a sequence of polytopes that I am trying to visualize, and I find that ConvexHullMesh sometimes excludes points from the convex hull, and it does so inconsistently.

In particular, notice the three convex hulls rendered in the PNG.

From left to right, the set of points is changing slightly -- a new plane (linear constraint) is added each time, which removes some vertices, but adds new ones as well. However, the convex hull drawn by Mathematica excludes different valid points in the first two plots, and includes them in the last one.

Regardless of how these points are generated, they should all be included in their convex hull (by definition), so this is clearly a bug.

UPDATE:

I got a reply from Mathematica indicating that it is a numerical precision issue (the Mathematica implementation of the convex hull seems to work at a lower precision than the data types used). Two work arounds are:

  1. Scale the data by a large number
  2. Approximate the data by rationals

In other words, use

PlotPolytope2[n_] := 
 Module[{V = 1000 Sort[Import["v-" <> ToString[n] <> ".csv"]]},
  Show[ConvexHullMesh[V[[All, Range[1, 3]]]], 
   Graphics3D[{Black, Point[V[[All, Range[1, 3]]]]}]]]

or

PlotPolytope3[n_] := 
 Module[{V = Rationalize[#, 10^-10] &@ Sort[Import["v-" <> ToString[n] <> ".csv"]]}, 
  Show[ConvexHullMesh[V[[All, Range[1, 3]]]], 
   Graphics3D[{Black, Point[V[[All, Range[1, 3]]]]}]]]
PlotPolytope[n_] :=
Module[
{
V = Sort[
Import["v-" <> ToString[n - 1] <> ".csv"]]
},
Show[
ConvexHullMesh[V[[All, Range[1, 3]]]],
Graphics3D[{Black, Point[V[[All, Range[1, 3]]]]}]
]
]
GraphicsGrid[
{
{PlotPolytope[8], PlotPolytope[9], PlotPolytope[10]}
}
]
.0005165098721880588 .0015515669789670711 -.01251025175147419 16 12 5 .16120820750771797
-.0005165098721890664 -.0015515669789650936 .012510251751474298 15 11 6 .16120820744984357
-.0008507440598360136 .0026544849042545652 -.01251025175147419 12 10 5 .16120186293323902
.0008507440598364653 -.0026544849042541953 .012510251751474298 11 9 6 .16120186287120156
-.003970392151389278 -.006983617490210843 -.01251025175147419 1 3 5 .16104851126792513
.003970392151389278 .006983617490210843 .012510251751474298 2 4 6 .16104851123251343
.0012472199968024589 -.002816462923055824 -.01251025175147419 14 16 5 .161038549270688
-.001247219996801969 .0028164629230615834 .012510251751474298 13 15 6 .161038549197668
.003970392151389278 -.006983617490210843 -.004717599096226144 2 3 14 .16101944389617268
-.003970392151389278 .006983617490210843 .0047175990962213836 1 4 13 .1610194437276739
.003970392151389278 -.0011654777781913064 .012510251751474298 2 9 6 .16100255312936146
-.003970392151389278 .0011654777781923303 -.01251025175147419 1 10 5 .1610025530987726
-.003970392151389278 -.006983617490210843 -.003342764532531761 1 3 7 .16100160827470855
.003970392151389278 .006983617490210843 .0033427645325304007 2 4 8 .16100160815852366
.003970392151389278 -.006983617490210843 .0020931929174617123 2 3 9 .1609917116527122
-.003970392151389278 .006983617490210843 -.002093192917462812 1 4 10 .16099171158751713
.0009729725668877669 -.006983617490210843 -.01251025175147419 14 3 5 .1609905867999351
-.0009729725668865791 .006983617490210843 .012510251751474298 13 4 6 .1609905867834729
-.003970392151389278 -.0016363876346678584 .006192450476481807 1 7 13 .1609721895336648
.003970392151389278 .001636387634669539 -.006192450476480725 2 8 14 .16097218951714043
-.0020784569734174662 -.002585445223684903 .01044595588029419 7 11 15 .16092570511231621
.0020784569734163833 .0025854452236878252 -.010445955880293846 8 12 16 .16092570506852957
-.0009128510997235257 -.0069836174902108425 .0062663093235872745 7 3 9 .1609196957407647
.0009128510997239935 .006983617490210844 -.006266309323586592 8 4 10 .16091969570272052
.002521868498427729 .002038826984607609 -.01002715943079007 8 16 14 .16086912785400764
-.0025218684984284517 -.0020388269846053006 .01002715943079031 7 15 13 .16086912783239612
.002521868498427729 .002038826984607609 -.01002715943079007 16 8 14 .160869127801118
.0009106728339929386 .005780703106575692 -.008418199800655323 8 10 12 .16082243587667525
-.000910672833992476 -.005780703106577376 .008418199800652858 7 9 11 .16082243576706376
.0009106728339929386 .005780703106575692 -.008418199800655323 8 12 10 .1608224357149947
-.0014833592005116807 -.0007716420284615859 .012510251751474298 13 11 6 .16122059270789346
.0014833592005109972 .000771642028463607 -.01251025175147419 14 12 5 .16122059268798788
-.0008507440598360136 .0026544849042545652 -.01251025175147419 12 10 5 .16120186293323902
.0008507440598364653 -.0026544849042541953 .012510251751474298 11 9 6 .16120186287120156
-.003970392151389278 -.006983617490210843 -.01251025175147419 1 3 5 .16104851126792513
.003970392151389278 .006983617490210843 .012510251751474298 2 4 6 .16104851123251343
.003970392151389278 -.006983617490210843 -.004717599096226144 2 3 14 .16101944389617268
-.003970392151389278 .006983617490210843 .0047175990962213836 1 4 13 .1610194437276739
.003970392151389278 -.0011654777781913064 .012510251751474298 2 9 6 .16100255312936146
-.003970392151389278 .0011654777781923303 -.01251025175147419 1 10 5 .1610025530987726
-.003970392151389278 -.006983617490210843 -.003342764532531761 1 3 7 .16100160827470855
.003970392151389278 .006983617490210843 .0033427645325304007 2 4 8 .16100160815852366
.003970392151389278 -.006983617490210843 .0020931929174617123 2 3 9 .1609917116527122
-.003970392151389278 .006983617490210843 -.002093192917462812 1 4 10 .16099171158751713
.0009729725668877669 -.006983617490210843 -.01251025175147419 14 3 5 .1609905867999351
-.0009729725668865791 .006983617490210843 .012510251751474298 13 4 6 .1609905867834729
-.003970392151389278 -.0016363876346678584 .006192450476481807 1 7 13 .1609721895336648
.003970392151389278 .001636387634669539 -.006192450476480725 2 8 14 .16097218951714043
.0022506974573605894 .0021141656905141154 -.010745036572253047 8 12 14 .16095979976856611
-.002250697457361473 -.002114165690511684 .010745036572253062 7 11 13 .1609597997669677
-.0009128510997235257 -.0069836174902108425 .0062663093235872745 7 3 9 .1609196957407647
.0009128510997239935 .006983617490210844 -.006266309323586592 8 4 10 .16091969570272052
.0009106728339929386 .005780703106575692 -.008418199800655323 8 10 12 .16082243587667525
-.000910672833992476 -.005780703106577376 .008418199800652858 7 9 11 .16082243576706376
.0009106728339929386 .005780703106575692 -.008418199800655323 8 12 10 .1608224357149947
.0014833592005109972 .000771642028463607 -.01251025175147419 14 12 5 .16122059268798788
-.0005165098721890664 -.0015515669789650936 .012510251751474298 15 11 6 .16120820744984357
-.0008507440598360136 .0026544849042545652 -.01251025175147419 12 10 5 .16120186293323902
.0008507440598364653 -.0026544849042541953 .012510251751474298 11 9 6 .16120186287120156
-.003970392151389278 -.006983617490210843 -.01251025175147419 1 3 5 .16104851126792513
.003970392151389278 .006983617490210843 .012510251751474298 2 4 6 .16104851123251343
-.001247219996801969 .0028164629230615834 .012510251751474298 13 15 6 .161038549197668
.003970392151389278 -.006983617490210843 -.004717599096226144 2 3 14 .16101944389617268
-.003970392151389278 .006983617490210843 .0047175990962213836 1 4 13 .1610194437276739
.003970392151389278 -.0011654777781913064 .012510251751474298 2 9 6 .16100255312936146
-.003970392151389278 .0011654777781923303 -.01251025175147419 1 10 5 .1610025530987726
-.003970392151389278 -.006983617490210843 -.003342764532531761 1 3 7 .16100160827470855
.003970392151389278 .006983617490210843 .0033427645325304007 2 4 8 .16100160815852366
.003970392151389278 -.006983617490210843 .0020931929174617123 2 3 9 .1609917116527122
-.003970392151389278 .006983617490210843 -.002093192917462812 1 4 10 .16099171158751713
.0009729725668877669 -.006983617490210843 -.01251025175147419 14 3 5 .1609905867999351
-.0009729725668865791 .006983617490210843 .012510251751474298 13 4 6 .1609905867834729
-.003970392151389278 -.0016363876346678584 .006192450476481807 1 7 13 .1609721895336648
.003970392151389278 .001636387634669539 -.006192450476480725 2 8 14 .16097218951714043
.0022506974573605894 .0021141656905141154 -.010745036572253047 8 12 14 .16095979976856611
-.0020784569734174662 -.002585445223684903 .01044595588029419 7 11 15 .16092570511231621
-.0009128510997235257 -.0069836174902108425 .0062663093235872745 7 3 9 .1609196957407647
.0009128510997239935 .006983617490210844 -.006266309323586592 8 4 10 .16091969570272052
-.0025218684984284517 -.0020388269846053006 .01002715943079031 7 15 13 .16086912783239612
.0009106728339929386 .005780703106575692 -.008418199800655323 8 10 12 .16082243587667525
-.000910672833992476 -.005780703106577376 .008418199800652858 7 9 11 .16082243576706376
.0009106728339929386 .005780703106575692 -.008418199800655323 8 12 10 .1608224357149947
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment