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.
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
(Depends on density & resolution settings)
(at higher density)
(Depends on density)
(Depends on resolution settings)
(if you don't care about missed points)
|Can handle identical values
(Depends on resolution settings)
*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
|Captures all points,
|Captures all points (?)
|Performance depends on adjustable resolution
|Performance/Reliability depends on adjustable resolution
Has a small error rate at high density
|Is ultra-fast *
|"Swallows" points very easily at higher density
|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!
- 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
These files are all adapted for use on point clouds (!)