0

Given an array of points in 3D space, I need to find the closest point to a given point p.

I am looking to optimize performance for this search. Currently, I am using square of magnitude as a metric to compare distances, because of the obvious optimization in comparison to Euclidean distance (which requires the calculation of the square root).

I am aware of the existence of the Manhattan distance. However, I am not sure whether this metric could be used in this case. I need to do this evaluation correctly.

Is there a better way of achieving this calculation, performance wise? Perhaps storing the points in a specialized data structure, or using a different metric?

I read the related question (Fastest way to find the closest point to a given point in 3D, in Python), but am still wondering about the optimal metric for comparison.

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
  • Do you need to do this every frame, or just once? – FINDarkside Mar 04 '17 at 21:43
  • I need to do it every frame, for different `p`. – Igor Ševo Mar 04 '17 at 23:58
  • If you need to do it just to one point per frame, I would guess that your current solution is the best way to go. If you need to do it for multiple points per frame, you could benefit from kd-tree or some other data structure depending on the amount of points and how many nearest points you need to calculate per frame. – FINDarkside Mar 05 '17 at 00:33
  • The only way would be to use squared magnitude instead of magnitude so you skip the square root calculation. You could also minimize the amount of calculation per frame. For instance, if you have a sorted collection, and you know your agents are not moving more than x units/second, you can discard the end of the collection knowing they are too far whatsoever. Only perform the full collection once per second for instance, and run only the first 10 or 20 for the rest of the second knowing others are out of reach. – Everts Mar 05 '17 at 06:59
  • I know you're trying to use fastest algo trying to find an Object in Unity3d. But I hope you're aware that In order for this to work, You're exerting effort in the engine to ask where they are and what they are. Which means, you will either Fire up your Method of finding, then the Enginge will search and Get the Object. Which in most case, will be harder. My suggestion is to take advantage of what Unity3D will offer you. Instead of aplying the Algo. Why don't you just use a invi Hitbox and and a function which ever hits it first will be the nearest. CONS:BOX needs to move. Just an Alternati – Aizen Mar 05 '17 at 11:10
  • @Aizen that doesn't really work if you really need to find the closest object. If there are multiple objects inside the box, you need to calculate all of them and if there's nothing inside the box, you'll need to do the same thing he's doing now. – FINDarkside Mar 05 '17 at 13:11

0 Answers0