Example #1
0
def step_1(x, t, dt):

	# from numpy array to uni file
	vel_fn = os.path.join(sim_dir, 'vel_%04d.uni')
	density_fn = os.path.join(sim_dir, 'density_%04d.uni')

	xr = x.reshape(1, 64, 64, 3)
	np_density = xr[:, :, :, [0]]
	np_vel = zeros((1, 64, 64, 3)) # manta takes 3d velocities
	np_vel[:, :, :, :2] = xr[:, :, :, 1:]

	uniio.writeUni(density_fn % (t / dt), generate_header(np_density), np_density)
	uniio.writeUni(vel_fn % (t / dt), generate_header(np_vel), np_vel)

	# loading written data to state
	density.load(density_fn % (t / dt))
	vel.load(vel_fn % (t / dt))

	# applying model
	euler_step()

	# writing to uni
	density.save(density_fn % ((t + dt) / dt))
	vel.save(vel_fn % ((t + dt) / dt))

	# loading back as numpy array
	_, np_density = uniio.readUni(density_fn % ((t + dt) / dt))
	_, np_vel = uniio.readUni(vel_fn % ((t + dt) / dt))

	xn = zeros((xr.shape[0], 64, 64, 3))
	xn[:, :, :, [0]] = np_density
	xn[:, :, :, 1:] = np_vel[:, :, :, :2] # manta takes 3d velocities
	xn = xn.reshape(1, 3 * 64 * 64)

	return xn
Example #2
0
def get_data_next_batch(data_x_addrs,
                        data_y_addrs,
                        ibatch,
                        batch_size,
                        export=False):
    data_x = []
    data_y = []
    for i in range(ibatch * batch_size, ibatch * batch_size + batch_size):
        if os.path.isfile(data_x_addrs[i]) and os.path.isfile(data_y_addrs[i]):
            # grid_x 			= read_streamline_txt(data_x_addrs[i])
            grid_x = read_streamline_bin(data_x_addrs[i])
            uni_header, grid_y = uniio.readUni(data_y_addrs[i])

            # binarify levelset grid: should not include zero(ex. crop levelset grid is 0 everywhere)
            grid_y = np.array(grid_y < 0.0, dtype='float32')

            data_x.append(grid_x)
            data_y.append(grid_y)
            # export sketch and levelset file to check if the two grid data are aligned correctly
            # the visulization routine is in cpp file!
            if (export):
                uniio.writeUni('../VisFluid/data/sketch_{:04}.uni'.format(i),
                               uni_header, grid_x)
                uniio.writeUni('../VisFluid/data/levelset_{:04}.uni'.format(i),
                               uni_header, grid_y)
        else:
            print('{:} or {:} does not exists!'.format(data_x_addrs[i],
                                                       data_y_addrs[i]))

    data_x = np.asarray(data_x, dtype='float32')
    data_y = np.asarray(data_y, dtype='float32')
    return data_x, data_y
Example #3
0
def arrayToUni(input, savePath, motherUniPath, imageHeight, imageWidth, is_vel=False):
	head, _ = uniio.readUni(motherUniPath)
	head['dimX'] = imageWidth
	head['dimY'] = imageHeight

	if not is_vel:
		fixedArray = np.zeros((imageHeight, imageWidth), dtype='f')
		for x in range(0, imageHeight):
			for y in range(0, imageWidth):
				fixedArray[x][y] = input[(imageHeight - 1) - x][y]
	else:
		fixedArray = np.zeros((imageHeight, imageWidth, 3), dtype='f')
		for x in range(0, imageHeight):
			for y in range(0, imageWidth):
				fixedArray[x][y] = input[(imageHeight - 1) - x][y]

	uniio.writeUni(savePath, head, fixedArray)
