Probable Bug in ConvexHullMesh
This is at least one bug, possibly more. Let me explain:
If we go one step further and use
Needs["TetGenLink`"]
tethull = TetGenConvexHull[pts];
bmr = BoundaryMeshRegion[tethull[[1]], {Polygon[tethull[[2]]]}]
BoundaryMeshRegion::binsect: "The boundary curves self-intersect or cross each other in BoundaryMeshRegion[{{1.,-0.999551,-0.000449248},{0.900969,-0.566116,-4.48799*10^-7},{0.222521,-1.97493,-4.48799*10^-7},<<46>>,{0.222521,1.8759,-0.433884},<<5751>>},<<1>>]"
So we know why ConvexHullMesh
failed, but I think ConvecHullMesh
could be a little more informative about that. The next question is why are there self intersections or crossings? This is much harder to say, I suspect that some interplay with the duplicate coordinates and TetGen goes south. That is going to take some time to track down. It seems the points are too regular for TetGen.
A possible workaround (depending on the application of this) is to perturbe the input data a bit:
npts = pts + RandomReal[10^-6*{-1, 1}, {Length[pts], 3}];
ConvexHullMesh[npts]
I had another look at this one. To me it seems that there is an issue within TetGen for this specific input. Let's delete the duplicate coordinats:
pf = {Cos[u], Sin[u] + Cos[v], Sin[v]};
data = Reap[ParametricPlot3D[Sow[pf], {u, 0, 2 Pi}, {v, -Pi, Pi}]][[2,
1]];
pts = Cases[data, {_?NumericQ, _?NumericQ, _?NumericQ}];
Graphics3D[Point[pts]];
Length[pts]
(*pts=DeleteDuplicates[pts];*)
pts = Region`Mesh`DeleteDuplicateCoordinates[pts][[1]];
Length[pts]
Lets export the coordinates and run tetgen on the command line:
Needs["TetGenLink`"]
inst = TetGenCreate[];
TetGenSetPoints[inst, pts];
TetGenExport["test.node", inst]
./tetgen -E test.node
When we reimport the result we get intersecting facets:
coords = Developer`ToPackedArray@
N@Import["test.1.node",
"Table"][[2 ;; -2]][[All, {2, 3, 4}]];
faces = Developer`ToPackedArray@
Import["test.1.face",
"Table"][[2 ;; -2]][[All, {2, 3, 4}]];
Length[coords]
{pts2, intersectingFacets} =
TetGenDetectIntersectingFacets[coords,
Developer`ToPackedArray@Partition[faces, 1]];
Graphics3D[GraphicsComplex[pts2, Polygon[intersectingFacets]]]
There is not much that can be done about this. I have informed the TetGen developer. Sorry about that.
In the current release, you can try the following:
pf = {Cos[u], Sin[u] + Cos[v], Sin[v]};
gr = ParametricPlot3D[pf, {u, 0, 2 Pi}, {v, -Pi, Pi}];
mr = DiscretizeGraphics[gr // Normal]
Disregard the message about Lighting
not supported, DiscretizeGraphics[gr //Normal]
will remove the duplicated points
Now this will work:
ConvexHullMesh[MeshCoordinates[mr]]