# Converting a mesh to constructive solid geometry

I have a mesh of a bowl that has the perfect shape of half a sphere. I want to easily convert it to the containing sphere solid and a box solid that will be subtracted from it.

Googling mesh to solid shows that in various tools such as 3ds Max, Fusion, etc., manual approximation of where the sphere might go is created manually by visually comparing to the mesh or the cross section when creating the solid but I am looking for the minimum enclosing sphere and box to be generated/calculated by the software.

Source file format is of course not an issue, it can be any known mesh file.

The answers by @markshancock and @fred-dot-u describe the process of creating a CAD primitive sphere and subtracting a cube; this is a trivial task in most any 3D CAD program. You could manually measure the mesh to determine its dimensions, and frankly, I would recommend this approach. (If you have a large task like converting 100,000 half-sphere meshes, I'd re-think the approach and go to the source that generated the meshes in the first place!)

However, these don't address converting the mesh to a primitive, which is normally not possible for arbitrary meshes (therefore a CAD program will not have such a function), and much more difficult than simply measuring and re-creating.

You assert that your mesh file "has the perfect shape of half a sphere". A mesh is basically on a collection of vertices, joined by edges and faces. The vertices are defined by numeric coordinates, and in a computer will always have some rounding errors, and the linear edges and planar faces are most definitely not round. Therefore, it may be incredibly close, but it will most definitely not be perfect.

In this very particular case, the diameter of the sphere is the same as the diameter of the circular face of your half-sphere. You could plausibly write a simple program that would locate the maximum distance between any two vertices in your mesh and use that for the diameter. The midpoint of the line between those two points would be the center of the sphere. This should produce a good result but may not be "exact" depending on the details of the mesh.

I won't try to explain how to create the actual program as that is not in the scope of 3D printing.

The calculations for your objective could be considered simple geometry, although the results in terms of formulae are a bit more complex than simple, but not by much.

According to Quora, the foundation for this goal is that the cube's eight vertices will be coincidental to the sphere's surface. If one desires to print a 3D object with this form, such an object may fail the requirement of being manifold, but may not, depending on the floating point operations of the software being used.

I found a simplistic formula which provides the radius of the sphere given the length of the side of the cube.

\$fn = 90;
edge = 10;
cube([edge, edge, edge], center = true);
sphere_radius = sqrt((3 * pow((edge/2), 2)));


The above code is done in OpenSCAD, resulting in this image with the sphere made transparent for clarity:

Translated into general english, it appears that one can take the edge length, divided by 2, then take the square of that result and triple it. Take the square root of that value and it becomes the radius of the sphere.

The above answer is courtesy of Math Forum and is represented verbatim as such:

        ____________________________
D = \| (L/2)^2 + (L/2)^2 + (L/2)^2


The letter D in this case appears to be slightly misrepresented as diameter when it should be referred to as radius.

As part of this fun exercise, I also subtracted the cube from the sphere, slicing it in half for visibility, resulting in this image:

So it depends a lot on what you want to do.

1. If you want to mathematically calculate what cube fits in what size sphere, that is straight forward. The diagonal for the cube has to fit into the diameter of the shere. For a cube with edge 'l' and a shpere with diamamer 'd' then <= d.

2. If you want to model a cube and a sphere in CAD (or one inside the other) you just do it. They are both primitives. Takes less than a minute.

1. You can use a mesh in a design too. I would assume if you are wanting to use a mesh, the mesh must contain something other than just a smooth bowl or what the point of the mesh? Fusion 360 allows you to import a mesh and play with it just like that and combine it with model objects (just like the box).

2. You can actually convert a mesh into a solid model if you want to too. Fusion 360 CAD has ways to help you convert that mesh to a sculpt. First create a sculpt object that represents the shape of the mesh object (like a sphere or a quadraball). Next import the mesh then collapse the model over the mesh (using it like a model to form a mold). You can then delett the mesh and use the molded sculpt.