示例#1
0
    def draw_bounding_rect(orig, rects):

        (xmin, xmax, ymin, ymax) = get_bounding_rect(rects)

        xmin -= 1
        ymin -= 1
        ymax += 1
        xmax += 1

        r = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
             (xmin, ymin)]

        put_thing(orig, r, (0, 0, 255))
示例#2
0
def infer_drawing(orig, ins):
    blank = (np.copy(orig) * 0) + 255
    sys = Constrainer()
    AutoConstrain.rectangles(sys, ins['rectangles'], blank)

    res = sys.solve()
    assert (res.status == SLVS_RESULT_OKAY)

    print('dof:', res.dof)
    for r in ins['rectangles']:
        lset = r.geom
        for i in lset:
            put_thing(blank, i.array(), (255, 0, 0))

    save(blank, 'output2.png')
    return ins
示例#3
0
    def colinear_groups(coline, im):

        #x = Shape()
        #x.init_from_line_group(group)
        x = coline
        s0, s1 = x.side_traces
        p0, p1 = x.abs_line
        y3 = s0 * s1

        for l in x.colinear_group:
            put_thing(im, l['abs-line'], (255, 0, 0), (0, 0), 3)

        #s0 = np.array([])
        #s1 = np.array([])
        #for x in group:
        #put_thing(im,x['abs-line'],(255,0,0),(0,0),3)
        #s0 = np.concatenate((s0,x['side-traces'][0]))
        #s1 = np.concatenate((s1,x['side-traces'][1]))
        #p0 = group[0]['abs-line'][0]
        #p1 = group[-1]['abs-line'][1]
        put_thing(im, (p0 + (3, -3), p0), (0, 0, 255), (0, 0), 3)
        put_thing(im, (p1 + (3, -3), p1), (0, 0, 255), (0, 0), 3)
        #y3 = s0 * s1
        #fakeline = {}
        t3 = Scatter(y=y3)

        return plotfuncs.side_traces(x, im)

        return [t3], None, '%d lines (%d,%d)->(%d,%d)' % (len(group), p0[0],
                                                          p0[1], p1[0], p1[1])
示例#4
0
    def colinear_rects(self, r1, r2, horizontal, orig=None):
        """ Constraint two same-sized rectangles to have colinear sides """

        if horizontal:
            r1p1 = r1[0].p1
            r1p2 = r1[0].p2
            r2p1 = r2[0].p1
            r2p2 = r2[0].p2
        else:
            r1p1 = r1[2].p1
            r1p2 = r1[2].p2
            r2p1 = r2[2].p1
            r2p2 = r2[2].p2

        if line_len((r1p1.array(), r2p2.array())) > line_len(
            (r1p2.array(), r2p1.array())):
            p1 = r1p1
            p2 = r2p2
            pn = r1p2
        else:
            p1 = r1p2
            p2 = r2p1
            pn = r1p1

        colin = self.Line(p1, p2)

        if orig is not None:
            l = [(p1.x.val, p1.y.val), (p2.x.val, p2.y.val)]
            l2 = [(pn.x.val - 2, pn.y.val - 2), (pn.x.val + 2, pn.y.val + 2)]
            put_thing(orig, l, (255, 0, 0), None, 3)
            put_thing(orig, l2, (0, 200, 0), None, 3)

        if horizontal:
            self.constr(SLVS_C_HORIZONTAL, 0, 0, 0, colin, 0)
        else:
            self.constr(SLVS_C_VERTICAL, 0, 0, 0, colin, 0)
def draw_trimmed_triangles(im, affected):
    """ supporting output for drawing triangle and its intersecting lines """
    for i, a in enumerate(affected):
        #if i not in range(5,15): continue
        #print('%dth triangle' % i)
        tri = a[0]
        lines = a[1]
        tri_c = tri['triangle']
        put_thing(im, tri_c, [0, 0, 255], tri['offset'])

        #for x in lines:
        l = lines[0]['line']
        #octr = lines[0]['ocontour']
        put_thing(im, l, [0, 128, 0], lines[0]['offset'], 2)
        l2 = lines[1]
        if l2 is not None:
            #print('intersects at %.2f degrs' % x[2])
            put_thing(im, l2, [128, 12, 0], (0, 0), 1)
        else:
            put_thing(im, l, [128, 128, 0], lines[0]['offset'], 2)
示例#6
0
 def lines(line, im):
     put_thing(im, line['abs-line'], (255, 0, 0), (0, 0), 3)
     return [Scatter(y=[])], None, None
