7

I wish to use RegionDifference to take a cube shape out of a cylinder. First I make the cylinder and cube and combine them in RegionUnion.

reg1 = Cylinder[{{0, 0, 0}, {10, 0, 0}}, 0.5];
reg2 = Cuboid[{5, 0, 0}, {10, 1, 1}];
Region[RegionUnion[reg1, reg2], Axes -> True]

Mathematica graphics

So this looks good so far. Now I wish to take the cuboid out of the cylinder leaving a notch in the cylinder. I try

reg = RegionDifference[reg1, reg2];
Region[reg, Axes -> True, PlotRange -> All]

Mathematica graphics

My cylinder is chopped off short and given a bad end (away from the subtraction). Is there a workaround?

Version 11.3 for windows.

Hugh
  • 16,387
  • 3
  • 31
  • 83
  • 1
    Wow, that's really weird. Please contact support. Honestly, I am quite disappointed with the almost nonexistent usability of the BooleanRegion facilities. – Henrik Schumacher Mar 14 '19 at 18:37
  • 2
    I have sent it off to support.I agree about being fed up. Second time in two days you have had to help me out -for which I am very grateful. – Hugh Mar 14 '19 at 19:24

2 Answers2

8

Please note the RegionBounds:

reg1 = Cylinder[{{0, 0, 0}, {10, 0, 0}}, 0.5];
reg2 = Cuboid[{5, 0, 0}, {10, 1, 1}];
reg = RegionDifference[reg1, reg2];

bounds = RegionBounds@reg;
Region[reg, Axes -> True, PlotRange -> bounds]

enter image description here

rmw
  • 1,950
  • 6
  • 9
  • 1
    Whoa. Why did PlotRange -> All not work? Anyways, good job! – Henrik Schumacher Mar 14 '19 at 21:43
  • I put in PlotRange All because I wondered if it was a plotting problem. Are there known issues with PlotRange? – Hugh Mar 14 '19 at 22:31
  • @Hugh How Mathematica works is probably only known to the developers. For the user remains only trial and error. But I have already encountered this problem earlier. You have sent it off to support, that's ok. – rmw Mar 15 '19 at 09:05
6

This seems to be a viable workaround although it is a shame that we have to discretize the cylinder that early.

reg1 = BoundaryDiscretizeRegion[Cylinder[{{0, 0, 0}, {10, 0, 0}}, 0.5], MaxCellMeasure -> 0.001];
reg2 = BoundaryDiscretizeRegion[Cuboid[{5, 0, 0}, {10, 1, 1}]];
reg = RegionDifference[reg1, reg2]

enter image description here

As a rule of thumb, I would strongly discourage applying boolean operations to graphics primitives and everything else which is neither a MeshRegion nor a BoundaryMeshRegion.

Jason B.
  • 68,381
  • 3
  • 139
  • 286
Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309