"The shortest edge trick"
To be honest...that's what I call this technique now.
In contrast to the quadratic complexity approach, I'm nowthis technique offers the advantage that only a little stunned myself at how complicatedfraction of the answer seemed at first, and how simplevertices necessary for the solutioncalculation is in the endcreated, since only a triangulated mesh is used as a basis.
So this approach has a clear advantage in terms of speed, especially when the number of points to be calculated is high (500+).
How does it work?
The basic idea here is to shorten all edges between the points proportionally, and thus find the closest point. This method saves a lot of calculations and complexity and is basically applicable to any shape (grid, sphere, cube, etc.):
In the case of a grid with slightly offset points, the principle of operation is simplified as follows:
If the points are now moved, the edge length changes, which always takes the shortest path due to triangulation:
Here we go: The only necessary basis is therefore to always have an optimally triangulated mesh as a starting point.
Depending on the type of mesh, this can be achieved in different ways.
- In the case of a sphere you can simply use the node
Convex Hull
.
- If you are using a grid with the
Distribute Points on Faces
node, you can achieve the triangulated mesh with the example I outlined in this answer: Selectively join points using geometry nodes
- And for other shapes you can use various other tricks.
The rule is simply: if the points are connected at least once over the shortest distance by an edge, then you already have the necessary information.
What is the result?
In this concrete example, the solution applied to a sphere looks like this in the final result:
Step by step to the solution
Useful Hints
- This solution works best by converting a mesh into triangles with the shortest edge length! Quads are less suitable here, because they may produce false positives.
- If you do not use a sphere, it is best to create the mesh using the
Triangulate
(Shortest Diagonal) node.
- If you use a sphere, this works best with a sphere of the type
Ico Sphere
in a higher resolution.
- Remember: If you use a sphere like in this example, the calculated distance is of course also the shortest straight path between the points. The real distance on a sphere would actually be the angle between the points multiplied by the radius of the sphere. The angle is obtained with the formula: $\alpha = 2 \ast \arcsin (\frac {s}{r \ast 2})$
- If you get false positive results with this method due to closely spaced points or highly stretched triangles, simply change the scaling. For example, instead of first reducing the length with a factor of $0.5$ and then multiplying by $4$, you can reduce by a factor of $0.8$ and then multiply by $10$.