Home > other >  Sort coordinates of pointcloud by distance to previous point
Sort coordinates of pointcloud by distance to previous point

Time:12-01

Pointcloud of rope with desired start and end point

I have a pointcloud of a rope-like object with about 300 points. I'd like to sort the 3D coordinates of that pointcloud, so that one end of the rope has index 0 and the other end has index 300 like shown in the image. Other pointclouds of that object might be U-shaped so I can't sort by X,Y or Z coordinate. Because of that I also can't sort by the distance to a single point.

I have looked at KDTree by sklearn or scipy to compute the nearest neighbour of each point but I don't know how to go from there and sort the points in an array without getting double entries.

Is there a way to sort these coordinates in an array, so that from a starting point the array gets appended with the coordinates of the next closest point?

CodePudding user response:

First of all, obviously, there is no strict solution to this problem (and even there is no strict definition of what you want to get). So anything you may write will be a heuristic of some sort, which will be failing in some cases, especially as your point cloud gets some non-trivial form (do you allow loops in your rope, for example?)

This said, a simple approach may be to build a graph with the points being the vertices, and every two points connected by an edge with a weight equal to the straight-line distance between these two points.

And then build a minimal spanning tree of this graph. This will provide a kind of skeleton for your point cloud, and you can devise any simple algorithm atop of this skeleton.

For example, sort all points by their distance to the start of the rope, measured along this tree. There is only one path between any two vertices of the tree, so for each vertex of the tree calculate the length of the single path to the rope start, and sort all the vertices by this distance.

CodePudding user response:

As suggested in other answer there is no strict solution to this problem and there can be some edge cases such as loop, spiral, tube, but you can go with heuristic approaches to solve for your use case. Read about some heuristic approaches such as hill climbing, simulated annealing, genetic algorithms etc.


For any heuristic approach you need a method to find how good is a solution, let's say if i give you two array of 3000 elements how will you identify which solution is better compared to other ? This methods depends on your use case.


One approach at top of my mind, hill climbing

method to measure the goodness of the solution : take the euclidian distance of all the adjacent elements of array and take the sum of their distance.

Steps :

  1. create randomised array of all the 3000 elements.
  2. now select two random index out of these 3000 and swap the elements at those indexes, and see if it improves your ans (if sum of euclidian distance of adjacent element reduces)
  3. If it improves your answer then keep those elements swapped
  4. repeat step 2/3 for large number of epochs(10^6)

This solution will lead into stagnation as there is lack of diversity. For better results use simulated annealing, genetic algorithms.

  • Related