def get_data_next_batch(data_addrs, ibatch, batch_size, export=False):
    data_streamline_occ = []
    data_streamline_vel = []
    data_grid_lvst = []
    data_grid_vel = []
    for i in range(ibatch * batch_size, ibatch * batch_size + batch_size):
        (data_sketch_addr, data_lvst_addr, data_vel_addr) = data_addrs[i]
        if os.path.isfile(data_sketch_addr) and os.path.isfile(
                data_lvst_addr) and os.path.isfile(data_vel_addr):
            grid_sketch_occ = read_streamline_bin(data_sketch_addr)
            header_lvst, grid_lvst = uniio.readUni(data_lvst_addr)
            header_vel, grid_vel = uniio.readUni(data_vel_addr)

            # clean the velocity field, using levelset as mask(must be same as the streamline program!)
            fluid_mask = (grid_lvst <= 1.0)
            grid_vel = grid_vel * fluid_mask

            # extract streamline velocity
            grid_sketch_vel = grid_sketch_occ * grid_vel

            # binarify levelset grid: should not include zero(ex. crop levelset grid is 0 everywhere)
            grid_lvst = np.array(grid_lvst < 0.0, dtype='float32')

            data_streamline_occ.append(grid_sketch_occ)
            data_streamline_vel.append(grid_sketch_vel)
            data_grid_lvst.append(grid_lvst)
            data_grid_vel.append(grid_vel)

            # export sketch and levelset file to check if the two grid data are aligned correctly
            # the visulization routine is in cpp file!
            if (export):
                uniio.writeUni(
                    '../VisFluid/data/sketch_occ_{:04}.uni'.format(i),
                    header_lvst, grid_sketch_occ)
                uniio.writeUni(
                    '../VisFluid/data/sketch_vel_{:04}.uni'.format(i),
                    header_vel, grid_sketch_vel)
                uniio.writeUni('../VisFluid/data/levelset_{:04}.uni'.format(i),
                               header_lvst, grid_lvst)
                uniio.writeUni('../VisFluid/data/vel_{:04}.uni'.format(i),
                               header_vel, grid_vel)

        else:
            print('{:} or {:} does not exists!'.format(data_sketch_addr,
                                                       data_lvst_addr))

    data_streamline_occ = np.asarray(data_streamline_occ, dtype='float32')
    data_streamline_vel = np.asarray(data_streamline_vel, dtype='float32')
    data_grid_lvst = np.asarray(data_grid_lvst, dtype='float32')
    data_grid_vel = np.asarray(data_grid_vel, dtype='float32')

    # concatenate occupancy field and velocity field
    data_streamline = np.concatenate(
        (data_streamline_occ, data_streamline_vel), axis=-1)
    data_grid = np.concatenate((data_grid_lvst, data_grid_vel), axis=-1)
    return data_streamline, data_grid
Example #5
0
def surface_reconstruction_from_levelset_update(threshold_value, result_dir,
                                                mat_dir, manta_dir,
                                                sketch_addr, lvst_addr,
                                                vel_addr):
    addr_lvst = lvst_addr
    filename_lvst = addr_lvst[int(addr_lvst.rfind(os.sep) +
                                  1):int(addr_lvst.rfind('.'))]
    frame_num = filename_lvst[int(filename_lvst.rfind('_')) + 1:]
    addr_lvst_pre = addr_lvst[0:int(addr_lvst.rfind(os.sep))]
    addr_lvst_pre_pre = addr_lvst_pre[0:int(addr_lvst_pre.rfind(os.sep))]
    simname = addr_lvst_pre[int(addr_lvst_pre.rfind(os.sep) + 1):]

    addr_sketch = sketch_addr
    addr_sketch_pre = addr_sketch[0:int(addr_sketch.rfind(os.sep))]
    addr_sketch_pre_pre = addr_sketch_pre[0:int(addr_sketch_pre.rfind(os.sep))]
    seed_num = addr_sketch_pre_pre[int(addr_sketch_pre_pre.rfind(os.sep) + 1):]

    # load the original predict levelset and velocity
    outpath_pred_lvst = os.path.join(
        result_dir, seed_num + '#' + simname + '#' + filename_lvst + '_pred')
    predict_vec = np.load(outpath_pred_lvst + '.npy')

    predict_occ = predict_vec[:, :, :, 0]
    predict_vel = predict_vec[:, :, :, 1:4]

    predict_lvst_copy = np.copy(predict_occ)
    predict_lvst_copy[predict_occ <= threshold_value] = 0.5  # outside
    predict_lvst_copy[predict_occ > threshold_value] = -0.5  # inside
    predict_lvst_copy = np.ascontiguousarray(predict_lvst_copy,
                                             dtype=np.float32)

    uniio.writeUni(outpath_pred_lvst + '.uni', header_example,
                   predict_lvst_copy)
    phi.load(outpath_pred_lvst + '.uni')
    phi.createMesh(mesh)
    # for iters in range(3):
    # 	smoothMesh(mesh=mesh, strength=1e-3, steps=10)
    # 	subdivideMesh(mesh=mesh, minAngle=0.01, minLength=0.5, maxLength=3*0.5, cutTubes=True)
    mesh.save(outpath_pred_lvst + '.obj')
    os.remove(outpath_pred_lvst + '.uni')
