10

I am trying to find a convex hull command for a Graphics3D object. Does it exist in Mathematica?

egg = Graphics3D[Cuboid /@ Position[DiskMatrix[{12, 10, 8}], 1]]

Mathematica graphics

The ConvexHull command of the "ComputationalGeometry`" package does not work on 3D objects:

Needs["ComputationalGeometry`"]
ConvexHull[egg]

Mathematica graphics

István Zachar
  • 47,032
  • 20
  • 143
  • 291
hhh
  • 2,603
  • 2
  • 19
  • 30

3 Answers3

17

Since ConvexHull doesn't support 3D points (and you incorrectly tried to compute the ConvexHull of the Graphics object) your code didn't work.

Here is one way to do what I think you want (I left out of the step of the Cuboids but if you want that basically just offset your convex hull).

Needs["TetGenLink`"]

pos = Position[DiskMatrix[{12, 10, 8}], 1];
Graphics3D[Point@pos]

Mathematica graphics

{pts, surface} = TetGenConvexHull[pos];
Graphics3D[GraphicsComplex[pts, Polygon[surface]]]

Mathematica graphics

If you do want to compute the ConvexHull of your Graphics3D object you can extract the points from the InputForm. This is fragile and will only work for this simple case.

pos = InputForm[egg][[1, 1, All, 1]]
s0rce
  • 9,632
  • 4
  • 45
  • 78
  • Thank you +1, solved. How did you realize this? Cannot understand why mathematica has no ConvexHull3D[...] command. – hhh Mar 22 '13 at 15:27
  • 4
    The documentation for ConvexHull shows the syntax: ConvexHull[{{x1,y1},{x2,y2},...}] no z coordinate implies that its 2D (x,y) only. Searching ConvexHull in the documentation doesn't appear to simply direct you to TetGenLink unfortunately. I just knew about it. – s0rce Mar 22 '13 at 15:31
  • I tried to do similar thing but for some reason it plots none, why? http://i.stack.imgur.com/fbthu.png – hhh Mar 22 '13 at 15:48
  • Your statement with Position doesn't seem to return anything for me. None of the statements are 1 they are Cuboid[{_,_,_}] – s0rce Mar 22 '13 at 15:57
  • 3
    @sOrce, The fact that this link is missing is an oversight on my part. I'll add a link such that when one searches for ConvexHull that the TetGenLink example is found. Thanks. –  Mar 24 '13 at 09:12
8

Without using the TetGen package:

Needs["ComputationalGeometry`"];
egg = Graphics3D[{Red, Cuboid /@ Position[DiskMatrix[{3, 4, 5}], 1]}];

v = Flatten[Cases[egg, Cuboid[x_]:> Outer[Plus, Tuples[{0, 1}, 3], {x}, 1][[All, 1]], 3], 1];
prism@v_ = Cases[ComputationalGeometry`Methods`ConvexHull3D[v], _GraphicsComplex, Infinity];


GraphicsRow@{egg, 
            Show[egg, Graphics3D[{Opacity[.3], Yellow, prism@v}]], 
            Graphics3D[{Opacity[1], Yellow, prism@v}]}

enter image description here

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • 1
    That does not appear to be a well-formed polyhedron: it has edges passing through each other and faces that overlap. –  Nov 26 '13 at 20:22
  • @RahulNarain Yes, it seems so. Apparently a bug in ConvexHull3D[] . What a shame! – Dr. belisarius Nov 26 '13 at 20:46
  • 1
    Well, dealing with degenerate cases like collinear and coplanar vertices is the hardest part to get right in computational geometry algorithms, and this point set has them in spades. That's probably why they kept the function internal... –  Nov 26 '13 at 20:51
8

In Version 10, there is now the built-in ConvexHullMesh to do exactly this.

pos = Position[DiskMatrix[{12, 10, 8}], 1];

To get the 3D convex hull:

ConvexHullMesh[pos]

Mathematica graphics

RunnyKine
  • 33,088
  • 3
  • 109
  • 176