Message Boards Message Boards

5
|
18667 Views
|
5 Replies
|
18 Total Likes
View groups...
Share
Share this post:

How to make a hole in a Graphics3D object?

My question is that: how to make a hole in a Graphics3D object? For example, there are two objects ,cub1 and cub2:

cub1=Cuboid[{0,0,0},{20,2,20}];
cub2=Cuboid[{12,0,8},{17,2,17}];
Graphics3D[{cub1,cub2}]

enter image description here

I want to make a window at the positon of the cub2, like under.

DiscretizeRegion[RegionDifference[cub1,cub2]]

enter image description here

But this object is obtained by use “DiscretizeRegion”, it is not a Graphics3D object, it is a MeshRegion object, and it make system too slow. Then, how to get a Graphics3D object?

POSTED BY: yang l
5 Replies

Let's start from your code:

cub1 = Cuboid[{0, 0, 0}, {20, 2, 20}];
cub2 = Cuboid[{12, 0, 8}, {17, 2, 17}];
Graphics3D[{cub1, cub2}];
reg = DiscretizeRegion[RegionDifference[cub1, cub2]]

You have a lot of tetrahedrons:

MeshCells[reg, 3] // Length

10093

And a lot of polygons:

MeshCells[reg, 2] // Length  

22096

You do not need all that for visual, - so get the surface (boundary):

surface = BoundaryDiscretizeRegion[reg];
polygons = MeshCells[surface, 2];
polygons // Length

3820

Now so few polygons! Get the coordinates of the mesh too:

points = MeshCoordinates[surface];
points // Length
> 1910

And here you go:

Graphics3D[{EdgeForm[Gray], 
  GraphicsComplex[points, Polygon[polygons /. Polygon[x_] -> x]]}, 
 Boxed -> False, Lighting -> "Neutral"]

enter image description here

POSTED BY: Vitaliy Kaurov

another option (made the cube little thicker to make it more clear ) there are many more options in the RegionPlot3D you can adjust also.

  cub1 = Cuboid[{0, 0, 0}, {20, 6, 20}];
  cub2 = Cuboid[{12, 0, 8}, {17, 6, 17}];
  r2 = RegionDifference[cub1, cub2];
  RegionPlot3D[r2, PlotPoints -> 60, Mesh -> 10]

enter image description here

POSTED BY: Nasser M. Abbasi

Wow, Nasser, such a nice compact solution!

POSTED BY: Vitaliy Kaurov

You can also apply Show to the mesh region. Show will preserve the styling and meshing.

POSTED BY: Charles Pooh

Thanks, it is a good solution. Wolfram should support Boolean operation for Graphics3D objects in future.

POSTED BY: yang l
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract