def boundingBoxFromBPY(ob, overwriteRadii=None): corners = [ob.matrix_world * Vector(corner) for corner in ob.bound_box] # Visualise the corners. WARNING: Extremely slow """for x in range(8): bpy.ops.object.empty_add(type='PLAIN_AXES') ob = bpy.context.object ob.location = corners[x]""" minx = min([c[0] for c in corners]) miny = min([c[1] for c in corners]) minz = min([c[2] for c in corners]) radiusx = (max([c[0] for c in corners]) - minx) / 2 radiusy = (max([c[1] for c in corners]) - miny) / 2 radiusz = (max([c[2] for c in corners]) - minz) / 2 x = minx + radiusx y = miny + radiusy z = minz + radiusz if overwriteRadii: radiusx, radiusy, radiusz = overwriteRadii # Visualise the box. WARNING: Very slow """bpy.ops.mesh.primitive_cube_add() ob = bpy.context.object ob.dimensions = (dimx, dimy, dimz) ob.location = (x + (dimx/2), y + (dimy/2), z + (dimz/2))""" return BoundingBox((x, y, z), (radiusx, radiusy, radiusz), ob.name)
def clusterMatch(sources, targets, srcAccessFunc, trgAccessFunc): """ :param sources: a list of objects representing the sources :param targets: a list of objects representing the targets :param srcAccessFunc: a function that given an element from sources will give back (x, y, z). :param trgAccessFunc: a function that given an element from targets will give back (x, y, z). """ t = [(x[0], Vector(trgAccessFunc(x[1]))) for x in enumerate(targets)] s = [(x[0], Vector(srcAccessFunc(x[1]))) for x in enumerate(sources)] result = matchGroups(s, t) check = [(x[0][0], x[1][0]) for x in result[1]] fst = [x[0] for x in check] scd = [x[1] for x in check] if len(set(fst)) != len(fst) or len(set(scd)) != len(scd): print("Match produced duplicates") return result
def KMean2(points, groups=None): """ :param points: [(enumerate, Vector)] :param groups: None or [(enumerate, Vector), (enumerate, Vector)] group 1 group 2 """ assert isinstance(points[0][0], int) and isinstance(points[0][1], Vector),\ "points is in the wrong format" if groups: group1pos = Vector() for p in groups[0]: group1pos += p[1] group1pos /= len(groups[0]) group2pos = Vector() for p in groups[1]: group2pos += p[1] group2pos /= len(groups[1]) else: group1pos = choice(points)[1] group2pos = choice(points)[1] while group1pos == group2pos: group2pos = choice(points)[1] assert isinstance(group1pos, Vector), "Should be converted to Vector" assert isinstance(group2pos, Vector), "Should be converted to Vector" groups = [[], []] for p in points: d1 = (p[1].x - group1pos.x)**2 + (p[1].y - group1pos.y)**2 \ + (p[1].z - group1pos.z)**2 d2 = (p[1].x - group2pos.x)**2 + (p[1].y - group2pos.y)**2 \ + (p[1].z - group2pos.z)**2 if d1 < d2: groups[0].append(p) else: groups[1].append(p) assert len(groups[0]) != 0 and len(groups[1]) != 0, "ERROR" + str(groups) return groups, group1pos, group2pos