def order(self, job, path): """ Sort subpaths by minimizing the distances between all start and end points. """ subpaths = split_painter_path(path) log.debug("Subpath count: {}".format(len(subpaths))) # Cache all start and end points now = time() # This is in the UI thread time_limit = now + self.plugin.optimizer_timeout zero = QVector2D(0, 0) for sp in subpaths: # Average start and end into one "vertex" start = sp.elementAt(0) end = sp.elementAt(sp.elementCount() - 1) sp.start_point = QVector2D(start.x, start.y) sp.end_point = QVector2D(end.x, end.y) distance = QVector2D.distanceToPoint original = subpaths[:] result = [] p = zero while subpaths: best = sys.maxsize shortest = None for sp in subpaths: d = distance(p, sp.start_point) if d < best: best = d shortest = sp p = shortest.end_point result.append(shortest) subpaths.remove(shortest) # time.time() is slow so limit the calls if time() > time_limit: result.extend(subpaths) # At least part of it is optimized log.warning( "Shortest path search aborted (time limit reached)") break duration = time() - now d = self.subpath_move_distance(zero, original) d = d - self.subpath_move_distance(zero, result) log.debug("Shortest path search: Saved {} in of movement in {}".format( to_unit(d, 'in'), duration)) return join_painter_paths(result)
def order(self, job, path): """ Sort subpaths by minimizing the distances between all start and end points. """ subpaths = split_painter_path(path) log.debug("Subpath count: {}".format(len(subpaths))) # Cache all start and end points time_limit = time()+self.time_limit zero = QVector2D(0, 0) for sp in subpaths: # Average start and end into one "vertex" start = sp.elementAt(0) end = sp.elementAt(sp.elementCount()-1) sp.start_point = QVector2D(start.x, start.y) sp.end_point = QVector2D(end.x, end.y) distance = QVector2D.distanceToPoint original = subpaths[:] result = [] p = zero while subpaths: best = sys.maxsize shortest = None for sp in subpaths: d = distance(p, sp.start_point) if d < best: best = d shortest = sp p = shortest.end_point result.append(shortest) subpaths.remove(shortest) # time.time() is slow so limit the calls if time() > time_limit: result.extend(subpaths) # At least part of it is optimized log.debug("Shortest path search aborted (time limit reached)") break d = self.subpath_move_distance(zero, original) d = d-self.subpath_move_distance(zero, result) log.debug("Shortest path search: Saved {} in of movement ".format( to_unit(d, 'in'))) return join_painter_paths(result)
def order_by_func(self, job, path, sort_func): subpaths = sorted(split_painter_path(path), key=sort_func) return join_painter_paths(subpaths)