Example #6
0
def manta_animate_from_reconstruction(result_dir, mat_dir, manta_dir,
                                      sketch_addr, lvst_addr, vel_addr,
                                      cur_frame, doOpen):
    outdir = './demo/' + '%04d/' % cur_frame
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    addr_lvst = lvst_addr
    filename_lvst = addr_lvst[int(addr_lvst.rfind(os.sep) +
                                  1):int(addr_lvst.rfind('.'))]
    frame_num = filename_lvst[int(filename_lvst.rfind('_')) + 1:]
    addr_lvst_pre = addr_lvst[0:int(addr_lvst.rfind(os.sep))]
    addr_lvst_pre_pre = addr_lvst_pre[0:int(addr_lvst_pre.rfind(os.sep))]
    simname = addr_lvst_pre[int(addr_lvst_pre.rfind(os.sep) + 1):]

    addr_sketch = sketch_addr
    addr_sketch_pre = addr_sketch[0:int(addr_sketch.rfind(os.sep))]
    addr_sketch_pre_pre = addr_sketch_pre[0:int(addr_sketch_pre.rfind(os.sep))]
    seed_num = addr_sketch_pre_pre[int(addr_sketch_pre_pre.rfind(os.sep) + 1):]

    # copy the streamline and original mesh
    src_line = os.path.join(mat_dir, seed_num, simname,
                            'flipStreamline_' + frame_num + '_resampled.txt')
    dst_line = os.path.join(
        outdir, seed_num + '#' + simname + '#' + 'flipStreamline_' +
        frame_num + '_resampled.txt')
    copyfile(src_line, dst_line)

    src_mesh = os.path.join(manta_dir, simname, 'flip_' + frame_num + '.gz')
    dst_mesh = os.path.join(outdir,
                            simname + '#' + 'flip_' + frame_num + '.gz')
    copyfile(src_mesh, dst_mesh)

    # load the predict levelset and velocity
    outpath_pred_lvst = os.path.join(
        result_dir, seed_num + '#' + simname + '#' + filename_lvst + '_pred')
    predict_vec = np.load(outpath_pred_lvst + '.npy')
    predict_occ = predict_vec[:, :, :, 0]
    predict_vel = predict_vec[:, :, :, 1:4]

    # mantaflow fluid simulation here
    global flags, phi, mesh_fluid, phiFluidIn, vel

    bWidth = 1
    flags.initDomain(boundaryWidth=bWidth, phiWalls=phiObs)
    if doOpen:
        setOpenBound(flags, bWidth, 'xXyYzZ', FlagOutflow | FlagEmpty)
    phi.initFromFlags(flags)

    # load fluid volume (obj file from reconstruction)
    mesh_fluid.load(outpath_pred_lvst + '.obj')
    print('mesh {} loaded successfully'.format(outpath_pred_lvst + '.obj'))
    mesh_fluid.scale(vec3(args.res, args.res, args.res))
    mesh_fluid.offset(vec3(args.res, args.res, args.res) * 0.5)
    mesh_fluid.computeLevelset(phiFluidIn, 2.)

    phi.join(phiFluidIn)

    # load the fluid velocity
    predict_vel_copy = np.copy(predict_vel)
    predict_vel_copy = np.ascontiguousarray(predict_vel_copy, dtype=np.float32)
    uniio.writeUni(outdir + 'vel.uni', header_vel_example, predict_vel_copy)
    vel_fluid.load(outdir + 'vel.uni')

    vel.copyFrom(vel_fluid)

    global pp, pvel, s, steps
    # update flag and sample particles
    flags.updateFromLevelset(phi)
    sampleLevelsetWithParticles(phi=phi,
                                flags=flags,
                                parts=pp,
                                discretization=2,
                                randomness=0.05)
    mapGridToPartsVec3(source=vel, parts=pp, target=pVel)

    if GUI:
        gui = Gui()
        gui.show()
        gui.setCamPos(0., 0., -2)
        gui.setCamRot(0, 0, 0)
        # gui.pause()

    for t in range(steps):
        maxVel = vel.getMax()
        s.adaptTimestep(maxVel)
        mantaMsg('\nFrame %i, timestep %f, timeTotal %f' %
                 (s.frame, s.timestep, s.timeTotal))

        flip(t)
        s.step()

        phi.createMesh(mesh)
        if saveData:
            # vel.save(outdir + 'flipVel_%04d.uni' % t)
            # pp.save( outdir + 'flipParts_%04d.uni' % t )
            # phi.save(outdir + 'flipLevelSet_%04d.uni' % t)
            mesh.save(outdir + 'flip_%04d.gz' % t)
            gui.screenshot(outdir + 'flip_%04d.png' % t)
