I wonder if there is a possibility to re-sort the indexes of a mesh according to a certain criterion?
Again and again in Geometry Nodes unfavorable situations arise where the point distribution or their indexes are in irregular order.
For example, I want to re-sort the points distributed on a grid with the node Distribute Points on Faces
along a certain axis so that I can process them in order.
If I convert the distributed points for example with the node Mesh Line
into a curve, a totally confused pattern comes out, because the indexes are distributed in a random order:
However, I would like the indexes to be sorted and numbered from right to left so I can create a line along those points:
Bonus task: The whole thing should at best also work with three-dimensional objects, because just a grid is sometimes pretty bland.
PS: I know there are "hacky solutions" out there, but that's what I really want to avoid and find a solid and as simple as possible logic based technique.
Techniques comparison
Even though it's not a contest here, I'm providing an overview of the answers below.
Each approach is very different, but each also has its justification. How, when, which technique is best depends very much on the situation.
I have now combined and modified all five techniques in such a way that they are meaningfully comparable, and run according to the following criteria:
- All get as input any test value (float/integer)
- All process points in the point domain
- All provide the sorted index as result for further processing
(Native Sorting, Quadratic Sorting, Resolution Sorting, Circular Sorting, Ramp Sorting)
Features & Capabilities
Native Sorting | Quadratic Sorting | Resolution Sorting | Circular Sorting | Ramp Sorting | |
---|---|---|---|---|---|
Reliability | Perfect | Perfect (?) (Review pending) |
Acceptable (Depends on density & resolution settings) |
Bad (at higher density) |
Acceptable (Depends on density) |
Performance assessment | Perfect | Acceptable * | Good * (Depends on resolution settings) |
Perfect (if you don't care about missed points) |
Bad (!!!) |
Can handle identical values | Yes | Yes | Yes, partially (Depends on resolution settings) |
No | No |
*With a few points (below ~400), Quadratic Sorting is actually faster than Resolution Sorting. However, Resolution Sorting shows its strength with many values to be sorted and clearly outperforms Quadratic Sorting!
Note: I tried to make a comparison of the timings, but since the variants work too differently in the end and that depends on too many factors, it's not really possible to do that in a meaningful way.
Advantages & Disadvantages
Advantages | Disadvantages | |
---|---|---|
Native Sorting | Captures all points, Best performance |
- |
Quadratic Sorting | Captures all points (?) (Review pending) |
Average performance |
Resolution Sorting | Performance depends on adjustable resolution | Performance/Reliability depends on adjustable resolution Has a small error rate at high density |
Circular Sorting | Is ultra-fast * | "Swallows" points very easily at higher density |
Ramp Sorting | - | Is computationally intensive at high density Does not work reliably at high density |
*The "Circular Sorting Technique" is so fast mainly because at high density the more points are discarded and thus no real comparison to other methods can be made!
Use cases
- If you set a high value on precision and no point should be lost: Native Sorting
- If you set a high value on precision and you have less then ~5000 (?) Points: Quadtratic Sorting
- If you need a good result, but can live with a small error rate: Resolution Sorting
- If you need it really fast, and if you don't care about precise detection: Circular Sorting
- If you are totally crazy and feel like experimenting: Ramp Sorting
Download the comparison and try it yourself
(Blender 3.2+, all methods except "Native Sorting")
(Blender 3.4+, all five methods)
These files are all adapted for use on point clouds (!)
$\bbox[red, 5px]{\color{white}{\mathbf{NO}}}$
(color table) $\endgroup$Sample Index
to the positions of a line. So the sorted index only says which point should put at this index, and it depends on you what you do with this information. Does this make more sense to you? $\endgroup$Sample Index
to fetch the sorted value, and make a comparison with the current index of the points. Something like this: i.sstatic.net/o8TQ3.png $\endgroup$