示例#7
0
    def get_dimensions(x, **kwargs):
        im = kwargs.get('im', None)
        s0 = np.array(x['side-traces'][0])
        s1 = np.array(x['side-traces'][1])
        dimensions = []

        y3 = s0 * s1

        mode = stats.mode(y3)[0][0]
        trigger = mode * 2 + 1

        humps, markers = TriangleHumps.get_humps(y3, trigger)
        groups = TriangleHumps.group_humps(humps)
        pairs, diff_traces = TriangleHumps.get_hump_pairs(y3, groups)

        if 'debug_diffs' in kwargs:
            kwargs['debug_diffs'] += diff_traces
        if 'debug_groups' in kwargs:
            kwargs['debug_groups'] += groups
        if 'debug_markers' in kwargs:
            kwargs['debug_markers'] += markers
        if 'y3' in kwargs:
            kwargs['y3'] += list(y3)

        syms = TriangleHumps.pass_symmetrical_pairs(y3, pairs)

        if x.id in (-1, ):
            print('examining', x.id)
            for p in pairs:
                p1 = p[0]['range']
                p2 = p[1]['range']
                sym1 = y3[p1[0]:p1[1] + 1]
                sym2 = y3[p2[0]:p2[1] + 1]
                #print('  pair: (%d->%d), (%d,%d)' % (p1[0],p1[1], p2[0],p2[1]))
                print('  pair: (%d), (%d)' % (len(sym1), len(sym2)))
                m1 = list(sym1).index(max(sym1))
                m2 = list(sym2).index(max(sym2))
                print('  max@%d, max@%d' % (m1, m2))
                print('  ', sym1)
                print('  ', sym2)

        badid = None
        if x.id in (-1, badid):
            print(x.id, 'has %d humps,%d groups' % (len(humps), len(groups)))
            print('  %d colinear lines' % len(x.colinear_group))
            for l in x.colinear_group:
                print(l.abs_line)
            print('  %d pairs, %d diff traces' %
                  (len(pairs), len(diff_traces)))
            print('  %d syms' % (len(syms)))

            for i in range(0, len(pairs)):
                print('  ', pairs[i][:2])
            print('   humps:')
            for h in humps:
                print('      ', h)

        if len(syms):
            #print('line %d has %d pairs of potential triangles' %(x.id, len(syms)))
            #for p in syms:
            #dire = TriangleHumps.points(y3,p)
            #if dire == TriangleHumps.OUT:
            #print('points out')
            #else:
            #print('points in')
            locs = x['side-locs']
            pm = x['pslope']
            ss = s0 + s1
            for a1, a2 in syms:
                ddir = TriangleHumps.points(y3, (a1, a2))
                p1, p2 = a1['range']
                p3, p4 = a2['range']

                if ddir == TriangleHumps.OUT:
                    base1 = locs[p2 + 0]
                    base2 = locs[p3 - 0]
                    tip1 = locs[max(p1 - 2, 0)]
                    tip2 = locs[min(p4 + 2, len(locs) - 1)]
                else:
                    base1 = locs[p1 + 0]
                    base2 = locs[p4 - 0]
                    tip1 = locs[max(p2 - 2, 0)]
                    tip2 = locs[min(p3 + 2, len(locs) - 1)]

                ang = math.atan(pm)
                dist = max(ss[p1:p2 + 1]) / 2. + 2

                dx = int(dist * math.cos(ang))
                dy = int(dist * math.sin(ang))
                pm1 = (base1[0] - dx, base1[1] - dy)
                pp1 = (base1[0] + dx, base1[1] + dy)

                pm2 = (base2[0] - dx, base2[1] - dy)
                pp2 = (base2[0] + dx, base2[1] + dy)

                length = line_len((base1, tip1))

                # skip stubby triangles
                if (length / dist) < 0.5:
                    if x.id == badid:
                        print('skipping (%d,%d), due to stubby' % (p1, p2))
                    continue

                # dont support diagnol lines
                if pm != 0 and pm != MAX_SLOPE:
                    if x.id == badid:
                        print('skipping (%d,%d), due to diagnol' % (p1, p2))
                    continue

                # skip infeasibly short dimensions
                if line_len((base1, base2)) < 5:
                    if x.id == badid:
                        print('skipping (%d,%d), due to infeasibly short' %
                              (p1, p2))
                    continue

                # skip highly unbalanced triangles
                def PolyArea(x, y):
                    return 0.5 * np.abs(
                        np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))

                tri1 = np.array((pm1, pp1, tip1))
                tri2 = np.array((pm2, pp2, tip2))
                area1 = PolyArea(tri1[:, 0], tri1[:, 1])
                area2 = PolyArea(tri2[:, 0], tri2[:, 1])

                # skip highly unbalanced triangles
                if area1 > (area2 * 3) or area1 < (area2 / 3):
                    if x.id == badid:
                        print('skipping (%d,%d), due to highly unbalanced' %
                              (p1, p2))
                    continue

                dim = Dimension(tri1, tri2)
                dimensions.append(dim)

                #print(base_pt)
                if im is not None:
                    #col = (randint(0,256),randint(0,256),randint(0,256),)
                    if x.id == badid:
                        print('plotting (%d,%d)' % (p1, p2))
                        print('plotting (%d,%d)' % (p2, p3))
                        print(tri1)
                        print(tri2)
                        col = (255, 0, 255)
                        put_thing(im, tri1, col, (0, 0), 1)
                        put_thing(im, tri2, col, (0, 0), 1)

                    else:
                        col = (0, 0, 255)
                        put_thing(im, tri1, col, (0, 0), 1)
                        put_thing(im, tri2, col, (0, 0), 1)
                    #put_thing(im,[base2],(0,0,255),(0,0),3)
        return dimensions