Get points within a given radius



  • Hi all,
    is there a python function to get the points of an object within a given radius of a defined position?
    I can iterate through all the points but this can get costly.



  • Hi Orestis,

    Unfortunately, all helpers classes available in C++ such as KDTree, DistanceQueryInterface or either VoxelizationInterface are not available in Python. So the only reliable way is to iterates over all the points like so.

    import c4d
    import sys
    
    # Main function
    def main():
        start = time.time()
        if not op: return
        if not isinstance(op, c4d.PointObject): return
    
        # WorldPosition to Match
        posToMatch = c4d.Vector(25, -13, -88)
    
        # Retrieves points position in World Position
        opMat = op.GetMg()
        pts = [pt * opMat for pt in op.GetAllPoints()]
        if not pts: return
      
        # Iterates overs all list to find the closest point
        nearestDist = sys.float_info.max
        ptId = None
        for x, pt in enumerate(pts):
            dist = (pt - posToMatch).GetLength()
            if dist >= nearestDist: continue
            
            nearestDist = dist
            ptId = x
        
        print nearestDist, ptId
    
    
    # Execute main()
    if __name__=='__main__':
        main()
    

    I have also tested to do some threading stuff, but due to the GIL in python, this does not bring any performance gain (even worse).
    Cheers,
    Maxime.



  • Thanks Maxime, that clears it out.



  • You may want to consider to use scipy.
    You have to do indexing once, but can the efficiently iterate over the points within a desired radius with the functions that come with the module.


Log in to reply