Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if box is covered by spheres

I have an axis alligned 3D box (cuboid), and a sphere at each of its vertices (each with different radius). How can I check if all points of the box are covered by any of the spheres?

I need an algorithm that is rather fast, and not necessarily exact. False negatives -- that is saying that the box is not covered when in fact it is -- are not a big problem, but still I would like to minimise this kind of errors. False positives are unacceptable.

Intended use is calculating volume of an object specified by a signed distance function. I'm recursively dividing space and if a given box is completely outside or inside of the object, I know I can stop recursion on this level. False negative would cause extra splitting of the box, but not an error in the result.

like image 812
cube Avatar asked May 14 '16 15:05

cube


People also ask

Can a box be a sphere?

Corrugated cardboard can't be formed into a sphere (it was specifically designed not to be able to warp), so these spheres would have to be made from another material. Styrofoam and plastic would be extremely unsustainable. Actual wood would be difficult to form into spheres.

Can a sphere fit inside a cube?

If s is the side length of the cube, we have that Vcube=s3. Notice that the largest possible sphere that can fit inside the cube is the inscribed sphere, which has radius 12s. Using the volume formula for a sphere, we find that Vsphere=43πr3=43πs38=π6s3.

How many spheres can you fit in a cube?

In a cube of 2x2x2 cm cube, we can fit one sphere. There are ( 20 x 20 x 20) / ( 2 x 2 x2) = 1000 cubes or spheres. Diameter of the sphere is 2 cm. In 20 cm side we can place 10 spheres .


1 Answers

(While I'm trying to find a geometrically optimal version, here's a simple idea that's sure to work, but it may return some false negatives.)

Consider two spheres at diagonally opposite corners of the box. Each of the spheres has 3 or more points where it intersects with the edges of the box. Seen from the opposite corner, one of these points is the furthest point inside the box on the sphere. That means that if all these points are covered by the opposite sphere, then the whole box is covered by these two spheres.

sphereBox3D covered

example 1: all points covered by diagonally opposite sphere

If the two spheres don't cover the whole box, check the other 3 pairs of diagonally opposite spheres. If one of the pairs covers the box, then it is covered by the 8 spheres. If none of the pairs covers the box, then it may or may not be covered by the 8 spheres (possible false negative).

sphereBox3D not-covered

example 2: some points not covered by diagonally opposite sphere


In the specific case of a cubic box, the radius of two diagonally opposite spheres which cover the whole cube with size 1 is given by these formulas:

0 ≤ ra ≤ 1 → rb ≥ √(2 + (1 - ra)2)
1 ≤ ra ≤ √2 → rb ≥ √(1 + (1 - √(ra2 - 1))2)
√2 ≤ ra ≤ √3 → rb ≥ 1 - √(ra2 - 2)

To avoid time-consuming calculations, using linear interpolation between a few points (including the breakpoints at 1 and √2) will give a conservative approximation. Or you can use the simpler but less precise approximation of ra2 + rb2 ≥ 3 (the blue circle on the graph below).

radius of opposite spheres that cover cube


There is a similar method where you consider the 4 spheres around the bottom corners of the box, find the "landscape" their surfaces create inside the box, and then find the lowest point in this "landscape". If you then do the same for the top spheres, and the minimum heights of the two landscapes sum to more than the height of the box, then the box is covered by the spheres.

You can then also check the left/right and front/rear minimum heights to see whether they add up to the width and depth of the box. If any of them do, then the box is covered by the spheres. If none does, it is unsure whether the box is covered (possible false negative). Since this method considers all the spheres at once, I think it'll give fewer false negatives than the diagonally-opposite spheres method.

borders between spheres

example 3a: finding the intersections of the 4 spheres

Seen from above, the intersection between any two spheres is the line between the two intersecting points of the circles where the spheres intersect the bottom side of the box.

lowest point of 4-sphere landscape

example 3b: finding the lowest points on the intersections

The intersections between spheres join up to form the "valleys" in the "landscape". The highest point of a valley between two neighbouring spheres is at the edge of the box, the highest point of a valley between two diagonally opposite spheres is on the diagonal between their centers. So the lowest points are where the "valleys" meet. Which of these points is the lowest, is determined by their distance to the diagonal between the centers of the two largest spheres.

lowest point is zero

example 3c: side not completely covered

If some of the "valleys" don't meet, then part of the bottom side is not covered by these 4 spheres, and the minimum height is obviously zero.


I've been fiddling around with code for the minimum-height method, and the geometry needed to calculate the lowest point between 4 spheres is actually quite simple:

  • For the lowest point to be higher than zero, two diagonally opposed spheres need to intersect, i.e. ra+rc or rb+rd is not less than the box side's diagonal.
  • The height of a sphere with radius r above a point that is distance d away from the sphere's center is √r2-d2.
  • The part of a smaller sphere inside the box is completely contained within a larger sphere if the height of the larger sphere above the center point of the smaller sphere is greater than the smaller sphere's radius. The smaller sphere can then be ignored, because it has no impact on the height of the "landscape".
  • Two spheres a and b, whose centers are at a distance d from each other, intersect at a distance d2+ra2-rb2 / 2×d from the center of sphere a.

sphereBox trigonometry

like image 183