Example #7
0
def surface_reconstruction_from_levelset(threshold_value, result_dir, mat_dir,
                                         manta_dir, batch, batch_size, iepoch,
                                         filetype, sketch_addrs, lvst_addrs,
                                         vel_addrs):
    sketch_file = os.path.join(result_dir,
                               '{}_input_{}.npy'.format(filetype, batch))
    gt_file = os.path.join(result_dir,
                           '{}_output_{}.npy'.format(filetype, batch))
    predict_file = os.path.join(
        result_dir, '{}_predict_{}_{}.npy'.format(filetype, batch, iepoch))

    input_data = np.load(sketch_file)
    gt_data = np.load(gt_file)
    predict_data = np.load(predict_file)
    # each .npy contains batch_size frames
    for i in range(batch_size):
        gt_vec = gt_data[i]
        predict_vec = predict_data[i]

        gt_occ = gt_vec[:, :, :, 0]
        gtt_vel = gt_vec[:, :, :, 1:4]

        predict_occ = predict_vec[:, :, :, 0]
        predict_vel = predict_vec[:, :, :, 1:4]

        # the corresponding filename and sim folder
        addr_lvst = lvst_addrs[i]
        filename_lvst = addr_lvst[int(addr_lvst.rfind(os.sep) +
                                      1):int(addr_lvst.rfind('.'))]
        frame_num = filename_lvst[int(filename_lvst.rfind('_')) + 1:]
        addr_lvst_pre = addr_lvst[0:int(addr_lvst.rfind(os.sep))]
        addr_lvst_pre_pre = addr_lvst_pre[0:int(addr_lvst_pre.rfind(os.sep))]
        simname = addr_lvst_pre[int(addr_lvst_pre.rfind(os.sep) + 1):]

        addr_sketch = sketch_addrs[i]
        addr_sketch_pre = addr_sketch[0:int(addr_sketch.rfind(os.sep))]
        addr_sketch_pre_pre = addr_sketch_pre[
            0:int(addr_sketch_pre.rfind(os.sep))]
        seed_num = addr_sketch_pre_pre[
            int(addr_sketch_pre_pre.rfind(os.sep) + 1):]
        # print(seed_num, simname, filename_lvst, frame_num)

        # groundtruth
        gt_occ_copy = np.copy(gt_occ)
        gt_occ_copy[gt_occ <= threshold_value] = 0.5  # outside
        gt_occ_copy[gt_occ > threshold_value] = -0.5  # inside
        gt_occ_copy = np.ascontiguousarray(gt_occ_copy, dtype=np.float32)

        outpath_gt_lvst = os.path.join(
            result_dir,
            seed_num + '#' + simname + '#' + filename_lvst + '_recons')
        uniio.writeUni(outpath_gt_lvst + '.uni', header_example, gt_occ_copy)
        phi.load(outpath_gt_lvst + '.uni')
        phi.createMesh(mesh)
        # for iters in range(3):
        # 	smoothMesh(mesh=mesh, strength=1e-3, steps=10)
        # 	subdivideMesh(mesh=mesh, minAngle=0.01, minLength=0.5, maxLength=3*0.5, cutTubes=True)
        mesh.save(outpath_gt_lvst + '.obj')
        os.remove(outpath_gt_lvst + '.uni')

        # prediction
        predict_lvst_copy = np.copy(predict_occ)
        predict_lvst_copy[predict_occ <= threshold_value] = 0.5  # outside
        predict_lvst_copy[predict_occ > threshold_value] = -0.5  # inside
        predict_lvst_copy = np.ascontiguousarray(predict_lvst_copy,
                                                 dtype=np.float32)

        outpath_pred_lvst = os.path.join(
            result_dir,
            seed_num + '#' + simname + '#' + filename_lvst + '_pred')
        # save current frame for update later
        np.save(outpath_pred_lvst + '.npy', predict_vec)
        uniio.writeUni(outpath_pred_lvst + '.uni', header_example,
                       predict_lvst_copy)
        phi.load(outpath_pred_lvst + '.uni')
        phi.createMesh(mesh)
        # for iters in range(3):
        # 	smoothMesh(mesh=mesh, strength=1e-3, steps=10)
        # 	subdivideMesh(mesh=mesh, minAngle=0.01, minLength=0.5, maxLength=3*0.5, cutTubes=True)
        mesh.save(outpath_pred_lvst + '.obj')
        os.remove(outpath_pred_lvst + '.uni')

        # the streamline and original mesh
        src_line = os.path.join(
            mat_dir, seed_num, simname,
            'flipStreamline_' + frame_num + '_resampled.txt')
        dst_line = os.path.join(
            result_dir, seed_num + '#' + simname + '#' + 'flipStreamline_' +
            frame_num + '_resampled.txt')
        copyfile(src_line, dst_line)

        src_mesh = os.path.join(manta_dir, simname,
                                'flip_' + frame_num + '.gz')
        dst_mesh = os.path.join(result_dir,
                                simname + '#' + 'flip_' + frame_num + '.gz')
        copyfile(src_mesh, dst_mesh)

    print('job: batch {:d} done'.format(batch))
