Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Radius nearest neighbors

Discussion in 'Scripting' started by Chuck-LePlant, May 2, 2014.

  1. Chuck-LePlant

    Chuck-LePlant

    Joined:
    Jan 26, 2013
    Posts:
    9
    Hi, I'm trying to have something similar to ANN or FLANN (approximate nearest neighbors libraries for c++). And I was wondering if there is anything similar already out there for Unity.

    What I was looking for is a radius filtering approach, given a Vector3 and a radius, what points (in the kdtree) are within the sphere described by those values?

    I was thinking KDTree for efficiency, checking all points is quite slow, I have around 60000 points.

    I found this kdtree class but it only gives a single Vector3. I guess it could be extended for this purpose but I don't see how..

    Thanks!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    It might seem counterintuitive, but taking advantage of Unity's physics might be a good choice. I know the physics engine does a fair bit of optimization/compartmentalization/etc along these lines. So, Physics.OverlapSphere might actually be really fast.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's not - I've just recently had to optimize out a bunch of calls to it. They bubble up into Physics.Simulate() in the Profiler. It also uses the collision matrix regardless of whether you give it a mask or not and some parts of it are treated like a physics collision. This leads me to believe that the physic simulation is actually using the OverlapSphere method from the API. I haven't seen similar behavior in Raycast so I don't know why this is different.

    That being said - if you're not doing it often and your scene isn't overly complex then it'll work fine. (We started to see performance dips at ~100 masked OverlapSphere calls per frame)
     
    Last edited: May 2, 2014
  4. Chuck-LePlant

    Chuck-LePlant

    Joined:
    Jan 26, 2013
    Posts:
    9
    But that would mean I need to attach a collider to each point, right? Is there something like a Point collider? Or would I need sphere colliders for each point? As I said I have a lot of points could even have the order 100K.

    Thanks for the tips!