logo

Topiary_BruteForce_RC

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext
from System.Drawing import Color
import random as rnd
import time
"""--------------------------------------------------------
RHINOCOMMON METHODS
One Time Search to find all points within specified radius
The points found inside the specified radius get sampled for closest neighbor
No Repeat Search
Search Sample = 1 Million Points
Search Radius = 500 Units
Search Space = Cube (5000,5000,5000)
--------------------------------------------------------"""
def closestSearch(pts, point):
    #function to sample point based on a search radius first then search for the closest inside that list mimic the rtree pass basically but with brute force
    ptsinRange = [] #dynamic list to store points found inside initial search
    for i, pt in enumerate(pts):
        dist = point.DistanceTo(pt) #find the distance from each point to the query point
        if dist < 500: 
            ptsinRange.append(pt) #if the distance is less than 500 append that point to the ptsinRange List
    cPoint = Rhino.Collections.Point3dList.ClosestPointInList(ptsinRange,point) #search for the closest point from that list of points in range
    if cPoint[0] == point[0] and cPoint[1] == point[1] and cPoint[2] == point[1]: #test against the closest neighbor being you
        print "IS THE SAME" 
    return cPoint,ptsinRange #return the closest neighbor and the list of points in range
def RunSearch():
    point = Rhino.Geometry.Point3d(5000/2,5000/2,5000/2)
    intNumPts = 1000000 #specify number of sample points
    pts = [] #store the random points in an empty list
    for x in range(0,intNumPts):
        randomPt = Rhino.Geometry.Point3d(rnd.random()*5000,rnd.random()*5000,rnd.random()*5000) #randomly place each point inside the 5k x 5k x 5k cube
        pts.append(randomPt) #append each random point to the pts list
    timeStart = time.time() #Start timer
    data = closestSearch(pts, point) #call closestsearch function Pass query pt and list of points to search
    print "ClosestPoint = {}".format(data[0]) #print the closest neighbor
    print "Number of Pts in Range = {}".format(len(data[1])) #print how many points were captures inside the search radius
    timeEnd = time.time() #stop the timer
    print "time taken: {}".format(timeEnd - timeStart) #print the amount of time the calculations took
if __name__=="__main__":
    RunSearch()
Update
  • Share