# Packing spheres in volume of shape

**URL:** <https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089>\
**Category:** Grasshopper\
**Created:** [May 30, 2019, 11:31pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089 "2019-05-30T23:31:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikele\_Schnitman](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mikele_schnitman/32/46405_2.png) [@Mikele\_Schnitman](https://discourse.mcneel.com/u/Mikele_Schnitman)\
**Post date:** [May 30, 2019, 11:31pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/1 "2019-05-30T23:31:46Z")

</div>

Hello,  
I have some visual tests to do.  
how can I get a packing of spheres inside a closed shape.  
goal is to know how many spheres would fit for further build.  
Ive looked around but didnt find a solution in the forums.

Example  
10in x 10in x10in cube.  
filled with 1in diameter spheres.  
would it be 1260 spheres ?  
Can grasshopper do this visually so i can change spheres diameters and box sizes ?  
eventually to get sphere counts in different non box shapes or meshes

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/1/e1308564086ff68db57c50e7072ab1a74d745d41.jpeg)  
 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/5/e/5e4d0d4e2299cfb9c46fe9eedaf59fef729ede64.png)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [May 31, 2019, 4:16am UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/2 "2019-05-31T04:16:16Z")

</div>

This need physics; use blender or other softwares

[https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/0/4/048ced0a42c2ef20e020dba7255d208f67aaee1e.mp4](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/0/4/048ced0a42c2ef20e020dba7255d208f67aaee1e.mp4)

---

<div class="post-metadata">