Example #8
0
        header['gridType'] = 12
        header['elementType'] = 2
        header['bytesPerElement'] = 12
        header[
            'info'] = b'mantaflow 0.12 64bit fp1 omp commit 15eaf4aa72da62e174df6c01f85ccd66fde20acc from Aug 14 2018, 15:47:38\x00\x90\xb2PI0\x7f\x00\x00\xf8\x8e\x0eP0\x7f\x00\x00\n\x00\x00\x00\x00\x00\x00\x00f\x8b:]0\x7f\x00\x00@\xb6PI0\x7f\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\xb0\xb1PI0\x7f\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x000\x00\x00\x00\x90\xb2PI0\x7f\x00\x00\xd0\xb1PI0\x7f\x00\x00\x10\xb6PI0\x7f\x00\x00\x16\x00\x00\x00\x00\x00\x00\x00\x00\x068*0\x7f\x00\x00\x10\xb6PI0\x7f\x00\x00\xf0\xb5PI0\x7f\x00\x00\xf0\xb5PI0\x7f\x00\x00\x17\x00\x00\x00\x00\x00\x00\x00 \xb6PI'
    else:
        raise NotImplementedError('Does not correspond to density or velocity')

    header['dimT'] = 0
    header['timestamp'] = int(time.time())

    return header


# sanity check
uniio.writeUni('test.uni', generate_header(content1), content1)
_, content1_new = uniio.readUni('test.uni')
print((content1 != content1_new).sum())

uniio.writeUni('test.uni', generate_header(content2), content2)
_, content2_new = uniio.readUni('test.uni')
print((content2 != content2_new).sum())

# print(dir(uniio))
exit()
res = 64
dim = 2
offset = 20
interval = 1

