I want to generate a random distribution of cylinders inside a cube using geometric nodes as shown in the related figure. The case where I used programming with pyton or matlab, I find the cylinder collision problem is a difficult problem to solve by programming. In the case of nodes used in Blender, their generation is faster, as shown on this page : enter link description here, or one of the members used a great method to generate spheres inside a cube. When changing the spheres with the cylinders in this example, I found a generation problem.
Here's a node setup that removes overlapping Cylinders:
The Custom Group:
Unfortunately increasing the number of cylinders eventually decreases the number of resulting cylinders. The reason for that is that even if the algorithm decides to not spawn cylinder A, because it overlaps with a cylinder B, the cylinder A still is on the raycasting geometry, so the cylinder B will overlap the cylinder A and therefore it also won't spawn. So eventually there's so many cylinders it's very unlikely for a cylinder to not overlap anything:
I tested a solution I mentioned in the comment, where I connect Index attribute to the Raycast node in the Nearest Integer mode, and then compare it with current Index multiplied by the number of loop faces of the cylinder (Domain Size, need to extract single instance geometry from within the custom group as output), and then compare those two indices - if the hit index is larger, current cylinder is the one with lower index and so it can be left. However, there's a huge problem with that: raycast doesn't give you a list of hit geometry, only the first one. It's therefore possible that a Cylinder #3 will hit a Cylinder #7, therefore you will decide to leave it in, but perhaps without Cylinder #7 the ray would hit a Cylinder #1... So with many cylinders you actually end up with collisions.
Below is roughly as dense setup of cylinders as I could get without collisions: