# How can I generate a Swiss-Cheese type region and render it transparent?

I am trying to visualize a percolation model that is equivalent to a swiss-cheese where the holes are conducting. At some point, the size of the holes gets large enough for them to touch and a percolating network of holes pervades the sample volume. I would like to visualize the percolated network of holes in a transparent body. I can´t seem to make it work so that i have a nicely rendered inner surface that can be seen through the semitransparent body.

I started with this, using a brute force approach to get a volume filled with non-touching spheres (taken from a different thread: Randomly packing spheres of fixed radius within a cube) and then increasing their size:

spheres = {};
cubesize = 1;(*size of the sample volume*)
diameter = 0.15;(*diameter of the small spheres*)
multi = 1.4;(*multiply small diameter with this to get large diameter*)
number = 10; (*number of spheres within the cube*)
spheredistance = 2.; (*distance of the spheres in multiples of the small diameter*)
Dynamic[Length[spheres]]
While[Length[spheres] < number,
s = RandomReal[{diameter, cubesize - diameter}, 3];
If[And @@ (Norm[# - s] > diameter*spheredistance & /@ spheres),
AppendTo[spheres, s]]];
{Graphics3D[{Opacity[0.4], Specularity[0.4],
smallPockets = Ball[#, diameter] & /@ spheres},
PlotRange -> {{cubesize*.05, cubesize*.95}, {cubesize*.05,
cubesize*.95}, {cubesize*.05, cubesize*.95}}, Boxed -> True,
BoxStyle -> Directive[Black, Thin, Dashed]],
Graphics3D[{Opacity[0.7], Specularity[0.1],
largePockets = Ball[#, diameter*multi] & /@ spheres},
PlotRange -> {{cubesize*.02, cubesize*.98}, {cubesize*.02,
cubesize*.98}, {cubesize*.02, cubesize*.98}}, Boxed -> True,
BoxStyle -> Directive[Black, Thin, Dashed]]}


giving:

My attempt to produce the inverse and make it transparent was the following:

largePocketsRegion = largePockets // Region /@ # &;
cube = Cuboid[{0, 0, 0}, {cubesize, cubesize, cubesize}] // Region;
cheese = Fold[RegionDifference, cube, largePocketsRegion ]


giving something like this:

Now I am unable to make this render nicely and transparent. Is there a way to do that?

• May 19 at 13:58
• Slightly related. May 19 at 13:58
• @J.M., Ah, that's the one I remembered... May 19 at 14:07
• Thanks for these! the suggestions in this one: mathematica.stackexchange.com/q/48537/51092 are very helpful and work right away if i use the positions of the pockets instead of a hexagonal arrangement. May 19 at 14:35

Needs["NDSolveFEM"];
Needs["OpenCascadeLink"];
"ShapeSurfaceMeshOptions" -> {"AngularDeflection" -> 0.1}];
RegionPlot3D[BoundaryMeshRegion[bm], Boxed -> False,
PlotStyle -> Opacity[.2]]


• Another example

Thanks @user21.

Needs["OpenCascadeLink"];
Fold[RegionDifference, Cube[],
Ball[#, .1] & /@ RandomPoint[Cube[], 100]];
"ShapeSurfaceMeshOptions" -> {"AngularDeflection" -> 0.1}];
mg = MeshRegion[bm];
Graphics3D[{EdgeForm[], FaceForm[{Opacity[.2], Yellow}], mg},
Boxed -> False]


• Although CSGRegion is faster,but upto the 13.0.1 version,it doesn't suport opacity.
reg = CSGRegion[
"Difference", {Cube[],
CSGRegion["Union", Ball[#, .1] & /@ RandomPoint[Cube[], 100]]},
BaseStyle -> Orange];
Graphics3D[{Opacity[.2], reg}, Lighting -> "ThreePoint",
Boxed -> False]


• BoundaryMeshRegion->MeshRegion May 20 at 7:51
• (+1). this is the way to go. It is more efficient to give the boolean region to OpenCascade then to give it the meshes. People have been 'educated' in doing that since the Boolean region stuff works better when the regions are discretized, May 20 at 14:19
• Here is a more efficient version: cube = Cuboid[{0, 0, 0}, {cubesize, cubesize, cubesize}]; cheese = Fold[RegionDifference, cube, largePockets]; shape = OpenCascadeShape[cheese]; bm = OpenCascadeShapeSurfaceMeshToBoundaryMesh[shape, "ShapeSurfaceMeshOptions" -> {"AngularDeflection" -> 0.1}]; RegionPlot3D[BoundaryMeshRegion[bm], Boxed -> False, PlotStyle -> Opacity[.2]] May 20 at 14:20
• @cvgmt: thanks, this produces the best quality! going for MeshRegion rather than BoundaryMeshRegion makes it also work for the 'smallPockets'. May 23 at 9:55

One way:

Show[BoundaryDiscretizeRegion[cheese, MaxCellMeasure -> 0.001]] /.
FaceForm[Directive[d__]] :> FaceForm[Directive[d, Opacity[0.5]]]


Another way:

RegionPlot3D[
BoundaryDiscretizeRegion[cheese, MaxCellMeasure -> 0.001],
PlotStyle -> Opacity[0.5]]


• thanks a lot, this works nicely! i had the feeling that i had already tried something like this and failed but it seems that i have used some wrong objects then. May 19 at 14:38
• @bertwood Maybe you tried something like RegionPlot3D[cheese, PlotStyle -> Opacity[0.5]], which fails for some reason. (I don't see why it should, if DiscretizeRegion and BoundaryDiscretizeRegion succeed.) May 19 at 14:54