scaleFactor = 4
Example #9
0
def generate3DUniForNewNetwork(imageindex=0,
                               outPath='../',
                               inputPer=3.0,
                               head=None):
    start = time.time()
    dim_output = []
    intermed_res1 = []

    batch_xs_tile = x_3d[imageindex]

    if not load_model_test_1 == -1:
        # z y x -> 2d conv on y - x (or different combination of axis, depending on transposeAxis)
        # and switch velocity channels depending on orientation
        if transposeAxis == 1:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, upRes, 1, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeHigh, simSizeLow, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                1, 0, 2, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 2:3])
            batch_xs_in[:, :, :, 2:3] = np.copy(temp_vel)
        elif transposeAxis == 2:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, 1, upRes, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeHigh, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                2, 1, 0, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 1:2])
            batch_xs_in[:, :, :, 1:2] = np.copy(temp_vel)
        elif transposeAxis == 3:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, 1, upRes, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeHigh, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                2, 0, 1, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            temp_vel2 = np.copy(batch_xs_in[:, :, :, 2:3])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 1:2])
            batch_xs_in[:, :, :, 2:3] = np.copy(temp_vel)
            batch_xs_in[:, :, :, 1:2] = np.copy(temp_vel2)
        else:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [upRes, 1, 1, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeLow, n_inputChannels])

        if add_adj_idcs1:
            batch_xs_in = np.concatenate(
                (batch_xs_in, np.zeros_like(batch_xs_in[:, :, :, 0:1])),
                axis=3)
            batch_xs_in = np.concatenate(
                (batch_xs_in, np.zeros_like(batch_xs_in[:, :, :, 0:1])),
                axis=3)

            for i in range(batch_xs_in.shape[0]):
                if i == 0:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = np.zeros_like(batch_xs_in[i:i + 1, :, :,
                                                               0:1])
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = batch_xs_in[i + 1:i + 2, :, :, 0:1]
                elif i == batch_xs_in.shape[0] - 1:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = batch_xs_in[i - 1:i, :, :, 0:1]
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = np.zeros_like(batch_xs_in[i - 1:i, :, :,
                                                               0:1])
                else:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = batch_xs_in[i - 1:i, :, :, 0:1]
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = batch_xs_in[i + 1:i + 2, :, :, 0:1]

        # start generating output of first network
        batch_sz_out = 8
        run_metadata = tf.RunMetadata()

        start = time.time()
        for j in range(0, batch_xs_in.shape[0] // batch_sz_out):
            #	x in shape (z,y,x,c)
            # 	-> 512 x 512 x 512
            results = sess.run(sampler,
                               feed_dict={
                                   x:
                                   batch_xs_in[j * batch_sz_out:(j + 1) *
                                               batch_sz_out].reshape(
                                                   -1, n_input),
                                   percentage:
                                   inputPer,
                                   train:
                                   False
                               })
            intermed_res1.extend(results)

            # exact timing of network performance...
            if 0:
                fetched_timeline = timeline.Timeline(run_metadata.step_stats)
                chrome_trace = fetched_timeline.generate_chrome_trace_format()
                with open('timeline_8x_%04d.json' % (j), 'w') as f:
                    f.write(chrome_trace)
        end = time.time()

        print("time for first network: {0:.6f}".format(end - start))

        dim_output = np.copy(
            np.array(intermed_res1).reshape(simSizeHigh, simSizeHigh,
                                            simSizeHigh)).transpose(2, 1, 0)

        save_img_3d(
            outPath + 'source_1st_{:04d}.png'.format(imageindex + frame_min),
            dim_output / 80)

    if not load_model_test_2 == -1:
        if transposeAxis == 3:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, upRes, 1, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeHigh, simSizeLow, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                1, 0, 2, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 2:3])
            batch_xs_in[:, :, :, 2:3] = np.copy(temp_vel)
        elif transposeAxis == 0:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, 1, upRes, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeHigh, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                2, 1, 0, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 1:2])
            batch_xs_in[:, :, :, 1:2] = np.copy(temp_vel)
        elif transposeAxis == 1:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, 1, upRes, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeHigh, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                2, 0, 1, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            temp_vel2 = np.copy(batch_xs_in[:, :, :, 2:3])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 1:2])
            batch_xs_in[:, :, :, 2:3] = np.copy(temp_vel)
            batch_xs_in[:, :, :, 1:2] = np.copy(temp_vel2)
        else:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [upRes, 1, 1, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeLow, n_inputChannels])

        if add_adj_idcs2:
            batch_xs_in = np.concatenate(
                (batch_xs_in, np.zeros_like(batch_xs_in[:, :, :, 0:1])),
                axis=3)

            for i in range(batch_xs_in.shape[0]):
                if i == 0:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = np.zeros_like(batch_xs_in[i:i + 1, :, :,
                                                               0:1])
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = batch_xs_in[i + 1:i + 2, :, :, 0:1]
                elif i == batch_xs_in.shape[0] - 1:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = batch_xs_in[i - 1:i, :, :, 0:1]
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = np.zeros_like(batch_xs_in[i - 1:i, :, :,
                                                               0:1])
                else:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = batch_xs_in[i - 1:i, :, :, 0:1]
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = batch_xs_in[i + 1:i + 2, :, :, 0:1]

        intermed_res1 = []
        batch_sz_out = 2

        start = time.time()
        for j in range(0, batch_xs_in.shape[0] // batch_sz_out):
            #	x in shape (z,y,x,c)
            # 	-> 64 x 256 x 256
            results = sess.run(
                sampler_2,
                feed_dict={
                    x:
                    batch_xs_in[j * batch_sz_out:(j + 1) *
                                batch_sz_out].reshape(-1, n_input),
                    y:
                    dim_output[j * batch_sz_out:(j + 1) *
                               batch_sz_out].reshape(-1, n_output),
                    percentage:
                    inputPer,
                    train:
                    False
                })
            intermed_res1.extend(results)

            # exact timing of network performance...
            if 0:
                fetched_timeline = timeline.Timeline(run_metadata.step_stats)
                chrome_trace = fetched_timeline.generate_chrome_trace_format()
                with open('timeline_8x_%04d.json' % (j), 'w') as f:
                    f.write(chrome_trace)
        end = time.time()

        print("time for second network: {0:.6f}".format(end - start))

        dim_output = np.array(intermed_res1).reshape(simSizeHigh, simSizeHigh,
                                                     simSizeHigh).transpose(
                                                         1, 2, 0)

        save_img_3d(
            outPath + 'source_2nd_{:04d}.png'.format(imageindex + frame_min),
            dim_output / 80)

    if not load_model_test_3 == -1:
        if transposeAxis == 0:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, upRes, 1, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeHigh, simSizeLow, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                1, 0, 2, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 2:3])
            batch_xs_in[:, :, :, 2:3] = np.copy(temp_vel)
        elif transposeAxis == 3:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, 1, upRes, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeHigh, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                0, 2, 1, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 2:3])
            batch_xs_in[:, :, :, 2:3] = np.copy(batch_xs_in[:, :, :, 1:2])
            batch_xs_in[:, :, :, 1:2] = np.copy(temp_vel)
        elif transposeAxis == 2:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [1, 1, upRes, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeHigh, n_inputChannels])
            batch_xs_in = np.reshape(batch_xs_in.transpose(
                1, 2, 0, 3), (-1, simSizeLow, simSizeLow, n_inputChannels))
            temp_vel = np.copy(batch_xs_in[:, :, :, 3:4])
            temp_vel2 = np.copy(batch_xs_in[:, :, :, 13])
            batch_xs_in[:, :, :, 3:4] = np.copy(batch_xs_in[:, :, :, 2:3])
            batch_xs_in[:, :, :, 2:3] = np.copy(batch_xs_in[:, :, :, 1:2])
            batch_xs_in[:, :, :, 1:2] = np.copy(temp_vel)
        else:
            batch_xs_in = np.reshape(
                scipy.ndimage.zoom(batch_xs_tile, [upRes, 1, 1, 1],
                                   order=1,
                                   mode='constant',
                                   cval=0.0),
                [-1, simSizeLow, simSizeLow, n_inputChannels])

        if add_adj_idcs3:
            batch_xs_in = np.concatenate(
                (batch_xs_in, np.zeros_like(batch_xs_in[:, :, :, 0:1])),
                axis=3)

            for i in range(batch_xs_in.shape[0]):
                if i == 0:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = np.zeros_like(batch_xs_in[i:i + 1, :, :,
                                                               0:1])
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = batch_xs_in[i + 1:i + 2, :, :, 0:1]
                elif i == batch_xs_in.shape[0] - 1:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = batch_xs_in[i - 1:i, :, :, 0:1]
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = np.zeros_like(batch_xs_in[i - 1:i, :, :,
                                                               0:1])
                else:
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels:n_inputChannels +
                                1] = batch_xs_in[i - 1:i, :, :, 0:1]
                    batch_xs_in[i:i + 1, :, :,
                                n_inputChannels + 1:n_inputChannels +
                                2] = batch_xs_in[i + 1:i + 2, :, :, 0:1]

        intermed_res1 = []
        batch_sz_out = 2

        start = time.time()
        for j in range(0, batch_xs_in.shape[0] // batch_sz_out):
            #	x in shape (z,y,x,c)
            # 	-> 64 x 256 x 256
            results = sess.run(
                sampler_3,
                feed_dict={
                    x:
                    batch_xs_in[j * batch_sz_out:(j + 1) *
                                batch_sz_out].reshape(-1, n_input),
                    y:
                    dim_output[j * batch_sz_out:(j + 1) *
                               batch_sz_out].reshape(-1, n_output),
                    percentage:
                    inputPer,
                    train:
                    False
                })
            intermed_res1.extend(results)

            # exact timing of network performance...
            if 0:
                fetched_timeline = timeline.Timeline(run_metadata.step_stats)
                chrome_trace = fetched_timeline.generate_chrome_trace_format()
                with open('timeline_8x_%04d.json' % (j), 'w') as f:
                    f.write(chrome_trace)
        end = time.time()

        print("time for third network: {0:.6f}".format(end - start))

        dim_output = np.array(intermed_res1).reshape(simSizeHigh, simSizeHigh,
                                                     simSizeHigh)

        save_img_3d(
            outPath + 'source_3rd_{:04d}.png'.format(imageindex + frame_min),
            dim_output / 80)

    if not load_model_no_2 == -1:
        dim_output = dim_output.transpose(2, 0, 1)
    if not load_model_no_1 == -1:
        dim_output = dim_output.transpose(2, 1, 0)

    # output for images of slices (along every dimension)
    if 1:
        for i in range(simSizeHigh // 2 - 1, simSizeHigh // 2 + 1):
            if np.average(dim_output[i]) > 0.0001:
                save_img(outPath + 'slice_xy_{:04d}_{:04d}.png'.format(
                    i, (imageindex + frame_min)),
                         dim_output[i])  #.transpose(2,1,0)
                save_img(
                    outPath + 'slice_yz_{:04d}_{:04d}.png'.format(
                        i, (imageindex + frame_min)),
                    dim_output.transpose(2, 1, 0)[i])
                save_img(
                    outPath + 'slice_xz_{:04d}_{:04d}.png'.format(
                        i, (imageindex + frame_min)),
                    dim_output.transpose(1, 0, 2)[i])
    if (imageindex + frame_min) == 110:
        for i in range(0, tileSizeHigh):
            if np.average(dim_output[i]) > 0.0001:
                save_img(outPath + 'slice_xy_{:04d}_{:04d}.png'.format(
                    (imageindex + frame_min), i),
                         dim_output[i])  #.transpose(2,1,0)
                save_img(
                    outPath + 'slice_yz_{:04d}_{:04d}.png'.format(
                        (imageindex + frame_min), i),
                    dim_output.transpose(2, 1, 0)[i])
                save_img(
                    outPath + 'slice_xz_{:04d}_{:04d}.png'.format(
                        (imageindex + frame_min), i),
                    dim_output.transpose(1, 0, 2)[i])

    if head is None:
        head, _ = uniio.readUni(packedSimPath +
                                "sim_%04d/density_low_%04d.uni" % (fromSim, 0))
    head['dimX'] = simSizeHigh
    head['dimY'] = simSizeHigh
    head['dimZ'] = simSizeHigh

    if generateUni:
        # set low density to zero to save storage space...
        cond_out = dim_output < 0.0005
        dim_output[cond_out] = 0
        uniio.writeUni(
            packedSimPath + '/sim_%04d/source_%04d.uni' %
            (fromSim, imageindex + frame_min), head, dim_output)
        print('stored .uni file')
    return
def surface_reconstruction_from_levelset(threshold_value, result_dir, mat_dir, manta_dir, ibatch, batch_size, iepoch, filetype, sketch_addrs, lvst_addrs):
	sketch_files 	= os.path.join(result_dir, '{}_input_{}.npy'.format(filetype, ibatch))
	gt_files 		= os.path.join(result_dir, '{}_output_{}.npy'.format(filetype, ibatch))
	predict_files 	= os.path.join(result_dir, '{}_predict_{}_{}.npy'.format(filetype, ibatch, iepoch))

	sketch_grid_data 		= np.load(sketch_files)
	gt_lvst_grid_data 		= np.load(gt_files)
	predict_lvst_grid_data 	= np.load(predict_files)
	# each .npy contains batch_size frames
	for i in range(batch_size):
		# the levelset grid is binarified during training
		gt_lvst 		= gt_lvst_grid_data[i]
		predict_lvst 	= predict_lvst_grid_data[i]

		# the corresponding filename and sim folder
		addr_lvst 			= lvst_addrs[i]
		filename_lvst 		= addr_lvst[int(addr_lvst.rfind(os.sep)+1):int(addr_lvst.rfind('.'))]
		frame_num 			= filename_lvst[int(filename_lvst.rfind('_'))+1:]
		addr_lvst_pre 		= addr_lvst[0:int(addr_lvst.rfind(os.sep))]
		addr_lvst_pre_pre 	= addr_lvst_pre[0:int(addr_lvst_pre.rfind(os.sep))]
		simname	 			= addr_lvst_pre[int(addr_lvst_pre.rfind(os.sep)+1):]

		addr_sketch 		= sketch_addrs[i]
		addr_sketch_pre     = addr_sketch[0:int(addr_sketch.rfind(os.sep))]
		addr_sketch_pre_pre = addr_sketch_pre[0:int(addr_sketch_pre.rfind(os.sep))]
		seed_num 			= addr_sketch_pre_pre[int(addr_sketch_pre_pre.rfind(os.sep)+1):]
		# print(seed_num, simname, filename_lvst, frame_num)

		# groundtruth
		# gt_lvst_copy 	= gt_lvst: Does not make a copy of gt_lvst, it merely creates a new reference to gt_lvst name!!!!
		gt_lvst_copy = np.copy(gt_lvst)
		gt_lvst_copy[gt_lvst<=threshold_value] = 0.5 # outside
		gt_lvst_copy[gt_lvst>threshold_value] = -0.5 # inside
		gt_lvst_copy = np.ascontiguousarray(gt_lvst_copy, dtype=np.float32)

		outpath_gt_lvst = os.path.join(result_dir,  seed_num+'#'+simname+'#'+filename_lvst+'_recons')
		uniio.writeUni(outpath_gt_lvst +'.uni', header_example, gt_lvst_copy)
		phi.load(outpath_gt_lvst +'.uni')		
		phi.createMesh(mesh)
		# for iters in range(3):
		# 	smoothMesh(mesh=mesh, strength=1e-3, steps=10) 
		# 	subdivideMesh(mesh=mesh, minAngle=0.01, minLength=0.5, maxLength=3*0.5, cutTubes=True)
		mesh.save(outpath_gt_lvst +'.obj')		
		os.remove(outpath_gt_lvst +'.uni')

		# prediction
		predict_lvst_copy = np.copy(predict_lvst)
		predict_lvst_copy[predict_lvst<=threshold_value] = 0.5 # outside
		predict_lvst_copy[predict_lvst>threshold_value] = -0.5 # inside
		predict_lvst_copy = np.ascontiguousarray(predict_lvst_copy, dtype=np.float32)

		outpath_pred_lvst = os.path.join(result_dir, seed_num+'#'+simname+'#'+filename_lvst+'_pred')
		uniio.writeUni(outpath_pred_lvst +'.uni', header_example, predict_lvst_copy)
		phi.load(outpath_pred_lvst +'.uni')		
		phi.createMesh(mesh)
		# for iters in range(3):
		# 	smoothMesh(mesh=mesh, strength=1e-3, steps=10) 
		# 	subdivideMesh(mesh=mesh, minAngle=0.01, minLength=0.5, maxLength=3*0.5, cutTubes=True)
		mesh.save(outpath_pred_lvst +'.obj')		
		os.remove(outpath_pred_lvst +'.uni')		

		# the streamline and original mesh
		src_line = os.path.join(mat_dir, seed_num, simname, 'flipStreamline_'+frame_num+'_resampled.txt')
		dst_line = os.path.join(result_dir, seed_num+'#'+simname+'#'+'flipStreamline_'+frame_num+'_resampled.txt')
		copyfile(src_line, dst_line)

		src_mesh = os.path.join(manta_dir, simname, 'flip_'+frame_num+'.gz')
		dst_mesh = os.path.join(result_dir, simname+'#'+'flip_'+frame_num+'.gz')
		copyfile(src_mesh, dst_mesh)

	print('job: ibatch {:d} done'.format(ibatch))