**Author:** ![beremiz.2000](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/beremiz.2000/32/612043_2.png) [@beremiz.2000](https://discourse.mcneel.com/u/beremiz.2000)\
**Post date:** [May 31, 2019, 6:43am UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/3 "2019-05-31T06:43:46Z")

</div>

take a look to [Bazil nut effect](https://discourse.mcneel.com/t/circle-packing-with-fixed-radius-and-fixed-number-of-circle-for-each-radius/53104/13), maybe you can extrapolate to 3d, this files from the kangaroo examples could be helpful too.  
[balloondemo.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/d/e/de833a61e30d503083c33ff122755515d3af8d9a.gh) (28.3 KB)  
[floorcollide.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/4/948e84f2cba4f8e321e82f9e3e8357dc0fcc62e9.gh) (13.1 KB)

---

<div class="post-metadata">

**Author:** ![akilli](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/akilli/32/514967_2.png) [@akilli](https://discourse.mcneel.com/u/akilli)\
**Post date:** [May 31, 2019, 1:22pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/4 "2019-05-31T13:22:03Z")

</div>

Do you even need to produce the spheres for this? If you know the volume of your shape - something which can be done easily enough with GH, and you know the system of units you’re working in, then you can calculate how many unit cells it contains. Multiply this by the number of spheres per unit cell and you derive the total number spheres.

CCP has four spheres per unit cell, You specified that the sphere has a diameter =1, so r = 1/2. If the radius is 1/2 inch, then, according to the formula above, unit cell edge = sqrt(8)\*(1/2) = sqrt(2). The volume of the cell is then sqrt(8). The number of cells of volume sqrt(8) in a 10 x 10 x 10 cube is 1000/sqrt(8).

numbers of spheres in volume = number of unit cells \* number of spheres/unit cell = (1000/sqrt(8))_4  
which is 1000_ sqrt(2), about 1414.

**OR** , more simply, if you already know the packing efficiency for the arrangement, which in this case is 74.05%, then the volume occupied by the spheres is 1000 \* .7405 = 740.5 . The volume of a single sphere is 4/3\* pi\*(1/2)^3 = 0.5236. so the number of spheres is 740.5 /.5236 = 1414 (rounded)

[http://mathworld.wolfram.com/SpherePacking.html](http://mathworld.wolfram.com/SpherePacking.html)

---

<div class="post-metadata">

**Author:** ![DanielPiker](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danielpiker/32/666337_2.png) [@DanielPiker](https://discourse.mcneel.com/u/DanielPiker)\
**Post date:** [May 31, 2019, 3:23pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/5 "2019-05-31T15:23:02Z")

</div>

I think the question isn’t clearly defined.

Do you want to know the exact number of spheres which can fit in some volume or just an approximation?

Do you want the absolute maximum number possible, or the maximum number while keeping them all in the cubic-close packed structure?

The densest possible arrangement within a given boundary isn’t necessarily just some subset of the densest arrangement in infinite space.

Consider the problem for just a single layer of spheres (ie circle packing within a boundary) - this is the densest possible packing for one ratio of circle to square size.

 ![svg](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/6/a6f170a5f2d860d0825f45181b75948018b79258.png)  
-so not just part of the hexagonal packing which is the densest infinite packing of circles

while for a different ratio you get this:

 ![svg](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/9/99d754e6f460b33ad06f97c555b2f556d1754652.png)  
-which, while it is part of a hexagonal packing, doesn’t achieve the same density, because you have half circle sized gaps near the edges

You could certainly use a physics based method, and I can share some more examples with Kangaroo if that is the approach you want to take. Generally this can be a way to find a fairly dense sphere packing, but not usually a perfectly regular arrangement, and not necessarily the absolute optimum one (and I believe there isn’t any known (non brute-force) way of finding a guaranteed optimum for this sort of problem).

If your boundary volume is reasonably smooth, and large relative to the size of the spheres, the volume fraction based approximation of the number of spheres should be fairly close though.

> **[Circle packing in a square](https://en.wikipedia.org/wiki/Circle_packing_in_a_square)**
>
> Circle packing in a square is a packing problem in recreational mathematics, where the aim is to pack n unit circles into the smallest possible square. Equivalently, the problem is to arrange n points in a unit square aiming to get the greatest minimal separation, dn, between points. To convert between these two formulations of the problem, the square side for unit circles will be 
>   
>     
>       
> L
> =
> 2
> +
> (
> 2
>         
> /
>         
> So...

---

<div class="post-metadata">

**Author:** ![Mikele\_Schnitman](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mikele_schnitman/32/46405_2.png) [@Mikele\_Schnitman](https://discourse.mcneel.com/u/Mikele_Schnitman)\
**Post date:** [May 31, 2019, 4:12pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/6 "2019-05-31T16:12:07Z")

</div>

Thanks for the good feedback.  
I understand the math better now (thanks Ethan!)  
I still want to make a 3D GH sphere pack calculator.  
and doesnt have to be super exact, just a good approximation.  
i found few topics but not a solution way yet:

[https://www.grasshopper3d.com/forum/topics/geometry-packing-sphere-packing](https://www.grasshopper3d.com/forum/topics/geometry-packing-sphere-packing)

> [@Circle packing on Sphere](https://discourse.mcneel.com/t/circle-packing-on-sphere/45276/2):
>
> I’d do it like this using Grasshopper… The radius is fixed for all the circles as you mentioned but there’s no attention to preventing overlaps… I suspect that’s where things get much more complicated so hopefully you don’t need that bit.

[Sphere Packing - #4 by sheridan](https://discourse.mcneel.com/t/sphere-packing/73943/4)[spheremeshtest.gh](https://global.discourse-cdn.com/mcneel/original/3X/1/1/11e07320f8fd0f0256c45b65e8d2385a1e3fe6e4.gh) (17.3 KB)

---

<div class="post-metadata">

**Author:** ![Mikele\_Schnitman](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mikele_schnitman/32/46405_2.png) [@Mikele\_Schnitman](https://discourse.mcneel.com/u/Mikele_Schnitman)\
**Post date:** [May 31, 2019, 4:14pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/7 "2019-05-31T16:14:38Z")

</div>

I dont know blender, but GH does physics too.

> [@Sphere Packing](https://discourse.mcneel.com/t/sphere-packing/73943):
>
> Has anyone been able to transfer 2D circle packing to 3D sphere packing? Surface tension may be more Cotonou? But I’m interested in solid sphere packing where the spheres stay spherical? Caveat : various sizes

im just not sure how to change this into a full packed volume with a number count.

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [May 31, 2019, 4:24pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/8 "2019-05-31T16:24:48Z")

</div>

This is an idea using voronoi if there is a way to create spheres inside the cells

 ![12](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/8/3/8355d4460975927648bdebadc61f6e594956c6e3.jpeg)

 ![sphere](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/1/9102f0a56696f3fccafa80f3dea4aa078816fb93.jpeg)

---

<div class="post-metadata">

**Author:** ![DanielPiker](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danielpiker/32/666337_2.png) [@DanielPiker](https://discourse.mcneel.com/u/DanielPiker)\
**Post date:** [May 31, 2019, 5:54pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/9 "2019-05-31T17:54:35Z")

</div>

For a rough approximation you can use the volume fraction approach Ethan gives above. The actual number of spheres would generally be less than predicted by this though, depending on their size relative to the volume, its shape, and how they are physically put in there

are these ball-bearings pouring into a hopper, oranges being carefully placed by hand…?

For a physics approach you can try this:  
[FillVolumeWithSpheres.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/c/4/c46c75e492493d742875ef7aac431161affdc2c6.gh) (22.4 KB)  
It uses a script which will ask for a reference assembly location, and you need to set the location of your KangarooSolver dll, which is generally  
C:\Program Files\Rhino 6\Plug-ins\Grasshopper\Components  
There’s a slider for the number of spheres, which you can gradually increase, and their colour is an indication of the pressure, or how much they are overlapping. So if you slowly slide up the number until they are no longer all green, you can see how many fit.  
You could also optionally add a Grab component to let you give them a stir if needed to see if they can rearrange to a denser arrangement.  
It would also be possible with a bit more work to script the adding of spheres, so it would automatically add them until it was full.

[https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/2/4/24fbb29f548a991d3fdb5057a01b0db6ff62d01c.mp4](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/2/4/24fbb29f548a991d3fdb5057a01b0db6ff62d01c.mp4)

---

<div class="post-metadata">

**Author:** ![akilli](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/akilli/32/514967_2.png) [@akilli](https://discourse.mcneel.com/u/akilli)\
**Post date:** [May 31, 2019, 7:38pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/10 "2019-05-31T19:38:27Z")

</div>

If, as Daniel stated, my previous method overstates the number of spheres, here’s a solution which will slightly understate it. Since the CCP packing has four spheres per unit cell, if you divide the cell into eight subcells, each one will contain 1/2 sphere. The ideas is to create a lattice of one eighth cells (or their centers more accurately) and then check to see how many are contained in your shape. Take the total number contained, and multiply by 1/2. Before, I got 1414 and this method gives 1372.

I hope this isn’t just about winning a jelly bean counting contest.

 ![Canvas%20at%2015%3B32%3B14](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/6/9/6955120723c894f6092360c97ed4f6a287bb1909.png)

[Spheres in shape.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/f/9/f906322846f2ef1e545fb55d134145be85ef611b.gh) (13.2 KB)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [May 31, 2019, 8:29pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/11 "2019-05-31T20:29:40Z")

</div>

This idea need a scripting maybe ; create network from points where the distance between points is fixed **x** or **\>x** and the spheres radius is **x/2**

 ![15](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/5/f/5f625efebf2e9ff43c1ed17aaa1a698ce4ce3bb1.jpeg)

---

<div class="post-metadata">

**Author:** ![Mikele\_Schnitman](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mikele_schnitman/32/46405_2.png) [@Mikele\_Schnitman](https://discourse.mcneel.com/u/Mikele_Schnitman)\
**Post date:** [May 31, 2019, 9:30pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/12 "2019-05-31T21:30:44Z")

</div>

Tnx,  
I think i can figure out thr Kangeroo2 version (i already haveit but maybe the wrong version)  
but what is the multiplication ? ? ? im missing and betweeen what is it connected ?

 ![1](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/d/9d0dd9484c8e0f5f7049320f1160f03d6543664d.jpeg)

---

<div class="post-metadata">

**Author:** ![DanielPiker](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danielpiker/32/666337_2.png) [@DanielPiker](https://discourse.mcneel.com/u/DanielPiker)\
**Post date:** [May 31, 2019, 9:39pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/14 "2019-05-31T21:39:47Z")

</div>

Which Rhino version and service release are you using?

---

<div class="post-metadata">

**Author:** ![Mikele\_Schnitman](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mikele_schnitman/32/46405_2.png) [@Mikele\_Schnitman](https://discourse.mcneel.com/u/Mikele_Schnitman)\
**Post date:** [May 31, 2019, 10:44pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/15 "2019-05-31T22:44:18Z")

</div>

![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/4/4/44280c4e528aa421da6bbb8f46098c4b73916009.png)

![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/d/e/deb0001713067889fbe8ece713a270454a32bff1.png)

![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/b/eb6406f1c1a2271c59499d31a768dabfa1a17ee2.png)

---

<div class="post-metadata">

**Author:** ![danilo.gigantelli](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danilo.gigantelli/32/350231_2.png) [@danilo.gigantelli](https://discourse.mcneel.com/u/danilo.gigantelli)\
**Post date:** [April 12, 2021, 7:33am UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/16 "2021-04-12T07:33:25Z")

</div>

Hi Daniel, I’m struggling to use this with different spheres radius, Can i get some help? thanks

---

<div class="post-metadata">

**Author:** ![DanielPiker](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danielpiker/32/666337_2.png) [@DanielPiker](https://discourse.mcneel.com/u/DanielPiker)\
**Post date:** [April 12, 2021, 8:36am UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/17 "2021-04-12T08:36:53Z")

</div>

Hi @danilo.gigantelli  
You could try this:

> [@Sphere Packing](https://discourse.mcneel.com/t/sphere-packing/73943/5):
>
> [spheresmesh] [spheremeshtest.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/1/1/11e07320f8fd0f0256c45b65e8d2385a1e3fe6e4.gh) (17.3 KB) Collisions between spheres of different radii is possible with the ‘collider’ goal. I realised though that there wasn’t really anything in there for collisions between spheres of different radii and meshes (you could kind of do it by offsetting the mesh for each different radius, but that would be rather slow)… So I put together a new goal for this. You’ll need to set the assembly reference location when you open it. I’ll include this as a standard …

---

<div class="post-metadata">

**Author:** ![danilo.gigantelli](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danilo.gigantelli/32/350231_2.png) [@danilo.gigantelli](https://discourse.mcneel.com/u/danilo.gigantelli)\
**Post date:** [April 12, 2021, 9:59am UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/18 "2021-04-12T09:59:18Z")

</div>

Oh I tried with this, but the problem is that i have to fill a closed mesh

---

<div class="post-metadata">

**Author:** ![DanielPiker](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danielpiker/32/666337_2.png) [@DanielPiker](https://discourse.mcneel.com/u/DanielPiker)\
**Post date:** [April 12, 2021, 10:25am UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/19 "2021-04-12T10:25:28Z")

</div>

The same approach works for closed meshes.  
Here’s a slightly modified version of that definition:  
[spherefill.gh](https://discourse.mcneel.com/uploads/short-url/plpCQJLyLTxaUqicDxMcFHJkrzO.gh) (93.8 KB)

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/8/1/6/8162df5d62f99d4e2cbe443dded5cac7d2bb55d2.jpeg)

---

<div class="post-metadata">

**Author:** ![danilo.gigantelli](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danilo.gigantelli/32/350231_2.png) [@danilo.gigantelli](https://discourse.mcneel.com/u/danilo.gigantelli)\
**Post date:** [October 25, 2021, 4:17pm UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/20 "2021-10-25T16:17:25Z")

</div>

Hi Daniel, I’ve been trying to apply local radius (With point attractor), but I haven’t found anything better than subdivide the initial volume into parts and then run single sphere packing simulation!  
Is there a way to optimize this?  
Thank you a lot  
[Capture.PNG|690x325](https://discourse.mcneel.com/uploads/short-url/ajh7yzXaQS7z3vKYGrPt5oLkhLh.jpeg)

---

<div class="post-metadata">

**Author:** ![zfq947254420](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/zfq947254420/32/627569_2.png) [@zfq947254420](https://discourse.mcneel.com/u/zfq947254420)\
**Post date:** [June 19, 2022, 10:57am UTC](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089/22 "2022-06-19T10:57:00Z")

</div>

Uploading: 61fdd128bcdddd5479ac58afbafe013.png…  
Hello Daniel Piker, this happens when I open your project file, what is the reason?

[Next page](https://discourse.mcneel.com/t/packing-spheres-in-volume-of-shape/84089.md?page=2)
