Skip to content

Commit

Permalink
Path: make PathUtil.sort_jobs more generic, allow tuning job weights
Browse files Browse the repository at this point in the history
  • Loading branch information
m0n5t3r authored and yorikvanhavre committed May 27, 2017
1 parent 53770df commit bed4255
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Mod/Path/PathScripts/PathUtils.py
Expand Up @@ -512,28 +512,45 @@ def rampPlunge(edge, rampangle, destZ, startZ):

return rampCmds

def sort_jobs(locations, keys):
def sort_jobs(locations, keys, attractors=[]):
""" sort holes by the nearest neighbor method
keys: two-element list of keys for X and Y coordinates. for example ['x','y']
originally written by m0n5t3r for PathHelix
"""
from Queue import PriorityQueue
from collections import defaultdict

attractors = attractors or [keys[0]]

def sqdist(a, b):
""" square Euclidean distance """
return (a[keys[0]] - b[keys[0]] ) ** 2 + (a[keys[1]] - b[keys[1]]) ** 2
d = 0
for k in keys:
d += (abs(a[k]) - abs(b[k])) ** 2

return d

def weight(location):
w = 0

for k in attractors:
w += abs(location[k])

return w

def find_closest(location_list, location, dist):
q = PriorityQueue()

for j in location_list:
q.put((dist(j, location) + location[keys[0]], j))
q.put((dist(j, location) + weight(j), j))

prio, result = q.get()

return result


out = []
zero = {keys[0]: 0,keys[1]: 0}
zero = defaultdict(lambda: 0)

out.append(find_closest(locations, zero, sqdist))

Expand Down

0 comments on commit bed4255

Please sign in to comment.