def semantic3d_sample_block(beg, bi, ri, rm, fs, fn, min_p, bsize, ds_stride):
    # from io_util import get_semantic3d_class_colors
    # colors=get_semantic3d_class_colors()
    lbls, pts = [], []
    pn=0
    for i in xrange(fn):
        points, labels = read_room_pkl('data/Semantic3D.Net/pkl/train/' + fs + '_{}.pkl'.format(i))
        idxs = libPointUtil.gridDownsampleGPU(points, ds_stride, False)

        points = points[idxs]
        labels = labels[idxs]
        points[:, :3] = np.dot(points[:, :3], rm)
        points[:, :3] -= np.expand_dims(min_p, axis=0)

        x_cond = (points[:, 0] >= beg[0]) & (points[:, 0] < beg[0] + bsize)
        y_cond = (points[:, 1] >= beg[1]) & (points[:, 1] < beg[1] + bsize)
        cond = x_cond & y_cond
        pn+=np.sum(cond)
        pts.append(points[cond])
        lbls.append(labels[cond])

    if pn>1024:
        pts = np.concatenate(pts, axis=0)
        lbls = np.concatenate(lbls, axis=0)

        print 'block {} pn {}'.format(bi,pn)
        save_room_pkl('data/Semantic3D.Net/block/train/' + fs + '_{}_{}.pkl'.format(bi,ri), pts, lbls)
def prepare_semantic3d():
    with open('cached/semantic3d_stems.txt','r') as f:
        fns=f.readlines()
        fns=[fn.strip('\n') for fn in fns]

    for fn in fns:
        labels=read_semantic3d_label_file('/home/pal/data/Semantic3D.Net/raw/train/'+fn+'.labels')
        points=read_semantic3d_points_file('/home/pal/data/Semantic3D.Net/raw/train/'+fn+'.txt')
        save_room_pkl('/home/pal/data/Semantic3D.Net/pkl/train/'+fn+'.pkl',points,labels)
        print 'done'
def prepare_semantic3d_partition():
    with open('cached/semantic3d_stems.txt','r') as f:
        fns=f.readlines()
        fns=[fn.strip('\n').split(' ')[0] for fn in fns]

    for fn in fns[:7]:
        lf=open('data/Semantic3D.Net/raw/train/' + fn + '.labels')
        pf=open('data/Semantic3D.Net/raw/train/' + fn + '.txt')

        count=0
        while True:
            line=lf.readline()
            if not line:
                break
            count+=1
        lf.seek(0)

        points,labels=[],[]
        pi,part_id=0,0
        while True:
            line=pf.readline()
            if not line:
                break
            line=line.strip('\n')
            sublines=line.split(' ')
            x=float(sublines[0])
            y=float(sublines[1])
            z=float(sublines[2])
            intensity=float(sublines[3])
            r=float(sublines[4])
            g=float(sublines[5])
            b=float(sublines[6])
            points.append(np.asarray([x,y,z,r,g,b,intensity],dtype=np.float32))
            labels.append(int(lf.readline()))
            pi+=1
            if pi>=10000000:
                print 'output {} part {}'.format(fn,part_id)
                pi=0
                save_room_pkl('data/Semantic3D.Net/pkl/train2/'+fn+'_{}.pkl'.format(part_id),
                              np.asarray(points,np.float32),np.asarray(labels,np.int32))
                points,labels=[],[]
                part_id+=1

        lf.close()
        pf.close()

        if pi!=0:
            save_room_pkl('data/Semantic3D.Net/pkl/train2/'+fn+'_{}.pkl'.format(part_id),
                          np.asarray(points,np.float32),np.asarray(labels,np.int32))

        print '{} done'.format(fn)
def downsample_and_save():
    train_list,test_list=get_block_train_test_split()
    idx=0
    train_list+=test_list
    with open('cached/room_block_ds0.03_stems.txt','w') as f:
        for _ in xrange(5):
            for fs in train_list:
                points,labels=read_room_pkl('data/S3DIS/room_block_10_10/'+fs)
                points, labels=downsample(points,labels,0.03)

                names=fs.split('_')
                names[0]=str(idx)
                nfs='_'.join(names)
                f.write(nfs+'\n')
                ofs='data/S3DIS/room_block_10_10_ds0.03/'+nfs
                save_room_pkl(ofs,points,labels)
                idx+=1
def prepare_semantic3d_test_partition():
    with open('cached/semantic3d_test_stems.txt','r') as f:
        fns=f.readlines()
        fns=[fn.strip('\n').split(' ')[0] for fn in fns]

    for fn in fns:
        pf=open('data/Semantic3D.Net/raw/test/' + fn + '.txt')

        count=0
        while True:
            line=pf.readline()
            if not line:
                break
            count+=1
        pf.seek(0)

        points,labels=[],[]
        while True:
            line=pf.readline()
            if not line:
                break
            line=line.strip('\n')
            sublines=line.split(' ')
            x=float(sublines[0])
            y=float(sublines[1])
            z=float(sublines[2])
            intensity=float(sublines[3])
            r=float(sublines[4])
            g=float(sublines[5])
            b=float(sublines[6])
            points.append(np.asarray([x,y,z,r,g,b,intensity],dtype=np.float32))
            labels.append(0)

        pf.close()

        save_room_pkl('data/Semantic3D.Net/pkl/test/'+fn+'.pkl',
                      np.asarray(points,np.float32),np.asarray(labels,np.int32))

        print '{} done'.format(fn)