# Geometry Nodes - Sort instances into selection groups by distance

I'm working on a geometry nodes hair system and would like to hear some ideas on how to better separate the children into groups for clumping.

To simplify this question here's the spawning surface, a couple of guide curves (tubes in red) and instances and their origin points. They were distributed using poisson disk to avoid overlapping the points so it's easier to turn them into nice hair ribbons later on.

The initial idea is to use a single Distribute Points on Faces node to spawn all children to take advantage of the poisson disk, sorting them out into groups later. To clump them around guides I'm using a Set Position node with the following configuration.

The principle works, but doesn't look like the best approach. I need to clump only the instances inside the distance range now (pink gradient) and the selection input could be used to do this, but I'd have to solve a few new problems like what to do with instances in overlapping ranges, to not speak of how manual all of this is because we got no loop nodes yet. I need to know at least the maximum number of curve guides beforehand to create a tree deforming each group of surrounding instances because an instance is either part of the selection and this particular clump or not.

It might be better to use Multiple Distribute Points on Faces and delete anything overlapping to skip the "sorting points into groups" part. I don't know, I'm a bit tired and frustrated. If it were you, how would you sort the instances into clumping groups?

If you are still interested, I have something which is totally unfinished but it might already solve this problem.

Disclaimer: I'm a beginner regarding Geometry Nodes, so I'm not sure this is 100% correct and I guess it could be simplified. And for sure it could be improved.

The most important part is in the left grey frame labeled "DISTRIBUTE". There it is looking for the nearest guide curve (using the starting point of the guide curves only) and then using the index for selecting the corresponding guide curve in the Instance on Points node.

The middle part is for the "clumping" and the right part just to create a 3D mesh for each hair (with a thin tip and a thicker root).

A few notes:

• I'm using two Collection info nodes (labeled "Guide Curves at orig. position" and "Guide Curves at (0,0,0)") for the same Collection of Guide Curves one for measuring distances the other with Reset Children checked for the actual instances on the points distributed on the surface. That assumes all Guide Curves have scale and rotation applied. That was the easiest way for me, but for sure it could be done also in a different way.
• As some other usages of Geometry Nodes I have seen, I'm assuming that if I create exactly one point per instance the indices are matching with the corresponding instances. Not sure if that is guaranteed for all times.
• The clumping part is searching for the closest guide again. This time by the closest end points, which might result in a hair of shape A (closest starting point) clumping with other hairs of shape B (closest end point). But that part is anyway really primitive so far :-)

• A bit late I know, and I ended up using a different approach, but I'm accepting this answer because it is useful nonetheless. The return of custom attributes as Named Attributes made it easy to keep track of which guide spline is whose parent. Just store their ID in the nearest points after generating them and it gets propagated to the final mesh without extra work. Commented May 28, 2022 at 21:05